Skip to content

Commit 11f8aa6

Browse files
author
Erik Thorsell
committed
Exclude pages recursively
Add get_excluded_pages() which will recursively expand user-excluded paths and return a list of all pages to be excluded. The previous implementation only looks at the exact paths provided in the mkdocs.yaml whereas this implementation will work both for excluding files and directories. Possible improvement: get_excluded_pages() is called once per is_excluded() which in turn is called once per on_post_page(). Since self._options.exclude_pages will not change between os_post_page() calls, this is inefficient and could be an issue in very large projects. Suggestions for how to make get_excluded_pages() only run once per .pdf-generation are appreciated! Closes #58
1 parent 65f36ba commit 11f8aa6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

mkdocs_with_pdf/generator.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
from importlib import import_module
55
from importlib.util import module_from_spec, spec_from_file_location
6+
from typing import List
67

78
from bs4 import BeautifulSoup, PageElement
89
from weasyprint import HTML, urls
@@ -43,8 +44,31 @@ def on_nav(self, nav):
4344
def on_post_page(self, output_content: str, page, pdf_path: str) -> str:
4445
""" on_post_page """
4546

47+
def get_excluded_pages(e_paths: List[str]) -> List[str]:
48+
49+
def get_files_in_dir(path: str) -> List[str]:
50+
files = list()
51+
for f in os.listdir(path):
52+
sub_path = os.path.join(path, f)
53+
if os.path.isdir(sub_path):
54+
files += get_files_in_dir(sub_path)
55+
else:
56+
files.append(os.path.splitext(sub_path)[0] + '/')
57+
return files
58+
59+
excluded_pages = list()
60+
cwd = os.getcwd()
61+
os.chdir("docs")
62+
for path in e_paths:
63+
if os.path.isdir(path):
64+
excluded_pages += get_files_in_dir(path)
65+
else:
66+
excluded_pages.append(path)
67+
os.chdir(cwd)
68+
return excluded_pages
69+
4670
def is_excluded(url: str) -> bool:
47-
return url in self._options.exclude_pages
71+
return url in get_excluded_pages(self._options.exclude_pages)
4872

4973
if is_excluded(page.url):
5074
self.logger.info(f'Page skipped: [{page.title}]({page.url})')

0 commit comments

Comments
 (0)