From 0dfa25fcdcd541464fbf867a63ba78c87257c219 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Feb 2015 20:48:37 +0000 Subject: Handle ordered lists in markdown writer --- mammoth/writers/markdown.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'mammoth') diff --git a/mammoth/writers/markdown.py b/mammoth/writers/markdown.py index 0183423..d355ff3 100644 --- a/mammoth/writers/markdown.py +++ b/mammoth/writers/markdown.py @@ -10,6 +10,16 @@ class _WriterOutput(object): self.anchor_position = anchor_position +class _MarkdownState(object): + def __init__(self): + self.list_state = _MarkdownListState() + + +class _MarkdownListState(object): + def __init__(self): + self.count = 0 + + def _symmetric_wrapped(end): return _Wrapped(end, end) @@ -19,11 +29,11 @@ class _Wrapped(object): self._start = start self._end = end - def __call__(self, attributes): + def __call__(self, attributes, markdown_state): return _WriterOutput(self._start, self._end) -def _hyperlink(attributes): +def _hyperlink(attributes, markdown_state): href = attributes.get("href", "") if href: return _WriterOutput( @@ -34,7 +44,7 @@ def _hyperlink(attributes): return _default_output -def _image(attributes): +def _image(attributes, markdown_state): src = attributes.get("src", "") alt_text = attributes.get("alt", "") if src or alt_text: @@ -43,6 +53,19 @@ def _image(attributes): return _default_output +def _list(ordered): + def call(attributes, markdown_state): + return _WriterOutput("", "\n") + + return call + + +def _list_item(attributes, markdown_state): + list_state = markdown_state.list_state + list_state.count += 1 + return _WriterOutput("{0}. ".format(list_state.count), "\n") + + def _init_writers(): writers = { "p": _Wrapped("", "\n\n"), @@ -51,6 +74,8 @@ def _init_writers(): "em": _symmetric_wrapped("*"), "a": _hyperlink, "img": _image, + "ol": _list(ordered=True), + "li": _list_item, } for level in range(1, 7): @@ -61,13 +86,14 @@ def _init_writers(): _writers = _init_writers() _default_output = _WriterOutput("", "") -_default_writer = lambda attributes: _default_output +_default_writer = lambda attributes, markdown_state: _default_output class MarkdownWriter(object): def __init__(self): self._fragments = [] self._element_stack = [] + self._markdown_state = _MarkdownState() def text(self, text): self._fragments.append(_escape_markdown(text)) @@ -76,7 +102,7 @@ class MarkdownWriter(object): if attributes is None: attributes = {} - output = _writers.get(name, _default_writer)(attributes) + output = _writers.get(name, _default_writer)(attributes, self._markdown_state) anchor_before_start = output.anchor_position == "before" if anchor_before_start: -- cgit v1.2.3