Skip to content

Commit

Permalink
Add option and docs to include a print button, closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Sep 16, 2020
1 parent 6aed712 commit 3276451
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
60 changes: 60 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Customization

You might want to customize your site to include a 'print' button on every page (like the one in the right corner of this page 👆)

MkDocs supports [theme extension](https://www.mkdocs.org/user-guide/styling-your-docs/#using-the-theme-custom_dir), an easy way to override parts of a theme.
You can use `page.url_to_print_page` to get the link to the site print page.

## Adding a print button to mkdocs-material theme

In `mkdocs-material` theme (see [customization](https://squidfunk.github.io/mkdocs-material/customization/#overriding-template-blocks)), first create a directory for overrides and update your `mkdocs.yml`:

```yml
theme:
name: material
custom_dir: docs/overrides
```
You can create the file `docs/overrides/main.html` as follows:

```
{% extends "base.html" %}

{% block content %}
{% if page.url_to_print_page %}
<a href="{{ page.url_to_print_page }}" title="Print Site" class="md-content__button md-icon">
{% include ".icons/material/printer.svg" %}
</a>
{% endif %}

{{ super() }}
{% endblock content %}
```
## Adding a print button to mkdocs theme
You can also [customize](https://www.mkdocs.org/user-guide/custom-themes/#creating-a-custom-theme) the base mkdocs theme, by first creating an `overrides` directory:
```yml
theme:
name: mkdocs
custom_dir: docs/overrides
```

And then adding a file `docs/overrides/main.html` with the following content:

```html
{% extends "base.html" %}

{% block repo %}
{% if page.url_to_print_page %}
<li class="nav-item">
<a href="{{ page.url_to_print_page }}" title="Print Site" class="nav-link">
<i class="fa fa-print"></i> Print
</a>
</li>
{% endif %}

{{ super() }}
{% endblock repo %}
```
11 changes: 11 additions & 0 deletions docs/overrides/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}

{% block content %}
{% if page.url_to_print_page %}
<a href="{{ page.url_to_print_page }}" title="Print Site" class="md-content__button md-icon">
{% include ".icons/material/printer.svg" %}
</a>
{% endif %}

{{ super() }}
{% endblock content %}
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ plugins:
nav:
- Home: index.md
- Options: options.md
- Customization: customization.md
- Demo Content: demo_content.md
- Contributing: contributing.md

theme:
name: material
custom_dir: docs/overrides
palette:
primary: indigo
accent: blue
Expand Down
4 changes: 4 additions & 0 deletions mkdocs_print_site_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def on_page_context(self, context, page, config, nav):
# And apply it to the print page as well (in on_post_build event)
self.context = context

# Save relative link to print page
# This can be used to customize a theme and add a print button to each page
page.url_to_print_page = self.print_file.url_relative_to(page.file)

def on_post_build(self, config):

# Add javascript file
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="mkdocs-print-site-plugin",
version="0.5.1",
version="0.5.2",
description="MkDocs plugin that adds a page to your site combining all pages, allowing your site visitors to *File > Print > Save as PDF* the entire site.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 3276451

Please sign in to comment.