22
33import argparse
44import frontmatter
5+ from html import escape
6+ from markdown import markdown
57from pathlib import Path
68import re
79
810from utils import _log , _str
911
1012
13+ HTML_TAG_RE = re .compile (r'<[^>]*>' )
1114SUITE_RE = re .compile (r'where:"suite","(.+?)"' )
1215
1316
@@ -41,6 +44,77 @@ def _get_suite(path, content):
4144 return m .group (1 )
4245
4346
47+ def _make_html (examples ):
48+ """Build HTML page full of examples."""
49+ accum = []
50+ for counter , (path , record ) in enumerate (examples ):
51+ header = record ["header" ]
52+ content = record ["content" ]
53+ accum .append ('<div class="section">\n ' )
54+ accum .append (' <div class="row auto-eg-padding">\n ' )
55+
56+ accum .append (_make_html_name (path , header ))
57+
58+ accum .append (' <div class="row">\n ' )
59+ accum .append (_make_html_text (path , header , content ))
60+ accum .append (' </div>\n ' )
61+
62+ accum .append (' </div>\n ' )
63+ accum .append ('</div>\n \n ' )
64+
65+ return "" .join (accum )
66+
67+
68+ HTML_NAME = """\
69+ <div class=" row twelve columns">
70+ <h3 id="{name}">
71+ <a class="no_underline plot-blue" href="#{name}">{name}</a>
72+ </h3>
73+ </div>
74+ """
75+
76+ def _make_html_name (path , header ):
77+ """Make example name block."""
78+ name = header ["name" ] if header ["name" ] else ""
79+ _log (not name , f"{ path } does not have name" )
80+ name = _strip_html (name .replace (" " , "-" ).replace ("," , "" ).lower ())
81+ return HTML_NAME .format (name = name )
82+
83+ HTML_TEXT = """\
84+ <div class="{columns} columns">
85+ {markdown_content}
86+ {page_content}
87+ {description}
88+ </div>
89+ """
90+
91+ HTML_TEXT_PAGE_CONTENT = """\
92+ <div class="z-depth-1">
93+ <pre><code class="javascript">{text}</code></pre>
94+ </div>
95+ """
96+
97+ HTML_TEXT_DESCRIPTION = """\
98+ <blockquote>
99+ {text}
100+ </blockquote>
101+ """
102+
103+ def _make_html_text (path , header , content ):
104+ """Make text of example."""
105+ columns = "twelve" if "horizontal" in header .get ("arrangement" , "" ) else "six"
106+ markdown_content = markdown (header .get ("markdown_content" , "" ))
107+ page_content = HTML_TEXT_PAGE_CONTENT .format (text = escape (content )) if content else ""
108+ description = header .get ("description" , "" )
109+ description = HTML_TEXT_DESCRIPTION .format (text = description ) if description else ""
110+ return HTML_TEXT .format (
111+ columns = columns ,
112+ markdown_content = markdown_content ,
113+ page_content = page_content ,
114+ description = description ,
115+ )
116+
117+
44118def _parse_args ():
45119 """Parse command-line arguments."""
46120 parser = argparse .ArgumentParser (description = "Generate HTML example documentation" )
@@ -56,18 +130,25 @@ def _process(args, path, record, example_files):
56130 if (suite := _get_suite (path , record ["content" ])) is None :
57131 return
58132
59- children = [
60- p for p , r in example_files .items ()
133+ examples = [
134+ ( p , r ) for p , r in example_files .items ()
61135 if r ["header" ].get ("suite" , None ) == suite
62136 ]
63- children .sort (key = lambda p : (example_files [p ] ["header" ]["order" ], str (p )))
137+ examples .sort (key = lambda pair : (example_files [pair [ 0 ]] ["header" ]["order" ], str (pair [ 0 ] )))
64138
65139 section = record ["header" ]["permalink" ].strip ("/" ).split ("/" )[- 1 ]
66- _log (args .verbose > 0 , f"...{ section } : { len (children )} " )
140+ _log (args .verbose > 0 , f"...{ section } : { len (examples )} " )
141+
142+ html = _make_html (examples )
67143
68144 output_path = args .outdir / section / "index.html"
69145 output_path .parent .mkdir (parents = True , exist_ok = True )
70- output_path .write_text (record ["header" ]["name" ])
146+ output_path .write_text (html )
147+
148+
149+ def _strip_html (text ):
150+ """Remove HTML tags from text."""
151+ return HTML_TAG_RE .sub ("" , text )
71152
72153
73154if __name__ == "__main__" :
0 commit comments