aboutsummaryrefslogtreecommitdiff
path: root/mammoth/writers/markdown.py
blob: 79779de5ca83c635055cb56a2976a46f7f6e5453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from __future__ import unicode_literals

from .abc import Writer
from . import html

import re


class _WriterOutput(object):
    def __init__(self, start, end=None, generate_end=None, anchor_position=None):
        if generate_end is None:
            generate_end = _constant(end)

        self.start = start
        self.generate_end = generate_end
        self.anchor_position = anchor_position


def _constant(value):
    def get():
        return value

    return get


class _MarkdownState(object):
    def __init__(self):
        self._list_state_stack = []
        self.list_state = None
        self.list_item_has_closed = False

    def update_list_state(self, list_state):
        self._list_state_stack.append(self.list_state)
        self.list_state = list_state

    def pop_list_state(self):
        self.list_state = self._list_state_stack.pop()


class _MarkdownListState(object):
    def __init__(self, ordered, indentation):
        self.ordered = ordered
        self.count = 0
        self.indentation = indentation


def _symmetric_wrapped(end):
    return _Wrapped(end, end)


class _Wrapped(object):
    def __init__(self, start, end):
        self._start = start
        self._end = end

    def __call__(self, attributes, markdown_state):
        return _WriterOutput(self._start, self._end)


def _hyperlink(attributes, markdown_state):
    href = attributes.get("href", "")
    if href:
        return _WriterOutput(
            "[", "]({0})".format(_escape_markdown(href)),
            anchor_position="before",
        )
    else:
        return _default_output


def _image(attributes, markdown_state):
    src = attributes.get("src", "")
    alt_text = attributes.get("alt", "")
    if src or alt_text:
        return _WriterOutput("![{0}]({1})".format(_escape_markdown(alt_text), _escape_markdown(src)), "")
    else:
        return _default_output


def _list(ordered):
    def call(attributes, markdown_state):
        if markdown_state.list_state is None:
            start = ""
            end_text = "\n"
            indentation = 0
        else:
            start = "\n"
            end_text = ""
            indentation = markdown_state.list_state.indentation + 1

        def generate_end():
            markdown_state.pop_list_state()
            return end_text

        markdown_state.update_list_state(_MarkdownListState(
            ordered=ordered,
            indentation=indentation,
        ))

        return _WriterOutput(start, generate_end=generate_end)

    return call


def _list_item(attributes, markdown_state):
    markdown_state.list_item_has_closed = False

    list_state = markdown_state.list_state or _MarkdownListState(ordered=False, indentation=0)
    list_state.count += 1

    if list_state.ordered:
        bullet = "{0}.".format(list_state.count)
    else:
        bullet = "-"

    def generate_end():
        if markdown_state.list_item_has_closed:
            return ""
        else:
            markdown_state.list_item_has_closed = True
            return "\n"

    return _WriterOutput(
        start=("\t" * list_state.indentation) + bullet + " ",
        generate_end=generate_end
    )


def _init_writers():
    writers = {
        "p": _Wrapped("", "\n\n"),
        "br": _Wrapped("", "  \n"),
        "strong": _symmetric_wrapped("__"),
        "em": _symmetric_wrapped("*"),
        "a": _hyperlink,
        "img": _image,
        "ol": _list(ordered=True),
        "ul": _list(ordered=False),
        "li": _list_item,
    }

    for level in range(1, 7):
        writers["h{0}".format(level)] = _Wrapped("#" * level + " ", "\n\n")

    return writers


_writers = _init_writers()
_default_output = _WriterOutput("", "")

def _default_writer(attributes, markdown_state):
    return _default_output


class MarkdownWriter(Writer):
    def __init__(self):
        self._fragments = []
        self._element_stack = []
        self._markdown_state = _MarkdownState()

    def text(self, text):
        self._fragments.append(_escape_markdown(text))

    def start(self, name, attributes=None):
        if attributes is None:
            attributes = {}

        output = _writers.get(name, _default_writer)(attributes, self._markdown_state)
        self._element_stack.append(output.generate_end)

        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)



    def end(self, name):
        end = self._element_stack.pop()
        output = end()
        self._fragments.append(output)

    def self_closing(self, name, attributes=None):
        self.start(name, attributes)
        self.end(name)

    def append(self, other):
        self._fragments.append(other)

    def as_string(self):
        return "".join(self._fragments)

    def _write_anchor(self, attributes):
        html_id = attributes.get("id")
        if html_id:
            writer = html.HtmlWriter()
            writer.start("a", {"id": html_id})
            writer.end("a")
            self._fragments.append(writer.as_string())


def _escape_markdown(value):
    return re.sub(r"([\`\*_\{\}\[\]\(\)\#\+\-\.\!])", r"\\\1", re.sub("\\\\", "\\\\\\\\", value))