-
Notifications
You must be signed in to change notification settings - Fork 3
/
dingus.py
514 lines (420 loc) · 14.4 KB
/
dingus.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from bottle import route, run, request, template
import markdown
from markdown.extensions import extra
import os
# Get a list of markdown extensions
extensions = [ext.name for ext in markdown.util.get_installed_extensions() if ext.name not in extra.extensions + ['extra']]
extensions.sort()
extra.extensions.sort()
@route('/babelmark')
def babelmark():
""" Provide a hook for http://johnmacfarlane.net/babelmark2/ to use. """
src = request.query.get('text', '')
return {
'name' : 'Python-Markdown',
'version': markdown.__version__,
'html' : markdown.markdown(src)
}
@route('/dingus', method=('GET', 'POST'))
def dingus():
context = {'extensions': extensions, 'extra': extra.extensions}
# Get data from GET or POST
context['src'] = request.params.get('src', '')
context['ext'] = request.params.getall('ext')
context['output_format'] = request.params.get('output_format', '')
# Build rest of context
context['version'] = markdown.__version__
# Build command
cmd = 'markdown.markdown(src'
if context['ext']:
cmd += ', extensions={}'.format(context['ext'])
if context['output_format']:
cmd += ", output_format='{}'".format(context['output_format'])
context['cmd'] = cmd + ')'
# Convert
kwargs = {'extensions': context['ext']}
if context['output_format']: kwargs['output_format'] = context['output_format']
context['result'] = markdown.markdown(context['src'], **kwargs)
return template(tmpl, **context)
tmpl = """<!doctype html>
<html>
<head>
<title>Python-Markdown: Dingus</title>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/github.min.css">
<style type="text/css">
/*
Basic layout courtesy of InkNoise's very nifty Layout-o-matic:
http://www.inknoise.com/experimental/layoutomatic.php
*/
#sidebar h1 {
font-size: 1.5em;
font-weight: bold;
}
#sidebar h2 {
font-size: 1.2em;
font-weight: bold;
margin-bottom: -.5em;
}
#sidebar h3 {
font-size: 1em;
font-weight: bold;
text-transform: none;
margin-bottom: .25em;
margin-top: 1.5em;
}
#sidebar code {
font-family: Monaco, ProFont, "Andale Mono", "Lucida Console", Courier, monospace;
font-size: 10px;
}
#sidebar pre {
line-height: 12px;
margin-top: 0;
background-color: #f5f5f5;
border: 1px solid #ccc;
padding: 4px;
}
#sidebar p {
margin-top: 0;
margin-bottom: 0;
}
body {
background-color: #eee;
font-family: "Lucida Grande", Verdana, sans-serif;
font-size: 11px;
line-height: 1.6em;
}
.renderbox {
background: white;
font-family: Georgia, serif;
font-size: 13px;
border: 1px #888 solid;
padding: 0 5px;
margin: 0;
width: 97%;
overflow: auto;
}
div.codehilite pre code, pre.renderbox code {
background: transparent;
}
.label {
margin-bottom: 4px;
}
#container {
border: 0px solid gray;
margin: 10px;
margin-left: auto;
margin-right: auto;
padding: 0px;
min-width: 750px;
/* For Win/IE: */
width:expression("800px" );
margin-left:expression("0");
}
#banner {
padding: 0;
margin-bottom: 5px;
background-color: transparent;
}
#app {
padding: 0 10px 0 10px;
margin-right: 270px;
background-color: transparent;
border-right: 0px solid #bbb;
}
#sidebar {
float: right;
width: 250px;
margin: 0;
margin-right: 10px;
padding: 0;
background-color: transparent;
}
.footer {
margin-top: 100px;
}
#buttonrow {
margin-top: 10px;
margin-bottom: 60px;
}
#convert {
width: 7em;
margin-left:20px;
}
textarea[name="src"] {
/* WinIE is retarded. Thanks to Sam Ruby for the workaround:
http://www.intertwingly.net/blog/1432.html */
width: 98%;
}
</style>
<style>
.codehilite code { background-color: #ffffcc }
.codehilite .hll { background-color: #ffffcc }
.codehilite { background: #f0f0f0; }
.codehilite .c { color: #60a0b0; font-style: italic } /* Comment */
.codehilite .err { border: 1px solid #FF0000 } /* Error */
.codehilite .k { color: #007020; font-weight: bold } /* Keyword */
.codehilite .o { color: #666666 } /* Operator */
.codehilite .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */
.codehilite .cp { color: #007020 } /* Comment.Preproc */
.codehilite .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */
.codehilite .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */
.codehilite .gd { color: #A00000 } /* Generic.Deleted */
.codehilite .ge { font-style: italic } /* Generic.Emph */
.codehilite .gr { color: #FF0000 } /* Generic.Error */
.codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.codehilite .gi { color: #00A000 } /* Generic.Inserted */
.codehilite .go { color: #808080 } /* Generic.Output */
.codehilite .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.codehilite .gs { font-weight: bold } /* Generic.Strong */
.codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.codehilite .gt { color: #0040D0 } /* Generic.Traceback */
.codehilite .kc { color: #007020; font-weight: bold } /* Keyword.Constant */
.codehilite .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */
.codehilite .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */
.codehilite .kp { color: #007020 } /* Keyword.Pseudo */
.codehilite .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */
.codehilite .kt { color: #902000 } /* Keyword.Type */
.codehilite .m { color: #40a070 } /* Literal.Number */
.codehilite .s { color: #4070a0 } /* Literal.String */
.codehilite .na { color: #4070a0 } /* Name.Attribute */
.codehilite .nb { color: #007020 } /* Name.Builtin */
.codehilite .nc { color: #0e84b5; font-weight: bold } /* Name.Class */
.codehilite .no { color: #60add5 } /* Name.Constant */
.codehilite .nd { color: #555555; font-weight: bold } /* Name.Decorator */
.codehilite .ni { color: #d55537; font-weight: bold } /* Name.Entity */
.codehilite .ne { color: #007020 } /* Name.Exception */
.codehilite .nf { color: #06287e } /* Name.Function */
.codehilite .nl { color: #002070; font-weight: bold } /* Name.Label */
.codehilite .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.codehilite .nt { color: #062873; font-weight: bold } /* Name.Tag */
.codehilite .nv { color: #bb60d5 } /* Name.Variable */
.codehilite .ow { color: #007020; font-weight: bold } /* Operator.Word */
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */
.codehilite .mf { color: #40a070 } /* Literal.Number.Float */
.codehilite .mh { color: #40a070 } /* Literal.Number.Hex */
.codehilite .mi { color: #40a070 } /* Literal.Number.Integer */
.codehilite .mo { color: #40a070 } /* Literal.Number.Oct */
.codehilite .sb { color: #4070a0 } /* Literal.String.Backtick */
.codehilite .sc { color: #4070a0 } /* Literal.String.Char */
.codehilite .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */
.codehilite .s2 { color: #4070a0 } /* Literal.String.Double */
.codehilite .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */
.codehilite .sh { color: #4070a0 } /* Literal.String.Heredoc */
.codehilite .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */
.codehilite .sx { color: #c65d09 } /* Literal.String.Other */
.codehilite .sr { color: #235388 } /* Literal.String.Regex */
.codehilite .s1 { color: #4070a0 } /* Literal.String.Single */
.codehilite .ss { color: #517918 } /* Literal.String.Symbol */
.codehilite .bp { color: #007020 } /* Name.Builtin.Pseudo */
.codehilite .vc { color: #bb60d5 } /* Name.Variable.Class */
.codehilite .vg { color: #bb60d5 } /* Name.Variable.Global */
.codehilite .vi { color: #bb60d5 } /* Name.Variable.Instance */
.codehilite .il { color: #40a070 } /* Literal.Number.Integer.Long */
</style>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// JS is active, so setup for it.
$("#extensions").hide();
var showExt = true;
$("#toggle_ext").show();
if ($("#extra input").is(":checked")) {
$("#extra-exts").find(':input:not(:disabled)').prop('disabled',true);
};
// Highlight "HTML Source" and "Python Code" - but not "HTML Preview" or cheatsheet blocks.
// If codehilite is used, that highlight codeblocks in the preview seperately.
$("pre.renderbox code").each(function(i, e) {hljs.highlightBlock(e)});
// Bind toggle click
$("#toggle_ext").click(function(event){
if (showExt == true) {
$("#extensions").slideDown("fast");
$(event.target).text("Hide Extensions ^");
showExt = false;
} else if (showExt == false) {
$("#extensions").slideUp("fast");
$(event.target).text("Show Extensions v");
showExt = true;
};
event.preventDefault();
});
// Bind Extra input change
$("#extra input").change(function(event){
if (event.target.checked == true) {
$("#extra-exts").find(':input:not(:disabled)').prop('disabled',true);
} else if (event.target.checked == false) {
$("#extra-exts").find(':input:disabled').prop('disabled',false);
};
});
});
</script>
</head>
<body>
<div id="container">
<div id="sidebar">
<h1>Python-Markdown: Dingus</h1>
<h2>Syntax Cheatsheet:</h2>
<h3>Phrase Emphasis</h3>
<pre><code>*italic* **bold**
_italic_ __bold__
</code></pre>
<h3>Links</h3>
<p>Inline:</p>
<pre><code>An [example](http://url.com/ "Title")
</code></pre>
<p>Reference-style labels (titles are optional):</p>
<pre><code>An [example][id]. Then, anywhere
else in the doc, define the link:
[id]: http://example.com/ "Title"
</code></pre>
<h3>Images</h3>
<p>Inline (titles are optional):</p>
<pre><code>![alt text](/path/img.jpg "Title")
</code></pre>
<p>Reference-style:</p>
<pre><code>![alt text][id]
[id]: /url/to/img.jpg "Title"
</code></pre>
<h3>Headers</h3>
<p>Setext-style:</p>
<pre><code>Header 1
========
Header 2
--------
</code></pre>
<p>atx-style (closing #'s are optional):</p>
<pre><code># Header 1 #
## Header 2 ##
###### Header 6
</code></pre>
<h3>Lists</h3>
<p>Ordered, without paragraphs:</p>
<pre><code>1. Foo
2. Bar
</code></pre>
<p>Unordered, with paragraphs:</p>
<pre><code>* A list item.
With multiple paragraphs.
* Bar
</code></pre>
<p>You can nest them:</p>
<pre><code>* Abacus
* answer
* Bubbles
1. bunk
2. bupkis
* BELITTLER
3. burper
* Cunning
</code></pre>
<h3>Blockquotes</h3>
<pre><code>> Email-style angle brackets
> are used for blockquotes.
> > And, they can be nested.
> #### Headers in blockquotes
>
> * You can quote a list.
> * Etc.
</code></pre>
<h3>Code Spans</h3>
<pre><code>`<code>` spans are delimited
by backticks.
You can include literal backticks
like `` `this` ``.
</code></pre>
<h3>Preformatted Code Blocks</h3>
<p>Indent every line of a code block by at least 4 spaces or 1 tab.</p>
<pre><code>This is a normal paragraph.
This is a preformatted
code block.
</code></pre>
<h3>Horizontal Rules</h3>
<p>Three or more dashes or asterisks:</p>
<pre><code>---
* * *
- - - -
</code></pre>
<h3>Manual Line Breaks</h3>
<p>End a line with two or more spaces:</p>
<pre><code>Roses are red,
Violets are blue.
</code></pre>
</div> <!-- sidebar -->
<div id="app">
<form action="/dingus" method="post">
<div id="src">
<p class="label">Markdown Source:</p>
<textarea name="src" rows="25" cols="80">{{ src }}</textarea>
</div> <!-- src -->
<div id="buttonrow">
<fieldset id="extensions">
<legend>Extensions</legend>
%for x in extensions:
<label id="{{ x }}"><input type="checkbox" name="ext" value="{{ x }}"\\\\
%if x in ext:
checked\\\\
%end
/><code>{{ x }}</code></label>
%end
<fieldset>
<legend><label id="extra"><input type="checkbox" name="ext" value="extra"\\\\
%if "extra" in ext:
checked\\\\
%end
/><code>extra</code></label></legend>
<div id="extra-exts">
%for x in extra:
<label id="{{ x }}"><input type="checkbox" name="ext" value="{{ x }}"\\\\
%if x in ext:
checked\\\\
%end
/><code>{{ x }}</code></label>
%end
</div> <!-- extra-exts -->
</fieldset>
</fieldset> <!-- extensions -->
<a href="#" id="toggle_ext" style="display:none">Show Extensions v</a>
<label id="output_format">Output_Format:
<select name="output_format" value="{{ output_format }}">
<option value="">xhtml (default)</option>
<option value="html"\\\\
%if output_format == "html":
selected="selected"\\\\
%end
>html</option>
</select>
</label>
<input type="submit" id="convert" value="Convert" />
</div> <!--buttonrow-->
</form>
%if result:
<p class="label">HTML Source:</p>
<pre id="rawoutput" class="renderbox"><code class="html">{{ result }}</code></pre>
<p class="label">HTML Preview:</p>
<div id="output" class="renderbox">
{{! result }}
</div>
<p class="label">Python Code:</p>
<pre id="python" class="renderbox"><code class="python">import markdown
html = {{ cmd }}</code></pre>
%end
<p class='footer'>
<a href="https://python-markdown.github.io/">Python-Markdown</a> version {{ version }}<br />
Copyright © 2007-2020 Python-Markdown Project (v. 1.7 and later)<br />
Copyright © 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)<br />
Copyright © 2004 Manfred Stienstra (the original version)<br />
<br />
<a href="http://daringfireball.net/projects/markdown/">Markdown</a> and
<a href="http://daringfireball.net/projects/markdown/dingus">Dingus</a> Copyright © 2004
<a href="http://daringfireball.net/colophon/">John Gruber</a><br />
Additions and Modifications to Dingus (extension support, etc.) Copyright © 2012-2023
<a href="https://github.com/waylan">Waylan Limberg</a>
</p>
</div> <!-- app -->
</div> <!-- container -->
</body>
</html>
"""
if __name__ == '__main__':
run(host='localhost', port=8080, reloader=True)