all repos — cgit @ 7d51120440346108aad74f007431ad65b307f6d7

a hyperfast web frontend for git written in c

filters/html-converters/md2html (view raw)

  1#!/usr/bin/env python3
  2import markdown
  3import sys
  4import io
  5from pygments.formatters import HtmlFormatter
  6sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
  7sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
  8sys.stdout.write('''
  9<style>
 10.markdown-body {
 11    font-size: 14px;
 12    line-height: 1.6;
 13    overflow: hidden;
 14}
 15.markdown-body>*:first-child {
 16    margin-top: 0 !important;
 17}
 18.markdown-body>*:last-child {
 19    margin-bottom: 0 !important;
 20}
 21.markdown-body a.absent {
 22    color: #c00;
 23}
 24.markdown-body a.anchor {
 25    display: block;
 26    padding-left: 30px;
 27    margin-left: -30px;
 28    cursor: pointer;
 29    position: absolute;
 30    top: 0;
 31    left: 0;
 32    bottom: 0;
 33}
 34.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 {
 35    margin: 20px 0 10px;
 36    padding: 0;
 37    font-weight: bold;
 38    -webkit-font-smoothing: antialiased;
 39    cursor: text;
 40    position: relative;
 41}
 42.markdown-body h1 .mini-icon-link, .markdown-body h2 .mini-icon-link, .markdown-body h3 .mini-icon-link, .markdown-body h4 .mini-icon-link, .markdown-body h5 .mini-icon-link, .markdown-body h6 .mini-icon-link {
 43    display: none;
 44    color: #000;
 45}
 46.markdown-body h1:hover a.anchor, .markdown-body h2:hover a.anchor, .markdown-body h3:hover a.anchor, .markdown-body h4:hover a.anchor, .markdown-body h5:hover a.anchor, .markdown-body h6:hover a.anchor {
 47    text-decoration: none;
 48    line-height: 1;
 49    padding-left: 0;
 50    margin-left: -22px;
 51    top: 15%}
 52.markdown-body h1:hover a.anchor .mini-icon-link, .markdown-body h2:hover a.anchor .mini-icon-link, .markdown-body h3:hover a.anchor .mini-icon-link, .markdown-body h4:hover a.anchor .mini-icon-link, .markdown-body h5:hover a.anchor .mini-icon-link, .markdown-body h6:hover a.anchor .mini-icon-link {
 53    display: inline-block;
 54}
 55.markdown-body h1 tt, .markdown-body h1 code, .markdown-body h2 tt, .markdown-body h2 code, .markdown-body h3 tt, .markdown-body h3 code, .markdown-body h4 tt, .markdown-body h4 code, .markdown-body h5 tt, .markdown-body h5 code, .markdown-body h6 tt, .markdown-body h6 code {
 56    font-size: inherit;
 57}
 58.markdown-body h1 {
 59    font-size: 28px;
 60    color: #000;
 61}
 62.markdown-body h2 {
 63    font-size: 24px;
 64    border-bottom: 1px solid #ccc;
 65    color: #000;
 66}
 67.markdown-body h3 {
 68    font-size: 18px;
 69}
 70.markdown-body h4 {
 71    font-size: 16px;
 72}
 73.markdown-body h5 {
 74    font-size: 14px;
 75}
 76.markdown-body h6 {
 77    color: #777;
 78    font-size: 14px;
 79}
 80.markdown-body p, .markdown-body blockquote, .markdown-body ul, .markdown-body ol, .markdown-body dl, .markdown-body table, .markdown-body pre {
 81    margin: 15px 0;
 82}
 83.markdown-body hr {
 84    background: transparent url("/dirty-shade.png") repeat-x 0 0;
 85    border: 0 none;
 86    color: #ccc;
 87    height: 4px;
 88    padding: 0;
 89}
 90.markdown-body>h2:first-child, .markdown-body>h1:first-child, .markdown-body>h1:first-child+h2, .markdown-body>h3:first-child, .markdown-body>h4:first-child, .markdown-body>h5:first-child, .markdown-body>h6:first-child {
 91    margin-top: 0;
 92    padding-top: 0;
 93}
 94.markdown-body a:first-child h1, .markdown-body a:first-child h2, .markdown-body a:first-child h3, .markdown-body a:first-child h4, .markdown-body a:first-child h5, .markdown-body a:first-child h6 {
 95    margin-top: 0;
 96    padding-top: 0;
 97}
 98.markdown-body h1+p, .markdown-body h2+p, .markdown-body h3+p, .markdown-body h4+p, .markdown-body h5+p, .markdown-body h6+p {
 99    margin-top: 0;
100}
101.markdown-body li p.first {
102    display: inline-block;
103}
104.markdown-body ul, .markdown-body ol {
105    padding-left: 30px;
106}
107.markdown-body ul.no-list, .markdown-body ol.no-list {
108    list-style-type: none;
109    padding: 0;
110}
111.markdown-body ul li>:first-child, .markdown-body ul li ul:first-of-type, .markdown-body ul li ol:first-of-type, .markdown-body ol li>:first-child, .markdown-body ol li ul:first-of-type, .markdown-body ol li ol:first-of-type {
112    margin-top: 0px;
113}
114.markdown-body ul li p:last-of-type, .markdown-body ol li p:last-of-type {
115    margin-bottom: 0;
116}
117.markdown-body ul ul, .markdown-body ul ol, .markdown-body ol ol, .markdown-body ol ul {
118    margin-bottom: 0;
119}
120.markdown-body dl {
121    padding: 0;
122}
123.markdown-body dl dt {
124    font-size: 14px;
125    font-weight: bold;
126    font-style: italic;
127    padding: 0;
128    margin: 15px 0 5px;
129}
130.markdown-body dl dt:first-child {
131    padding: 0;
132}
133.markdown-body dl dt>:first-child {
134    margin-top: 0px;
135}
136.markdown-body dl dt>:last-child {
137    margin-bottom: 0px;
138}
139.markdown-body dl dd {
140    margin: 0 0 15px;
141    padding: 0 15px;
142}
143.markdown-body dl dd>:first-child {
144    margin-top: 0px;
145}
146.markdown-body dl dd>:last-child {
147    margin-bottom: 0px;
148}
149.markdown-body blockquote {
150    border-left: 4px solid #DDD;
151    padding: 0 15px;
152    color: #777;
153}
154.markdown-body blockquote>:first-child {
155    margin-top: 0px;
156}
157.markdown-body blockquote>:last-child {
158    margin-bottom: 0px;
159}
160.markdown-body table th {
161    font-weight: bold;
162}
163.markdown-body table th, .markdown-body table td {
164    border: 1px solid #ccc;
165    padding: 6px 13px;
166}
167.markdown-body table tr {
168    border-top: 1px solid #ccc;
169    background-color: #fff;
170}
171.markdown-body table tr:nth-child(2n) {
172    background-color: #f8f8f8;
173}
174.markdown-body img {
175    max-width: 100%;
176    -moz-box-sizing: border-box;
177    box-sizing: border-box;
178}
179.markdown-body span.frame {
180    display: block;
181    overflow: hidden;
182}
183.markdown-body span.frame>span {
184    border: 1px solid #ddd;
185    display: block;
186    float: left;
187    overflow: hidden;
188    margin: 13px 0 0;
189    padding: 7px;
190    width: auto;
191}
192.markdown-body span.frame span img {
193    display: block;
194    float: left;
195}
196.markdown-body span.frame span span {
197    clear: both;
198    color: #333;
199    display: block;
200    padding: 5px 0 0;
201}
202.markdown-body span.align-center {
203    display: block;
204    overflow: hidden;
205    clear: both;
206}
207.markdown-body span.align-center>span {
208    display: block;
209    overflow: hidden;
210    margin: 13px auto 0;
211    text-align: center;
212}
213.markdown-body span.align-center span img {
214    margin: 0 auto;
215    text-align: center;
216}
217.markdown-body span.align-right {
218    display: block;
219    overflow: hidden;
220    clear: both;
221}
222.markdown-body span.align-right>span {
223    display: block;
224    overflow: hidden;
225    margin: 13px 0 0;
226    text-align: right;
227}
228.markdown-body span.align-right span img {
229    margin: 0;
230    text-align: right;
231}
232.markdown-body span.float-left {
233    display: block;
234    margin-right: 13px;
235    overflow: hidden;
236    float: left;
237}
238.markdown-body span.float-left span {
239    margin: 13px 0 0;
240}
241.markdown-body span.float-right {
242    display: block;
243    margin-left: 13px;
244    overflow: hidden;
245    float: right;
246}
247.markdown-body span.float-right>span {
248    display: block;
249    overflow: hidden;
250    margin: 13px auto 0;
251    text-align: right;
252}
253.markdown-body code, .markdown-body tt {
254    margin: 0 2px;
255    padding: 0px 5px;
256    border: 1px solid #eaeaea;
257    background-color: #f8f8f8;
258    border-radius: 3px;
259}
260.markdown-body code {
261    white-space: nowrap;
262}
263.markdown-body pre>code {
264    margin: 0;
265    padding: 0;
266    white-space: pre;
267    border: none;
268    background: transparent;
269}
270.markdown-body .highlight pre, .markdown-body pre {
271    background-color: #f8f8f8;
272    border: 1px solid #ccc;
273    font-size: 13px;
274    line-height: 19px;
275    overflow: auto;
276    padding: 6px 10px;
277    border-radius: 3px;
278}
279.markdown-body pre code, .markdown-body pre tt {
280    margin: 0;
281    padding: 0;
282    background-color: transparent;
283    border: none;
284}
285''')
286sys.stdout.write(HtmlFormatter(style='pastie').get_style_defs('.highlight'))
287sys.stdout.write('''
288</style>   
289''')
290sys.stdout.write("<div class='markdown-body'>")
291sys.stdout.flush()
292# Note: you may want to run this through bleach for sanitization
293markdown.markdownFromFile(output_format="html5", extensions=["markdown.extensions.fenced_code", "markdown.extensions.codehilite", "markdown.extensions.tables"], extension_configs={"markdown.extensions.codehilite":{"css_class":"highlight"}})
294sys.stdout.write("</div>")