Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Add a book preprocessor #910

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,5 @@ jobs:
run: cargo build --verbose --examples
- name: Build the book
run: mdbook build docs
- name: Build the book using the script
run: python3 docs/build_book.py
- name: Run book tests
run: mdbook test -L target/debug/deps docs
15 changes: 9 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ But they are removed when building the book
cargo install mdbook
```

Build the book (simple method, contains `Sphinx` artifacts):
Book build process uses preprocessor to remove Sphinx artifacts.
Due to limitation of mdbook, it can only be built either from main directory,
using `mdbook X docs` or from `docs` directory, using `mdbook X`, where
`X` is mdbook command such as `build` / `serve` / `test` etc.

If the book is built from another directory (e.g. scylla, using `mdbook build ../docs`),
preprocessor won't be found, so the result will contain Sphinx artifacts.

Build the book.
```bash
mdbook build docs
# HTML will be in docs/book
```

To build the release version use a script which automatically removes `Sphinx` chunks:
```bash
python3 docs/build_book.py
# HTML will be in docs/book/scriptbuild/book
```

Or serve it on a local http server (automatically refreshes on changes)
```bash
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ build:

.PHONY: docs
docs:
./docs/build_book.py
mdbook build docs

.PHONY: up
up:
Expand Down
11 changes: 10 additions & 1 deletion docs/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ edition = "2021"

[output.html]
default-theme = "ayu"
git-repository-url = "https://github.com/scylladb/scylla-rust-driver"
git-repository-url = "https://github.com/scylladb/scylla-rust-driver"

[preprocessor.sphinx]
command = '''bash -c '
if test -f "./docs/sphinx_preprocessor.py"; then
python ./docs/sphinx_preprocessor.py "$@"
else
python ./sphinx_preprocessor.py "$@"
fi' run_preprocessor
'''
66 changes: 0 additions & 66 deletions docs/build_book.py

This file was deleted.

34 changes: 34 additions & 0 deletions docs/sphinx_preprocessor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: how about adding a shebang? We had it in the previous script.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script is not supposed to be run manually, so I don't see how shebang helps?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense not to do it.

import sys

def remove_sphinx_markdown(md_file_text):
text_split = md_file_text.split("```eval_rst")

result = text_split[0]

for i in range(1, len(text_split)):
cur_chunk = text_split[i]
result += cur_chunk[cur_chunk.find("```") + 3:]

return result

def process_section(section):
if 'Chapter' in section:
section['Chapter']['content'] = remove_sphinx_markdown(section['Chapter']['content'])
for s in section['Chapter']['sub_items']:
process_section(s)

if __name__ == '__main__':
if len(sys.argv) > 1: # we check if we received any argument
if sys.argv[1] == "supports":
# then we are good to return an exit status code of 0, since the other argument will just be the renderer's name
sys.exit(0)

# load both the context and the book representations from stdin
context, book = json.load(sys.stdin)

for section in book['sections']:
process_section(section)

# we are done with the book's modification, we can just print it to stdout
print(json.dumps(book))