From 965e73d0270d4677bd24b0b1e0f6101cd435c4ed Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 10:14:03 +0200 Subject: [PATCH 01/25] Add publish docs to deploy gh action --- .github/workflows/pythonpublish.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml index 218c1bc..a7a931e 100644 --- a/.github/workflows/pythonpublish.yml +++ b/.github/workflows/pythonpublish.yml @@ -8,9 +8,9 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Set up Python - uses: actions/setup-python@v1 + uses: actions/setup-python@v2 with: python-version: '3.x' - name: Install dependencies @@ -29,3 +29,7 @@ jobs: run: | python setup.py sdist bdist_wheel twine upload dist/* + - name: Deploy mkdocs site + run: | + mkdocs gh-deploy --force + From 4fd5a396e68d8206e8c521d7152415b4e30eea62 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 10:19:16 +0200 Subject: [PATCH 02/25] New default: will not add print site to navigation by default --- mkdocs_print_site_plugin/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index 2298750..c7da8b5 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -23,7 +23,7 @@ class PrintSitePlugin(BasePlugin): """ config_scheme = ( - ("add_to_navigation", config_options.Type(bool, default=True)), + ("add_to_navigation", config_options.Type(bool, default=False)), ("print_page_title", config_options.Type(str, default="Print Site")), ("add_table_of_contents", config_options.Type(bool, default=True)), ("toc_title", config_options.Type(str, default="Table of Contents")), From 29660f270ef6e02e67f26c9139235a974f779469 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 11:12:53 +0200 Subject: [PATCH 03/25] Remove navigation side and top bars from print page --- mkdocs.yml | 1 + mkdocs_print_site_plugin/css/print-site.css | 1 - mkdocs_print_site_plugin/js/print-site.js | 26 +++++++++++++++++++++ mkdocs_print_site_plugin/plugin.py | 3 ++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 093200c..e5cf184 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -10,6 +10,7 @@ use_directory_urls: false plugins: - search - print-site: + add_to_navigation: true add_full_urls: false add_table_of_contents: true toc_title: "Table of Contents" diff --git a/mkdocs_print_site_plugin/css/print-site.css b/mkdocs_print_site_plugin/css/print-site.css index 618e841..d40a98f 100644 --- a/mkdocs_print_site_plugin/css/print-site.css +++ b/mkdocs_print_site_plugin/css/print-site.css @@ -282,7 +282,6 @@ For now, we added them for use only in the table of contents */ line-height: 1.3; color: var(--md-default-fg-color--light); } - #print-site-page h1.nav-section-title-end, #print-site-page h2.nav-section-title-end, #print-site-page h3.nav-section-title-end, diff --git a/mkdocs_print_site_plugin/js/print-site.js b/mkdocs_print_site_plugin/js/print-site.js index c0983da..92bffff 100644 --- a/mkdocs_print_site_plugin/js/print-site.js +++ b/mkdocs_print_site_plugin/js/print-site.js @@ -120,4 +120,30 @@ function generate_toc() { document.querySelectorAll("#print-page-toc nav")[0].insertAdjacentHTML("beforeend", ToC); +} + + +function remove_material_navigation() { + + // Remove left sidebar on print page + remove_element_by_classname('md-sidebar--primary') + // Remove tabs navigation on print page + remove_element_by_classname('md-tabs') + // Remove search + remove_element_by_classname('md-search') + +} + +function remove_mkdocs_theme_navigation() { + + // Remove top navigation bar + remove_element_by_classname('navbar') +} + + +function remove_element_by_classname(class_name) { + var el = document.getElementsByClassName(class_name); + if( el.length > 0) { + el[0].style.display = "none" + } } \ No newline at end of file diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index c7da8b5..ecc499c 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -240,7 +240,8 @@ def on_post_build(self, config): html = template.render(self.context) # Determine calls to required javascript functions - js_calls = "" + js_calls = "remove_material_navigation();" + js_calls += "remove_mkdocs_theme_navigation();" if self.config.get("add_table_of_contents"): js_calls += "generate_toc();" From 50011c7d1802622a79c4995cc7182d6abd7ae914 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 14:46:39 +0200 Subject: [PATCH 04/25] Add a TOC sidebar to the HTML version of print page --- mkdocs_print_site_plugin/plugin.py | 2 ++ mkdocs_print_site_plugin/renderer.py | 45 +++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index ecc499c..564a302 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -231,6 +231,8 @@ def on_post_build(self, config): # Combine the HTML of all pages present in the navigation self.print_page.content = self.renderer.write_combined() + # Generate a TOC sidebar for HTML version of print page + self.print_page.toc = self.renderer.get_toc_sidebar() # Get the info for MkDocs to be able to apply a theme template on our print page env = config["theme"].get_env() diff --git a/mkdocs_print_site_plugin/renderer.py b/mkdocs_print_site_plugin/renderer.py index ade3ba2..ff6eda7 100644 --- a/mkdocs_print_site_plugin/renderer.py +++ b/mkdocs_print_site_plugin/renderer.py @@ -1,7 +1,9 @@ import jinja2 import logging -from mkdocs_print_site_plugin.urls import fix_internal_links +from mkdocs.structure.toc import AnchorLink, TableOfContents + +from mkdocs_print_site_plugin.urls import fix_internal_links, get_page_key from mkdocs_print_site_plugin.exclude import exclude logger = logging.getLogger("mkdocs.plugins") @@ -159,3 +161,44 @@ def _toc(self): """ + + def get_toc_sidebar(self) -> TableOfContents: + """ + Generate a MkDocs a navigation sidebar. + + We want to generate one for the print page also, so we can export HTML. + Here we go over each page with a toc. Then we fix the anchor links. + + See also https://github.com/mkdocs/mkdocs/blob/master/mkdocs/structure/toc.py + """ + toc = [] + + for item in self.items: + if hasattr(item, "toc"): + page_key = get_page_key(item.url) + item_toc = item.toc + if hasattr(item_toc, "items"): + for toc_link in item.toc.items: + toc_link = update_toc_item(page_key, toc_link) + toc += [toc_link] + + return TableOfContents(toc) + + +def update_toc_item(page_key: str, toc_link: AnchorLink) -> AnchorLink: + """ + Updates a AnchorItem to work on the print page. + """ + # update the link to point to the anchor in print page instead of actual page + toc_link.id = f"{page_key}-{toc_link.id}" + + # MkDocs-material parses the TOC and does not display level 1 + # This is a small hack that will set all levels off by 1 + # Will work just fine in base mkdocs theme also + toc_link.level = toc_link.level - 1 + + if len(toc_link.children) > 0: + for link in toc_link.children: + update_toc_item(page_key, link) + + return toc_link From 5f121e96a5edfe761232759301794fcc759cbb2e Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 14:49:52 +0200 Subject: [PATCH 05/25] New default: print site banner is not displayed by default --- docs/options.md | 7 ++++--- mkdocs_print_site_plugin/plugin.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/options.md b/docs/options.md index 13d86c6..eeadb6f 100644 --- a/docs/options.md +++ b/docs/options.md @@ -5,8 +5,9 @@ You can customize `mkdocs-print-site-plugin` in your `mkdocs.yml` with the follo ```yaml plugins: - print-site: - add_to_navigation: true + add_to_navigation: false print_page_title: 'Print Site' + add_print_site_banner: false # Table of contents add_table_of_contents: true toc_title: 'Table of Contents' @@ -23,7 +24,7 @@ plugins: ``` `add_to_navigation` -: Default is `true`. Adds a link 'Print Site' to your site navigation. You can also set to `false` and explicitly include the link in your navigation (`/print_page` or `/print_page.html`). +: Default is `false`. Adds a link 'Print Site' to your site navigation. You can also set to `false` and explicitly include the link in your navigation (`/print_page` or `/print_page.html`). `print_page_title` : Default is `'Print Site'`. When `add_to_navigation` is set to `true` this setting controls the name of the print page in the navigation of the site. This setting is ignored when `add_to_navigation` is set to `false`. @@ -57,7 +58,7 @@ plugins: : Default `""`. The path to a custom cover page template to use. See [Customizing the Cover Page](customization/cover_page.md) for more info. `add_print_site_banner` -: Default `true`. When enabled, a banner is added to the top of the print page, explaining to users the current page contains all site pages. +: Default `false`. When enabled, a banner is added to the top of the HTML print page, explaining to users the current page contains all site pages. `print_site_banner_template` : Default `""`. The path to a custom print site banner template to use. See [Customizing the print site banner](customization/banner.md) for more info. diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index 564a302..77b4671 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -33,7 +33,7 @@ class PrintSitePlugin(BasePlugin): ("enumerate_figures", config_options.Type(bool, default=False)), ("add_cover_page", config_options.Type(bool, default=False)), ("cover_page_template", config_options.Type(str, default="")), - ("add_print_site_banner", config_options.Type(bool, default=True)), + ("add_print_site_banner", config_options.Type(bool, default=False)), ("print_site_banner_template", config_options.Type(str, default="")), ("path_to_pdf", config_options.Type(str, default="")), ("enabled", config_options.Type(bool, default=True)), From 4e3a2351ab9f40b26f7f1d9dc8e88c07165a2096 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 14:55:43 +0200 Subject: [PATCH 06/25] Fix bug where setting 'add_print_site_banner' to false would lead to a 'No such file found' error --- mkdocs_print_site_plugin/renderer.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mkdocs_print_site_plugin/renderer.py b/mkdocs_print_site_plugin/renderer.py index ff6eda7..0f8467b 100644 --- a/mkdocs_print_site_plugin/renderer.py +++ b/mkdocs_print_site_plugin/renderer.py @@ -20,7 +20,6 @@ def __init__( mkdocs_config={}, cover_page_template_path="", banner_template_path="", - insert_print_site_banner=True, print_page=None, ): """ @@ -30,7 +29,6 @@ def __init__( self.mkdocs_config = mkdocs_config self.cover_page_template_path = cover_page_template_path self.banner_template_path = banner_template_path - self.insert_print_site_banner = insert_print_site_banner self.print_page = print_page self.items = [] @@ -59,7 +57,7 @@ def write_combined(self): if self.plugin_config.get("add_cover_page"): html += self._cover_page() - if self.insert_print_site_banner: + if self.plugin_config.get("add_print_site_banner"): html += self._print_site_banner() if self.plugin_config.get("add_table_of_contents"): From eb2fdc26da57853f7b1854824233da2895959127 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 14:56:29 +0200 Subject: [PATCH 07/25] New default: toc_depth is now set to 3 instead of 6 --- docs/options.md | 2 +- mkdocs_print_site_plugin/plugin.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/options.md b/docs/options.md index eeadb6f..7e6118a 100644 --- a/docs/options.md +++ b/docs/options.md @@ -36,7 +36,7 @@ plugins: : Default is `'Table of Contents'`. When `add_table_of_contents` is set to `true` this setting controls the name of the table of contents. This setting is ignored when `add_table_of_contents` is set to `false`. `toc_depth` -: Default is `6`. When `add_table_of_contents` is set to `true` this setting controls the depth of the table of contents. This setting is ignored when `add_table_of_contents` is set to `false`. +: Default is `3`. When `add_table_of_contents` is set to `true` this setting controls the depth of the table of contents. This setting is ignored when `add_table_of_contents` is set to `false`. `add_full_urls` : Default is `false`. When printing a page, you cannot see the target of a link. This option adds the target url in parenthesis behind a link. diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index 77b4671..d9105aa 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -27,7 +27,7 @@ class PrintSitePlugin(BasePlugin): ("print_page_title", config_options.Type(str, default="Print Site")), ("add_table_of_contents", config_options.Type(bool, default=True)), ("toc_title", config_options.Type(str, default="Table of Contents")), - ("toc_depth", config_options.Type(int, default=6)), + ("toc_depth", config_options.Type(int, default=3)), ("add_full_urls", config_options.Type(bool, default=False)), ("enumerate_headings", config_options.Type(bool, default=False)), ("enumerate_figures", config_options.Type(bool, default=False)), From d4d4fe3d2c83a08e53038bf9cddcbc7a26b5af4f Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 15:18:19 +0200 Subject: [PATCH 08/25] Make sure sidebar toc on print page HTML also respects TOC depth parameter --- mkdocs_print_site_plugin/renderer.py | 17 +++++++++++++++++ .../projects/basic/mkdocs_toc_depth.yml | 6 ++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/fixtures/projects/basic/mkdocs_toc_depth.yml diff --git a/mkdocs_print_site_plugin/renderer.py b/mkdocs_print_site_plugin/renderer.py index 0f8467b..1ca3442 100644 --- a/mkdocs_print_site_plugin/renderer.py +++ b/mkdocs_print_site_plugin/renderer.py @@ -180,9 +180,26 @@ def get_toc_sidebar(self) -> TableOfContents: toc_link = update_toc_item(page_key, toc_link) toc += [toc_link] + # Make sure the sidebar ToC also respects + # the plugin's toc_depth parameter + toc = clear_children_up_to(self.plugin_config.get("toc_depth"), toc) + return TableOfContents(toc) +def clear_children_up_to(depth, items): + """ + Filters items in sidebar table of contents to a given depth. + """ + for toc_item in items: + # note the -1 is there because of the hack on levels in update_toc_item() + if toc_item.level == depth - 1: + toc_item.children = [] + else: + toc_item.children = clear_children_up_to(depth, toc_item.children) + return items + + def update_toc_item(page_key: str, toc_link: AnchorLink) -> AnchorLink: """ Updates a AnchorItem to work on the print page. diff --git a/tests/fixtures/projects/basic/mkdocs_toc_depth.yml b/tests/fixtures/projects/basic/mkdocs_toc_depth.yml new file mode 100644 index 0000000..4ef8f8b --- /dev/null +++ b/tests/fixtures/projects/basic/mkdocs_toc_depth.yml @@ -0,0 +1,6 @@ +site_name: Test + +plugins: + - print-site: + toc_depth: 1 + \ No newline at end of file From 436fbad0d57dd27f3c085f548b722772aa0b3027 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 15:31:49 +0200 Subject: [PATCH 09/25] The table of contents (when enabled) will now only be displayed in the print version --- docs/options.md | 2 +- mkdocs_print_site_plugin/css/print-site.css | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/options.md b/docs/options.md index 7e6118a..6b9bbe0 100644 --- a/docs/options.md +++ b/docs/options.md @@ -30,7 +30,7 @@ plugins: : Default is `'Print Site'`. When `add_to_navigation` is set to `true` this setting controls the name of the print page in the navigation of the site. This setting is ignored when `add_to_navigation` is set to `false`. `add_table_of_contents` -: Default is `true`. Adds a table of contents section at the beginning of the print page. +: Default is `true`. Adds a table of contents section at the beginning of the print page (in print version, not HTML version). `toc_title` : Default is `'Table of Contents'`. When `add_table_of_contents` is set to `true` this setting controls the name of the table of contents. This setting is ignored when `add_table_of_contents` is set to `false`. diff --git a/mkdocs_print_site_plugin/css/print-site.css b/mkdocs_print_site_plugin/css/print-site.css index d40a98f..e7515e7 100644 --- a/mkdocs_print_site_plugin/css/print-site.css +++ b/mkdocs_print_site_plugin/css/print-site.css @@ -196,6 +196,10 @@ div.print-site-add-full-url section.print-page a[href^="http"]::after{ /* Print site table of contents styling */ +/* Don't display the table of contents in HTML version */ +#print-page-toc { display: none } + + .print-page-toc-nav { padding-bottom: 2em; } @@ -321,6 +325,10 @@ For now, we added them for use only in the table of contents */ /* Remove print site banner */ #print-site-banner { display: none; } + /* display the table of contents in print version */ + #print-page-toc { display: block } + + /* Ensure all tabbed content is displayed and printed https://squidfunk.github.io/mkdocs-material/reference/content-tabs/ */ /* #print-site-page div.tabbed-content { display: block !important; } */ From b49b29b5508a09be12f019361eea57b4d361210e Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 16:30:28 +0200 Subject: [PATCH 10/25] Improve default cover page template --- .../templates/cover_page.tpl | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/mkdocs_print_site_plugin/templates/cover_page.tpl b/mkdocs_print_site_plugin/templates/cover_page.tpl index 0907d2f..64f7bd8 100644 --- a/mkdocs_print_site_plugin/templates/cover_page.tpl +++ b/mkdocs_print_site_plugin/templates/cover_page.tpl @@ -1,28 +1,46 @@ -
+
{% if config.site_name %}

{{ config.site_name }}

{% endif %} - {% if config.site_description %} -

{{ config.site_description }}

- {% endif %} -
-{% if config.site_author %} -

Authors: {{ config.site_author }}

-{% endif %} -

- {% if config.site_url %} - Website: {{ config.site_url }}
+ + + {% if config.site_description %} + + + + + {% endif %} + + {% if config.site_author %} + + + + {% endif %} + {% if config.repo_url %} - Repo: {{ config.repo_url }}
+ + + + {% endif %} + {% if config.copyright %} - {{ config.copyright }}
+ + + + {% endif %} -

+ +
Description{{ config.site_description }}
Author(s){{ config.site_author }}
Repository{{ config.repo_url }}
Copyright{{ config.copyright }}
+ + + + + From 967d6837562395b3491f3ffa4483ca8f3ed02190 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 16:36:58 +0200 Subject: [PATCH 11/25] Cover page is now displayed also on HTML page --- mkdocs.yml | 2 +- mkdocs_print_site_plugin/css/print-site.css | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index e5cf184..5948b19 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,4 +1,4 @@ -site_name: mkdocs-print-site-plugin Docs +site_name: Documentation mkdocs-print-site-plugin repo_url: https://github.com/timvink/mkdocs-print-site-plugin site_url: https://timvink.github.io/mkdocs-print-site-plugin/ site_description: MkDocs Plugin allowing your site visitors to *File > Print > Save as PDF* the entire site. diff --git a/mkdocs_print_site_plugin/css/print-site.css b/mkdocs_print_site_plugin/css/print-site.css index e7515e7..08e1f82 100644 --- a/mkdocs_print_site_plugin/css/print-site.css +++ b/mkdocs_print_site_plugin/css/print-site.css @@ -267,8 +267,6 @@ ul.print-site-toc-level-2 li a { -/* Don't display cover page when not in print mode */ -#print-site-cover-page { display: none; } /* Don't display the section headings that we added For now, we added them for use only in the table of contents */ From 06e64ecdd290e43ca97b8c4a8419f88bc8eee456 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 19:34:00 +0200 Subject: [PATCH 12/25] Prettier highlight of anchor when clicking link --- mkdocs_print_site_plugin/css/print-site.css | 35 ++++++++++++--------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/mkdocs_print_site_plugin/css/print-site.css b/mkdocs_print_site_plugin/css/print-site.css index 08e1f82..c34c5ce 100644 --- a/mkdocs_print_site_plugin/css/print-site.css +++ b/mkdocs_print_site_plugin/css/print-site.css @@ -193,6 +193,27 @@ div.print-site-add-full-url section.print-page a[href^="http"]::after{ } +/* Do some nice animations when clicking on a ToC link */ +#print-site-page h1:target, +#print-site-page h2:target, +#print-site-page h3:target, +#print-site-page h4:target, +#print-site-page h5:target, +#print-site-page h6:target { + animation: highlight 1.5s ease; +} +@keyframes highlight { + from { color: orange; } + to { color: none; } +} +/* Make sure to offset for the header when using anchor links */ +/* #print-site-page .headerlink::before { + content: ''; + display: block; + height: 180px; + margin-top: -180px; +} */ + /* Print site table of contents styling */ @@ -204,19 +225,6 @@ Print site table of contents styling padding-bottom: 2em; } -#print-site-page h1:target, -#print-site-page h2:target, -#print-site-page h3:target, -#print-site-page h4:target, -#print-site-page h5:target, -#print-site-page h6:target { - animation: highlight 1s ease; -} -@keyframes highlight { - from { background: yellow; } - to { background: white; } -} - #print-page-toc ul { /* margin-left: 1.6em; */ margin-top: 0; @@ -372,7 +380,6 @@ For now, we added them for use only in the table of contents */ #print-site-page footer { display : none; } - #print-site-cover-page { display: block; From 403a25b81974569269ed6ec9ab38eafb6afee73c Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 19:38:00 +0200 Subject: [PATCH 13/25] Bump mkdocs-material requirement to 7.2.6 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 99975d9..d1cf4c3 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ "Topic :: Documentation", "Topic :: Text Processing", ], - install_requires=["mkdocs-material>=7.1.5"], + install_requires=["mkdocs-material>=7.2.6"], packages=find_packages(), entry_points={"mkdocs.plugins": ["print-site = mkdocs_print_site_plugin.plugin:PrintSitePlugin"]}, ) From c8fad648baeea87996a8ccb15509124bc72e67bb Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Mon, 13 Sep 2021 20:15:20 +0200 Subject: [PATCH 14/25] Fix bug where clicking on an anchor link would jump to a place where the header was hidden under the navbar (in mkdocs-material) Note new situation is still buggy, causing a newline after enumeration somehow --- .../css/print-site-material.css | 5 ---- mkdocs_print_site_plugin/css/print-site.css | 28 ++++--------------- .../projects/basic/mkdocs_with_enum.yml | 6 ++++ 3 files changed, 12 insertions(+), 27 deletions(-) create mode 100644 tests/fixtures/projects/basic/mkdocs_with_enum.yml diff --git a/mkdocs_print_site_plugin/css/print-site-material.css b/mkdocs_print_site_plugin/css/print-site-material.css index 4e812a1..159fbcf 100644 --- a/mkdocs_print_site_plugin/css/print-site-material.css +++ b/mkdocs_print_site_plugin/css/print-site-material.css @@ -3,11 +3,6 @@ https://github.com/squidfunk/mkdocs-material */ /* Table of Contents styling */ -.print-page-toc-title { - padding-bottom: 0em; - margin-bottom: 0; -} - #print-site-page ul.toc-section-line-border { border-left: 5px solid var(--md-default-fg-color--lightest); } diff --git a/mkdocs_print_site_plugin/css/print-site.css b/mkdocs_print_site_plugin/css/print-site.css index c34c5ce..1770ff8 100644 --- a/mkdocs_print_site_plugin/css/print-site.css +++ b/mkdocs_print_site_plugin/css/print-site.css @@ -23,16 +23,6 @@ print-site: - print-site-enumerate_headings: true */ -/* Ensure that when adding enumeration to headings, this happens inline */ -.print-site-enumerate-headings h1:before, -.print-site-enumerate-headings h2:before, -.print-site-enumerate-headings h3:before, -.print-site-enumerate-headings h4:before, -.print-site-enumerate-headings h5:before, -.print-site-enumerate-headings h6:before { - display: inline !important; -} - /* Reset all enumeration at start of page */ body {counter-reset: chapter sec-top toc-chapter toc-sec-chapter figurecounter;} @@ -75,7 +65,7 @@ that are part of an original included page */ } .print-site-enumerate-headings .print-page h6:before { counter-increment: last; - content: counter(chapter) "." counter(section) "." counter(sub-section) "." counter(composite) "." counter(detail) "." counter(last) !important; + content: counter(chapter) "." counter(section) "." counter(sub-section) "." counter(composite) "." counter(detail) "." counter(last) " " !important; } /* Enumerate headings of SECTIONS, @@ -107,7 +97,7 @@ that are part of an original included page */ } .print-site-enumerate-headings h6.nav-section-title:before { counter-increment: sec-last; - content: counter(sec-top, upper-roman) "." counter(sec-section, upper-roman) "." counter(sec-sub-section, upper-roman) "." counter(sec-composite, upper-roman) "." counter(sec-detail, upper-roman) "." counter(sec-last, upper-roman) !important; + content: counter(sec-top, upper-roman) "." counter(sec-section, upper-roman) "." counter(sec-sub-section, upper-roman) "." counter(sec-composite, upper-roman) "." counter(sec-detail, upper-roman) "." counter(sec-last, upper-roman) " " !important; } @@ -139,7 +129,7 @@ that are part of an original included page */ } .print-site-enumerate-headings .print-site-toc-level-6 > li a:before { counter-increment: toc-last; - content: counter(toc-chapter) "." counter(toc-section) "." counter(toc-sub-section) "." counter(toc-composite) "." counter(toc-detail) "." counter(toc-last); + content: counter(toc-chapter) "." counter(toc-section) "." counter(toc-sub-section) "." counter(toc-composite) "." counter(toc-detail) "." counter(toc-last) " "; } /* Enumerate SECTIONS in table of contents also */ @@ -170,7 +160,7 @@ that are part of an original included page */ } .print-site-enumerate-headings li.toc-nav-section-title-level-6:before { counter-increment: toc-sec-last; - content: counter(toc-sec-chapter, upper-roman) "." counter(toc-sec-section, upper-roman) "." counter(toc-sec-sub-section, upper-roman) "." counter(toc-sec-composite, upper-roman) "." counter(toc-sec-detail, upper-roman) "." counter(toc-last, upper-roman); + content: counter(toc-sec-chapter, upper-roman) "." counter(toc-sec-section, upper-roman) "." counter(toc-sec-sub-section, upper-roman) "." counter(toc-sec-composite, upper-roman) "." counter(toc-sec-detail, upper-roman) "." counter(toc-last, upper-roman) " "; } #print-page-toc li a.headerlink:before { @@ -205,14 +195,8 @@ div.print-site-add-full-url section.print-page a[href^="http"]::after{ @keyframes highlight { from { color: orange; } to { color: none; } -} -/* Make sure to offset for the header when using anchor links */ -/* #print-site-page .headerlink::before { - content: ''; - display: block; - height: 180px; - margin-top: -180px; -} */ +} + /* Print site table of contents styling diff --git a/tests/fixtures/projects/basic/mkdocs_with_enum.yml b/tests/fixtures/projects/basic/mkdocs_with_enum.yml new file mode 100644 index 0000000..4f9c289 --- /dev/null +++ b/tests/fixtures/projects/basic/mkdocs_with_enum.yml @@ -0,0 +1,6 @@ +site_name: Test + +plugins: + - print-site: + enumerate_headings: true + \ No newline at end of file From 9f204b89765bdb1f396ffdace26430874243c2bd Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Tue, 14 Sep 2021 10:30:19 +0200 Subject: [PATCH 15/25] New defaults: enumerate_headings and enumerate_figures set to true --- docs/options.md | 4 ++-- mkdocs_print_site_plugin/plugin.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/options.md b/docs/options.md index 6b9bbe0..8ba58c1 100644 --- a/docs/options.md +++ b/docs/options.md @@ -44,12 +44,12 @@ plugins: For example "[google.com](https://www.google.com)" will be replaced by "[google.com](https://www.google.com) (https://www.google.com)" `enumerate_headings` -: Default `false`. This will add numbering (enumeration) to all headings as well as the table of contents. Note this will only enumerate the print site page; if you want to enumerate the entire site, you can use [mkdocs-enumerate-headings-plugin](https://github.com/timvink/mkdocs-enumerate-headings-plugin). +: Default `true`. This will add numbering (enumeration) to all headings as well as the table of contents. Note this will only enumerate the print site page; if you want to enumerate the entire site, you can use [mkdocs-enumerate-headings-plugin](https://github.com/timvink/mkdocs-enumerate-headings-plugin). Example "1.2 A chapter subsection". `enumerate_figures` -: Default `false`. This will add numbering to all figure captions (for example "Figure 1: "). Works especially well with [mkdocs-img2fig-plugin](https://github.com/stuebersystems/mkdocs-img2fig-plugin). +: Default `true`. This will add numbering to all figure captions (for example "Figure 1: "). Works especially well with [mkdocs-img2fig-plugin](https://github.com/stuebersystems/mkdocs-img2fig-plugin). `add_cover_page` : Default `false`. When enabled, a cover page is added to the print page, displaying the `site_title` and other information from the `mkdocs.yml` file. See also [Customizing the cover page](customization/cover_page.md) diff --git a/mkdocs_print_site_plugin/plugin.py b/mkdocs_print_site_plugin/plugin.py index d9105aa..ca5df60 100644 --- a/mkdocs_print_site_plugin/plugin.py +++ b/mkdocs_print_site_plugin/plugin.py @@ -29,8 +29,8 @@ class PrintSitePlugin(BasePlugin): ("toc_title", config_options.Type(str, default="Table of Contents")), ("toc_depth", config_options.Type(int, default=3)), ("add_full_urls", config_options.Type(bool, default=False)), - ("enumerate_headings", config_options.Type(bool, default=False)), - ("enumerate_figures", config_options.Type(bool, default=False)), + ("enumerate_headings", config_options.Type(bool, default=True)), + ("enumerate_figures", config_options.Type(bool, default=True)), ("add_cover_page", config_options.Type(bool, default=False)), ("cover_page_template", config_options.Type(str, default="")), ("add_print_site_banner", config_options.Type(bool, default=False)), From 071ee7e2d5809ac225a014685ee5906e9e6f1757 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Tue, 14 Sep 2021 15:57:11 +0200 Subject: [PATCH 16/25] Fix bug in tab content not displaying on print page, closes #53 --- mkdocs_print_site_plugin/urls.py | 40 +++++++++++--------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/mkdocs_print_site_plugin/urls.py b/mkdocs_print_site_plugin/urls.py index c077696..29f8b81 100644 --- a/mkdocs_print_site_plugin/urls.py +++ b/mkdocs_print_site_plugin/urls.py @@ -167,36 +167,22 @@ def fix_tabbed_content(page_html, page_key): - we should change id, and

+ ${title} / +
`; + + footerHtml = ` `; + + + (async() => { + const browser = await puppeteer.launch({ + headless: true, + executablePath: process.env.CHROME_BIN || null, + args: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-dev-shm-usage'] + }); + + const page = await browser.newPage(); + await page.goto(url, { waitUntil: 'networkidle2' }); + await page.pdf({ + path: pdfPath, // path to save pdf file + format: 'A4', // page format + displayHeaderFooter: true, // display header and footer (in this example, required!) + printBackground: true, // print background + landscape: false, // use horizontal page layout + headerTemplate: headerHtml, // indicate html template for header + footerTemplate: footerHtml, + scale: 1, //Scale amount must be between 0.1 and 2 + margin: { // increase margins (in this example, required!) + top: 80, + bottom: 80, + left: 30, + right: 30 + } + }); + + await browser.close(); + })(); + ``` \ No newline at end of file diff --git a/docs/customization/pdf_button.md b/docs/how-to/pdf_button.md similarity index 100% rename from docs/customization/pdf_button.md rename to docs/how-to/pdf_button.md diff --git a/docs/customization/print_button.md b/docs/how-to/print_button.md similarity index 100% rename from docs/customization/print_button.md rename to docs/how-to/print_button.md diff --git a/docs/index.md b/docs/index.md index 4decddc..9c35dda 100644 --- a/docs/index.md +++ b/docs/index.md @@ -29,3 +29,9 @@ plugins: > :warning: Make sure to put `print-site` to the **bottom** of the plugin list. This is because other plugins might alter your site (like the navigation), and you want these changes included in the print page. > If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set. + +## Usage + +- Navigate to `/print_page/` or `print_page.html` +- Export to standalone HTML (see [export to HTML](how-to/export-HTML.html)) +- Export to PDF using your browser using *File > Print > Save as PDF* (see [export to PDF](how-to/export-PDF.html)) diff --git a/docs/options.md b/docs/options.md index 8ba58c1..e9c0d3f 100644 --- a/docs/options.md +++ b/docs/options.md @@ -52,19 +52,19 @@ plugins: : Default `true`. This will add numbering to all figure captions (for example "Figure 1: "). Works especially well with [mkdocs-img2fig-plugin](https://github.com/stuebersystems/mkdocs-img2fig-plugin). `add_cover_page` -: Default `false`. When enabled, a cover page is added to the print page, displaying the `site_title` and other information from the `mkdocs.yml` file. See also [Customizing the cover page](customization/cover_page.md) +: Default `false`. When enabled, a cover page is added to the print page, displaying the `site_title` and other information from the `mkdocs.yml` file. See also [Customizing the cover page](how-to/cover_page.md) `cover_page_template` -: Default `""`. The path to a custom cover page template to use. See [Customizing the Cover Page](customization/cover_page.md) for more info. +: Default `""`. The path to a custom cover page template to use. See [Customizing the Cover Page](how-to/cover_page.md) for more info. `add_print_site_banner` : Default `false`. When enabled, a banner is added to the top of the HTML print page, explaining to users the current page contains all site pages. `print_site_banner_template` -: Default `""`. The path to a custom print site banner template to use. See [Customizing the print site banner](customization/banner.md) for more info. +: Default `""`. The path to a custom print site banner template to use. See [Customizing the print site banner](how-to/banner.md) for more info. `path_to_pdf` -: Default is empty. Option to make it easier to add a link to the PDF version of the site on each page. See [Adding a PDF button](customization/pdf_button.md) for more info. +: Default is empty. Option to make it easier to add a link to the PDF version of the site on each page. See [Adding a PDF button](how-to/pdf_button.md) for more info. `enabled` : Default is `true`. Enables you to deactivate this plugin. A possible use case is local development where you might want faster build times. It's recommended to use this option with an environment variable together with a default fallback (introduced in `mkdocs` v1.2.1, see [docs](https://www.mkdocs.org/user-guide/configuration/#environment-variables)). Example: @@ -84,4 +84,4 @@ plugins: ``` `exclude` -: Default is empty. Allows to specify a list of page source paths that should not be included in the print page. See [Do Not Print](customization/do_not_print.md#ignoring-an-entire-page) for more info. \ No newline at end of file +: Default is empty. Allows to specify a list of page source paths that should not be included in the print page. See [Do Not Print](how-to/do_not_print.md#ignoring-an-entire-page) for more info. \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index a48e91b..104903d 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -24,17 +24,23 @@ plugins: nav: - Home: index.md - Options: options.md - - Customization: - - customization/print_button.md - - customization/pdf_button.md - - customization/cover_page.md - - customization/do_not_print.md + - How to guides: + - Export to PDF: how-to/export-PDF.md + - Export to HTML: how-to/export-HTML.md + - Add a print button: how-to/print_button.md + - Add a PDF button: how-to/pdf_button.md + - Add a cover page: how-to/cover_page.md + - Add a banner: how-to/banner.md + - Exclude content: how-to/do_not_print.md - Demo Content: demo_content.md - Contributing: contributing.md theme: name: material custom_dir: docs/overrides + icon: + admonition: + example: fontawesome/solid/flask palette: - media: "(prefers-color-scheme: light)" scheme: default diff --git a/setup.py b/setup.py index d1cf4c3..c47164f 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="mkdocs-print-site-plugin", version="1.3.0", - description="MkDocs plugin that adds a page with all site pages, enabling printing to PDF for users.", + description="MkDocs plugin that combines all pages into one, allowing for easy export to PDF and standalone HTML.", long_description=long_description, long_description_content_type="text/markdown", keywords="mkdocs plugin print pdf", diff --git a/standalone.html b/standalone.html new file mode 100644 index 0000000..f169da1 --- /dev/null +++ b/standalone.html @@ -0,0 +1,1227 @@ + + + + + + + + + + +Print Site - Documentation mkdocs-print-site-plugin + + + + + + + + + + + + + + + + +
+
+
+ +
+
+
+
+ + +
+
+ +
+
+
+
+ +
+
+
+
+ + + + + + + + \ No newline at end of file From 90e3ac104de45852b08c5fa03a26a2c52b6f9cb4 Mon Sep 17 00:00:00 2001 From: Tim Vink Date: Wed, 15 Sep 2021 13:30:20 +0200 Subject: [PATCH 19/25] Improve and extend documentation --- docs/how-to/banner.md | 42 +++++++++++++++++-- docs/how-to/cover_page.md | 16 +++---- docs/how-to/do_not_print.md | 20 +++++---- docs/how-to/export-HTML.md | 2 +- docs/how-to/export-PDF.md | 2 +- docs/how-to/pdf_button.md | 19 ++++----- docs/how-to/print_button.md | 21 +++++----- docs/index.md | 4 +- .../templates/print_site_banner.tpl | 4 +- 9 files changed, 84 insertions(+), 46 deletions(-) diff --git a/docs/how-to/banner.md b/docs/how-to/banner.md index 1e28059..a602764 100644 --- a/docs/how-to/banner.md +++ b/docs/how-to/banner.md @@ -1,8 +1,8 @@ # Customize the print site banner -When the `add_print_site_banner` option is set to true, `mkdocs-print-site-plugin` will add a banner to the top of the print page. You might want to customize this banner, for example by translating it to your language. +When a user visits the print page, it might not be immediately obvious how to use it. You can set the `add_print_site_banner` option to `true` to add a banner to the top of the HTML print page that will be hidden when printing. -You can do that by specifying the path to a custom banner template in the `mkdocs.yml` file. This file should be a standard [jinja2 template](https://jinja.palletsprojects.com/en/2.11.x/templates/) where you can combine HTML and special jinja2 variables, such as the information specified in `mkdocs.yml` (see [mkdocs project information](https://www.mkdocs.org/user-guide/configuration/#project-information)). +You might want to customize this banner, for example by translating it to your language. You can do that by specifying the path to a custom banner template in the `mkdocs.yml` file. This file should be a standard [jinja2 template](https://jinja.palletsprojects.com/en/2.11.x/templates/) where you can combine HTML and jinja2 variables. The information specified in `mkdocs.yml` will already by available as jinja2 variables (see [mkdocs project information](https://www.mkdocs.org/user-guide/configuration/#project-information)). _Example_: @@ -18,7 +18,43 @@ _Example_: === "docs/assets/templates/custom_banner.tpl" ```jinja - +

