aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mammoth/writers/markdown.py32
-rw-r--r--tests/writers/markdown_tests.py9
2 files changed, 34 insertions, 7 deletions
diff --git a/mammoth/writers/markdown.py b/mammoth/writers/markdown.py
index 07b5fc1..10550d4 100644
--- a/mammoth/writers/markdown.py
+++ b/mammoth/writers/markdown.py
@@ -3,6 +3,13 @@ from __future__ import unicode_literals
import re
+class _WriterOutput(object):
+ def __init__(self, start, end, anchor_position=None):
+ self.start = start
+ self.end = end
+ self.anchor_position = anchor_position
+
+
def _symmetric_wrapped(end):
return _Wrapped(end, end)
@@ -13,13 +20,16 @@ class _Wrapped(object):
self._end = end
def __call__(self, attributes):
- return self._start, self._end
+ return _WriterOutput(self._start, self._end)
def _hyperlink(attributes):
href = attributes.get("href", "")
if href:
- return "[", "]({0})".format(href)
+ return _WriterOutput(
+ "[", "]({0})".format(href),
+ anchor_position="before",
+ )
else:
return _default_output
@@ -40,7 +50,7 @@ def _init_writers():
_writers = _init_writers()
-_default_output = ("", "")
+_default_output = _WriterOutput("", "")
_default_writer = lambda attributes: _default_output
@@ -56,10 +66,18 @@ class MarkdownWriter(object):
if attributes is None:
attributes = {}
- start, end = _writers.get(name, _default_writer)(attributes)
- self._fragments.append(start)
- self._write_anchor(attributes)
- self._element_stack.append(end)
+ output = _writers.get(name, _default_writer)(attributes)
+
+ anchor_before_start = output.anchor_position == "before"
+ if anchor_before_start:
+ self._write_anchor(attributes)
+
+ self._fragments.append(output.start)
+
+ if not anchor_before_start:
+ self._write_anchor(attributes)
+
+ self._element_stack.append(output.end)
def end(self, name):
end = self._element_stack.pop()
diff --git a/tests/writers/markdown_tests.py b/tests/writers/markdown_tests.py
index 79ddb6d..82131b4 100644
--- a/tests/writers/markdown_tests.py
+++ b/tests/writers/markdown_tests.py
@@ -101,6 +101,15 @@ def elements_with_ids_have_anchor_tags_with_ids_appended_to_start_of_markdown_el
writer.text("Hello")
writer.end("h1")
assert_equal('# <a id="start"></a>Hello\n\n', writer.as_string())
+
+
+@istest
+def links_have_anchors_before_opening_square_bracket():
+ writer = _create_writer()
+ writer.start("a", {"href": "http://example.com", "id": "start"})
+ writer.text("Hello")
+ writer.end("a")
+ assert_equal('<a id="start"></a>[Hello](http://example.com)', writer.as_string())
def _create_writer():