Skip to content

Commit

Permalink
Merge pull request #4 from Ymil/master
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenwigg authored Oct 3, 2023
2 parents cd157b6 + a86364d commit 53483b2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ This plugin supports following options to allow you better handle the customisat
| `output_path` | The file name of the generated PDF, relative to the `site_dir`. By default this location is set to `pdf/combined.pdf`|
| `pdf_links` | Create link to download the generated PDF to the top of each HTML page. By default this is enabled |
| `design` | Relative to your `MkDocs repository`, this option is the location of the CSS file defining the layout of the generated PDF. If this option is not defined the default design will be used. Defining an non existing file will cause the build or serve failure. |
| `generate_html` | Save the html from which the PDF is generated to be able to debug it in the browser. Default is False |

## Contributing

Expand Down
23 changes: 18 additions & 5 deletions mkpdfs_mkdocs/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ def write(self):
pdf_path = os.path.join(self.mkdconfig['site_dir'],
self.config['output_path'])
os.makedirs(os.path.dirname(pdf_path), exist_ok=True)

if self.config["generate_html"]:
html_path = os.path.dirname(pdf_path)+"/pdf.html"
f = open(html_path, "w")
f.write(str(self.html))
self.logger.log(msg=f'The html version of the documentation has been generated in {html_path}', level=logging.INFO, )

html = HTML(string=str(self.html)).write_pdf(pdf_path,
font_config=font_config)
self.logger.log(msg='The PDF version of the documentation has been generated.', level=logging.INFO, )
self.logger.log(msg=f'The PDF version of the documentation has been generated in {pdf_path}.', level=logging.INFO, )

def add_nav(self, nav):
self.nav = nav
Expand Down Expand Up @@ -152,7 +159,7 @@ def add_tocs(self):
h3 = self.html.new_tag('h3')
h3.insert(0, n.title)
self._toc.append(h3)
if n.is_page:
if n.is_page and n.toc:
ptoc = self._gen_toc_page(n.file.url, n.toc)
self._toc.append(ptoc)
else:
Expand Down Expand Up @@ -183,17 +190,23 @@ def get_path_to_pdf(self, start):
return os.path.join(os.path.relpath(pdf_split[0], start_dir),
pdf_split[1])

def _gen_toc_section(self, section):
if section.children: # External Links do not have children
def _gen_toc_section(self, section, level=1):
if section.children:
for p in section.children:
if p.is_page and p.meta and 'pdf' \
in p.meta and not p.meta['pdf']:
continue
if p.children:
h2 = self.html.new_tag("h4", **{'class': f'level-{level}'})
h2.insert(0, p.title)
self._toc.append(h2)
self._gen_toc_section(p, level=level+1)
continue
if not hasattr(p, 'file'):
# Skip external links
continue
stoc = self._gen_toc_for_section(p.file.url, p)
child = self.html.new_tag('div')
child = self.html.new_tag('div',**{'class': f'level-{level}'})
child.append(stoc)
self._toc.append(child)

Expand Down
1 change: 1 addition & 0 deletions mkpdfs_mkdocs/mkpdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Mkpdfs(BasePlugin):
('toc_position', config_options.Type(str, default="pre")),
('pdf_links', config_options.Type(bool, default=True)),
('output_path', config_options.Type(str, default="pdf/combined.pdf")),
('generate_html', config_options.Type(bool, default=False))
)

def __init__(self):
Expand Down

0 comments on commit 53483b2

Please sign in to comment.