+ This box will disappear when printing + mkdocs-print-site-plugin +

+

This page has combined all site pages into one. You can export to PDF using File > Print > Save as PDF.

+

See also [export to PDF](https://timvink.github.io/mkdocs-print-site-plugin/how-to/export-PDF.html) and [export to standalone HTML](https://timvink.github.io/mkdocs-print-site-plugin/how-to/export-HTML.html).

``` As an example, have a look at the default [print_site_banner.tpl](https://github.com/timvink/mkdocs-print-site-plugin/tree/master/mkdocs_print_site_plugin/templates/print_site_banner.tpl). + +## Adding configurable content + +You might want to add some content to your cover page that's not yet specified in your `mkdocs.yml` file. +Of course you could just hard-code it in your custom template file, but you could also make use of MkDocs's [extra context](https://www.mkdocs.org/user-guide/custom-themes/#extra-context) feature, allowing you to use custom variables from your config file with `{{ config.extra. }}`. + +_Example_: + +=== "mkdocs.yml" + + ```yaml + plugins: + - print-site: + add_print_site_banner: true + print_site_banner_template: "docs/assets/templates/custom_banner.tpl" + + extra: + banner_message: "Save this page using File > Print > Save as PDF" + ``` + +=== "docs/assets/templates/custom_banner.tpl" + + ```jinja +

+ This box will disappear when printing + mkdocs-print-site-plugin +

+

{{ config.extra.banner_message }}

+ ``` + + diff --git a/docs/how-to/cover_page.md b/docs/how-to/cover_page.md index 95fed7f..f41d3b4 100644 --- a/docs/how-to/cover_page.md +++ b/docs/how-to/cover_page.md @@ -1,8 +1,8 @@ # Customize the cover page -When the `add_cover_page` option is set to true, `mkdocs-print-site-plugin` will generate a cover page when printing. This cover page is quite basic, and you might want to customize it. +By default the `add_cover_page` option is set to `true`, which will add a cover page to the print page. You might want to customize it more to your liking. -You can do that by specifying the path to a custom cover page template in the `mkdocs.yml` file. This file should be a standard [jinja2 template](https://jinja.palletsprojects.com/en/2.11.x/templates/) where you can combine HTML and jinja2 variables such as the information specified in `mkdocs.yml` (see [mkdocs project information](https://www.mkdocs.org/user-guide/configuration/#project-information)). +You can do that by specifying the path to a custom cover page template in the `mkdocs.yml` file. This file should be a standard [jinja2 template](https://jinja.palletsprojects.com/en/2.11.x/templates/) where you can combine HTML and jinja2 variables. The information specified in `mkdocs.yml` will already by available as jinja2 variables (see [mkdocs project information](https://www.mkdocs.org/user-guide/configuration/#project-information)). _Example_: @@ -24,7 +24,7 @@ _Example_:

This is my custom print cover page

``` -As an example, have a look at the default [cover_page.tpl](https://github.com/timvink/mkdocs-print-site-plugin/tree/master/mkdocs_print_site_plugin/templates/cover_page.tpl). +To get you started have a look at the default [cover_page.tpl](https://github.com/timvink/mkdocs-print-site-plugin/tree/master/mkdocs_print_site_plugin/templates/cover_page.tpl). ## Adding images @@ -41,11 +41,11 @@ _Example_: ``` -For a full example have a look at this [custom cover page with an image](https://github.com/timvink/mkdocs-print-site-plugin/blob/master/tests/fixtures/projects/with_markdown_ext/other_cover_page.tpl). +For a full working example have a look at this [custom cover page with an image](https://github.com/timvink/mkdocs-print-site-plugin/blob/master/tests/fixtures/projects/with_markdown_ext/other_cover_page.tpl). -## Adding extra content +## Adding configurable content -You might want to add some content to your cover page that's not yet specified in your `mkdocs.yml` file. Of course you could just hard-code it in your custom template file, but you could also make use of MkDocs's [extra context](https://www.mkdocs.org/user-guide/custom-themes/#extra-context) feature, allowing you to use `{{ config.extra. }}` +You might want to add some content to your cover page that's not yet specified in your `mkdocs.yml` file. Of course you could just hard-code it in your custom template file, but you could also make use of MkDocs's [extra context](https://www.mkdocs.org/user-guide/custom-themes/#extra-context) feature, allowing you to use custom variables from your config file with `{{ config.extra. }}`. _Example_: @@ -72,7 +72,7 @@ _Example_: ## Change the styling -You'll likely also want to change the styling to your liking. You can add your own CSS file using the [extra_css](https://www.mkdocs.org/user-guide/configuration/#extra_css) option from MkDocs. `mkdocs-print-site-plugin` wraps the cover page in a `
` with id `print-site-cover-page`. You should use this in your CSS to ensure not affecting other pages. +You'll likely also want to change the styling of the cover page to your liking. You can add your own CSS file using the [extra_css](https://www.mkdocs.org/user-guide/configuration/#extra_css) option from MkDocs. `mkdocs-print-site-plugin` wraps the cover page in a `