Skip to content

Commit 3276451

Browse files
committed
Add option and docs to include a print button, closes #6
1 parent 6aed712 commit 3276451

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

docs/customization.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Customization
2+
3+
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 👆)
4+
5+
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.
6+
You can use `page.url_to_print_page` to get the link to the site print page.
7+
8+
## Adding a print button to mkdocs-material theme
9+
10+
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`:
11+
12+
```yml
13+
theme:
14+
name: material
15+
custom_dir: docs/overrides
16+
```
17+
18+
You can create the file `docs/overrides/main.html` as follows:
19+
20+
```
21+
{% extends "base.html" %}
22+
23+
{% block content %}
24+
{% if page.url_to_print_page %}
25+
<a href="{{ page.url_to_print_page }}" title="Print Site" class="md-content__button md-icon">
26+
{% include ".icons/material/printer.svg" %}
27+
</a>
28+
{% endif %}
29+
30+
{{ super() }}
31+
{% endblock content %}
32+
```
33+
34+
## Adding a print button to mkdocs theme
35+
36+
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:
37+
38+
```yml
39+
theme:
40+
name: mkdocs
41+
custom_dir: docs/overrides
42+
```
43+
44+
And then adding a file `docs/overrides/main.html` with the following content:
45+
46+
```html
47+
{% extends "base.html" %}
48+
49+
{% block repo %}
50+
{% if page.url_to_print_page %}
51+
<li class="nav-item">
52+
<a href="{{ page.url_to_print_page }}" title="Print Site" class="nav-link">
53+
<i class="fa fa-print"></i> Print
54+
</a>
55+
</li>
56+
{% endif %}
57+
58+
{{ super() }}
59+
{% endblock repo %}
60+
```

docs/overrides/main.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
{% if page.url_to_print_page %}
5+
<a href="{{ page.url_to_print_page }}" title="Print Site" class="md-content__button md-icon">
6+
{% include ".icons/material/printer.svg" %}
7+
</a>
8+
{% endif %}
9+
10+
{{ super() }}
11+
{% endblock content %}

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ plugins:
1717
nav:
1818
- Home: index.md
1919
- Options: options.md
20+
- Customization: customization.md
2021
- Demo Content: demo_content.md
2122
- Contributing: contributing.md
2223

2324
theme:
2425
name: material
26+
custom_dir: docs/overrides
2527
palette:
2628
primary: indigo
2729
accent: blue

mkdocs_print_site_plugin/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def on_page_context(self, context, page, config, nav):
116116
# And apply it to the print page as well (in on_post_build event)
117117
self.context = context
118118

119+
# Save relative link to print page
120+
# This can be used to customize a theme and add a print button to each page
121+
page.url_to_print_page = self.print_file.url_relative_to(page.file)
122+
119123
def on_post_build(self, config):
120124

121125
# Add javascript file

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name="mkdocs-print-site-plugin",
12-
version="0.5.1",
12+
version="0.5.2",
1313
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.",
1414
long_description=long_description,
1515
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)