Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/markdown_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
CODE_BLOCK_START = r"```([a-z]+)"
CODE_BLOCK_END = r"```"
HEADING_PATTERN = r"^(#+) (.*)"
IMAGE_PATTERN = r'^!\[([^\]]+)\]\(([^ ]+) "([^\)]+)"\)'
# The title is delimited by the quotes, so it may contain any character except a quote (e.g. parentheses)
IMAGE_PATTERN = r'^!\[([^\]]+)\]\(([^ ]+) "([^"]+)"\)'
ITALIC_START = ITALIC_END = "_"
ITALIC_ALTERNATIVE_START = ITALIC_ALTERNATIVE_END = "*"
LINK_PATTERN = r"^\[([^\]]+)\]\(([^\)]+)\)"
# The URL may contain balanced parentheses (e.g. https://en.wikipedia.org/wiki/Foo_(disambiguation)), one level deep
LINK_PATTERN = r"^\[([^\]]+)\]\(((?:[^()]|\([^()]*\))*)\)"
NUMBERED_LIST_PATTERN = r"^[0-9A-Za-z]+\. "
STRIKETROUGH_START = STRIKETROUGH_END = "~~"
TABLE_MARKER = "|"
Expand Down
21 changes: 21 additions & 0 deletions tests/test_markdown_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ def test_image(self):
self.assertEqual("image.png", image.attrib[xmltags.IMAGE_SRC])
self.assertEqual("description", image.attrib[xmltags.IMAGE_TITLE])

@patch(read_text, Mock(return_value='![Anchor](image_(1).png "Figure 1 (cropped)")'))
def test_image_with_brackets(self):
"""Test an image whose source and title contain brackets."""
image = self.find(f".//{xmltags.IMAGE}")
self.assertEqual("image_(1).png", image.attrib[xmltags.IMAGE_SRC])
self.assertEqual("Figure 1 (cropped)", image.attrib[xmltags.IMAGE_TITLE])

@patch(read_text, Mock(return_value="* Bullet\n* list\n\n"))
def test_bullet_list(self):
"""Test bullet list."""
Expand Down Expand Up @@ -227,3 +234,17 @@ def test_code_blok(self):
"""Test that a code block can be included in the Markdown."""
code_block = self.find(xmltags.CODE_BLOCK)
self.assertEqual("print('Hello world')\n", code_block.text)

@patch(read_text, Mock(return_value="See: [more info](https://more.info)\n"))
def test_url(self):
"""Test a URL."""
paragraph = self.find(xmltags.PARAGRAPH)
anchor_link = self.find(xmltags.ANCHOR, paragraph).attrib[xmltags.ANCHOR_LINK]
self.assertEqual("https://more.info", anchor_link)

@patch(read_text, Mock(return_value="See: [more info](https://en.wikipedia.org/wiki/ZAP_(software))\n"))
def test_url_with_brackets(self):
"""Test a URL with brackets."""
paragraph = self.find(xmltags.PARAGRAPH)
anchor_link = self.find(xmltags.ANCHOR, paragraph).attrib[xmltags.ANCHOR_LINK]
self.assertEqual("https://en.wikipedia.org/wiki/ZAP_(software)", anchor_link)