Skip to content

Commit

Permalink
Support for Mermaid Diagrams using ReLaXed JS
Browse files Browse the repository at this point in the history
Add option to use 'ReLaXeD' JS renderer for PDF
instead of Headless Chrome.

option added: use_relaxed_js_renderer

option changed:
 - output_pdf_name: default 'document' as before
 - output_path:     contains now only directory name
                    not a file name

Issue orzih#34
  • Loading branch information
Pawel Sikora committed Nov 1, 2020
1 parent 0a20bde commit 5a41971
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ plugins:
> <ANY_SITE_URL(eg. 'https://google.com')>
> ```
* `use_relaxed_js_renderer`
Set the value to `true` if you're using e.g. '[Mermaid](https://mermaid-js.github.io) diagrams and Headless Chrome is not working for you.
Require "ReLaXed" Javascript PDF renderer to be installed on your system. See: '[ReLaXed](https://github.com/RelaxedJS/ReLaXed)'.
Please use 'theme_handler_path' option to specify custom JS sources and CSS Stylesheets which covers your needs. E.g. for Material
theme it would be **material.py**. See: **mkdocs-with-pdf/mkdocs_with_pdf/themes/material.py** for implementation details.
**default**: `false`
> Install on your system:
> ```
> $ npm i -g relaxedjs
> $ relaxed --version
> ```
##### ... and more
* `output_path`
Expand Down
26 changes: 20 additions & 6 deletions mkdocs_with_pdf/generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import re
import subprocess
from importlib import import_module
from importlib.util import module_from_spec, spec_from_file_location

Expand Down Expand Up @@ -123,15 +124,24 @@ def add_stylesheet(stylesheet: str):
if self._options.debug_html:
print(f'{html_string}')

self.logger.info("Rendering for PDF.")
html = HTML(string=html_string)
render = html.render()

abs_pdf_path = os.path.join(config['site_dir'], output_path)
output_pdf_name = self._options.output_pdf_name
os.makedirs(os.path.dirname(abs_pdf_path), exist_ok=True)

self.logger.info("Rendering for PDF.")
self.logger.info(f'Output a PDF to "{abs_pdf_path}".')
render.write_pdf(abs_pdf_path)

if self._options.use_relaxed_js_renderer:
self.logger.info("Use 'Relaxed' JS renderer.")
output_html_name = os.path.join(abs_pdf_path, output_pdf_name + '.html')
with open(output_html_name, 'w+') as f:
f.write(html_string)
f.close()
subprocess.call(["relaxed", output_html_name, "--build-once"])
else:
html = HTML(string=html_string)
render = html.render()
render.write_pdf(abs_pdf_path + output_pdf_name + ".pdf")

# ------------------------
def _remove_empty_tags(self, soup: PageElement):
Expand Down Expand Up @@ -351,4 +361,8 @@ def _render_js(self, soup):
if body:
for src in scripts:
body.append(soup.new_tag('script', src=f'file://{src}'))
return self._options.js_renderer.render(str(soup))

if self._options.use_relaxed_js_renderer:
return str(soup)
else:
return self._options.js_renderer.render(str(soup))
13 changes: 11 additions & 2 deletions mkdocs_with_pdf/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Options(object):
('debug_html', config_options.Type(bool, default=False)),
('show_anchors', config_options.Type(bool, default=False)),

('output_path', config_options.Type(str, default="pdf/document.pdf")),
('output_pdf_name', config_options.Type(str, default="document")),
('output_path', config_options.Type(str, default="pdf/")),
('theme_handler_path', config_options.Type(str, default=None)),

('author', config_options.Type(str, default=None)),
Expand All @@ -43,7 +44,9 @@ class Options(object):

('render_js', config_options.Type(bool, default=False)),
('headless_chrome_path',
config_options.Type(str, default='chromium-browser'))
config_options.Type(str, default='chromium-browser')),
('use_relaxed_js_renderer',
config_options.Type(bool, default=False)),
)

def __init__(self, local_config, config, logger: logging):
Expand All @@ -54,6 +57,7 @@ def __init__(self, local_config, config, logger: logging):
self.show_anchors = local_config['show_anchors']

self.output_path = local_config.get('output_path', None)
self.output_pdf_name = local_config.get('output_pdf_name', None)
self.theme_handler_path = local_config.get('theme_handler_path', None)

# Author and Copyright
Expand Down Expand Up @@ -85,6 +89,11 @@ def __init__(self, local_config, config, logger: logging):

self.two_columns_level = local_config['two_columns_level']

if local_config['use_relaxed_js_renderer']:
self.use_relaxed_js_renderer = True
else:
self.use_relaxed_js_renderer = False

# ...etc.
self.js_renderer = None
if local_config['render_js']:
Expand Down

0 comments on commit 5a41971

Please sign in to comment.