1
0
mirror of https://github.com/funkypenguin/geek-cookbook/ synced 2025-12-13 17:56:26 +00:00

Prepare for PDF generation

This commit is contained in:
David Young
2022-07-08 23:36:17 +00:00
parent e197494046
commit 85bd92692e
4 changed files with 179 additions and 19 deletions

98
extra_sass/style.css.scss Normal file
View File

@@ -0,0 +1,98 @@
@media print {
.md-typeset {
line-height: 1.4;
}
.md-typeset ul li,
.md-typeset ol li {
margin-bottom: .25rem;
}
#doc-toc ul li>a>span,
.md-typeset h1>span,
.md-typeset h2>span,
.md-typeset h3>span {
display: inline-block;
}
.md-typeset h1>span,
.md-typeset h2>span,
.md-typeset h3>span {
padding-right: 0.5rem;
}
/** workaround [[ */
.md-typeset h2>span.twemoji,
.md-typeset h2>span.twemoji>img {
height: 1.363em;
}
.md-typeset h2>span.twemoji>img {
margin-top: -0.3em;
}
#doc-toc span.twemoji,
#doc-toc span.twemoji img {
height: 1.125rem;
vertical-align: middle;
}
.md-typeset span.twemoji,
.md-typeset span.twemoji img {
display: inline-block;
}
.md-typeset figure {
display: block;
& > img {
margin: 0 auto;
}
}
/* ]] **/
.md-typeset .tx-content__footer hr,
footer.md-typeset hr {
background-color: transparent;
border-bottom: 0.5px solid #e91e63;
}
footer.md-typeset a {
display: flex;
justify-content: center;
vertical-align: middle;
}
.md-typeset .tx-insiders-container,
.md-typeset .mdx-insiders-container {
display: block;
clear: both;
& .tx-insiders-list,
& .mdx-insiders-list {
display: flex;
flex-wrap: wrap;
& .tx-insiders-list__item,
& .mdx-insiders-list__item {
float: none;
}
}
}
section.md-typeset[data-url="/changelog/"] {
& h4+ul {
margin: 0;
}
& ul>li {
margin-bottom: 0;
}
}
article a,
article blockquote,
article pre,
article table,
article ul,
article ol,
article * + div.highlight,
section + section * + div.highlight {
page-break-before: auto !important;
page-break-inside: auto !important;
}
}

View File

@@ -1,23 +1,26 @@
# Used to trigger a PDF build of the site
INHERIT: mkdocs.yml
plugins:
with-pdf:
author: David Young
copyright: ANY TEXT
cover: false
# Slimmed down version of recipes
docs_dir: 'docs_to_pdf'
plugins:
extra-sass:
with-pdf:
#author: David Young
#copyright: ANY TEXT
cover: true
back_cover: true
cover_title: TITLE TEXT
cover_subtitle: SUBTITLE TEXT
custom_template_path: TEMPLATES PATH
toc_title: TOC TITLE TEXT
heading_shift: false
#cover_title: TITLE TEXT
#cover_subtitle: SUBTITLE TEXT
#custom_template_path: TEMPLATES PATH
#toc_title: TOC TITLE TEXT
#heading_shift: true
toc_level: 3
ordered_chapter_level: 2
ordered_chapter_level: 4
excludes_children:
- 'release-notes/:upgrading'
- 'release-notes/:changelog'
exclude_pages:
- 'bugs/'
- 'appendix/contribute/'
@@ -26,14 +29,11 @@ plugins:
img: POSTER IMAGE URL
text: ALTERNATE TEXT
- src: ...
two_columns_level: 3
# two_columns_level: 3
render_js: true
headless_chrome_path: headless-chromium
output_path: any-place/document.pdf
enabled_if_env: ENABLE_PDF_EXPORT
# headless_chrome_path: headless-chromium
output_path: funky-penguins-geek-cookbook.pdf
debug_html: true
show_anchors: true
verbose: true

View File

