Clean-up DocsWriter

This commit is contained in:
Lonami Exo 2018-05-24 11:48:15 +02:00
parent c4c41645e7
commit 6726eab045
2 changed files with 41 additions and 64 deletions

View File

@ -35,34 +35,28 @@ class DocsWriter:
"""Writes the head part for the generated document, """Writes the head part for the generated document,
with the given title and CSS with the given title and CSS
""" """
self.write('''<!DOCTYPE html> self.write(
'''<!DOCTYPE html>
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>''') <title>{title}</title>
self.write(title)
self.write('''</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link id="style" href="''') <link id="style" href="{rel_css}/docs.{def_css}.css" rel="stylesheet">
self.write(relative_css_path)
self.write('/docs.')
self.write(default_css)
self.write('''.css" rel="stylesheet">
<script> <script>
document.getElementById("style").href = "''') document.getElementById("style").href = "{rel_css}/docs."
self.write(relative_css_path) + (document.cookie.split(";")[0].split("=")[1] || "{def_css}")
self.write('/docs.') + ".css";
self.write('''" + (document.cookie
.split(";")[0].split("=")[1] || "light") + ".css";
</script> </script>
<link href="https://fonts.googleapis.com/css?family=Nunito|Source+Code+Pro" rel="stylesheet"> <link href="https://fonts.googleapis.com/css?family=Nunito|Source+Code+Pro"
rel="stylesheet">
</head> </head>
<body> <body>
<div id="main_div">''') <div id="main_div">''',
title=title,
rel_css=relative_css_path.rstrip('/'),
def_css=default_css
)
def set_menu_separator(self, relative_image_path): def set_menu_separator(self, relative_image_path):
"""Sets the menu separator. """Sets the menu separator.
@ -86,9 +80,7 @@ class DocsWriter:
self.write('<li>') self.write('<li>')
if link: if link:
self.write('<a href="') self.write('<a href="{}">', link)
self.write(link)
self.write('">')
# Write the real menu entry text # Write the real menu entry text
self.write(name) self.write(name)
@ -107,26 +99,21 @@ class DocsWriter:
"""Writes a title header in the document body, """Writes a title header in the document body,
with an optional depth level with an optional depth level
""" """
self.write('<h%d>' % level) self.write('<h{level}>{title}</h{level}>', title=title, level=level)
self.write(title)
self.write('</h%d>' % level)
def write_code(self, tlobject): def write_code(self, tlobject):
"""Writes the code for the given 'tlobject' properly """Writes the code for the given 'tlobject' properly
formatted with hyperlinks formatted with hyperlinks
""" """
self.write('<pre>---') self.write('<pre>---{}---\n',
self.write('functions' if tlobject.is_function else 'types') 'functions' if tlobject.is_function else 'types')
self.write('---\n')
# Write the function or type and its ID # Write the function or type and its ID
if tlobject.namespace: if tlobject.namespace:
self.write(tlobject.namespace) self.write(tlobject.namespace)
self.write('.') self.write('.')
self.write(tlobject.name) self.write('{}#{:08x}', tlobject.name, tlobject.id)
self.write('#')
self.write(hex(tlobject.id)[2:].rjust(8, '0'))
# Write all the arguments (or do nothing if there's none) # Write all the arguments (or do nothing if there's none)
for arg in tlobject.args: for arg in tlobject.args:
@ -143,20 +130,19 @@ class DocsWriter:
# "Opening" modifiers # "Opening" modifiers
if arg.is_flag: if arg.is_flag:
self.write('flags.%d?' % arg.flag_index) self.write('flags.{}?', arg.flag_index)
if arg.is_generic: if arg.is_generic:
self.write('!') self.write('!')
if arg.is_vector: if arg.is_vector:
self.write( self.write('<a href="{}">Vector</a>&lt;',
'<a href="%s">Vector</a>&lt;' % self.type_to_path('vector') self.type_to_path('vector'))
)
# Argument type # Argument type
if arg.type: if arg.type:
if add_link: if add_link:
self.write('<a href="%s">' % self.type_to_path(arg.type)) self.write('<a href="{}">', self.type_to_path(arg.type))
self.write(arg.type) self.write(arg.type)
if add_link: if add_link:
self.write('</a>') self.write('</a>')
@ -185,19 +171,14 @@ class DocsWriter:
# use a lower type name for it (see #81) # use a lower type name for it (see #81)
vector, inner = tlobject.result.split('<') vector, inner = tlobject.result.split('<')
inner = inner.strip('>') inner = inner.strip('>')
self.write('<a href="') self.write('<a href="{}">{}</a>&lt;',
self.write(self.type_to_path(vector)) self.type_to_path(vector), vector)
self.write('">%s</a>&lt;' % vector)
self.write('<a href="') self.write('<a href="{}">{}</a>&gt;',
self.write(self.type_to_path(inner)) self.type_to_path(inner), inner)
self.write('">%s</a>' % inner)
self.write('&gt;')
else: else:
self.write('<a href="') self.write('<a href="{}">{}</a>',
self.write(self.type_to_path(tlobject.result)) self.type_to_path(tlobject.result), tlobject.result)
self.write('">%s</a>' % tlobject.result)
self.write('</pre>') self.write('</pre>')
@ -218,17 +199,13 @@ class DocsWriter:
self.write('<td') self.write('<td')
if align: if align:
self.write(' style="text-align:') self.write(' style="text-align:{}"', align)
self.write(align)
self.write('"')
self.write('>') self.write('>')
if bold: if bold:
self.write('<b>') self.write('<b>')
if link: if link:
self.write('<a href="') self.write('<a href="{}">', link)
self.write(link)
self.write('">')
# Finally write the real table data, the given text # Finally write the real table data, the given text
self.write(text) self.write(text)
@ -253,9 +230,7 @@ class DocsWriter:
def write_text(self, text): def write_text(self, text):
"""Writes a paragraph of text""" """Writes a paragraph of text"""
self.write('<p>') self.write('<p>{}</p>', text)
self.write(text)
self.write('</p>')
def write_copy_button(self, text, text_to_copy): def write_copy_button(self, text, text_to_copy):
"""Writes a button with 'text' which can be used """Writes a button with 'text' which can be used
@ -282,16 +257,18 @@ class DocsWriter:
'c.select();' 'c.select();'
'try{document.execCommand("copy")}' 'try{document.execCommand("copy")}'
'catch(e){}}' 'catch(e){}}'
'</script>') '</script>'
)
self.write('</div>') self.write('</div>{}</body></html>', self._script)
self.write(self._script)
self.write('</body></html>')
# "Low" level writing # "Low" level writing
def write(self, s): def write(self, s, *args, **kwargs):
"""Wrapper around handle.write""" """Wrapper around handle.write"""
self.handle.write(s) if args or kwargs:
self.handle.write(s.format(*args, **kwargs))
else:
self.handle.write(s)
# With block # With block
def __enter__(self): def __enter__(self):

View File

@ -210,7 +210,6 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir):
# TODO Tried using 'defaultdict(list)' with strange results, make it work. # TODO Tried using 'defaultdict(list)' with strange results, make it work.
original_paths = { original_paths = {
'css': 'css', 'css': 'css',
'default_css': 'docs.light.css',
'arrow': 'img/arrow.svg', 'arrow': 'img/arrow.svg',
'search.js': 'js/search.js', 'search.js': 'js/search.js',
'404': '404.html', '404': '404.html',
@ -222,6 +221,7 @@ def _write_html_pages(tlobjects, errors, layer, input_res, output_dir):
original_paths = {k: os.path.join(output_dir, v) original_paths = {k: os.path.join(output_dir, v)
for k, v in original_paths.items()} for k, v in original_paths.items()}
original_paths['default_css'] = 'light' # docs.<name>.css, local path
type_to_constructors = {} type_to_constructors = {}
type_to_functions = {} type_to_functions = {}
for tlobject in tlobjects: for tlobject in tlobjects: