Skip to content

Commit c9aecf4

Browse files
authored
Merge pull request #910 from Lorak-mmk/book-preprocessor
Docs: Add a book preprocessor
2 parents befa148 + 9b8ef00 commit c9aecf4

File tree

6 files changed

+54
-76
lines changed

6 files changed

+54
-76
lines changed

.github/workflows/book.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,5 @@ jobs:
3333
run: cargo build --verbose --examples
3434
- name: Build the book
3535
run: mdbook build docs
36-
- name: Build the book using the script
37-
run: python3 docs/build_book.py
3836
- name: Run book tests
3937
run: mdbook test -L target/debug/deps docs

CONTRIBUTING.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,20 @@ But they are removed when building the book
7272
cargo install mdbook
7373
```
7474

75-
Build the book (simple method, contains `Sphinx` artifacts):
75+
Book build process uses preprocessor to remove Sphinx artifacts.
76+
Due to limitation of mdbook, it can only be built either from main directory,
77+
using `mdbook X docs` or from `docs` directory, using `mdbook X`, where
78+
`X` is mdbook command such as `build` / `serve` / `test` etc.
79+
80+
If the book is built from another directory (e.g. scylla, using `mdbook build ../docs`),
81+
preprocessor won't be found, so the result will contain Sphinx artifacts.
82+
83+
Build the book.
7684
```bash
7785
mdbook build docs
7886
# HTML will be in docs/book
7987
```
8088
81-
To build the release version use a script which automatically removes `Sphinx` chunks:
82-
```bash
83-
python3 docs/build_book.py
84-
# HTML will be in docs/book/scriptbuild/book
85-
```
8689
8790
Or serve it on a local http server (automatically refreshes on changes)
8891
```bash

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ build:
4646

4747
.PHONY: docs
4848
docs:
49-
./docs/build_book.py
49+
mdbook build docs
5050

5151
.PHONY: up
5252
up:

docs/book.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ edition = "2021"
1111

1212
[output.html]
1313
default-theme = "ayu"
14-
git-repository-url = "https://github.com/scylladb/scylla-rust-driver"
14+
git-repository-url = "https://github.com/scylladb/scylla-rust-driver"
15+
16+
[preprocessor.sphinx]
17+
command = '''bash -c '
18+
if test -f "./docs/sphinx_preprocessor.py"; then
19+
python ./docs/sphinx_preprocessor.py "$@"
20+
else
21+
python ./sphinx_preprocessor.py "$@"
22+
fi' run_preprocessor
23+
'''

docs/build_book.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

docs/sphinx_preprocessor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import json
2+
import sys
3+
4+
def remove_sphinx_markdown(md_file_text):
5+
text_split = md_file_text.split("```eval_rst")
6+
7+
result = text_split[0]
8+
9+
for i in range(1, len(text_split)):
10+
cur_chunk = text_split[i]
11+
result += cur_chunk[cur_chunk.find("```") + 3:]
12+
13+
return result
14+
15+
def process_section(section):
16+
if 'Chapter' in section:
17+
section['Chapter']['content'] = remove_sphinx_markdown(section['Chapter']['content'])
18+
for s in section['Chapter']['sub_items']:
19+
process_section(s)
20+
21+
if __name__ == '__main__':
22+
if len(sys.argv) > 1: # we check if we received any argument
23+
if sys.argv[1] == "supports":
24+
# then we are good to return an exit status code of 0, since the other argument will just be the renderer's name
25+
sys.exit(0)
26+
27+
# load both the context and the book representations from stdin
28+
context, book = json.load(sys.stdin)
29+
30+
for section in book['sections']:
31+
process_section(section)
32+
33+
# we are done with the book's modification, we can just print it to stdout
34+
print(json.dumps(book))

0 commit comments

Comments
 (0)