Make complete use of pathlib (fix up 8224e5a)

This commit is contained in:
Lonami Exo
2018-12-21 18:18:18 +01:00
parent 8224e5aabf
commit aefa429236
2 changed files with 110 additions and 151 deletions

View File

@@ -3,25 +3,25 @@ import re
class DocsWriter:
"""Utility class used to write the HTML files used on the documentation"""
def __init__(self, filename, type_to_path):
"""Initializes the writer to the specified output file,
creating the parent directories when used if required.
'type_to_path_function' should be a function which, given a type
name and a named argument relative_to, returns the file path for
the specified type, relative to the given filename
"""
Utility class used to write the HTML files used on the documentation.
"""
def __init__(self, root, filename, type_to_path):
"""
Initializes the writer to the specified output file,
creating the parent directories when used if required.
"""
self.root = root
self.filename = filename
self._parent = str(self.filename.parent)
self.handle = None
self.title = ''
# Should be set before calling adding items to the menu
self.menu_separator_tag = None
# Utility functions TODO There must be a better way
self.type_to_path = lambda t: type_to_path(
t, relative_to=self.filename
)
# Utility functions
self.type_to_path = lambda t: self._rel(type_to_path(t))
# Control signals
self.menu_began = False
@@ -30,11 +30,20 @@ class DocsWriter:
self.write_copy_script = False
self._script = ''
def _rel(self, path):
"""
Get the relative path for the given path from the current
file by working around https://bugs.python.org/issue20012.
"""
return os.path.relpath(str(path), self._parent)
# High level writing
def write_head(self, title, relative_css_path, default_css):
def write_head(self, title, css_path, default_css):
"""Writes the head part for the generated document,
with the given title and CSS
"""
#
self.title = title
self.write(
'''<!DOCTYPE html>
<html>
@@ -54,17 +63,17 @@ class DocsWriter:
<body>
<div id="main_div">''',
title=title,
rel_css=str(relative_css_path).rstrip('/'),
rel_css=self._rel(css_path),
def_css=default_css
)
def set_menu_separator(self, relative_image_path):
def set_menu_separator(self, img):
"""Sets the menu separator.
Must be called before adding entries to the menu
"""
if relative_image_path:
self.menu_separator_tag = \
'<img src="{}" alt="/" />'.format(relative_image_path)
if img:
self.menu_separator_tag = '<img src="{}" alt="/" />'.format(
self._rel(img))
else:
self.menu_separator_tag = None
@@ -80,7 +89,7 @@ class DocsWriter:
self.write('<li>')
if link:
self.write('<a href="{}">', link)
self.write('<a href="{}">', self._rel(link))
# Write the real menu entry text
self.write(name)
@@ -210,7 +219,7 @@ class DocsWriter:
if bold:
self.write('<b>')
if link:
self.write('<a href="{}">', link)
self.write('<a href="{}">', self._rel(link))
# Finally write the real table data, the given text
self.write(text)