Make links prettier
This commit is contained in:
parent
012872d405
commit
0f91704180
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,5 @@
|
||||
themes/
|
||||
public/
|
||||
book/
|
||||
.vscode/
|
||||
__pycache__/
|
||||
|
@ -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:
|
||||
|
||||
|
1
build
1
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' {} +
|
||||
|
@ -1,3 +0,0 @@
|
||||
+++
|
||||
redirect_to = "welcome/stavros-notes/"
|
||||
+++
|
@ -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!
|
||||
|
||||
|
@ -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."""
|
53
move_html_to_dir
Executable file
53
move_html_to_dir
Executable 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]))
|
Loading…
Reference in New Issue
Block a user