summaryrefslogtreecommitdiff
path: root/modifiedmarkdown/mdown.py
blob: 41ff6c565577f012b016504c8036049b51aa53a4 (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
"""
    MoinMoin - Parser for Markdown

    Syntax:

        To use in a code block:
    
            {{{{#!markdown
            <add markdown text here>
            }}}}

        To use for an entire page:

            #format markdown
            <add markdown text here>

    @copyright: 2009 by Jason Fruit (JasonFruit at g mail dot com)
    @license: GNU GPL, see http://www.gnu.org/licenses/gpl for details

"""


from markdown import util
from markdown import Markdown
from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern


class ToDoPattern(Pattern):

    def __init__(self):
        Pattern.__init__(self, "^\[([xX ])\]")
        self.counter = 0

    def handleMatch(self, m):
        node = util.etree.Element("input")

        node.attrib["type"] = "checkbox"
        node.attrib["id"] = "md_cbx_{0}".format(self.counter)
        self.counter += 1
        node.attrib["value"] = m.group(3).strip()

        if m.group(2).upper() == "X":
            node.attrib["checked"] = "checked"

        return node


class ToDoExtension(Extension):

    def extendMarkdown(self, md, md_globals):
        md.inlinePatterns.add('todo', ToDoPattern(), "_begin")



Dependencies = ['user']

class Parser:
    """
    A thin wrapper around a Python implementation
    (http://www.freewisdom.org/projects/python-markdown/) of John
    Gruber's Markdown (http://daringfireball.net/projects/markdown/)
    to make it suitable for use with MoinMoin.
    """
    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request

    def format(self, formatter):
        todo = ToDoExtension(configs={})
        output_html = Markdown(extensions=[todo]).convert(self.raw)

        try:
            self.request.write(formatter.rawHTML(output_html))
        except:
            self.request.write(formatter.escapedText(output_html))