md Python script


  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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python3

# create a color scheme
#   pygmentize -S default -f html -a .codehilite > styles.css
# List all color schemes
#   pygmentize -L style


# pip install markdown
# python3 md.py markdown/ processed/
import sys
import markdown
from markdown.extensions.toc import TocExtension

from os import listdir
from os.path import isfile, join

HEADER = """
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
        <meta charset="UTF-8"> 
        <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@200&display=swap" rel="stylesheet">
        <style>
        :root {
            font-family: 'Roboto Mono', monospace;
            font-size: 14px;
            background-color: #333;
            color: #FFF;
        }

        body {
            width: 100%;
            margin: 0;
            padding: 0;
            display:flex;
            align-items:center;
            flex-direction: column; 
        }
        .main-body {
            width: 1000px;
            margin: 25px;
        }
        @media only screen and (max-width: 1050px) {
            .main-body {
                width: 700px;
            }
        }

        @media only screen and (max-width: 750px) {
            .main-body {
                width: 95%;
                font-size: .9rem;
            }
        }


        .codehilite {
            box-shadow: inset 0 0 4px #000;
            background: #1d1d1dfc;
            border: 2px inset #3e3e3ed6;
            border-left: 3px inset #f99b15;
            color: #666;
            page-break-inside: avoid;
            max-width: 100%;
            padding: .5em 1em;
            display: block;
            word-wrap: break-word;

            overflow: auto;
            overflow-x: auto;
            counter-reset: line;
        }

        .codehilitetable {
            border: 2px inset #3e3e3ed6;
            background: #1d1d1dfc;
            box-shadow: inset 0 0 7px #000;
        }

        .codehilitetable .codehilite {
            border: 0px solid #3e3e3ed6;
            border-left: 3px solid #f99b15;
            box-shadow: inset 0 0 0px #000;
        }

        .codehilite:hover,
        .codehilite:focus {
            border-left: 3px solid #66ffa6;
        }



        a:link, a:visited {
        color: white;
        border: 2px solid #66ffa6;
        padding: 10px 20px;
        margin: 2px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        width: 150px;
        background-color: #3e3e3ed6;
        }

        a:hover, a:active {
        background-color: #66ffa6;
        color: black;
        font-weight: bold;
        }

        .codehilite .hll { background-color: #4f424c }
        # .codehilite  { background: #2f1e2e; color: #e7e9db }
        .codehilite .c { color: #776e71 } /* Comment */
        .codehilite .err { color: #ef6155 } /* Error */
        .codehilite .k { color: #815ba4 } /* Keyword */
        .codehilite .l { color: #f99b15 } /* Literal */
        .codehilite .n { color: #e7e9db } /* Name */
        .codehilite .o { color: #5bc4bf } /* Operator */
        .codehilite .p { color: #e7e9db } /* Punctuation */
        .codehilite .ch { color: #776e71 } /* Comment.Hashbang */
        .codehilite .cm { color: #776e71 } /* Comment.Multiline */
        .codehilite .cp { color: #776e71 } /* Comment.Preproc */
        .codehilite .cpf { color: #776e71 } /* Comment.PreprocFile */
        .codehilite .c1 { color: #776e71 } /* Comment.Single */
        .codehilite .cs { color: #776e71 } /* Comment.Special */
        .codehilite .gd { color: #ef6155 } /* Generic.Deleted */
        .codehilite .ge { font-style: italic } /* Generic.Emph */
        .codehilite .gh { color: #e7e9db; font-weight: bold } /* Generic.Heading */
        .codehilite .gi { color: #48b685 } /* Generic.Inserted */
        .codehilite .gp { color: #776e71; font-weight: bold } /* Generic.Prompt */
        .codehilite .gs { font-weight: bold } /* Generic.Strong */
        .codehilite .gu { color: #5bc4bf; font-weight: bold } /* Generic.Subheading */
        .codehilite .kc { color: #815ba4 } /* Keyword.Constant */
        .codehilite .kd { color: #815ba4 } /* Keyword.Declaration */
        .codehilite .kn { color: #5bc4bf } /* Keyword.Namespace */
        .codehilite .kp { color: #815ba4 } /* Keyword.Pseudo */
        .codehilite .kr { color: #815ba4 } /* Keyword.Reserved */
        .codehilite .kt { color: #fec418 } /* Keyword.Type */
        .codehilite .ld { color: #48b685 } /* Literal.Date */
        .codehilite .m { color: #f99b15 } /* Literal.Number */
        .codehilite .s { color: #48b685 } /* Literal.String */
        .codehilite .na { color: #06b6ef } /* Name.Attribute */
        .codehilite .nb { color: #e7e9db } /* Name.Builtin */
        .codehilite .nc { color: #fec418 } /* Name.Class */
        .codehilite .no { color: #ef6155 } /* Name.Constant */
        .codehilite .nd { color: #5bc4bf } /* Name.Decorator */
        .codehilite .ni { color: #e7e9db } /* Name.Entity */
        .codehilite .ne { color: #ef6155 } /* Name.Exception */
        .codehilite .nf { color: #06b6ef } /* Name.Function */
        .codehilite .nl { color: #e7e9db } /* Name.Label */
        .codehilite .nn { color: #fec418 } /* Name.Namespace */
        .codehilite .nx { color: #06b6ef } /* Name.Other */
        .codehilite .py { color: #e7e9db } /* Name.Property */
        .codehilite .nt { color: #5bc4bf } /* Name.Tag */
        .codehilite .nv { color: #ef6155 } /* Name.Variable */
        .codehilite .ow { color: #5bc4bf } /* Operator.Word */
        .codehilite .w { color: #e7e9db } /* Text.Whitespace */
        .codehilite .mb { color: #f99b15 } /* Literal.Number.Bin */
        .codehilite .mf { color: #f99b15 } /* Literal.Number.Float */
        .codehilite .mh { color: #f99b15 } /* Literal.Number.Hex */
        .codehilite .mi { color: #f99b15 } /* Literal.Number.Integer */
        .codehilite .mo { color: #f99b15 } /* Literal.Number.Oct */
        .codehilite .sa { color: #48b685 } /* Literal.String.Affix */
        .codehilite .sb { color: #48b685 } /* Literal.String.Backtick */
        .codehilite .sc { color: #e7e9db } /* Literal.String.Char */
        .codehilite .dl { color: #48b685 } /* Literal.String.Delimiter */
        .codehilite .sd { color: #776e71 } /* Literal.String.Doc */
        .codehilite .s2 { color: #48b685 } /* Literal.String.Double */
        .codehilite .se { color: #f99b15 } /* Literal.String.Escape */
        .codehilite .sh { color: #48b685 } /* Literal.String.Heredoc */
        .codehilite .si { color: #f99b15 } /* Literal.String.Interpol */
        .codehilite .sx { color: #48b685 } /* Literal.String.Other */
        .codehilite .sr { color: #48b685 } /* Literal.String.Regex */
        .codehilite .s1 { color: #48b685 } /* Literal.String.Single */
        .codehilite .ss { color: #48b685 } /* Literal.String.Symbol */
        .codehilite .bp { color: #e7e9db } /* Name.Builtin.Pseudo */
        .codehilite .fm { color: #06b6ef } /* Name.Function.Magic */
        .codehilite .vc { color: #ef6155 } /* Name.Variable.Class */
        .codehilite .vg { color: #ef6155 } /* Name.Variable.Global */
        .codehilite .vi { color: #ef6155 } /* Name.Variable.Instance */
        .codehilite .vm { color: #ef6155 } /* Name.Variable.Magic */
        .codehilite .il { color: #f99b15 } /* Literal.Number.Integer.Long */



        </style>
    </head> 
    <body>
    <div class="main-body">
"""

FOOTER = """
    </div>
    </body>
</html>
"""

########################################################################################

def proccess_md(file, in_folder, out_folder):
    with open(md_folder+ '/' +file, 'r') as f:
        text = f.read()
        html = markdown.markdown(text, extensions=['tables', 'fenced_code', 'nl2br', TocExtension(toc_depth ="2-6"), 'codehilite'])

    with open(out_folder + '/' + file+".html", 'w') as f:
        content = HEADER + html + FOOTER
        f.write(content)

    print(file, '->', file+'.html')

########################################################################################

if len(sys.argv) != 3:
    print("ERROR: must have 2 arguments -> <md folder path> <output folder path>")
    exit()

md_folder = sys.argv[1]
out_folder = sys.argv[2]

if md_folder[-1] == '/':
        md_folder = md_folder[0:-1]

if out_folder[-1] == '/':
        out_folder = out_folder[0:-1]

all_md_files = [f for f in listdir(md_folder) if isfile(join(md_folder, f))]

for f in all_md_files:
    proccess_md(f, md_folder, out_folder)

Home