sphinx_weasyprint_builder/sphinx_weasyprint_builder/weasyprint_builder.py

79 lines
2.3 KiB
Python
Raw Normal View History

2020-06-10 22:14:35 +02:00
#!/usr/bin/env python3
import os
from typing import Dict, Set, Tuple
from sphinx.builders.singlehtml import SingleFileHTMLBuilder
from sphinx.util import progress_message
from sphinx.util.osutil import os_path
from sphinx.locale import __
import weasyprint
2020-06-17 20:25:21 +02:00
from .loghandler import init_wpsphinx_log
init_wpsphinx_log()
2020-06-10 22:14:35 +02:00
class WeasyPrintPDFBuilder(SingleFileHTMLBuilder):
name = 'weasyprint'
epilog = __('The PDF file has been saved in %(outdir)s.')
embedded = True
search = False
def _get_translations_js(self) -> str:
return
def copy_translation_js(self) -> None:
return
def copy_stemmer_js(self) -> None:
return
def copy_html_favicon(self) -> None:
return
def get_theme_config(self) -> Tuple[str, Dict]:
return (
self.config.weasyprint_theme,
self.config.weasyprint_theme_options
)
def init_js_files(self) -> None:
return
def add_js_file(self, filename: str, **kwargs: str) -> None:
return
def prepare_writing(self, docnames: Set[str]) -> None:
super(WeasyPrintPDFBuilder, self).prepare_writing(docnames)
if self.config.weasyprint_style is not None:
stylename = self.config.weasyprint_style
elif self.theme:
stylename = self.theme.get_config('theme', 'stylesheet')
else:
stylename = 'default.css'
self.globalcontext['use_opensearch'] = False
self.globalcontext['docstitle'] = self.config.weasyprint_title
self.globalcontext['shorttitle'] = self.config.weasyprint_short_title
self.globalcontext['show_copyright'] = self.config.weasyprint_show_copyright
self.globalcontext['show_sphinx'] = self.config.weasyprint_show_sphinx
self.globalcontext['style'] = stylename
self.globalcontext['favicon'] = None
def finish(self) -> None:
super(WeasyPrintPDFBuilder, self).finish()
progress_message('Starting conversion to PDF with WeasyPrint')
infile = os.path.join(
self.outdir,
os_path(self.config.master_doc) + self.out_suffix
)
outfile = os.path.join(
self.outdir,
self.config.weasyprint_basename + '.pdf'
)
weasy_html = weasyprint.HTML(infile)
weasy_html.write_pdf(outfile)
# progress_message('Signing PDF')