From cc0f66bfafb1b3e86b6c61bf1c9a6ad585af7df2 Mon Sep 17 00:00:00 2001 From: Adrien Brignon Date: Tue, 9 May 2023 13:31:06 +0200 Subject: [PATCH] feat: explicit mode --- mkdocs_exporter/browser.py | 3 +++ mkdocs_exporter/plugins/extras/plugin.py | 11 +++++++++- mkdocs_exporter/plugins/pdf/config.py | 3 +++ mkdocs_exporter/plugins/pdf/plugin.py | 28 +++++++++++++++++------- mkdocs_exporter/preprocessor.py | 3 --- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/mkdocs_exporter/browser.py b/mkdocs_exporter/browser.py index 0397a16..5091d7f 100644 --- a/mkdocs_exporter/browser.py +++ b/mkdocs_exporter/browser.py @@ -26,7 +26,10 @@ def launched(self): def __init__(self): """The constructor.""" + self.browser = None + self.context = None self._launched = False + self.playwright = None self.lock = asyncio.Lock() diff --git a/mkdocs_exporter/plugins/extras/plugin.py b/mkdocs_exporter/plugins/extras/plugin.py index ffd9fed..21497da 100644 --- a/mkdocs_exporter/plugins/extras/plugin.py +++ b/mkdocs_exporter/plugins/extras/plugin.py @@ -1,6 +1,7 @@ from typing import Optional from mkdocs.plugins import BasePlugin from mkdocs_exporter.page import Page +from mkdocs.plugins import event_priority from mkdocs_exporter.preprocessor import Preprocessor from mkdocs_exporter.plugins.extras.config import Config @@ -9,14 +10,22 @@ class Plugin(BasePlugin[Config]): """The plugin.""" + @event_priority(-85) def on_post_page(self, html: str, page: Page, **kwargs) -> Optional[str]: """Invoked after a page has been built.""" + def resolve(value): + if callable(value): + return value(page) + + return value + preprocessor = Preprocessor() preprocessor.preprocess(html) for button in self.config.buttons: - preprocessor.button(**{k: v(page) if callable(v) else v for k, v in button.items()}) + if resolve(button['enabled']): + preprocessor.button(**{k: resolve(v) for k, v in button.items()}) return preprocessor.done() diff --git a/mkdocs_exporter/plugins/pdf/config.py b/mkdocs_exporter/plugins/pdf/config.py index a049ff8..4810e33 100644 --- a/mkdocs_exporter/plugins/pdf/config.py +++ b/mkdocs_exporter/plugins/pdf/config.py @@ -18,6 +18,9 @@ class Config(BaseConfig): enabled = c.Type(bool, default=True) """Is the generator enabled?""" + explicit = c.Type(bool, default=False) + """Should pages specify explicitly that they should be rendered as PDF?""" + concurrency = c.Type(int, default=4) """The maximum number of concurrent PDF generation tasks.""" diff --git a/mkdocs_exporter/plugins/pdf/plugin.py b/mkdocs_exporter/plugins/pdf/plugin.py index e214a7b..fa558fc 100644 --- a/mkdocs_exporter/plugins/pdf/plugin.py +++ b/mkdocs_exporter/plugins/pdf/plugin.py @@ -45,7 +45,7 @@ def resolve(path: str) -> str: def on_serve(self, server: LiveReloadServer, **kwargs) -> LiveReloadServer: """Invoked when the website is being served.""" - if not self.config.enabled: + if not self._enabled(): return for path in [*self.config.stylesheets, *self.config.scripts]: server.watch(path) @@ -59,7 +59,7 @@ def on_serve(self, server: LiveReloadServer, **kwargs) -> LiveReloadServer: def on_page_markdown(self, markdown: str, page: Page, **kwargs) -> str: """Invoked when the page's markdown has been loaded.""" - if not self.config.enabled or 'cover' in page.meta.get('hide', []): + if not self._enabled(page) or 'cover' in page.meta.get('hide', []): return content = markdown @@ -77,7 +77,7 @@ def on_page_markdown(self, markdown: str, page: Page, **kwargs) -> str: def on_pre_build(self, **kwargs) -> None: """Invoked before the build process starts.""" - if not self.config.enabled: + if not self._enabled(): return if self.loop and self.loop.is_running(): self.loop.close() @@ -95,15 +95,14 @@ def on_pre_build(self, **kwargs) -> None: def on_pre_page(self, page: Page, config: dict, **kwargs): """Invoked before building the page.""" - if not self.config.enabled: + if not self._enabled(): return directory = os.path.dirname(page.file.abs_dest_path) filename = os.path.splitext(os.path.basename(page.file.abs_src_path))[0] + '.pdf' fullpath = os.path.join(directory, filename) - if page.meta.get('pdf', True): - page.formats['pdf'] = os.path.relpath(fullpath, config['site_dir']) + page.formats['pdf'] = os.path.relpath(fullpath, config['site_dir']) @event_priority(-75) @@ -112,7 +111,9 @@ def on_post_page(self, html: str, page: Page, config: dict) -> Optional[str]: page.html = html - if not self.config.enabled or 'pdf' not in page.formats: + if not self._enabled(page): + del page.formats['pdf'] + if 'pdf' not in page.formats: return html async def render(page: Page) -> None: @@ -134,7 +135,7 @@ async def render(page: Page) -> None: def on_post_build(self, **kwargs) -> None: """Invoked after the build process.""" - if not self.config.enabled: + if not self._enabled(): return self.loop = asyncio.new_event_loop() @@ -158,3 +159,14 @@ def on_shutdown(self) -> None: if self.loop and self.loop.is_running(): self.loop.stop() + + + def _enabled(self, page: Page = None) -> bool: + """Is the plugin enabled for this page?""" + + if not self.config.enabled: + return False + if page and not page.meta.get('pdf', not self.config.explicit): + return False + + return True diff --git a/mkdocs_exporter/preprocessor.py b/mkdocs_exporter/preprocessor.py index 90776df..025a232 100644 --- a/mkdocs_exporter/preprocessor.py +++ b/mkdocs_exporter/preprocessor.py @@ -29,9 +29,6 @@ def preprocess(self, html: str) -> Preprocessor: def button(self, title: str, href: str, icon: str, **kwargs) -> Preprocessor: """Adds a button at the top of the page.""" - if not kwargs.get('enabled', True): - return self - tags = self.html.find('nav', {'class': 'md-tags'}) button = self.html.new_tag('a', title=title, href=href, **kwargs, attrs={'class': 'md-content__button md-icon'}) svg = BeautifulSoup(icon, 'lxml')