Skip to content

Commit 282359e

Browse files
committed
order PDF sections using the same metadata as the site
1 parent 2c6964a commit 282359e

File tree

15 files changed

+87
-12
lines changed

15 files changed

+87
-12
lines changed

.github/workflows/deploy-develop.yml

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ jobs:
4848
run: |
4949
curl -L https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb > wkhtmltopdf.deb
5050
sudo apt-get install ./wkhtmltopdf.deb
51+
- name: Install Python dependencies
52+
run: pip install -r requirements-ci.txt
5153

5254
- name: Build all pdfs
5355
run: npm run pdf:build-all-ci

.github/workflows/deploy-main.yml

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ jobs:
5050
run: |
5151
curl -L https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb > wkhtmltopdf.deb
5252
sudo apt-get install ./wkhtmltopdf.deb
53+
- name: Install Python dependencies
54+
run: pip install -r requirements-ci.txt
5355

5456
- name: Build all pdfs
5557
run: npm run pdf:build-all-ci

docker/images/Dockerfile.pdf-builder

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ARG WKHTML_TO_PDF_DEB=wkhtmltox_${WKHTML_TO_PDF_VERSION}-1.focal_amd64.deb
77

88
RUN apt-get update && apt-get install --no-install-recommends -y \
99
python3 \
10+
python3-pip \
1011
wget \
1112
&& apt-get clean \
1213
&& rm -rf /var/lib/apt/lists/*
@@ -25,3 +26,7 @@ RUN wget --quiet --no-check-certificate -P /tmp https://github.com/wkhtmltopdf/p
2526
&& apt-get clean \
2627
&& rm -rf /var/lib/apt/lists/* \
2728
&& rm -rf /tmp/*
29+
30+
# Install python dependencies
31+
COPY requirements-ci.txt .
32+
RUN pip install --no-cache-dir -r requirements-ci.txt

requirements-ci.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-frontmatter
2+

requirements-ci.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# This file is autogenerated by pip-compile with python 3.10
3+
# To update, run:
4+
#
5+
# pip-compile requirements-ci.in
6+
#
7+
python-frontmatter==1.0.0
8+
# via -r requirements-ci.in
9+
pyyaml==6.0
10+
# via python-frontmatter

requirements.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pdftotext
22
pip-tools
33
pytest
4+
python-frontmatter

requirements.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# This file is autogenerated by pip-compile
2+
# This file is autogenerated by pip-compile with python 3.10
33
# To update, run:
44
#
55
# pip-compile
@@ -26,6 +26,10 @@ pyparsing==2.4.7
2626
# via packaging
2727
pytest==6.2.4
2828
# via -r requirements.in
29+
python-frontmatter==1.0.0
30+
# via -r requirements.in
31+
pyyaml==6.0
32+
# via python-frontmatter
2933
toml==0.10.2
3034
# via pytest
3135
tomli==1.0.4

scripts/pdf/__tests__/combine_mdx/expected.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
### Heading 2
1010

11-
### Heading 3
11+
#### Heading 3
1212

13-
#### Heading 4
13+
##### Heading 4
1414

1515

1616
---
@@ -23,7 +23,7 @@
2323

2424
### Heading 2
2525

26-
### Heading 3
26+
#### Heading 3
2727

28-
#### Heading 4
28+
##### Heading 4
2929

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: 'Title'
3+
---
4+
5+
# Title
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: 'Title'
3+
---
4+
5+
# Title
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: 'Title'
3+
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: 'Title'
3+
---
4+
5+
# Title
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pathlib import Path
2+
3+
from .....generate_pdf import TocItem
4+
5+
expected = [
6+
TocItem(filename=Path(__file__).parent / "index.mdx", chapter=[1]),
7+
TocItem(filename=Path(__file__).parent / "2.mdx", chapter=[2]),
8+
TocItem(filename=Path(__file__).parent / "2.1/2.1.mdx", chapter=[3]),
9+
TocItem(filename=Path(__file__).parent / "1.mdx", chapter=[4]),
10+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: 'Title'
3+
navigation:
4+
- "2"
5+
- "2.1"
6+
- "1"
7+
---
8+
9+
# Title

scripts/pdf/generate_pdf.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pathlib import Path
88
from subprocess import run
99
from typing import List
10+
import frontmatter
1011

1112
BASE_DIR = Path(__file__).resolve().parent
1213

@@ -121,8 +122,8 @@ def main(args):
121122

122123
def setup(args):
123124
doc_path = args.doc_path
124-
product = doc_path.parts[2]
125-
version = doc_path.parts[3]
125+
product = doc_path.parts[-2] if len(doc_path.parts) >= 4 else doc_path.parts[-1]
126+
version = doc_path.parts[-1] if len(doc_path.parts) >= 4 else ''
126127

127128
_file_prefix = f"{product}_v{version}_documentation"
128129

@@ -140,8 +141,9 @@ def setup(args):
140141
def list_files(doc_path, chapter=None, is_root_dir=True):
141142
chapter = [1] if chapter is None else chapter
142143
all_files = []
144+
nav_order = load_nav_index(doc_path)
143145
directory_contents = sorted(
144-
filter(filter_path, doc_path.iterdir()), key=put_index_first
146+
filter(filter_path, doc_path.iterdir()), key=lambda file: put_index_first(file, nav_order)
145147
)
146148

147149
for i, entry in enumerate(directory_contents):
@@ -226,10 +228,20 @@ def parse_mdx(mdx_file):
226228
front_matter, _, content = mdx_file.read_text().partition("---")[2].partition("---")
227229
return front_matter.strip(), content.strip()
228230

229-
230-
def put_index_first(path):
231-
filename = str(path)
232-
return filename.replace("index.mdx", "00_index.mdx")
231+
def load_nav_index(path):
232+
try:
233+
index = frontmatter.load(path / "index.mdx")["navigation"]
234+
return index
235+
except (FileNotFoundError, KeyError):
236+
return None
237+
238+
def put_index_first(path, nav_order):
239+
filename = path.name if path.suffix != ".mdx" else path.stem
240+
nav_order = nav_order or [filename]
241+
indice = 0
242+
if path.name != "index.mdx":
243+
indice = nav_order.index(filename)+1 if filename in nav_order else len(nav_order)
244+
return f'{indice:03d}_{filename}'
233245

234246

235247
def get_title(doc_path):

0 commit comments

Comments
 (0)