Skip to content

Commit

Permalink
✨ Add script to generate pages.json
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Sep 1, 2023
1 parent 8d16d94 commit da7e696
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/scripts/generate_pages_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import argparse
import json
import re
from pathlib import Path

from transcribee_backend.config import PageConfig

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("input_dir", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("--footer-pages", nargs="+", default=[])
args = parser.parse_args()

pages = {}
for file in args.input_dir.glob("*.md"):
name = file.stem
with open(file) as f:
text = f.read()
match = re.search(r"^#\s+(.*?)$", text, re.MULTILINE)
if match:
name = match.group(1)
pages[file.stem] = {"name": name, "text": text}
footer_position = None
try:
footer_position = args.footer_pages.index(file.stem)
except ValueError:
pass

pages[file.stem] = PageConfig(
name=name, footer_position=footer_position, text=text
)

with open(args.output, "w") as f:
json.dump({k: dict(v) for k, v in pages.items()}, f)
18 changes: 18 additions & 0 deletions doc/development_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ If you do more development on transcribee, you may wish to do the following thin
- Install the [`pre-commit`](https://pre-commit.com/) hook so that your changes are automatically
linted before you commit them. Run: `pre-commit install`
## Add pages
`transcribee` contains a minimal page system.
To add pages, add a `pages.json` in `backend/data`.
You can generate such a `pages.json` from a directory of markdown-files using the following command in the `backend/`-folder.
```shell
pdm run generate_pages_json PATH/TO/MARKDOWN/FOLDER data/pages.json
```
You can select which pages should be always shown in the footer using the `--footer-pages` option to the script. For example if you wanted the content of `page1.md` and `page2.md` to be shown in the footer, run
```
pdm run generate_pages_json PATH/TO/MARKDOWN/FOLDER data/pages.json --footer-pages page1 page2
```
The link text is automatically generated from the first first-level heading in the document.
## More!
There are more specific instructions in the respective readme files of the
Expand Down

0 comments on commit da7e696

Please sign in to comment.