diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-02-08 20:32:40 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-02-08 20:32:40 +0000 |
| commit | 54827b4def89b5c0135fd90507665c76ae2a2efd (patch) | |
| tree | b8048b24dc740a2d012a21de8885f2d58ec5c9eb /mammoth/writers | |
| parent | 7aa2d2ba54ab0c2d908a4b0fbe683bce63a54f45 (diff) | |
Write anchor for ID before starting hyperlink in markdown writer
Diffstat (limited to 'mammoth/writers')
| -rw-r--r-- | mammoth/writers/markdown.py | 32 |
1 files changed, 25 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() |
