-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcda4a5
commit 85bca98
Showing
21 changed files
with
289 additions
and
52 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,4 +1,3 @@ | ||
nav: | ||
- getting-started.md | ||
- setup | ||
- reference.md |
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
Empty file.
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
nav: | ||
- setting-up-documents.md | ||
- setting-up-buttons.md | ||
- ... |
This file was deleted.
Oops, something went wrong.
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 +1,91 @@ | ||
--- | ||
buttons: | ||
- title: I'm Feeling Lucky | ||
href: https://www.youtube.com/watch?v=dQw4w9WgXcQ | ||
icon: material-star-outline | ||
target: _blank | ||
--- | ||
|
||
# Setting up buttons | ||
|
||
You can define custom buttons at the top of your pages. | ||
|
||
## Configuration | ||
|
||
As this feature is provided by the `mkdocs/exporter/extras` plugin, you'll need to add it to your list of plugins: | ||
|
||
```yaml | ||
plugins: | ||
- mkdocs/exporter | ||
- mkdocs/exporter/extras | ||
``` | ||
## Usage | ||
### Adding a download button | ||
This example will add a download button at the top of all pages that have a corresponding PDF document: | ||
```yaml | ||
plugins: | ||
- mkdocs/exporter/extras: | ||
buttons: | ||
- title: Download as PDF | ||
icon: material-file-download-outline | ||
enabled: !!python/name:mkdocs_exporter.plugins.pdf.button.enabled | ||
href: !!python/name:mkdocs_exporter.plugins.pdf.button.href | ||
download: !!python/name:mkdocs_exporter.plugins.pdf.button.download | ||
``` | ||
The functions referenced in this configuration are provided by the **MkDocs Exporter** plugin. | ||
### Defining a dynamic button | ||
As you've seen in the previous example, you can use Python functions to resolve the attributes of a button dynamically. | ||
Let's write a button that when clicked, it starts a search on Google with the current page's title as query. | ||
First of all, let's write the function that will resolve to the `href` attribute's of the button: | ||
|
||
```python | ||
from urllib.parse import urlencode | ||
from mkdocs_exporter.page import Page | ||
def href(page: Page) -> str: | ||
"""The button's 'href' attribute.""" | ||
return 'https://google.com/search' + urlencode({q: page.title}) | ||
``` | ||
|
||
Then, we can define a button and specify the previously defined function (assuming it has been saved to `my_module/button.py`): | ||
|
||
```yaml | ||
plugins: | ||
- mkdocs/exporter/extras: | ||
buttons: | ||
- title: Search on Google | ||
icon: material-google | ||
href: !!python/name:my_module.button.href | ||
``` | ||
|
||
Rinse and repeat, you can use this method for any property of a button. | ||
|
||
### Adding a button on a page | ||
|
||
You can also use `meta` tags to define buttons on a per-page basis. | ||
Here's the configuration used by this page: | ||
|
||
```yaml | ||
--- | ||
{% set button = page.meta.buttons[0] -%} | ||
buttons: | ||
- title: {{ button.title }} | ||
href: {{ button.href }} | ||
icon: {{ button.icon }} | ||
target: {{ button.target }} | ||
--- | ||
# {{ page.title }} | ||
[...] | ||
``` |
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
# Setting up documents | ||
|
||
## Configuration | ||
|
||
First of all, you'll need to register the `mkdocs/exporter/pdf` plugin (**after** the `mkdocs/exporter` one) to your configuration: | ||
|
||
```yaml | ||
plugins: | ||
- mkdocs/exporter | ||
- mkdocs/exporter/pdf | ||
``` | ||
???+ question "Why an additional plugin?" | ||
**MkDocs Exporter** comes with various plugins in a single package. | ||
This architecture was chosen to reduce code duplication and maintain a generic base that can be used | ||
to export your pages to formats other than PDF (although this is the only format currently supported). | ||
Basically, the `mkdocs/exporter` must always be registered first as it provides a common ground for | ||
other plugins to use. | ||
|
||
## Usage | ||
|
||
### Toggle documents generation | ||
|
||
The documents generation can be enabled or disabled at any time. | ||
This feature is especially useful during your development process, when you don't want to slow down your iterations because of document generation. | ||
|
||
```yaml | ||
plugins: | ||
- mkdocs/exporter/pdf: | ||
enabled: ![MKDOCS_EXPORTER_ENABLED, true] | ||
``` | ||
|
||
You can now use the `MKDOCS_EXPORTER_ENABLED` environment variable to toggle the PDF generation. | ||
|
||
### Increase concurrency | ||
|
||
PDF are, by default, generated concurrently which greatly reduces build time. | ||
You may want to override the default value of **4**, based on your current hardware. | ||
|
||
```yaml | ||
plugins: | ||
- mkdocs/exporter/pdf: | ||
concurrency: 16 | ||
``` | ||
|
||
With this configuration, up to **16** PDF documents can be generated concurrently. | ||
As you've guessed, a value of **1** will build PDF documents sequentially. | ||
|
||
### Exclude some pages | ||
|
||
Sometimes, you may want to prevent a page from being converted to a PDF document. | ||
You can use the `pdf` meta tag on your page to do so: | ||
|
||
```yaml | ||
--- | ||
pdf: false | ||
--- | ||
# Lorem ipsum dolor sit amet | ||
[...] | ||
``` | ||
|
||
If you exclude more pages than you include, you can take the problem the other way around and explicitly define the pages for which PDF documents should be generated. | ||
We call that the `explicit` mode, it can be enabled in your configuration file: | ||
|
||
```yaml | ||
plugins: | ||
- mkdocs/exporter/pdf: | ||
explicit: true | ||
``` | ||
|
||
Only pages with a truthy value in the `pdf` meta tag will have a PDF document generated. | ||
|
||
```yaml | ||
--- | ||
pdf: true | ||
--- | ||
# Lorem ipsum dolor sit amet | ||
[...] | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from materialx.emoji import twemoji, to_svg | ||
|
||
|
||
class HTMLStash: | ||
"""Markdown HTML stash stub.""" | ||
|
||
def store(self, str): | ||
return str | ||
|
||
|
||
class MarkdownEmoji: | ||
"""Markdown Emojij stub.""" | ||
|
||
emoji_index = twemoji({}, None) | ||
|
||
|
||
class Markdown: | ||
"""Markdown stub.""" | ||
|
||
htmlStash = HTMLStash() | ||
inlinePatterns = { | ||
'emoji': MarkdownEmoji() | ||
} | ||
|
||
|
||
def get_svg_icon(name): | ||
"""Gets an icon by its name.""" | ||
|
||
if not name.startswith(':'): | ||
name = ':' + name | ||
if not name.endswith(':'): | ||
name = name + ':' | ||
|
||
try: | ||
icon = to_svg('twemoji', name, None, None, None, None, None, {}, Markdown()) | ||
|
||
if icon is not None: | ||
return icon.text | ||
except KeyError: | ||
return None |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
from bs4 import BeautifulSoup | ||
from mkdocs_exporter.plugins.extras.icon import get_svg_icon | ||
from mkdocs_exporter.preprocessor import Preprocessor as BasePreprocessor | ||
|
||
|
||
class Preprocessor(BasePreprocessor): | ||
"""An extended preprocessor.""" | ||
|
||
|
||
def button(self, title: str, href: str, icon: str, **kwargs) -> Preprocessor: | ||
"""Adds a button at the top of the page.""" | ||
|
||
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(get_svg_icon(icon) or get_svg_icon('material-progress-question'), 'lxml') | ||
|
||
button.append(svg) | ||
|
||
if tags: | ||
tags.insert_after(button) | ||
else: | ||
self.html.find('article', {'class': 'md-content__inner'}).insert(0, button) | ||
|
||
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
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.