Skip to content

Commit

Permalink
Prep first release and blacken code
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Sep 5, 2020
1 parent c27a9d8 commit 9dfd0f2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

# mkdocs-print-site-plugin

> ⚠️ **This plugin is under development and the first version has not yet been released. Expected end of August :)**
[MkDocs](https://www.mkdocs.org/) plugin that adds a page to your site combining all pages, allowing your site visitors to *File > Print > Save as PDF* the entire site.

## Features :star2:
Expand Down
24 changes: 13 additions & 11 deletions mkdocs_print_site_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,35 @@ class PrintSitePlugin(BasePlugin):

def on_config(self, config, **kwargs):

# Because other plugins can alter the navigation
# Because other plugins can alter the navigation
# (and thus which pages should be in the print page)
# it is important 'print-site' is defined last in the 'plugins'
plugins = config.get('plugins')
print_site_position = [*dict(plugins)].index('print-site')
plugins = config.get("plugins")
print_site_position = [*dict(plugins)].index("print-site")
if print_site_position != len(plugins) - 1:
warnings.warn("[mkdocs-print-site] 'print-site' should be defined as the *last* plugin, to ensure the print page has any changes other plugins make. Please update the 'plugins:' section in your mkdocs.yml")

warnings.warn(
"[mkdocs-print-site] 'print-site' should be defined as the *last* plugin, to ensure the print page has any changes other plugins make. Please update the 'plugins:' section in your mkdocs.yml"
)

# Create the (empty) print page file in temp directory
tmp_dir = tempfile.gettempdir()
tmp_path = os.path.join(tmp_dir, "print_page.md")
f = open(tmp_path, "w")
f.write("")
f.close()
assert os.path.exists(tmp_path)

self.print_file = File(
path="print_page.md",
src_dir=tmp_dir,
dest_dir=config["site_dir"],
use_directory_urls=config.get('use_directory_urls'),
use_directory_urls=config.get("use_directory_urls"),
)

# Insert 'print page' to the end of the nav, if it exists
# We'll optionally remove the print page from navigation later on
# Because when nav is not defined, all files incl print_page are part of the nav
if config.get('nav'):
if config.get("nav"):
config.get("nav").append({"updated_later_on": "print_page.md"})

# Warn if we don't have CSS styles corresponding to current theme
Expand All @@ -72,7 +74,7 @@ def on_files(self, files, config, **kwargs):
# This ensures we can capture all other page HTMLs
# before inserting all of them into the print page.
files.append(self.print_file)

# Add all plugin CSS files to files directory
# Note we only insert the relevant css files per theme into the print page file
css_dest_dir = os.path.join(config["site_dir"], "css")
Expand All @@ -93,8 +95,8 @@ def on_nav(self, nav, config, files, **kwargs):
# Save print file
self.print_page = self.print_file.page
self.print_page.title = self.config.get("print_page_title")
self.print_page.edit_url = None # Ensure no edit icon on the print page.
self.print_page.edit_url = None # Ensure no edit icon on the print page.

# Save the (order of) pages in the navigation
nav_pages = [p for p in nav.pages if p != self.print_page]
self.renderer.pages = nav_pages
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from setuptools import setup, find_packages

# Read README in UTF-8
with open("README.md", 'r',encoding='UTF-8') as f:
with open("README.md", "r", encoding="UTF-8") as f:
long_description = ""
for line in f:
long_description += line


setup(
name="mkdocs-print-site-plugin",
version="0.0.1",
description="Add print site page",
version="0.1",
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",
keywords="mkdocs plugin",
keywords="mkdocs plugin print pdf",
url="https://github.com/timvink/mkdocs-print-site-plugin",
author="Tim Vink",
author_email="vinktim@gmail.com",
Expand Down
28 changes: 18 additions & 10 deletions tests/test_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
"""

assert os.path.exists(mkdocs_yml_path)

testproject_path = output_path / "testproject"

# Create empty 'testproject' folder
Expand Down Expand Up @@ -78,7 +78,7 @@ def build_docs_setup(testproject_path):

cwd = os.getcwd()
os.chdir(str(testproject_path))

try:
run = CliRunner().invoke(build_command, catch_exceptions=True)
os.chdir(cwd)
Expand All @@ -93,8 +93,13 @@ def check_build(tmp_path, project_mkdocs, exit_code=0):
"tests/fixtures/projects/%s" % project_mkdocs, tmp_path
)
result = build_docs_setup(tmp_proj)

msg = "cwd: %s, result: %s, exception: %s, exc_info: %s" % (os.getcwd(), result, result.exception, result.exc_info)

msg = "cwd: %s, result: %s, exception: %s, exc_info: %s" % (
os.getcwd(),
result,
result.exception,
result.exc_info,
)
assert result.exit_code == exit_code, msg
return tmp_proj

Expand All @@ -109,20 +114,23 @@ def text_in_page(tmp_proj, page_path, text):
#### Tests ####



def test_add_to_nav_works(tmp_path):
prj_path = check_build(tmp_path, "basic/mkdocs_addtonav_false.yml")

# print page should not be in navigation
assert not text_in_page(prj_path, "print_page/index.html", "class=\"nav-link\">Print Site</a>")

assert not text_in_page(
prj_path, "print_page/index.html", 'class="nav-link">Print Site</a>'
)


def test_basic_build(tmp_path):
prj_path = check_build(tmp_path, "basic/mkdocs.yml")

# Print page should be in the navigation
assert text_in_page(prj_path, "print_page/index.html", "class=\"nav-link\">Print Site</a>")

assert text_in_page(
prj_path, "print_page/index.html", 'class="nav-link">Print Site</a>'
)

# Make sure all 3 pages are combined and present
assert text_in_page(
prj_path, "print_page/index.html", '<h1 id="index-homepage">Homepage'
Expand Down

0 comments on commit 9dfd0f2

Please sign in to comment.