diff --git a/.gitignore b/.gitignore index 9cb5620..3d1e31d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ themes/ public/ book/ +.vscode/ +__pycache__/ diff --git a/README.md b/README.md index 10bf493..877b93f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is a static website that gets autogenerated from a bunch of [Joplin](https: I wrote a small script to read the notes, export them, and generate a static site. If you want to use the script and generate such a site for yourself, feel free to fork this repository. -Just run `./joplinexport.py`, it will read the database and create a site like this one and upload it to GitLab pages. +Just run `./joplinexport`, it will read the database and create a site like this one and upload it to GitLab pages. You can see the final site at: diff --git a/build b/build index d82c0b9..c763b8c 100755 --- a/build +++ b/build @@ -14,6 +14,7 @@ else ./mdbook build -d public fi +./move_html_to_dir public/ # Make checkboxes editable, just in case the user wants to keep their own checklist. find public/ -name "*.html" -type f -exec sed -i 's/input disabled=""/input/g' {} + diff --git a/content/index.md b/content/index.md deleted file mode 100644 index 19e9260..0000000 --- a/content/index.md +++ /dev/null @@ -1,3 +0,0 @@ -+++ -redirect_to = "welcome/stavros-notes/" -+++ \ No newline at end of file diff --git a/content/welcome/stavros-notes.md b/content/welcome/stavros-notes.md index ee05c63..23f08ac 100644 --- a/content/welcome/stavros-notes.md +++ b/content/welcome/stavros-notes.md @@ -8,7 +8,7 @@ If you want to use the script and generate such a site for yourself, feel free t [https://gitlab.com/stavros/notes/](https://gitlab.com/stavros/notes/) -Just run `./joplinexport.py` on your computer, it will read the database and create a site like this one and upload it to GitLab pages. +Just run `./joplinexport` on your computer, it will read the database and create a site like this one and upload it to GitLab pages. Otherwise, enjoy my notes! diff --git a/joplinexport.py b/joplinexport similarity index 98% rename from joplinexport.py rename to joplinexport index 64ceb84..a7c5118 100755 --- a/joplinexport.py +++ b/joplinexport @@ -150,8 +150,6 @@ class JoplinExporter: rmtree(self.static_dir, ignore_errors=True) self.content_dir.mkdir(parents=True) self.static_dir.mkdir(parents=True) - with open(self.content_dir / "index.md", mode="w") as outfile: - outfile.write('+++\nredirect_to = "welcome/stavros-notes/"\n+++') def resolve_note_links(self, note: Note) -> str: """Resolve the links between notes and replace them in the body.""" diff --git a/move_html_to_dir b/move_html_to_dir new file mode 100755 index 0000000..3723988 --- /dev/null +++ b/move_html_to_dir @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +""" +Rename files like `foo/bar.html` to `foo/bar/index.html` for prettier URLs. +""" +import os +import re +import sys +from pathlib import Path +from typing import Dict + + +def replace_links(path: Path, replacements: Dict[str, str]): + for filename in path.glob("**/*.html"): + with filename.open("r+") as f: + contents = f.read() + f.truncate(0) + f.seek(0) + for source, target in replacements.items(): + contents = contents.replace(source, target) + + # Convert absolute links to relative. + contents = re.sub( + '((?:href|src|root)\s*=\s*")((?:\.\./)+)([^\.])', r"\1/\3", contents + ) + + f.write(contents) + + +def main(path: Path): + replacements: Dict[str, str] = {} + for p in path.glob("**/*.html"): + if str(p.parent) == ".": + # Don't convert top-level files. + continue + + if p.name == "index.html": + # Don't convert top-level files. + continue + + print(f"Converting {p}...") + + dir_path = p.parent / p.stem + dir_path.mkdir(parents=True, exist_ok=True) + + new_path = dir_path / "index.html" + p.rename(new_path) + replacements[str(p.relative_to(path))] = str(new_path.relative_to(path)) + + replace_links(path, replacements) + + +if __name__ == "__main__": + main(Path(sys.argv[1])) diff --git a/update.sh b/update.sh index 3090d6e..5e63b64 100755 --- a/update.sh +++ b/update.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash git pull -./joplinexport.py +./joplinexport git add . git diff --cached git cma Updates