-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.py
1462 lines (1164 loc) · 44.4 KB
/
custom.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
import errno
import itertools
import json
import logging
import os
import re
import shlex
import subprocess
import jinja2
import six
import markdown
from markdown import inlinepatterns
from markdown import treeprocessors
from markdown.blockprocessors import BlockProcessor
from markdown.extensions import Extension
from markdown.extensions.toc import slugify
from markdown.extensions import fenced_code
from markdown.util import etree
import mkdocs.config as _ # pylint: disable=unused-import
# work around for mkdocs import cycle
from mkdocs.plugins import BasePlugin
from mkdocs.nav import Page, Header
from guild import guildfile
log = logging.getLogger("mkdocs")
nav_pages = []
nav_pages_index = None
class NavResolvePlugin(BasePlugin):
def on_nav(self, nav, config):
"""Process nav items.
We use page meta for nav title and other config settings so we
need to read the page source. To avoid re-reading we disable
the read_source function.
"""
globals()["nav_pages"] = []
globals()["nav_pages_index"] = None
for item in nav:
self._ensure_loaded(item, config)
return nav
def _ensure_loaded(self, item, config):
if isinstance(item, Header):
for item in item.children:
self._ensure_loaded(item, config)
elif isinstance(item, Page):
item.read_source(config)
item.read_source = lambda **_kw: None
nav_pages.append(item)
class FixTocProcessor(treeprocessors.Treeprocessor):
def run(self, doc):
for toc in self._iter_toc(doc):
self._fix_toc(toc)
@staticmethod
def _iter_toc(root):
for el in root.iter():
if el.get("class") == "toc":
yield el
def _fix_toc(self, toc):
self._fix_tree(toc)
self._ul_to_ol(toc)
@staticmethod
def _fix_tree(toc):
children = list(toc)
if len(children) != 1 or children[0].tag != "ul":
raise AssertionError(etree.tostring(toc))
ul = children[0]
toc.tag = "ol"
toc.remove(ul)
items = list(ul)
if len(items) == 1:
item1 = items[0]
item1_children = list(item1)
if (len(item1_children) == 1 and
item1_children[0].tag == "a"):
items = []
elif (len(item1_children) == 2 and
item1_children[0].tag == "a" and
item1_children[1].tag == "ul"):
items = list(item1_children[1])
else:
raise AssertionError(etree.tostring(item1))
toc.extend(items)
@staticmethod
def _ul_to_ol(toc):
for item in toc.iter():
if item.tag == "ul":
item.tag = "ol"
class FixToc(Extension):
"""Reformats toc elements.
Changes to toc:
- Outer div is replaced with ol + "toc" class
- If there's a single level 1 entry, that entry is removed end
its child entries are promoted to level 1
To illustrate, we'll configure markdown with TocExtension and our
extension:
>>> md = markdown.Markdown(extensions=["toc", FixToc()])
Here's a simple document with a toc in the format we use in this
site:
>>> print(md.convert("[TOC]\\n# 1\\n## 1_1\\n## 1_2"))
<ol class="toc">
<li><a href="#1_1">1_1</a></li>
<li><a href="#1_2">1_2</a></li>
</ol>
<h1 id="1">1</h1>
<h2 id="1_1">1_1</h2>
<h2 id="1_2">1_2</h2>
Here's a doc with two toc levels:
>>> print(md.convert("[TOC]\\n# 1\\n## 1_1\\n### 1_1_1"))
<ol class="toc">
<li><a href="#1_1">1_1</a><ol>
<li><a href="#1_1_1">1_1_1</a></li>
</ol>
</li>
</ol>
<h1 id="1">1</h1>
<h2 id="1_1">1_1</h2>
<h3 id="1_1_1">1_1_1</h3>
A toc with one heading:
>>> print(md.convert("# 1\\n\\n[TOC]"))
<h1 id="1">1</h1>
<ol class="toc">
</ol>
A toc with no headings:
>>> print(md.convert("[TOC]"))
<ol class="toc">
</ol>
"""
def extendMarkdown(self, md, _globals):
md.treeprocessors.add("fix_toc", FixTocProcessor(md), "_end")
class DefIdProcessor(treeprocessors.Treeprocessor):
def run(self, doc):
for dt in doc.iter("dt"):
if dt.text:
dt.set("id", _slugify(dt.text))
def _slugify(s):
return slugify(six.text_type(s), "-")
class DefinitionId(Extension):
"""Adds ids to definition terms.
With ids, definition terms can be referenced within a page using
hashes.
Ids are generated using the toc extension's slugify function using
"-" as the separator.
To illustrate, let's configure markdown with DefinitionList and
our extension:
>>> md = markdown.Markdown(extensions=["def_list", DefinitionId()])
Here's a definition list:
>>> print(md.convert("foo\\n: Overused example\\n\\nbar baz\\n: See foo"))
<dl>
<dt id="foo">foo</dt>
<dd>Overused example</dd>
<dt id="bar-baz">bar baz</dt>
<dd>See foo</dd>
</dl>
"""
def extendMarkdown(self, md, _globals):
md.treeprocessors.add("def_id", DefIdProcessor(md), "_end")
class ClassifyProcessor(treeprocessors.Treeprocessor):
def __init__(self, md, tags, class_):
super(ClassifyProcessor, self).__init__(md)
self.tags = tags
self.class_ = class_
def run(self, doc):
for el in doc.iter():
if not el.get("class") and el.tag in self.tags:
el.set("class", self.class_)
class Classify(Extension):
"""Add a class to tags.
This is used to style elements created from markdown.
>>> md = markdown.Markdown(extensions=[Classify(tags=['ul', 'ol'])])
>>> print(md.convert("- a\\n- b"))
<ul class="md">
<li>a</li>
<li>b</li>
</ul>
>>> print(md.convert("1. a\\n2. b"))
<ol class="md">
<li>a</li>
<li>b</li>
</ol>
"""
def __init__(self, tags, **kw):
super(Classify, self).__init__()
self.tags = tags
self.class_ = kw.get("class", "md")
def extendMarkdown(self, md, _globals):
md.treeprocessors.add(
"classify",
ClassifyProcessor(md, self.tags, self.class_),
"_end")
class LinkTemplate(object):
def __init__(self, config):
self._text_pattern = self._pattern(config, 'text_pattern')
self._link_pattern = self._pattern(config, 'link_pattern')
self._text = config.get("text")
self._href = config.get("href")
self._class = config.get("class")
self._arg_maps = config.get("arg_maps", [])
self._attrs = config
@staticmethod
def _pattern(config, key):
try:
pattern = config[key]
except KeyError:
return None
else:
return re.compile(pattern + "$", re.DOTALL)
def try_apply(self, link):
unset = object()
link_body = self._link_body(link)
text_m = (
self._text_pattern.match(link_body or "")
if self._text_pattern
else unset)
link_m = (
self._link_pattern.match(link.get("href", ""))
if self._link_pattern
else unset)
if text_m is not None and link_m is not None:
m_args = None
if text_m is not unset:
m_args = text_m.groups()
if link_m is not unset:
m_args = (m_args or ()) + link_m.groups()
if m_args is not None:
args = self._format_args(m_args)
self._apply_link(args, link_body, link)
def _format_args(self, args):
for arg, arg_map in self._zip_longest(args, self._arg_maps):
mapped = arg_map.get(arg, arg) if arg_map else arg
args += (mapped,)
return args + ("",) * 10
@staticmethod
def _zip_longest(a, b):
try:
zip_longest = itertools.zip_longest
except AttributeError:
return map(None, a, b)
else:
return zip_longest(a, b)
def _apply_link(self, args, link_body, link):
new_link = self._applied_link(args, link_body, link)
link.clear()
for name, val in new_link.items():
link.set(name, val)
link.text = new_link.text
for child in new_link.getchildren():
link.append(child)
link.tail = new_link.tail
def _applied_link(self, args, link_body, link):
body = self._applied_link_body(args, link_body, link)
href = self._applied_link_href(args, link)
cls = self._applied_link_class(args, link)
target = self._applied_link_attr("target", args, link)
tail = link.tail
s_parts = ["<a href=\"%s\"" % href]
if cls:
s_parts.append(" class=\"%s\"" % cls)
if target:
s_parts.append(" target=\"%s\"" % target)
s_parts.append(">%s</a>" % body)
applied = etree.fromstring("".join(s_parts))
applied.tail = tail
return applied
def _applied_link_body(self, args, link_body, link):
if self._text and (not link_body or self._text_pattern):
return self._text.format(*args).strip()
return self._link_body(link)
@staticmethod
def _link_body(link):
children = "".join([
etree.tostring(e).decode() for e in link.getchildren()
])
return (link.text or "") + children
def _applied_link_href(self, args, link):
link_href = link.get("href")
if self._href and (not link_href or self._link_pattern):
return self._href.format(*args).strip()
return link_href
def _applied_link_class(self, args, link):
link_class = link.get("class")
if self._class:
cur = link_class
formatted = self._class.format(*args).strip()
return " ".join((cur, formatted)) if cur else formatted
return link_class
def _applied_link_attr(self, attr_name, args, link):
try:
val = self._attrs[attr_name]
except KeyError:
return link.get(attr_name)
else:
return val.format(*args).strip()
class LinkProcessor(treeprocessors.Treeprocessor):
def __init__(self, md, templates):
super(LinkProcessor, self).__init__(md)
self._templates = [LinkTemplate(t) for t in templates]
def run(self, doc):
for link in doc.iter("a"):
for t in self._templates:
if t.try_apply(link):
break
class Link(Extension):
"""Extends markdown links using simple templates.
Templates are specified as a list of attributes:
>>> templates = []
Templates match links on their text and link attributes:
[text](link)
Patterns are configued as regular expressions in the
`text_pattern` and `link_pattern` attributes:
>>> simple_template = {
... "text_pattern": "\\$text",
... "link_pattern": "\\$link"
... }
>>> templates.append(simple_template)
At least one pattern is required. If both are specified, both must
match a link for the template to apply.
Patterns may include regular expression groups to capture values
from the link text and link attributes. Captured values are
referred to as *template arguments*. Argument order is preserved
based on the captured group order starting with link text followed
by the link attribute.
>>> arg_template = {
... "text_pattern": "\\$text (.+)",
... "link_pattern": "\\$link ([^ ]+) ([^ ]+)"
... }
>>> templates.append(arg_template)
In this example, template arguments would be presented as three
values: the one captured text arg followed by the two captured
link args.
When a template matches a link, template attributes are applied to
the link. Template attributes may include:
- text
- href
- class
- target
>>> simple_template.update({
... "text": "a simple link",
... "href": "simple.html"
... })
Attribute values may Python's advanced string formatting (PEP
3101) to include placeholders for argument values. Arguments
applied in order using Python's string `format` method.
>>> arg_template.update({
... "text": "Go to {0}",
... "href": "{1}",
... "class": "{2}"
... })
If a template provides `text` but does not provide `text_pattern`
a special rule applies: the links original text value takes
precedence over the template's. Here's an example that uses
`link_pattern` to match and provides `text`:
>>> link_template = {
... "link_pattern": "link\\.html",
... "text": "My link"
... }
>>> templates.append(link_template)
In this case, `text` will be used only if the link text is empty,
otherwise the link text will be unaltered. This allows templates
to provide default text values that are used when the link text is
not provided.
The `target` attribute may be used to specify the link's
target. Here's a template that uses a pattern in the link text to
implement a link that opens in a new window/tab:
>>> ext_link_template = {
... "text_pattern": r"(.+?) ->",
... "text": "{}",
... "class": "ext",
... "target": "_blank"
... }
>>> templates.append(ext_link_template)
Templates may also provide an `arg_maps` list of dicts, which map
user-provided arg values to template-defined values. Items in the
arg maps list correspond to the positional arguments defined in
the templates.
Mapped arguments are appended to the list of arguments used for
formatting in the order corresponding to their position in the
`arg_maps` list. For example, if there is a single arg map, a
single additional mapped argument will be appended to the argument
list. If there are two maps, two additional arguments will be
appended, and so on.
Here's an example of a template that maps the first (an only)
argument and uses the mapped value as its text while using the
original value as its href attribute.
>>> arg_maps_template = {
... "link_pattern": "mapped:(.+?)",
... "href": "{0}",
... "text": "{1}",
... "arg_maps": [{ "a": "A", "b": "B", "c": "C" }]
... }
>>> templates.append(arg_maps_template)
Let's initialize markdown with our extension and sample templates:
>>> md = markdown.Markdown(extensions=[Link(templates)])
In the case of the simple template, we have to match on both text
and link because both patterns are provided:
>>> print(md.convert("[$text]($link)"))
<p><a href="simple.html">a simple link</a></p>
If either does not match, the template isn't applied:
>>> print(md.convert("[text]($link)"))
<p><a href="$link">text</a></p>
>>> print(md.convert("[$text](link)"))
<p><a href="link">$text</a></p>
The argument template also uses both text and links to match:
>>> print(md.convert("[$text Foo]($link foo bar)"))
<p><a class="bar" href="foo">Go to Foo</a></p>
The link template can be used with text:
>>> print(md.convert("[click here](link.html)"))
<p><a href="link.html">click here</a></p>
It can also be used without text, in which case the template text
is used:
>>> print(md.convert("[](link.html)"))
<p><a href="link.html">My link</a></p>
Here's an example of using a text pattern to implement an external
link (i.e. a link that opens in a new tab/window):
>>> print(md.convert("[Open ->](external.html)"))
<p><a class="ext" href="external.html" target="_blank">Open</a></p>
If formatting is applied, this fails:
>>> print(md.convert("[`Open` ->](external.html)"))
<p><a href="external.html"><code>Open</code> -></a></p>
The failure here is due to the conversion of `>` to `>`, which
isn't matched by the pattern above. Patterns must support
inadvertent text conversions because the filter is applied _after_
inline conversions.
Argument maps are applied to user-provided arguments:
>>> print(md.convert("[](mapped:a)"))
<p><a href="a">A</a></p>
>>> print(md.convert("[`foo`](mapped:a)"))
<p><a href="a"><code>foo</code></a></p>
>>> print(md.convert("foo [bar](mapped:a) baz"))
<p>foo <a href="a">bar</a> baz</p>
"""
def __init__(self, templates):
super(Link, self).__init__()
self._templates = templates
def extendMarkdown(self, md, _globals):
md.treeprocessors.add(
"custom_link",
LinkProcessor(md, self._templates),
">inline")
class PagesIndex(object):
def __init__(self):
assert nav_pages, "nav_resolve plugin is required"
self._by_tag = {}
for page in nav_pages:
for tag in self._page_tags(page):
self._by_tag.setdefault(tag, []).append(page)
@staticmethod
def _page_tags(page):
return [s.strip() for s in page.meta.get("tags", "").split(",")]
def iter_pages(self, url_prefix, tag):
for page in self._by_tag.get(tag, []):
if page.abs_url.startswith(url_prefix):
yield page
class CategoriesProcessor(treeprocessors.Treeprocessor):
def run(self, doc):
if nav_pages_index is None:
log.info("Generating nav pages index")
globals()["nav_pages_index"] = PagesIndex()
for ul in doc.iter("ul"):
if self._is_category_list(ul):
self._apply_category_list(ul, nav_pages_index)
@staticmethod
def _is_category_list(ul):
link = ul.find("li/a")
return link is not None and link.get("href", "").startswith("category")
def _apply_category_list(self, ul, index):
links = ul.findall("li/a")
ul.clear()
ul.set("class", "categorized-view")
for link in links:
cat_info = self._try_cat_info(link)
if not cat_info:
continue
url_prefix, cat_tag, cat_title = cat_info
li = etree.Element("li")
ul.append(li)
if cat_title:
h5 = etree.Element("h5")
li.append(h5)
h5.text = cat_title
h5.set("class", "category-title")
for page in index.iter_pages(url_prefix, cat_tag):
self._apply_category_page(page, li)
@staticmethod
def _try_cat_info(link):
href = link.get("href", "")
if not href.startswith("category:"):
return None
parts = href[9:].split("#", 1)
if len(parts) != 2:
log.warning("invalid category URL '%s' (missing hash)", href)
return None
return parts + [link.text]
def _apply_category_page(self, page, parent):
item_div = etree.Element("div")
parent.append(item_div)
item_div.set("class", "category-item")
link_text, desc = self._split_overview_title(page)
item_div.append(self._category_link(page.abs_url, link_text))
if desc:
item_div.append(self._category_desc(desc))
@staticmethod
def _split_overview_title(page):
overview_title = page.meta.get("overview_title")
if overview_title:
parts = overview_title.split(" :: ", 1)
if len(parts) == 2:
return parts
else:
return parts[0], None
return page.title, None
@staticmethod
def _category_link(url, text):
a = etree.Element("a")
a.text = text
a.set("href", url)
return a
@staticmethod
def _category_desc(text):
span = etree.Element("span")
span.text = text
return span
class Categories(Extension):
"""Generates a categorized view."""
def extendMarkdown(self, md, _globals):
md.treeprocessors.add("categories", CategoriesProcessor(md), ">inline")
class BacktickPattern(inlinepatterns.BacktickPattern):
def __init__(self):
super(BacktickPattern, self).__init__(inlinepatterns.BACKTICK_RE)
def handleMatch(self, m):
el = super(BacktickPattern, self).handleMatch(m)
if m.group(4) and m.group(3) == "``":
el.set("class", "lit")
return el
class Backtick(Extension):
"""Replaces default backtick support with an extended version.
Guild distinguishes between single and double backticks. Single
backticks are used for literal text while double backticks are
used for code samples.
The extension preserves the use of code tags as per the markdown
specification but adds the class 'lit' (literal) to code tags
generated from double backticks.
To illustrate, we'll initialize markdown with the extension:
>>> md = markdown.Markdown(extensions=[Backtick()])
Here are the two code version:
>>> print(md.convert("`term`"))
<p><code>term</code></p>
>>> print(md.convert("``code sample``"))
<p><code class="lit">code sample</code></p>
"""
def extendMarkdown(self, md, _globals):
md.inlinePatterns["backtick"] = BacktickPattern()
class RefProcessor(treeprocessors.Treeprocessor):
def run(self, doc):
for link in doc.iter("a"):
href = link.get("href", "")
if href.startswith("ref:"):
ref = href[4:]
link.set("href", "#" + ref)
link.set("class", "ref")
target = doc.find("*[@id='{}']".format(ref))
if target is not None:
link.text = self._el_text(target)
@staticmethod
def _el_text(el):
return etree.tostring(el, method="text").strip().decode("UTF-8")
class Ref(Extension):
"""Replaces link text with a referenced target text.
References are specified using a link in the format:
ref:REF
where REF is the ID of an element on the same page.
The plugin will replace the link text with that of the target
element. It will also add the 'ref' class to the link for optional
styling.
This plugin is designed to work in conjunction with the toc
plugin, which assigns id slugs to headers.
>>> md = markdown.Markdown(extensions=["toc", Ref()])
Here's a basic example of a reference:
>>> print(md.convert("[](ref:hello)\\n## hello"))
<p><a class="ref" href="#hello">hello</a></p>
<h2 id="hello">hello</h2>
Target text is stripped of its markup:
>>> print(md.convert("[](ref:hello-there)\\n## hello *there*"))
<p><a class="ref" href="#hello-there">hello there</a></p>
<h2 id="hello-there">hello <em>there</em></h2>
"""
def extendMarkdown(self, md, _globals):
md.treeprocessors.add("ref", RefProcessor(md), "_end")
class FigureProcessor(treeprocessors.Treeprocessor):
def run(self, root):
prev = None
for el in list(root):
if (el.tag == "p" and
el.text and el.text.startswith("^ ") and
prev is not None):
self._apply_figure(prev, el, root)
self.run(el)
prev = el
@staticmethod
def _apply_figure(target, caption, parent):
for i, el in zip(range(len(parent)), parent):
if el is target:
parent.remove(target)
parent.remove(caption)
figure = etree.Element("figure")
parent.insert(i, figure)
figure.append(target)
figure.append(caption)
caption.tag = "figcaption"
caption.text = caption.text[2:]
break
else:
assert False, (target, caption, parent)
class Figure(Extension):
"""Support HTML figure element with captions.
Figures are designated by adding a caption below the figure
content. Captions must occur as a separate block and on a single
line starting with "^ ". For example, to create a figure for an
image:

^ A sample image
Let's configure markdown with our extension:
>>> md = markdown.Markdown(extensions=["tables", Figure()])
We can use a caption to create a figure containing an image:
>>> print(md.convert("\\n\\n^ A sample image"))
<figure><p><img alt="" src="img.png" /></p>
<figcaption>A sample image</figcaption>
</figure>
Here's a table figure:
>>> print(md.convert("a | b\\n--- | ---\\nc | d\\n\\n^ A sample image"))
<figure><table>
<thead>
<tr>
<th>a</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>c</td>
<td>d</td>
</tr>
</tbody>
</table>
<figcaption>A sample image</figcaption>
</figure>
"""
def extendMarkdown(self, md, _globals):
md.treeprocessors.add("figure", FigureProcessor(md), "_end")
class FencedCodeProcessor(fenced_code.FencedBlockPreprocessor):
LANG_TAG = ' class="language-%s"'
class FencedCode(Extension):
def extendMarkdown(self, md, _globals):
md.preprocessors["fenced_code_block"] = FencedCodeProcessor(md)
class CmdHelpContext(object):
def __init__(self, cmd_help):
self._cmd_url_base = self._init_cmd_url_base(cmd_help)
@staticmethod
def _init_cmd_url_base(cmd_help):
prog = cmd_help["usage"]["prog"]
cmd_path = prog.split(" ")[1:]
return "/commands/{}".format("-".join(cmd_path))
def cmd_url(self, cmd):
return "{}-{}/".format(self._cmd_url_base, cmd)
class CmdHelpProcessor(treeprocessors.Treeprocessor):
_marker_re = re.compile(r"\[CMD-HELP\s*(.+?)\s*\]$")
_cmd_with_src_re = re.compile(r"(.+?)(?:\s+\((.+)\))")
def __init__(self, md, src_path):
super(CmdHelpProcessor, self).__init__(md)
self._src_path = src_path
env = jinja2.Environment(loader=jinja2.FileSystemLoader("src"))
env.filters.update({
"format_text": self._format_text_filter,
"cmd_url": self._cmd_url_filter,
})
self._md = markdown.Markdown(extensions=[
Backtick(),
AutoUrl(),
CmdHelpUrl(),
])
self._template = env.get_template("cmd-help.html")
def _format_text_filter(self, text):
if text == "Show this message and exit.":
return "Show command help and exit."
else:
return self._md.convert(text)
@staticmethod
def _cmd_url_filter(cmd, ctx):
cmd_name = cmd["term"].split(", ")[0]
return ctx.cmd_url(cmd_name)
def run(self, root):
for el in root:
if el.tag == "p":
m = self._marker_re.match(el.text or "")
if m:
cmd, src = self._parse_marker_arg(m.group(1))
assert src.endswith(".py") and os.path.exists(src), (
"invalid src %r for cmd %r (defined in %r)"
% (src, cmd, el.text)
)
self._handle_cmd(cmd, src, el, root)
def _parse_marker_arg(self, arg):
"""Returns a tuple of cmd, cmd_src for arg
arg must be in the format `CMD` or `CMD (SRC)`.
"""
m = self._cmd_with_src_re.match(arg)
if m:
return m.groups()
return arg, self._cmd_src(arg)
def _cmd_src(self, cmd):
basename = re.sub(r"[ \-]", "_", cmd)
patterns = [
os.path.join(self._src_path, "{}.py"),
os.path.join(self._src_path, "{}_.py"),
]
for p in patterns:
path = p.format(basename)
if os.path.exists(path):
return path
assert False, "cannot find source for cmd %r" % cmd
def _handle_cmd(self, cmd, src, target, parent):
cmd_help = self._get_cmd_help(cmd, src)
ctx = CmdHelpContext(cmd_help)
rendered = self._template.render(cmd=cmd_help, ctx=ctx)
rendered = rendered.replace("<p>\b\n", "<p style=\"white-space: pre\">")
try:
help_el = etree.fromstring(rendered)
except Exception:
print("ERROR processing %s cmd" % cmd)
print("Rendered:")
print(rendered)
raise
_replace_el(parent, target, help_el)
def _get_cmd_help(self, cmd, src):
cmd_help = self._get_cached_cmd_help(cmd, src)
if cmd_help is None:
log.info("Generating help for '%s' command", cmd)
args = ["guild/guild/scripts/guild"] + shlex.split(cmd) + ["--help"]
env = {"GUILD_HELP_JSON": "1"}
env.update(os.environ)
out = subprocess.check_output(args, env=env)
cmd_help = json.loads(out.decode("UTF-8"))
self._cache_cmd_help(cmd, cmd_help)
return cmd_help
def _get_cached_cmd_help(self, cmd, src):
cache_path = self._cached_cmd_help_filename(cmd)
if not os.path.exists(cache_path):
return None
if os.path.getmtime(src) > os.path.getmtime(cache_path):
return None
return json.load(open(cache_path, "r"))
@staticmethod
def _cached_cmd_help_filename(cmd):
basename = cmd.replace(" ", "-")
return "/tmp/guild-ai-cmd-help/{}.json".format(basename)
def _cache_cmd_help(self, cmd, cmd_help):
path = self._cached_cmd_help_filename(cmd)
path_dir = os.path.dirname(path)
try:
os.makedirs(path_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
json.dump(cmd_help, open(path, "w"))
def _replace_el(parent, target_el, new_el):
for i, el in zip(range(len(parent)), parent):
if el is target_el:
parent.remove(target_el)
parent.insert(i, new_el)
break
else:
raise AssertionError()
class CmdHelp(Extension):
"""Replaces [CMD-HELP <cmd>] with formatted Guild command help.
"""
def __init__(self, src_path):
super(CmdHelp, self).__init__()
self._src_path = src_path
def extendMarkdown(self, md, _globals):
md.treeprocessors.add(
"cmd-help",
CmdHelpProcessor(md, self._src_path),
"_end")
class PkgHelpProcessor(treeprocessors.Treeprocessor):
_marker_re = re.compile(r"\[PKG-HELP (.+?)\]$")
def __init__(self, md, src_path):
super(PkgHelpProcessor, self).__init__(md)
self._src_path = src_path
env = jinja2.Environment(loader=jinja2.FileSystemLoader("src"))