Make links prettier

This commit is contained in:
Stavros Korokithakis 2021-11-20 17:22:59 +02:00
parent 012872d405
commit 0f91704180
No known key found for this signature in database
GPG Key ID: 26EA345ECD4C2A63
8 changed files with 59 additions and 8 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
themes/ themes/
public/ public/
book/ book/
.vscode/
__pycache__/

View File

@ -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. 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. 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: You can see the final site at:

1
build
View File

@ -14,6 +14,7 @@ else
./mdbook build -d public ./mdbook build -d public
fi fi
./move_html_to_dir public/
# Make checkboxes editable, just in case the user wants to keep their own checklist. # 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' {} + find public/ -name "*.html" -type f -exec sed -i 's/input disabled=""/input/g' {} +

View File

@ -1,3 +0,0 @@
+++
redirect_to = "welcome/stavros-notes/"
+++

View File

@ -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/) [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! Otherwise, enjoy my notes!

View File

@ -150,8 +150,6 @@ class JoplinExporter:
rmtree(self.static_dir, ignore_errors=True) rmtree(self.static_dir, ignore_errors=True)
self.content_dir.mkdir(parents=True) self.content_dir.mkdir(parents=True)
self.static_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: def resolve_note_links(self, note: Note) -> str:
"""Resolve the links between notes and replace them in the body.""" """Resolve the links between notes and replace them in the body."""

53
move_html_to_dir Executable file
View File

@ -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]))

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
git pull git pull
./joplinexport.py ./joplinexport
git add . git add .
git diff --cached git diff --cached
git cma Updates git cma Updates