doc: wikiparser: Handle markup inside italic/bold markup

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-08-26 20:24:12 -07:00 committed by James Valleroy
parent 8742547a13
commit 7ae46e4ffa
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -100,13 +100,15 @@ class Url(Text):
class ItalicText(Text): class ItalicText(Text):
def to_docbook(self, context=None): def to_docbook(self, context=None):
return f'<emphasis>{escape(self.content)}</emphasis>' xml = ''.join([item.to_docbook() for item in self.content])
return f'<emphasis>{xml}</emphasis>'
class BoldText(Text): class BoldText(Text):
def to_docbook(self, context=None): def to_docbook(self, context=None):
return f'<emphasis role="strong">{escape(self.content)}</emphasis>' xml = ''.join([item.to_docbook() for item in self.content])
return f'<emphasis role="strong">{xml}</emphasis>'
class MonospaceText(Text): class MonospaceText(Text):
@ -630,32 +632,14 @@ def parse_text(line, context=None, parse_links=True):
# Bold text # Bold text
content, line = split_formatted(line, "'''") content, line = split_formatted(line, "'''")
if content: if content:
result.append(BoldText(content)) result.append(BoldText(parse_text(content, context)))
continue continue
# Italic text # Italic text
content, line = split_formatted(line, "''") content, line = split_formatted(line, "''")
if content: if content:
if content.startswith('[[') and content.endswith(']]'): result.append(ItalicText(parse_text(content, context)))
# Special handling for links within emphasis continue
content = content.lstrip('[[').rstrip(']]')
target, _, remaining = content.partition('|')
text = None
if remaining:
text, _, remaining = remaining.partition('|')
params = None
if remaining:
params, _, remaining = remaining.partition('|')
text = text or get_url_text(target)
link = Link(target.strip(), [ItalicText(text.strip())], params)
result.append(link)
continue
else:
result.append(ItalicText(content))
continue
# Monospace text # Monospace text
content, line = split_formatted(line, '`') content, line = split_formatted(line, '`')
@ -940,12 +924,12 @@ def parse_wiki(text, context=None, begin_marker=None, end_marker=None):
>>> parse_wiki('https://freedombox.org') >>> parse_wiki('https://freedombox.org')
[Paragraph([Url('https://freedombox.org'), PlainText(' ')])] [Paragraph([Url('https://freedombox.org'), PlainText(' ')])]
>>> parse_wiki("''italic''") >>> parse_wiki("''italic''")
[Paragraph([ItalicText('italic'), PlainText(' ')])] [Paragraph([ItalicText([PlainText('italic')]), PlainText(' ')])]
>>> parse_wiki("'''bold'''") >>> parse_wiki("'''bold'''")
[Paragraph([BoldText('bold'), PlainText(' ')])] [Paragraph([BoldText([PlainText('bold')]), PlainText(' ')])]
>>> parse_wiki("normal text followed by '''bold text'''") >>> parse_wiki("normal text followed by '''bold text'''")
[Paragraph([PlainText('normal text followed by '), BoldText('bold text'), \ [Paragraph([PlainText('normal text followed by '), \
PlainText(' ')])] BoldText([PlainText('bold text')]), PlainText(' ')])]
>>> parse_wiki('`monospace`') >>> parse_wiki('`monospace`')
[Paragraph([MonospaceText('monospace'), PlainText(' ')])] [Paragraph([MonospaceText('monospace'), PlainText(' ')])]
>>> parse_wiki('``not-monospace``') >>> parse_wiki('``not-monospace``')
@ -1034,9 +1018,9 @@ Paragraph([PlainText('multipara item ')])])])]
[HorizontalRule(10)] [HorizontalRule(10)]
>>> parse_wiki("||'''A'''||'''B'''||'''C'''||\\n||1 ||2 ||3 ||") >>> parse_wiki("||'''A'''||'''B'''||'''C'''||\\n||1 ||2 ||3 ||")
[Table([TableRow([TableItem([Paragraph([BoldText('A')])]), \ [Table([TableRow([TableItem([Paragraph([BoldText([PlainText('A')])])]), \
TableItem([Paragraph([BoldText('B')])]), \ TableItem([Paragraph([BoldText([PlainText('B')])])]), \
TableItem([Paragraph([BoldText('C')])])]), \ TableItem([Paragraph([BoldText([PlainText('C')])])])]), \
TableRow([TableItem([Paragraph([PlainText('1 ')])]), \ TableRow([TableItem([Paragraph([PlainText('1 ')])]), \
TableItem([Paragraph([PlainText('2 ')])]), \ TableItem([Paragraph([PlainText('2 ')])]), \
TableItem([Paragraph([PlainText('3 ')])])])])] TableItem([Paragraph([PlainText('3 ')])])])])]
@ -1186,9 +1170,9 @@ calendar/addressbook ')])])])])])])])]
''[[https://creativecommons.org/licenses/by-sa/4.0/|Creative Commons \ ''[[https://creativecommons.org/licenses/by-sa/4.0/|Creative Commons \
Attribution-ShareAlike 4.0 International]]'' (from June 13rd 2016).") Attribution-ShareAlike 4.0 International]]'' (from June 13rd 2016).")
[List('bulleted', [ListItem([Paragraph([PlainText('New wiki and manual \ [List('bulleted', [ListItem([Paragraph([PlainText('New wiki and manual \
content licence: '), Link('https://creativecommons.org/licenses/by-sa/4.0/', \ content licence: '), ItalicText([Link('https://creativecommons.org/licenses/\
[ItalicText('Creative Commons Attribution-ShareAlike 4.0 International')]), \ by-sa/4.0/', [PlainText('Creative Commons Attribution-ShareAlike 4.0 \
PlainText(' (from June 13rd 2016). ')])])])] International')])]), PlainText(' (from June 13rd 2016). ')])])])]
>>> parse_wiki('An alternative to downloading these images is to \ >>> parse_wiki('An alternative to downloading these images is to \
[[InstallingDebianOn/TI/BeagleBone|install Debian]] on the !BeagleBone and \ [[InstallingDebianOn/TI/BeagleBone|install Debian]] on the !BeagleBone and \
@ -1204,8 +1188,8 @@ existing accounts or \\"Create ...\\" in case of new accounts (see the second \
screenshot below).\\n 1. Check the checkboxes for the address books and \ screenshot below).\\n 1. Check the checkboxes for the address books and \
calendars you want to synchronize and click on the sync button in the header. \ calendars you want to synchronize and click on the sync button in the header. \
(see the third screenshot below)") (see the third screenshot below)")
[Paragraph([BoldText('Synchronizing contacts'), PlainText(' ')]), \ [Paragraph([BoldText([PlainText('Synchronizing contacts')]), \
List('numbered', \ PlainText(' ')]), List('numbered', \
[ListItem([Paragraph([PlainText('Click on the hamburger menus of CalDAV and \ [ListItem([Paragraph([PlainText('Click on the hamburger menus of CalDAV and \
CardDAV and select either "Refresh ..." in case of existing accounts or \ CardDAV and select either "Refresh ..." in case of existing accounts or \
"Create ..." in case of new accounts (see the second screenshot below). ')])]),\ "Create ..." in case of new accounts (see the second screenshot below). ')])]),\
@ -1227,17 +1211,18 @@ server address. For example, ''imaps://imap.example.org''.")
[Paragraph([PlainText('After Roundcube is installed, it can be accessed \ [Paragraph([PlainText('After Roundcube is installed, it can be accessed \
at '), CodeText('https://<your freedombox>/roundcube'), PlainText('. Enter \ at '), CodeText('https://<your freedombox>/roundcube'), PlainText('. Enter \
your username and password. The username for many mail services will be the \ your username and password. The username for many mail services will be the \
full email address such as '), ItalicText('exampleuser@example.org'), \ full email address such as '), ItalicText([PlainText('exampleuser@example.org'\
PlainText(' and not just the username like '), ItalicText('exampleuser'), \ )]), PlainText(' and not just the username like '), ItalicText([PlainText(\
PlainText(". Enter the address of your email service's IMAP server address in \ 'exampleuser')]), PlainText(". Enter the address of your email service's IMAP \
the "), ItalicText('Server'), PlainText(' field. You can try providing your \ server address in the "), ItalicText([PlainText('Server')]), PlainText(' \
domain name here such as '), ItalicText('example.org'), PlainText(' for email \ field. You can try providing your domain name here such as '), ItalicText(\
address '), ItalicText('exampleuser@example.org'), PlainText(" and if this \ [PlainText('example.org')]), PlainText(' for email address '), ItalicText(\
[PlainText('exampleuser@example.org')]), PlainText(" and if this \
does not work, consult your email provider's documentation for the address \ does not work, consult your email provider's documentation for the address \
of the IMAP server. Using encrypted connection to your IMAP server is \ of the IMAP server. Using encrypted connection to your IMAP server is \
strongly recommended. To do this, prepend 'imaps://' at the beginning of \ strongly recommended. To do this, prepend 'imaps://' at the beginning of \
your IMAP server address. For example, "), \ your IMAP server address. For example, "), ItalicText([PlainText(\
ItalicText('imaps://imap.example.org'), PlainText('. ')])] 'imaps://imap.example.org')]), PlainText('. ')])]
>>> parse_wiki('Tor Browser is the recommended way to browse the web \ >>> parse_wiki('Tor Browser is the recommended way to browse the web \
using Tor. You can download the Tor Browser from \ using Tor. You can download the Tor Browser from \
@ -1295,8 +1280,9 @@ that this exposes the !FreedomBox's services to your entire local network.")
the virtual machine's Settings -> [Network] -> [Adapter 1]->[Attached to:] \ the virtual machine's Settings -> [Network] -> [Adapter 1]->[Attached to:] \
and choose the network type your want the machine to use according to the \ and choose the network type your want the machine to use according to the \
explanation in Network Configuration below. The recommended type is the "), \ explanation in Network Configuration below. The recommended type is the "), \
ItalicText('Bridged adapter'), PlainText(" option, but be aware that this \ ItalicText([PlainText('Bridged adapter')]), PlainText(" option, but be aware \
exposes the FreedomBox's services to your entire local network. ")])])])] that this exposes the FreedomBox's services to your entire local network. \
")])])])]
>>> parse_wiki('After logging in, you can become root with the command \ >>> parse_wiki('After logging in, you can become root with the command \
`sudo su`.\\n \\n=== Build Image ===') `sudo su`.\\n \\n=== Build Image ===')
@ -1613,14 +1599,15 @@ Heading(2, 'heading 2nd level'), \
>>> generate_inner_docbook([Paragraph([Url('https://freedombox.org')])]) >>> generate_inner_docbook([Paragraph([Url('https://freedombox.org')])])
'<para><ulink url="https://freedombox.org"/></para>' '<para><ulink url="https://freedombox.org"/></para>'
>>> generate_inner_docbook([Paragraph([ItalicText('italic')])]) >>> generate_inner_docbook([Paragraph([ItalicText([\
PlainText('italic')])])])
'<para><emphasis>italic</emphasis></para>' '<para><emphasis>italic</emphasis></para>'
>>> generate_inner_docbook([Paragraph([BoldText('bold')])]) >>> generate_inner_docbook([Paragraph([BoldText([PlainText('bold')])])])
'<para><emphasis role="strong">bold</emphasis></para>' '<para><emphasis role="strong">bold</emphasis></para>'
>>> generate_inner_docbook([Paragraph([\ >>> generate_inner_docbook([Paragraph([\
PlainText('normal text followed by '), BoldText('bold text')])]) PlainText('normal text followed by '), BoldText([PlainText('bold text')])])])
'<para>normal text followed by \ '<para>normal text followed by \
<emphasis role="strong">bold text</emphasis></para>' <emphasis role="strong">bold text</emphasis></para>'