-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Plugins are now called "formats". From now, only the 'exporter' plugin needs to be registered. - Formats are still based on plugins, but are loaded dynamically by the 'exporter' one.
- Loading branch information
1 parent
78c5c72
commit 7f49ac6
Showing
23 changed files
with
261 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,41 @@ | ||
from typing import Callable | ||
|
||
from mkdocs.config import config_options as c | ||
from mkdocs.config.base import Config as BaseConfig | ||
|
||
from mkdocs_exporter.formats.pdf.config import Config as PDFFormatConfig | ||
|
||
|
||
class FormatsConfig(BaseConfig): | ||
|
||
"""The PDF format configuration.""" | ||
pdf = c.SubConfig(PDFFormatConfig) | ||
|
||
|
||
class ButtonConfig(BaseConfig): | ||
"""The configuration of a button.""" | ||
|
||
enabled = c.Type((bool, Callable), default=True) | ||
"""Is the button enabled?""" | ||
|
||
title = c.Type((str, Callable)) | ||
"""The button's title.""" | ||
|
||
icon = c.Type((str, Callable)) | ||
"""The button's icon (typically, an SVG element).""" | ||
|
||
attributes = c.Type((dict, Callable), default={}) | ||
"""Some extra attributes to add to the button.""" | ||
|
||
|
||
class Config(BaseConfig): | ||
"""The plugin's configuration.""" | ||
|
||
theme = c.Optional(c.Theme(default=None)) | ||
"""Override the theme used by your MkDocs instance.""" | ||
|
||
formats = c.SubConfig(FormatsConfig) | ||
"""The formats to generate.""" | ||
|
||
buttons = c.ListOfItems(c.SubConfig(ButtonConfig)) | ||
"""The buttons to add.""" |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
from pypdf import PdfReader, PdfWriter | ||
|
||
|
||
class Aggregator: | ||
"""Aggregates PDF documents together.""" | ||
|
||
|
||
def open(self, path: str) -> Aggregator: | ||
"""Opens the aggregator.""" | ||
|
||
self.path = path | ||
self.writer = PdfWriter() | ||
|
||
|
||
def covers(self, mode: str) -> Aggregator: | ||
"""Defines the way of handling cover pages.""" | ||
|
||
self._covers = mode | ||
|
||
|
||
def aggregate(self, pages: list) -> Aggregator: | ||
"""Aggregates pages together.""" | ||
|
||
for n, page in enumerate(pages): | ||
if 'pdf' not in page.formats: | ||
continue | ||
|
||
bounds = None | ||
pdf = page.formats['pdf']['path'] | ||
total = len(PdfReader(pdf).pages) | ||
|
||
if 'covers' in page.formats['pdf']: | ||
covers = page.formats['pdf']['covers'] | ||
|
||
if self._covers == 'none': | ||
bounds = (1 if covers['front'] else 0, (total - 1) if covers['back'] else total) | ||
if self._covers == 'limits': | ||
bounds = (1 if n != 0 and covers['front'] else 0, (total - 1) if n != (len(pages) - 1) and covers['back'] else total) | ||
|
||
self.writer.append(pdf, pages=bounds) | ||
|
||
|
||
def save(self, metadata={}) -> Aggregator: | ||
"""Saves the aggregated document.""" | ||
|
||
os.makedirs(os.path.dirname(self.path), exist_ok=True) | ||
|
||
self.writer.add_metadata({'/Producer': 'MkDocs Exporter', **metadata}) | ||
self.writer.write(self.path) | ||
self.writer.close() | ||
|
||
self.writer = None | ||
|
||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.