Skip to content

Commit

Permalink
Added the ability to not include some markdown files from the pdf
Browse files Browse the repository at this point in the history
This commit fixes #3
  • Loading branch information
comwes committed Jun 23, 2019
1 parent 637038f commit 1d71715
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ What makes this plugin particular, is that:
1. Your documentation is exported as a single PDF file
1. The order of pages fits the navigation as defined in the MkDocs configuration file
1. The ability to override the default design to make it fit your needs
1. The ability to exclude some files from the generated PDF
1. No layout issues
1. No conflict with the theme design
1. Table of contents integrated in the PDF
Expand Down
18 changes: 18 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Before installing [MkDocs][1], you need to make sure you have Python and `pip`
– the Python package manager – up and running. You can verify if you're already
good to go with the following commands:

[1]: https://www.mkdocs.org

``` sh
python --version
# Python 3.6.7
Expand Down Expand Up @@ -57,5 +59,21 @@ plugins:
- toc_title: ToC
```
### Hide file content from the generated PDF
Sometime it can be interesting to hide a given documentation file from the PDF.
This can be achieved by using the [Mkdocs YAML Style Meta-Data](https://www.mkdocs.org/user-guide/writing-your-docs/#yaml-style-meta-data) features.
For this, define a `pdf` metadata and set it to `False` in the top of your Markdown file like in the following example.

``` markdown
---
pdf: False
---
#Page title
```

### Documentation design
You have the ability to design the layout of your Generated PDF by using CSS. You can find out complete documentation by visiting our [Layout customisation](layout-design.md) section.
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pip show mkpdfs-mkdocs
* The plugin was breaking the documentation generation (#1).
Now if the theme is not compatible, the PDF version of the documentation won't be created and a warning will be displayed without breaking the documentation generation.
* Enhance the view by adding a section page in the documentation (#2)
* Added the ability to remove the inclusion of some Markdown files in the generated pdf (#3)

### 1.0.0 <small>- April 15, 2019</small>

Expand Down
20 changes: 19 additions & 1 deletion mkpdfs_mkdocs/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def add_nav(self, nav):
self.addToOrder(p)

def addToOrder(self, page):
if page.is_page and page.meta != None and 'pdf' in page.meta and page.meta['pdf'] == False:
print(page.meta)
exit(1)
return;
if page.is_page :
self._page_order.append(page.file.url)
else :
Expand All @@ -86,6 +90,10 @@ def addToOrder(self, page):
self.addToOrder(child)


def remove_from_order(self, item):

return

def add_article(self, content, page, base_url):
if not self.generate:
return None
Expand All @@ -106,6 +114,9 @@ def add_article(self, content, page, base_url):
span = soup.new_tag('span')
span['id'] = 'mkpdf-{}'.format(url)
article.insert(0, span)
if page.meta != None and 'pdf' in page.meta and page.meta['pdf'] == False:
# print(page.meta)
return self.get_path_to_pdf(page.file.dest_path)
self._articles[page.file.url] = article
return self.get_path_to_pdf(page.file.dest_path)

Expand All @@ -130,6 +141,9 @@ def add_tocs(self):
self._toc = self.html.new_tag('article', id='contents')
self._toc.insert(0, title)
for n in self.nav:
if n.is_page and n.meta != None and 'pdf' in n.meta \
and n.meta['pdf'] == False:
continue
h3 = self.html.new_tag('h3')
h3.insert(0, n.title)
self._toc.append(h3)
Expand All @@ -153,7 +167,8 @@ def gen_articles (self):
if self.config['toc_position'] == 'pre' :
self.add_tocs()
for url in self._page_order:
self.html.body.append(self._articles[url])
if url in self._articles:
self.html.body.append(self._articles[url])
if self.config['toc_position'] == 'post' :
self.add_tocs()

Expand All @@ -165,6 +180,9 @@ def get_path_to_pdf(self, start):

def _gen_toc_section(self, section):
for p in section.children:
if p.is_page and p.meta != None and 'pdf' \
in p.meta and p.meta['pdf'] == False:
continue
stoc = self._gen_toc_for_section(p.file.url, p)
child = self.html.new_tag('div')
child.append(stoc)
Expand Down

0 comments on commit 1d71715

Please sign in to comment.