OILS / doctools / doc_html.py View on Github | oils.pub

116 lines, 37 significant
1#!/usr/bin/env python2
2"""doc_html.py.
3
4Note: this file is also run with python3, by the Soil CI, when imported via
5doctools/html_head.py
6
7Because outside containers we don't have python2.
8"""
9from __future__ import print_function
10
11try:
12 import html
13except ImportError:
14 # only for cgi.escape -> html.escape
15 import cgi as html # type: ignore
16
17# Used by html_head.py
18JS_FMT = '<script type="text/javascript" src="%s"></script>\n'
19
20CSS_FMT = '<link rel="stylesheet" type="text/css" href="%s" />\n'
21
22
23def Header(meta, f, draft_warning=False):
24 css_files = [x for x in meta['css_files'].split() if x]
25
26 meta['css_links'] = ''.join(CSS_FMT % url for url in css_files)
27
28 # CSS links are NOT escaped
29 meta['title'] = html.escape(meta['title'])
30
31 # NOTE: 'meta viewport' so it's not small on mobile browsers
32 f.write('''\
33<!DOCTYPE html>
34<html>
35 <head>
36 <meta name="viewport" content="width=device-width, initial-scale=1">
37 <title>%(title)s</title>
38 %(css_links)s
39 </head>
40 <body class="%(body_css_class)s">
41 <p id="home-link">
42''' % meta)
43
44 compact_title = meta.get('compact_title')
45 if compact_title:
46 f.write('''\
47<span id="compact-title">%(title)s</span>
48''' % meta)
49
50 f.write('''\
51 <span id="why-sponsor"><a href="/why-sponsor.html">Why Sponsor Oils?</a></span> |
52 <a href="https://github.com/oilshell/oil/blob/master/%(repo_url)s" id="source-link">source</a> |
53''' % meta)
54
55 if meta.get('all_docs_url') != '-':
56 f.write('''\
57 <span id="all-docs"><a href="%(all_docs_url)s">all docs</a>
58 for <span id="version-in-header">version %(oil_version)s</span></span> |
59''' % meta)
60 elif meta.get('version_url') != '-':
61 # The doc/ URL needs to go back
62 f.write('''\
63 <a href="..">version %(oil_version)s</a> |
64''' % meta)
65
66 f.write('''\
67 <a href="/releases.html">all versions</a> |
68 <a href="/">oils.pub</a>
69''' % meta)
70
71 if draft_warning:
72 f.write('''\
73 <span id="draft-warning" style="visibility: hidden;"></span>
74
75 <script type="text/javascript">
76 function showWarning(el) {
77 el.innerHTML = '<br/>This is a DRAFT. Latest docs are at <a href="/release/latest/doc/">/release/latest/doc/</a> ';
78 el.style.visibility = "visible";
79 }
80 function removeVersion(el) {
81 el.innerHTML = '<a href=".">drafts</a>';
82 }
83
84 var url = window.location.href;
85 if (url.indexOf('/preview/') === -1) {
86 console.log("Not a draft");
87 } else {
88 showWarning(document.querySelector('#draft-warning'));
89 removeVersion(document.querySelector('#all-docs'));
90 }
91 </script>
92''')
93
94 f.write('</p>')
95
96 if 'in_progress' in meta:
97 f.write('''\
98 <p style="background-color: mistyrose; font-size: large;
99 text-align: center; padding: 1em;">
100
101 <b>Warning: Work in progress!</b> Leave feedback on <a
102 href="https://oilshell.zulipchat.com">Zulip</a> or <a
103 href="https://github.com/oilshell/oil/issues">Github</a> if you'd like
104 this doc to be updated.
105 </p>
106''')
107
108
109def Footer(meta, f):
110 f.write('''\
111 <div id="build-timestamp">
112 <i>Generated on %(build_timestamp)s</i>
113 </div>
114 </body>
115</html>
116''' % meta)