blog

Add more parameters and docs to markdown transpiler

Author
Maarten 'Vngngdn' Vangeneugden
Date
July 18, 2017, 12:48 a.m.
Hash
d3365599fb04a10e62959e03fc0ee9884a656800
Parent
dfc1bea9bc6a47546d45a9b0bd70779a63cf34c0
Modified file
markdown.py

markdown.py

47 additions and 1 deletion.

View changes Hide changes
1
1
fucking shit, I've decided to write my own implementation. Contary to the one in
2
2
PyPI, my version handles **all** cases, and is a **full implementation** of the
3
3
reference.
4
4
"""
+
5
Oh, and just so you know: You don't need an entire shitty object oriented system
+
6
to make something decent. Sometimes the solution is a function. Period.
+
7
"""
+
8
+
9
"""
5
10
+
11
    - headers need to have their ID's be the same as the title. BUT! id's
+
12
      mustn't have spaces, and need to be unique. The latter isn't that big of a
+
13
      deal, but spaces in the header title must be converted to dashes.
+
14
    - HTML code needs to be escaped; & must become &amp;, < and > become &lt;
+
15
      and &gt; and so on. This isn't necessary for UTF-8 symbols such as ©,
+
16
      which can be put in place as is, instead of converting to &copy;.
+
17
    - Some elements have to be placed in the tag itself, such as links in <a />.
+
18
      This is noted with the {#} tags. The context in which they are used in the
+
19
      defaults should give a good explanation on what number points to what.
+
20
    - Remember to support 2 trailing spaces as <br />!
+
21
    - There are also "closing ATX headers": "# title" is the same as 
+
22
      "# title ####" and "# title #". (So it's purely cosmetic, remove the
+
23
      trailing whitespace in these cases)
+
24
    - When code is used, call Pygments to markup the code properly. If a code
+
25
      tag is provided (e.g. "Python", "C", ...), tell that to Pygments as well,
+
26
      so it can do a better job. If nothing is provided, leave it as is. When
+
27
      it's an inline code block (`CODE`), leave that always as is.
+
28
      Look how to do it at
+
29
      <http://docs.getpelican.com/en/stable/content.html#syntax-highlighting>.
+
30
    """
+
31
6
32
def toHTML(
7
33
        file,
8
34
        emphasis = "<em>",
9
35
        emphasis_end  ="</em>",
10
36
        strong = "<strong>",
11
37
        strong_end = "</strong>",
12
38
        unordered_list = "<ul>",
13
39
        unordered_list_end = "</ul>",
14
40
        ordered_list = "<ol>",
15
41
        ordered_list_end = "</ol>",
16
42
        list_item = "<li>",
17
43
        list_item_end = "</li>",
18
44
        hyperlink = "<a href=\"{0}\">",
19
-
        hyperlink_end = "</a>",
+
45
        hyperlink_end = "</a>",
20
46
        image = "<img src=\"{0}\">",
21
47
        image_end = "</img>",
22
48
        ):
+
49
        paragraph_end = "</p>",
+
50
        blockquote = "<quote>",
+
51
        blockquote_end = "</quote>",
+
52
        header1 = "<h1 id=\"{0}\">",
+
53
        header1_end = "</h1>",
+
54
        header2 = "<h2 id=\"{0}\">",
+
55
        header2_end = "</h2>",
+
56
        header3 = "<h3 id=\"{0}\">",
+
57
        header3_end = "</h3>",
+
58
        header4 = "<h4 id=\"{0}\">",
+
59
        header4_end = "</h4>",
+
60
        header5 = "<h5 id=\"{0}\">",
+
61
        header5_end = "</h5>",
+
62
        header6 = "<h6 id=\"{0}\">",
+
63
        header6_end = "</h6>",
+
64
        code = "<code>",
+
65
        code_end = "</code>",
+
66
        ):
23
67
    # Zoom zoom insert magic code here
24
68
    return markdown_code
+
69
+
70
    return markdown_code
25
71