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