Split uri into url and destination, remove trailing slashes from links
This commit is contained in:
@@ -13,11 +13,11 @@ else:
|
||||
|
||||
|
||||
class Article(Content):
|
||||
def __init__(self, path: Path, meta: dict[str, Any]):
|
||||
def __init__(self, source: Path, meta: dict[str, Any]):
|
||||
if "tags" not in meta:
|
||||
log.warning("no tags for article in '%s'", path)
|
||||
log.warning("no tags for article in '%s'", source)
|
||||
super().__init__(
|
||||
path,
|
||||
source,
|
||||
meta["section"],
|
||||
meta["slug"],
|
||||
meta["date"],
|
||||
@@ -35,7 +35,7 @@ class Article(Content):
|
||||
template = site.jinja_env.get_template("article.html")
|
||||
html = template.render(article=self, content=content, site=site)
|
||||
|
||||
out_path = site.output_path / self.uri
|
||||
out_path = site.output_path / self.destination
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
out_path.write_text(html, encoding="utf-8")
|
||||
log.debug("Rendered %s -> %s", self.source, out_path)
|
||||
|
||||
@@ -13,7 +13,7 @@ class Content(Output):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
path: Path,
|
||||
source: Path,
|
||||
section: str,
|
||||
slug: str,
|
||||
created: date,
|
||||
@@ -21,10 +21,10 @@ class Content(Output):
|
||||
tags: set[str] | None = None,
|
||||
summary: str | None = None,
|
||||
):
|
||||
super().__init__(Content.build_uri(section, slug))
|
||||
super().__init__(Content.build_path(section, slug))
|
||||
if tags is None:
|
||||
tags = set()
|
||||
self.source = path
|
||||
self.source = source
|
||||
self.section: str = section
|
||||
self.created = created
|
||||
self.updated = updated
|
||||
@@ -32,11 +32,11 @@ class Content(Output):
|
||||
self.summary: str | None = summary
|
||||
|
||||
@staticmethod
|
||||
def build_uri(section: str, slug: str) -> str:
|
||||
def build_path(section: str, slug: str) -> Path:
|
||||
if not Content._SECTION_RE.match(section):
|
||||
raise ContentError(f"Invalid section '{section}'")
|
||||
uri = f"{section}/{slug}.html"
|
||||
return uri
|
||||
path = Path(f"{section}/{slug}.html")
|
||||
return path
|
||||
|
||||
def read_body(self) -> str:
|
||||
with self.source.open("r", encoding="utf-8") as f:
|
||||
|
||||
@@ -13,12 +13,12 @@ else:
|
||||
|
||||
|
||||
class File(Output):
|
||||
def __init__(self, uri: str, source: Path):
|
||||
super().__init__(uri)
|
||||
def __init__(self, path: Path, source: Path):
|
||||
super().__init__(path)
|
||||
self.source = source
|
||||
|
||||
def write(self, site: Site) -> None:
|
||||
dest = site.output_path / self.uri
|
||||
dest = site.output_path / self.destination
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(self.source, dest)
|
||||
log.debug("Copied %s -> %s", self.source, dest)
|
||||
|
||||
+11
-10
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from math import ceil
|
||||
from itertools import batched
|
||||
from typing import TYPE_CHECKING, Literal
|
||||
@@ -22,7 +23,7 @@ class Index(Output):
|
||||
total_pages: int,
|
||||
label: str | None,
|
||||
):
|
||||
super().__init__(Index.build_uri(kind, page_num, label))
|
||||
super().__init__(Index.build_path(kind, page_num, label))
|
||||
self.kind = kind
|
||||
self.items = items
|
||||
self.page_num = page_num
|
||||
@@ -30,25 +31,25 @@ class Index(Output):
|
||||
self.label = label
|
||||
|
||||
@staticmethod
|
||||
def build_uri(
|
||||
def build_path(
|
||||
kind: Literal["root", "section", "tag"],
|
||||
page_num: int,
|
||||
label: str | None,
|
||||
) -> str:
|
||||
) -> Path:
|
||||
if page_num == 1:
|
||||
file = "index.html"
|
||||
file = Path("index.html")
|
||||
else:
|
||||
file = f"page-{page_num}.html"
|
||||
file = Path(f"page-{page_num}.html")
|
||||
|
||||
if kind == "root":
|
||||
uri = file
|
||||
path = file
|
||||
elif kind == "section":
|
||||
assert label, "label can't be None if kind is not root"
|
||||
uri = f"{label}/{file}"
|
||||
path = Path(label) / file
|
||||
else:
|
||||
assert label, "label can't be None if kind is not root"
|
||||
uri = f"tags/{label}/{file}"
|
||||
return uri
|
||||
path = Path("tags") / label / file
|
||||
return path
|
||||
|
||||
def write(self, site: Site) -> None:
|
||||
template = site.jinja_env.get_template("index.html")
|
||||
@@ -60,7 +61,7 @@ class Index(Output):
|
||||
kind=self.kind,
|
||||
label=self.label,
|
||||
)
|
||||
dest = site.output_path / self.uri
|
||||
dest = site.output_path / self.destination
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_text(html, encoding="utf-8")
|
||||
log.debug("Wrote %s", dest)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from omicron.ssg.output.output import Output
|
||||
|
||||
@@ -11,12 +12,12 @@ else:
|
||||
|
||||
|
||||
class Memory(Output):
|
||||
def __init__(self, uri: str, content: str):
|
||||
super().__init__(uri)
|
||||
def __init__(self, destination: Path, content: str):
|
||||
super().__init__(destination)
|
||||
self.content = content
|
||||
|
||||
def write(self, site: Site) -> None:
|
||||
dest = site.output_path / self.uri
|
||||
dest = site.output_path / self.destination
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_text(self.content, encoding="utf-8")
|
||||
log.debug("Wrote %s", dest)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from typing import TYPE_CHECKING
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from omicron.ssg.site import Site # for static checking with mypy
|
||||
@@ -8,8 +9,13 @@ else:
|
||||
|
||||
|
||||
class Output(ABC):
|
||||
def __init__(self, uri: str):
|
||||
self.uri = uri
|
||||
def __init__(self, destination: Path):
|
||||
self.destination = destination
|
||||
url = "/" + destination.as_posix()
|
||||
url = url.removesuffix("/index.html")
|
||||
if url == "":
|
||||
url = "/"
|
||||
self.url = url
|
||||
|
||||
@abstractmethod
|
||||
def write(self, site: Site) -> None:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from omicron.ssg.output.output import Output
|
||||
|
||||
@@ -12,13 +13,13 @@ else:
|
||||
|
||||
class Tags(Output):
|
||||
def __init__(self, tags: list[tuple[str, int]]):
|
||||
super().__init__("tags/index.html")
|
||||
super().__init__(Path("tags/index.html"))
|
||||
self.tags = tags
|
||||
|
||||
def write(self, site: Site) -> None:
|
||||
template = site.jinja_env.get_template("tags.html")
|
||||
html = template.render(site=site, tags=self.tags)
|
||||
dest = site.output_path / self.uri
|
||||
dest = site.output_path / self.destination
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_text(html, encoding="utf-8")
|
||||
log.debug("Wrote %s", dest)
|
||||
|
||||
+22
-24
@@ -32,7 +32,7 @@ class Site:
|
||||
self.content: list[Content] = []
|
||||
self.other_outputs: list[Output] = []
|
||||
self.by_tag: dict[str, list[Content]] = {}
|
||||
self.by_uri: dict[str, Output] = {}
|
||||
self.by_path: dict[Path, Output] = {}
|
||||
self.by_section: dict[str, list[Content]] = {}
|
||||
self.tags_by_count: list[tuple[str, int]] = []
|
||||
|
||||
@@ -40,7 +40,7 @@ class Site:
|
||||
self.name: str = config["name"]
|
||||
self.template: str = config["template"]
|
||||
self.items_per_page: int = int(config.get("items_per_page", 20))
|
||||
self.origin, self.base_dir = Site.parse_base_uri(config.get("base_uri"))
|
||||
self.origin, self.base_dir = Site.parse_base_url(config.get("base_url"))
|
||||
self.template_path: Path = Site.resolve_template_path(self.template)
|
||||
self.output_path: Path = self.site_path / "output"
|
||||
self.jinja_env = Environment(loader=FileSystemLoader(self.template_path))
|
||||
@@ -59,9 +59,9 @@ class Site:
|
||||
return cast(dict[str, Any], config)
|
||||
|
||||
@staticmethod
|
||||
def parse_base_uri(value: str | None) -> tuple[str, str]:
|
||||
def parse_base_url(value: str | None) -> tuple[str, str]:
|
||||
if not value:
|
||||
return ("", "/")
|
||||
return ("", "")
|
||||
parsed = urlparse(value)
|
||||
if parsed.scheme and parsed.netloc:
|
||||
origin = f"{parsed.scheme}://{parsed.netloc}"
|
||||
@@ -70,9 +70,7 @@ class Site:
|
||||
else:
|
||||
origin = ""
|
||||
|
||||
path = parsed.path
|
||||
if not path.endswith("/"):
|
||||
path += "/"
|
||||
path = parsed.path.removesuffix("/")
|
||||
return (origin, path)
|
||||
|
||||
@staticmethod
|
||||
@@ -93,12 +91,12 @@ class Site:
|
||||
self.by_tag[tag] = []
|
||||
self.by_tag[tag].append(content)
|
||||
|
||||
def add_by_uri(self, output: Output) -> None:
|
||||
if output.uri in self.by_uri:
|
||||
def add_by_path(self, output: Output) -> None:
|
||||
if output.destination in self.by_path:
|
||||
raise ContentError(
|
||||
f"uri '{output.uri}' is already claimed by another output"
|
||||
f"path '{output.destination}' is already claimed by another output"
|
||||
)
|
||||
self.by_uri[output.uri] = output
|
||||
self.by_path[output.destination] = output
|
||||
|
||||
def update_tags_by_count(self) -> None:
|
||||
tags = [(tag, len(items)) for tag, items in self.by_tag.items()]
|
||||
@@ -111,43 +109,43 @@ class Site:
|
||||
return
|
||||
for file in assets_path.rglob("*"):
|
||||
if file.is_file():
|
||||
uri = "assets/" + file.relative_to(assets_path).as_posix()
|
||||
asset = File(uri, file)
|
||||
self.add_by_uri(asset)
|
||||
path = Path("assets") / file.relative_to(assets_path)
|
||||
asset = File(path, file)
|
||||
self.add_by_path(asset)
|
||||
self.other_outputs.append(asset)
|
||||
log.debug("Discovered template asset %s", uri)
|
||||
log.debug("Discovered template asset %s", path)
|
||||
|
||||
def discover(self) -> None:
|
||||
log.info("Discovering content...")
|
||||
self.discover_assets()
|
||||
pygments = Memory("assets/pygments.css", highlight_style())
|
||||
pygments = Memory(Path("assets/pygments.css"), highlight_style())
|
||||
self.other_outputs.append(pygments)
|
||||
self.add_by_uri(pygments)
|
||||
self.add_by_path(pygments)
|
||||
content_path = self.site_path / "content"
|
||||
for file in content_path.rglob("*"):
|
||||
if file.is_file() and is_content(file):
|
||||
content = create_content(file)
|
||||
self.add_by_uri(content)
|
||||
self.add_by_path(content)
|
||||
self.add_by_section(content)
|
||||
self.add_by_tag(content)
|
||||
self.content.append(content)
|
||||
self.update_tags_by_count()
|
||||
for index in discover_index(self):
|
||||
self.add_by_uri(index)
|
||||
self.add_by_path(index)
|
||||
self.other_outputs.append(index)
|
||||
tags_page = discover_tags(self)
|
||||
self.add_by_uri(tags_page)
|
||||
self.add_by_path(tags_page)
|
||||
self.other_outputs.append(tags_page)
|
||||
log.info("Discovered %d content items", len(self.content))
|
||||
|
||||
def build(self) -> None:
|
||||
if not self.origin:
|
||||
log.warning(
|
||||
"base_uri config value is missing a domain name, "
|
||||
"can't add canonical uri to content"
|
||||
"base_url config value is missing a domain name, "
|
||||
"can't add canonical url to content"
|
||||
)
|
||||
if not self.base_dir.startswith("/"):
|
||||
raise ConfigError("base_uri config value must be an absolute path")
|
||||
if self.base_dir and not self.base_dir.startswith("/"):
|
||||
raise ConfigError("base_url config value must be an absolute path")
|
||||
self.discover()
|
||||
self.output_path.mkdir(parents=True, exist_ok=True)
|
||||
for content in self.content:
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
{% block title %}{{ article.title }} — {{ site.name }}{% endblock %}
|
||||
|
||||
{% block head %}<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}{{ article.uri }}">{% endblock %}
|
||||
{% block head %}<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}{{ article.url }}">{% endblock %}
|
||||
|
||||
{% block breadcrumb %}
|
||||
<nav aria-label="breadcrumb">
|
||||
<a href="{{ site.base_dir }}">{{ site.name }}</a> ›
|
||||
<a href="{{ site.base_dir }}{{ article.section }}/">{{ article.section }}</a> ›
|
||||
<a href="{{ site.base_dir or "/" }}">{{ site.name }}</a> ›
|
||||
<a href="{{ site.base_dir }}/{{ article.section }}">{{ article.section }}</a> ›
|
||||
{{ article.title }}
|
||||
</nav>
|
||||
{% endblock %}
|
||||
@@ -25,7 +25,7 @@
|
||||
{% if article.tags %}
|
||||
<ul>
|
||||
{% for tag in article.tags | sort %}
|
||||
<li><a href="{{ site.base_dir }}tags/{{ tag }}/">{{ tag }}</a></li>
|
||||
<li><a href="{{ site.base_dir }}/tags/{{ tag }}">{{ tag }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{% block title %}{{ site.name }}{% endblock %}</title>
|
||||
<link rel="stylesheet" href="{{ site.base_dir }}assets/style.css">
|
||||
<link rel="stylesheet" href="{{ site.base_dir }}assets/pygments.css">
|
||||
<link rel="stylesheet" href="{{ site.base_dir }}/assets/style.css">
|
||||
<link rel="stylesheet" href="{{ site.base_dir }}/assets/pygments.css">
|
||||
{% block head %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
{% block breadcrumb %}<a href="{{ site.base_dir }}">{{ site.name }}</a>{% endblock %}
|
||||
{% block breadcrumb %}<a href="{{ site.base_dir or "/" }}">{{ site.name }}</a>{% endblock %}
|
||||
</header>
|
||||
<main>
|
||||
{% block main %}{% endblock %}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
{% extends "base.html" %}
|
||||
{% set prefix = (label + "/") if kind == "section" else (("tags/" + label + "/") if kind == "tag" else "") %}
|
||||
{% set base_path = ("/" + label) if kind == "section" else (("/tags/" + label) if kind == "tag" else "") %}
|
||||
{% set heading = label or "" %}
|
||||
|
||||
{% block title %}{% if heading %}{{ heading }} — {% endif %}{{ site.name }}{% if page_num > 1 %} — Page {{ page_num }}{% endif %}{% endblock %}
|
||||
|
||||
{% block head %}
|
||||
<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}{{ prefix }}{% if page_num > 1 %}page-{{ page_num }}.html{% endif %}">
|
||||
<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}{% if page_num == 1 %}{{ base_path or "/" }}{% else %}{{ base_path }}/page-{{ page_num }}.html{% endif %}">
|
||||
{% if page_num > 1 %}<meta name="robots" content="noindex, follow">{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block breadcrumb %}
|
||||
{% if kind == "section" %}
|
||||
<nav aria-label="breadcrumb">
|
||||
<a href="{{ site.base_dir }}">{{ site.name }}</a> ›
|
||||
<a href="{{ site.base_dir or "/" }}">{{ site.name }}</a> ›
|
||||
{{ label }}
|
||||
</nav>
|
||||
{% elif kind == "tag" %}
|
||||
<nav aria-label="breadcrumb">
|
||||
<a href="{{ site.base_dir }}">{{ site.name }}</a> ›
|
||||
<a href="{{ site.base_dir }}tags/">Tags</a> ›
|
||||
<a href="{{ site.base_dir or "/" }}">{{ site.name }}</a> ›
|
||||
<a href="{{ site.base_dir }}/tags">Tags</a> ›
|
||||
{{ label }}
|
||||
</nav>
|
||||
{% else %}
|
||||
@@ -31,7 +31,7 @@
|
||||
{% for item in items %}
|
||||
<article>
|
||||
<header>
|
||||
<h2><a href="{{ site.base_dir }}{{ item.uri }}">{{ item.title }}</a></h2>
|
||||
<h2><a href="{{ site.base_dir }}{{ item.url }}">{{ item.title }}</a></h2>
|
||||
<time datetime="{{ item.created }}">{{ item.created }}</time>
|
||||
</header>
|
||||
{% if item.summary %}<p>{{ item.summary }}</p>{% endif %}
|
||||
@@ -40,15 +40,15 @@
|
||||
{% if total_pages > 1 %}
|
||||
<nav>
|
||||
{% if page_num > 1 %}
|
||||
<a href="{{ site.base_dir }}{{ prefix }}{{ "" if page_num - 1 == 1 else "page-" ~ (page_num - 1) ~ ".html" }}">← Newer</a>
|
||||
<a href="{{ site.base_dir }}{% if page_num - 1 == 1 %}{{ base_path or "/" }}{% else %}{{ base_path }}/page-{{ page_num - 1 }}.html{% endif %}">← Newer</a>
|
||||
{% endif %}
|
||||
{% for p in range(1, total_pages + 1) %}
|
||||
{% if p == page_num %}<strong>{{ p }}</strong>
|
||||
{% else %}<a href="{{ site.base_dir }}{{ prefix }}{{ "" if p == 1 else "page-" ~ p ~ ".html" }}">{{ p }}</a>
|
||||
{% else %}<a href="{{ site.base_dir }}{% if p == 1 %}{{ base_path or "/" }}{% else %}{{ base_path }}/page-{{ p }}.html{% endif %}">{{ p }}</a>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% if page_num < total_pages %}
|
||||
<a href="{{ site.base_dir }}{{ prefix }}page-{{ page_num + 1 }}.html">Older →</a>
|
||||
<a href="{{ site.base_dir }}{{ base_path }}/page-{{ page_num + 1 }}.html">Older →</a>
|
||||
{% endif %}
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
{% block title %}Tags — {{ site.name }}{% endblock %}
|
||||
|
||||
{% block head %}
|
||||
<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}tags/">
|
||||
<link rel="canonical" href="{{ site.origin }}{{ site.base_dir }}/tags">
|
||||
{% endblock %}
|
||||
|
||||
{% block breadcrumb %}
|
||||
<nav aria-label="breadcrumb">
|
||||
<a href="{{ site.base_dir }}">{{ site.name }}</a> ›
|
||||
<a href="{{ site.base_dir or "/" }}">{{ site.name }}</a> ›
|
||||
Tags
|
||||
</nav>
|
||||
{% endblock %}
|
||||
@@ -17,7 +17,7 @@
|
||||
<h1>Tags</h1>
|
||||
<ul>
|
||||
{% for tag, count in tags %}
|
||||
<li><a href="{{ site.base_dir }}tags/{{ tag }}/">{{ tag }}</a> ({{ count }})</li>
|
||||
<li><a href="{{ site.base_dir }}/tags/{{ tag }}">{{ tag }}</a> ({{ count }})</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user