|
|
def _apply_description_images(doc):
html = lxml.html.document_fromstring(doc)
for element, _attribute, _link, _pos in html.iterlinks():
if element.get('src') and not '//' in element.get('src') and not 'static/' in element.get('src'):
element.set('src', "/%s/static/description/%s" % (module.name, element.get('src')))
return tools.html_sanitize(lxml.html.tostring(html, encoding='unicode'))
for module in self:
if not module.name:
module.description_html = False
continue
path = os.path.join(module.name, 'static/description/index.html')
try:
with tools.file_open(path, 'rb') as desc_file:
doc = desc_file.read().decode()
module.description_html = _apply_description_images(doc)
except FileNotFoundError:
overrides = {
'embed_stylesheet': False,
'doctitle_xform': False,
'output_encoding': 'unicode',
'xml_declaration': False,
'file_insertion_enabled': False,
}
raw_description = module.description or ''
try:
output = publish_string(source=raw_description, settings_overrides=overrides, writer=MyWriter())
except Exception as e: # noqa: BLE001
_logger.warning("Failed to render module description for %s: %s. Falling back to raw description.", module.name, e)
output = Markup('<pre><code>%s</code></pre>') % raw_description
module.description_html = _apply_description_images(output)
|
|