Skip to content

Commit

Permalink
feat: explicit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbrignon committed May 9, 2023
1 parent 21524d9 commit cc0f66b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
3 changes: 3 additions & 0 deletions mkdocs_exporter/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
11 changes: 10 additions & 1 deletion mkdocs_exporter/plugins/extras/plugin.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
3 changes: 3 additions & 0 deletions mkdocs_exporter/plugins/pdf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
28 changes: 20 additions & 8 deletions mkdocs_exporter/plugins/pdf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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
3 changes: 0 additions & 3 deletions mkdocs_exporter/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit cc0f66b

Please sign in to comment.