@@ -0,0 +1,56 @@
import logging
from bs4 import BeautifulSoup
from mkdocs.structure.pages import Page
def inject_link(html: str, href: str,
page: Page, logger: logging) -> str:
"""Adding PDF View button on navigation bar(using material theme)"""
def _pdf_icon():
_ICON = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z" fill="#E2E5E7"/>
<path d="m384 128h96l-128-128v96c0 17.6 14.4 32 32 32z" fill="#B0B7BD"/>
<polygon points="480 224 384 128 480 128" fill="#CAD1D8"/>
<path d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z" fill="#F15642"/>
<g fill="#fff">
<path d="m101.74 303.15c0-4.224 3.328-8.832 8.688-8.832h29.552c16.64 0 31.616 11.136 31.616 32.48 0 20.224-14.976 31.488-31.616 31.488h-21.36v16.896c0 5.632-3.584 8.816-8.192 8.816-4.224 0-8.688-3.184-8.688-8.816v-72.032zm16.88 7.28v31.872h21.36c8.576 0 15.36-7.568 15.36-15.504 0-8.944-6.784-16.368-15.36-16.368h-21.36z"/>
<path d="m196.66 384c-4.224 0-8.832-2.304-8.832-7.92v-72.672c0-4.592 4.608-7.936 8.832-7.936h29.296c58.464 0 57.184 88.528 1.152 88.528h-30.448zm8.064-72.912v57.312h21.232c34.544 0 36.08-57.312 0-57.312h-21.232z"/>
<path d="m303.87 312.11v20.336h32.624c4.608 0 9.216 4.608 9.216 9.072 0 4.224-4.608 7.68-9.216 7.68h-32.624v26.864c0 4.48-3.184 7.92-7.664 7.92-5.632 0-9.072-3.44-9.072-7.92v-72.672c0-4.592 3.456-7.936 9.072-7.936h44.912c5.632 0 8.96 3.344 8.96 7.936 0 4.096-3.328 8.704-8.96 8.704h-37.248v0.016z"/>
</g>
<path d="m400 432h-304v16h304c8.8 0 16-7.2 16-16v-16c0 8.8-7.2 16-16 16z" fill="#CAD1D8"/>
</svg>
''' # noqa: E501
return BeautifulSoup(_ICON, 'html.parser')
logger.info('(hook on inject_link: %s)', page.title)
soup = BeautifulSoup(html, 'html.parser')
nav = soup.find(class_='md-header-nav')
if not nav:
# after 7.x
nav = soup.find('nav', class_='md-header__inner')
if nav:
a = soup.new_tag('a', href=href, title='PDF',
**{'class': 'md-header-nav__button md-icon'})
a.append(_pdf_icon())
nav.append(a)
return str(soup)
return html
# def pre_js_render(soup: BeautifulSoup, logger: logging) -> BeautifulSoup:
# logger.info('(hook on pre_js_render)')
# return soup
# def pre_pdf_render(soup: BeautifulSoup, logger: logging) -> BeautifulSoup:
# logger.info('(hook on pre_pdf_render)')
# tag = soup.find(lambda tag: tag.name ==
# 'body' and 'data-md-color-scheme' in tag.attrs)
# if tag:
# tag['data-md-color-scheme'] = 'print'
# return soup

View File

@@ -1,4 +1,10 @@
#!/bin/bash
set -e
docker build --build-arg FROM_SOURCE=ghcr.io/geek-cookbook/mkdocs-material-insiders . -t funkypenguin/mkdocs-material --platform amd64
docker run --rm --name mkdocs-material -e ENABLE_PDF_EXPORT=1 -v ${PWD}:/docs funkypenguin/mkdocs-material build -f mkdocs-pdf-print.yml
# docker build --build-arg FROM_SOURCE=ghcr.io/geek-cookbook/mkdocs-material-insiders . -t funkypenguin/mkdocs-material --platform amd64
# Prepare slimmed-down versions for PDFing
cp -rf manuscript docs_to_pdf
find docs_to_pdf -type f -exec sed -i -e 's/recipe-footer.md/common-links.md/g' {} \;
# Build PDF from slimmed recipes
docker run --rm --name mkdocs-material -e ENABLE_PDF_EXPORT=1 -v ${PWD}/docs_to_pdf:/docs funkypenguin/mkdocs-material build -f mkdocs-pdf-print.yml