Skip to content

Commit 01f4db6

Browse files
committed
Change page exclusion method from directory search to pattern matching #58.
1 parent 231d6c6 commit 01f4db6

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

mkdocs_with_pdf/generator.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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
76

87
from bs4 import BeautifulSoup, PageElement
98
from weasyprint import HTML, urls
@@ -35,6 +34,18 @@ def __init__(self, options: Options):
3534
self._scraped_scripts = []
3635
self._mixed_script = ''
3736

37+
def to_pattern(s: str) -> re.Pattern:
38+
if s.startswith('^'):
39+
return re.compile(s)
40+
return re.compile(f'^{s}')
41+
42+
self._exclude_page_patterns = list(map(
43+
to_pattern,
44+
self._options.exclude_pages
45+
))
46+
self._options.logger.debug(
47+
f'Exclude page patterns: {self._exclude_page_patterns}')
48+
3849
def on_nav(self, nav):
3950
""" on_nav """
4051
self._nav = nav
@@ -44,31 +55,11 @@ def on_nav(self, nav):
4455
def on_post_page(self, output_content: str, page, pdf_path: str) -> str:
4556
""" on_post_page """
4657

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-
7058
def is_excluded(url: str) -> bool:
71-
return url in get_excluded_pages(self._options.exclude_pages)
59+
for p in self._exclude_page_patterns:
60+
if p.match(url):
61+
return True
62+
return False
7263

7364
if is_excluded(page.url):
7465
self.logger.info(f'Page skipped: [{page.title}]({page.url})')
@@ -234,7 +225,7 @@ def shift_heading(elem, page):
234225
elem.insert(0, h1)
235226
return elem
236227

237-
def cleanup_class(classes: []):
228+
def cleanup_class(classes):
238229
if classes and len(classes):
239230
excludes = ['md-content__inner']
240231
return [c for c in classes if not (c in excludes)]

0 commit comments

Comments
 (0)