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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
#!/usr/bin/env python3
import markdown
import sys
import io
import re
from pygments.formatters import HtmlFormatter
from markdown.extensions.toc import TocExtension
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
# Python-Markdown (unlike CommonMark/GitHub) refuses to let a list interrupt a
# paragraph unless there's a blank line between them, so a very common README
# shape renders as one run-on paragraph instead of a list:
#
# Passing:
# - foo
# - bar
#
# This preprocessor restores the GitHub behavior: when a bullet ( - * + ) or a
# "1."/"1)" ordered-list marker immediately follows a paragraph line, insert a
# blank line so the list starts. We deliberately only interrupt on "1" for
# ordered lists (matching CommonMark) so prose like "... the year\n1985. was
# ..." is not turned into a list. Fenced code, indented code, and lines that
# already follow a list item are left untouched.
_MD_FENCE = re.compile(r'^(\s{0,3})(`{3,}|~{3,})')
_MD_BULLET = re.compile(r'^ {0,3}[-*+][ \t]+\S')
_MD_ORDERED1 = re.compile(r'^ {0,3}1[.)][ \t]+\S')
_MD_ANYITEM = re.compile(r'^ {0,3}(?:[-*+]|\d{1,9}[.)])[ \t]')
class _ListInterruptPreprocessor(Preprocessor):
def run(self, lines):
out, in_fence, fence = [], False, None
# Track whether we're already inside a list. A bullet that merely
# continues an existing list must NOT get a blank line inserted before
# it: that would turn a tight list into a loose one (every item wrapped
# in <p>), which is exactly what happens when a preceding item wraps onto
# an indented continuation line and we mistake the next bullet for a
# paragraph->list transition. We only want to interrupt a real paragraph.
in_list = False
for line in lines:
m = _MD_FENCE.match(line)
if m:
tok = m.group(2)[0]
if not in_fence:
in_fence, fence = True, tok
elif fence == tok:
in_fence, fence = False, None
out.append(line)
continue
if in_fence:
out.append(line)
continue
if not line.strip():
# a blank line doesn't end a list (loose lists / lazy
# continuation), so leave in_list untouched
out.append(line)
continue
is_item = bool(_MD_ANYITEM.match(line))
indented = line[:1] in (' ', '\t')
# a non-indented, non-item line after a list closes that list
if in_list and not is_item and not indented:
in_list = False
if out:
prev = out[-1]
if (prev.strip() and not in_list
and not _MD_ANYITEM.match(prev)
and not prev.startswith((' ', '\t'))
and (_MD_BULLET.match(line) or _MD_ORDERED1.match(line))):
out.append('')
if is_item:
in_list = True
out.append(line)
return out
class ListInterruptExtension(Extension):
def extendMarkdown(self, md):
# priority 21: just above the built-in 'normalize_whitespace' (20) so we
# operate on clean lines before block parsing.
md.preprocessors.register(
_ListInterruptPreprocessor(md), 'list_interrupt', 21)
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stdout.write('''
<style>
.markdown-body {
font-size: 14px;
line-height: 1.6;
overflow: hidden;
}
.markdown-body>*:first-child {
margin-top: 0 !important;
}
.markdown-body>*:last-child {
margin-bottom: 0 !important;
}
.markdown-body a.absent {
color: #c00;
}
.markdown-body a.anchor {
display: block;
padding-left: 30px;
margin-left: -30px;
cursor: pointer;
position: absolute;
top: 0;
left: 0;
bottom: 0;
}
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
cursor: text;
position: relative;
}
.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 {
display: none;
color: #000;
}
.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 {
text-decoration: none;
line-height: 1;
padding-left: 0;
margin-left: -22px;
top: 15%;
}
.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 {
display: inline-block;
}
div#cgit .markdown-body h1 a.toclink, div#cgit .markdown-body h2 a.toclink, div#cgit .markdown-body h3 a.toclink, div#cgit .markdown-body h4 a.toclink, div#cgit .markdown-body h5 a.toclink, div#cgit .markdown-body h6 a.toclink {
color: black;
}
.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 {
font-size: inherit;
}
.markdown-body h1 {
font-size: 28px;
color: #000;
}
.markdown-body h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
.markdown-body h3 {
font-size: 18px;
}
.markdown-body h4 {
font-size: 16px;
}
.markdown-body h5 {
font-size: 14px;
}
.markdown-body h6 {
color: #777;
font-size: 14px;
}
.markdown-body p, .markdown-body blockquote, .markdown-body ul, .markdown-body ol, .markdown-body dl, .markdown-body table, .markdown-body pre {
margin: 15px 0;
}
.markdown-body hr {
border: 2px solid #ccc;
}
.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 {
margin-top: 0;
padding-top: 0;
}
.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 {
margin-top: 0;
padding-top: 0;
}
.markdown-body h1+p, .markdown-body h2+p, .markdown-body h3+p, .markdown-body h4+p, .markdown-body h5+p, .markdown-body h6+p {
margin-top: 0;
}
.markdown-body li p.first {
display: inline-block;
}
.markdown-body ul, .markdown-body ol {
padding-left: 30px;
}
.markdown-body ul.no-list, .markdown-body ol.no-list {
list-style-type: none;
padding: 0;
}
.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 {
margin-top: 0px;
}
.markdown-body ul li p:last-of-type, .markdown-body ol li p:last-of-type {
margin-bottom: 0;
}
.markdown-body ul ul, .markdown-body ul ol, .markdown-body ol ol, .markdown-body ol ul {
margin-bottom: 0;
}
.markdown-body dl {
padding: 0;
}
.markdown-body dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
.markdown-body dl dt:first-child {
padding: 0;
}
.markdown-body dl dt>:first-child {
margin-top: 0px;
}
.markdown-body dl dt>:last-child {
margin-bottom: 0px;
}
.markdown-body dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
.markdown-body dl dd>:first-child {
margin-top: 0px;
}
.markdown-body dl dd>:last-child {
margin-bottom: 0px;
}
.markdown-body blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
.markdown-body blockquote>:first-child {
margin-top: 0px;
}
.markdown-body blockquote>:last-child {
margin-bottom: 0px;
}
.markdown-body table th {
font-weight: bold;
}
.markdown-body table th, .markdown-body table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
.markdown-body table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
.markdown-body table tr:nth-child(2n) {
background-color: #f8f8f8;
}
.markdown-body img {
max-width: 100%;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.markdown-body span.frame {
display: block;
overflow: hidden;
}
.markdown-body span.frame>span {
border: 1px solid #ddd;
display: block;
float: left;
overflow: hidden;
margin: 13px 0 0;
padding: 7px;
width: auto;
}
.markdown-body span.frame span img {
display: block;
float: left;
}
.markdown-body span.frame span span {
clear: both;
color: #333;
display: block;
padding: 5px 0 0;
}
.markdown-body span.align-center {
display: block;
overflow: hidden;
clear: both;
}
.markdown-body span.align-center>span {
display: block;
overflow: hidden;
margin: 13px auto 0;
text-align: center;
}
.markdown-body span.align-center span img {
margin: 0 auto;
text-align: center;
}
.markdown-body span.align-right {
display: block;
overflow: hidden;
clear: both;
}
.markdown-body span.align-right>span {
display: block;
overflow: hidden;
margin: 13px 0 0;
text-align: right;
}
.markdown-body span.align-right span img {
margin: 0;
text-align: right;
}
.markdown-body span.float-left {
display: block;
margin-right: 13px;
overflow: hidden;
float: left;
}
.markdown-body span.float-left span {
margin: 13px 0 0;
}
.markdown-body span.float-right {
display: block;
margin-left: 13px;
overflow: hidden;
float: right;
}
.markdown-body span.float-right>span {
display: block;
overflow: hidden;
margin: 13px auto 0;
text-align: right;
}
.markdown-body code, .markdown-body tt {
margin: 0 2px;
padding: 0px 5px;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
.markdown-body code {
white-space: nowrap;
}
.markdown-body pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
.markdown-body .highlight pre, .markdown-body pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
.markdown-body pre code, .markdown-body pre tt {
margin: 0;
padding: 0;
background-color: transparent;
border: none;
}
''')
sys.stdout.write(HtmlFormatter(style='pastie').get_style_defs('.highlight'))
sys.stdout.write('''
</style>
''')
# Render to a string (not straight to stdout) so we can post-process it.
# Note: you may want to run this through bleach for sanitization
html = markdown.markdown(
sys.stdin.read(),
output_format="html5",
extensions=[
"markdown.extensions.fenced_code",
"markdown.extensions.codehilite",
"markdown.extensions.tables",
"markdown.extensions.sane_lists",
ListInterruptExtension(),
TocExtension(anchorlink=True)],
extension_configs={
"markdown.extensions.codehilite":{"css_class":"highlight"}})
# A README's relative paths point at repo content, but a browser resolves them
# against the page the readme is shown on (/<repo>/about/... or the summary page
# /<repo>/), not against the file's own location — so they 404 or hit the wrong
# file. cgit serves this content through three commands, so rewrite relative
# references to absolute cgit URLs (absolute so they work on the about page and
# the summary page alike):
# <img src> -> /<repo>/plain/<path>?h=<branch> (raw bytes)
# <a href> *.md -> /<repo>/about/<path> (rendered markdown)
# <a href> other/dir -> /<repo>/tree/<path>?h=<branch> (browsable blob view)
# cgit hands the about-filter the path of the file being rendered as argv[1]
# (README.md, or docs/foo.md for a rendered subpage), so relative links resolve
# against *that* file's directory and links inside a rendered subpage work too.
# A href that climbs out of the repo with ".." (e.g. ../sibling) is resolved
# against the repo's URL location (-> /sibling). The about view renders against
# the configured readme ref, so about/ links carry no ?h=. If the repo context
# env vars are absent we leave the references untouched.
import os, re, subprocess
from urllib.parse import urljoin
repo = os.environ.get("CGIT_REPO_URL", "").strip("/")
if repo:
branch = os.environ.get("CGIT_REPO_DEFBRANCH", "").strip()
repo_root = "/" + repo + "/"
plain = repo_root + "plain/"
query = ("?h=" + branch) if branch else ""
# directory of the file being rendered; relative links resolve against it.
# argv[1] is the repo-relative readme/subpage path cgit passes the filter.
cur = sys.argv[1] if len(sys.argv) > 1 else ""
base_dir = "" if cur.startswith("/") else os.path.dirname(cur)
# markdown files are rendered via the about view; everything else is a blob
md_ext = re.compile(r"\.(?:md|markdown|mdown|mkd)$", re.I)
# leave absolute, protocol-relative, root-relative, anchor, query-only and
# scheme (mailto:, http:, data:, ...) URLs alone
skip = re.compile(r"^(?:[a-z][a-z0-9+.\-]*:|//|/|[#?])", re.I)
def _split_frag(url):
# split off a trailing #fragment (kept verbatim on the rewritten URL)
if "#" in url:
path, frag = url.split("#", 1)
return path, "#" + frag
return url, ""
def _resolve(path):
# resolve `path` (relative to base_dir) into a repo-root-relative path,
# collapsing "." / ".."; returns (climbs, cleaned) where `climbs` counts
# ".." segments that escaped above the repo root.
combined = (base_dir + "/" + path) if base_dir else path
out, climbs = [], 0
for seg in combined.split("/"):
if seg in ("", "."):
continue
if seg == "..":
if out:
out.pop()
else:
climbs += 1
else:
out.append(seg)
return climbs, "/".join(out)
def rewrite_img(m):
url = m.group(2)
if not url or skip.match(url):
return m.group(0)
path, frag = _split_frag(url)
climbs, cleaned = _resolve(path)
if climbs or not cleaned:
return m.group(0)
return m.group(1) + plain + cleaned + query + frag + m.group(3)
def rewrite_href(m):
url = m.group(2)
if not url or skip.match(url):
return m.group(0)
path, frag = _split_frag(url)
climbs, cleaned = _resolve(path)
if climbs:
# escapes the repo (sibling repo etc.): resolve against repo location
dest = urljoin(repo_root, "../" * climbs + cleaned)
elif not cleaned:
dest = repo_root + (query or "")
elif md_ext.search(cleaned):
# rendered through the about-filter (renders against the readme ref)
dest = repo_root + "about/" + cleaned
else:
dest = repo_root + "tree/" + cleaned + query
return m.group(1) + dest + frag + m.group(3)
html = re.sub(r'(<img\b[^>]*?\bsrc=")([^"]*)(")', rewrite_img, html, flags=re.I)
html = re.sub(r'(<a\b[^>]*?\bhref=")([^"]*)(")', rewrite_href, html, flags=re.I)
# Auto-link bare repo paths. A README that merely *mentions* a file/dir in
# prose or an inline `code` span (e.g. `src/cpu.c`, tools/gbgif.py, Makefile)
# is far more navigable if those become links. To avoid linking garbage we
# only link tokens that resolve to a path that ACTUALLY EXISTS in the repo,
# so we ask git for the full file+dir list (at the repo's default branch) and
# match against it. Destinations follow the same routing as explicit links
# (*.md -> about/, everything else -> tree/). We never touch <pre> code
# blocks, existing <a> links (no nested anchors), or tag attributes.
def dest_for(p, is_md=None):
if md_ext.search(p) if is_md is None else is_md:
return repo_root + "about/" + p
return repo_root + "tree/" + p + query
def _norm(path):
# collapse "."/".." with no base (repo-root-relative); like _resolve but
# without base_dir, used to also accept paths written from the repo root.
out, climbs = [], 0
for seg in path.split("/"):
if seg in ("", "."):
continue
if seg == "..":
if out:
out.pop()
else:
climbs += 1
else:
out.append(seg)
return climbs, "/".join(out)
known = set()
repo_path = os.environ.get("CGIT_REPO_PATH", "")
if repo_path and ("<code>" in html or "/" in html):
try:
ls = subprocess.run(
["git", "-C", repo_path, "-c", "core.quotepath=false",
"ls-tree", "-r", "-t", "-z", "--name-only", branch or "HEAD"],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=5)
known = set(p for p in ls.stdout.decode("utf-8", "replace").split("\0") if p)
except Exception:
known = set()
if known:
def resolve_known(token):
t = token.strip()
# need a real name: reject blanks, tokens with spaces, and pure
# relative markers like "." / ".." / "./" (which would otherwise
# resolve to the base directory and mislink).
if not t or " " in t or not re.search(r"[A-Za-z0-9]", t):
return None
for climbs, cleaned in (_resolve(t), _norm(t)):
if not climbs and cleaned in known:
return cleaned
return None
def link(cleaned, text):
return '<a class="pathlink" href="%s">%s</a>' % (dest_for(cleaned), text)
# A path token: word chars / . / - / _ , optionally multi-segment. In
# prose we require at least one "/" (a strong path signal) to avoid
# linking ordinary words that happen to match a top-level filename; in an
# inline code span the whole content is matched verbatim, so a bare
# `Makefile` or `README` still links.
prose_tok = re.compile(r"(?<![\w./-])([\w.\-]+(?:/[\w.\-]+)+)(?![\w./-])")
def linkify_prose(text):
def repl(m):
c = resolve_known(m.group(1))
return link(c, m.group(1)) if c else m.group(0)
return prose_tok.sub(repl, text)
def linkify_code(text):
c = resolve_known(text)
return link(c, text) if c else text
# Mask <pre>...</pre> and existing <a>...</a> so they're never touched
# (no linking inside code blocks, no nested anchors / re-linked hrefs).
masks = []
def _mask(m):
masks.append(m.group(0))
return "\x00%d\x00" % (len(masks) - 1)
masked = re.sub(r"<pre\b.*?</pre>", _mask, html, flags=re.I | re.S)
masked = re.sub(r"<a\b.*?</a>", _mask, masked, flags=re.I | re.S)
# Walk text nodes only (odd list items are tags); inside an inline <code>
# use verbatim matching, elsewhere use the stricter prose matcher.
parts = re.split(r"(<[^>]+>)", masked)
for i in range(0, len(parts), 2):
text = parts[i]
if not text:
continue
prev = parts[i - 1] if i > 0 else ""
if prev[:5].lower() == "<code":
parts[i] = linkify_code(text)
else:
parts[i] = linkify_prose(text)
masked = "".join(parts)
html = re.sub(r"\x00(\d+)\x00", lambda m: masks[int(m.group(1))], masked)
sys.stdout.write("<div class='markdown-body'>")
sys.stdout.write(html)
sys.stdout.write("</div>")
|