-def transform(wldoc, stylesheet='legacy', options=None, flags=None, css=None, gallery_path='img/', gallery_url='img/', base_url='file://./'):
- """Transforms the WL document to XHTML.
-
- If output_filename is None, returns an XML,
- otherwise returns True if file has been written,False if it hasn't.
- File won't be written if it has no content.
- """
- # Parse XSLT
- try:
- style_filename = get_stylesheet(stylesheet)
- style = etree.parse(style_filename)
-
- document = copy.deepcopy(wldoc)
- del wldoc
- document.swap_endlines()
-
- if flags:
- for flag in flags:
- document.edoc.getroot().set(flag, 'yes')
-
- document.clean_ed_note()
- document.clean_ed_note('abstrakt')
-
- if not options:
- options = {}
-
- try:
- os.makedirs(gallery_path)
- except OSError:
- pass
-
- add_image_sizes(document.edoc, gallery_path, gallery_url, base_url)
-
- css = (
- css
- or 'https://static.wolnelektury.pl/css/compressed/book_text.css'
- )
- css = "'%s'" % css
- result = document.transform(style, css=css, **options)
- del document # no longer needed large object :)
-
- if html_has_content(result):
- add_anchors(result.getroot())
- add_table_of_themes(result.getroot())
- add_table_of_contents(result.getroot())
-
- return OutputFile.from_bytes(etree.tostring(
- result, method='html', xml_declaration=False,
- pretty_print=True, encoding='utf-8'
- ))
- else:
- return None
- except KeyError:
- raise ValueError("'%s' is not a valid stylesheet.")
- except (XMLSyntaxError, XSLTApplyError) as e:
- raise ParseError(e)
-
-
-@six.python_2_unicode_compatible
-class Fragment(object):