Compare commits
10 Commits
feature/is
...
f02d37a6af
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f02d37a6af | ||
|
|
f84fb65aa7 | ||
|
|
d019884dbe | ||
|
|
39a8d7c1db | ||
| 7d1ceded8d | |||
| b3e23a53d9 | |||
| b2513f7caf | |||
| 4743456bd8 | |||
| 6994662bb4 | |||
| db23ceac78 |
25
README.md
25
README.md
@@ -0,0 +1,25 @@
|
||||
# Installing
|
||||
|
||||
```sh
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
# Execute
|
||||
|
||||
To start a local http server in the dist folder you can simply do:
|
||||
|
||||
```sh
|
||||
python3 -m http.server 1312
|
||||
```
|
||||
|
||||
Then visit `localhost:1312`
|
||||
|
||||
```
|
||||
# build it normally
|
||||
stsg
|
||||
|
||||
# start a hot reload server
|
||||
stsg_dev
|
||||
```
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
## Sunt aevis
|
||||
|
||||
{{example.link}}
|
||||
|
||||
Lorem markdownum turbavere prisca Aeacidae morando esse. Quam Styga spectata,
|
||||
pariter Iove iunctis exercere Solis? Atlantis possit succurrere quam!
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ meow
|
||||
|
||||
## Usque de celebrabant puer
|
||||
|
||||
[{{index_page.title}}]({{index_page.url}})
|
||||
|
||||
Lorem markdownum, nec ora et vero me nec natae suadent. Nec damno ignorat
|
||||
propiore aliquid temptata decipienda habetur. Vulnera lacrimis aequoreo madidos,
|
||||
copia uvae, herbosaque quoque, per harenas, canos fui monstro Peleus.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Navê Gotarê
|
||||
|
||||
[{article_title:example}]({article_url:example})
|
||||
[{{example.title}}]({{example.url}})
|
||||
|
||||
Ev gotar, mînakek placeholder ya bi zimanê Kurdî ye. Nivîs, weşan û rûpelên nûçe di vê belgeyê de têne pêşniyar kirin. Ev metn ji bo testkirina layout an jî demo-yê hatiye çêkirin û çend bingehên cihanî yên naveroka gotarê nîşan dide.
|
||||
|
||||
@@ -35,4 +35,4 @@ Kodê li ser vê koda simplesa gotinê ye û dikare weşana nûçe an demo-yê p
|
||||
# Encama Gotarê
|
||||
|
||||
Di encama gotarê de, hûn dikarin her çend beşên bingehîn yên nûçe, daxuyanî û şirove yên navekî bifikirin.
|
||||
Bê guman, ev placeholder bi awayek qelew li ser çalakiya te ya malper, blog an jî her sedema dijîtal tê de karîger e.
|
||||
Bê guman, ev placeholder bi awayek qelew li ser çalakiya te ya malper, blog an jî her sedema dijîtal tê de karîger e.
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
<link rel="stylesheet" href="/static/bulma.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<!-- Header (Navbar) -->
|
||||
<nav
|
||||
class="navbar is-primary"
|
||||
role="navigation"
|
||||
@@ -23,22 +22,31 @@
|
||||
<section class="section">
|
||||
{% if translations|length %}
|
||||
<div class="container content">
|
||||
<div class="column is-half is-offset-one-quarter">
|
||||
<div class="content">
|
||||
<h1>Translations</h1>
|
||||
</div>
|
||||
<div class="column is-half is-offset-one-quarter">
|
||||
<div class="columns is-multiline">
|
||||
{% for t in translations %}
|
||||
<div class="card mb-4" lang="{{t.language.code}}">
|
||||
<a href="{{t.url}}" hreflang="{{t.language.code}}" class="card mb-4" style="color: inherit; text-decoration: none;">
|
||||
<div class="column is-half">
|
||||
<div class="card mb-4" lang="{{t.language.code}}" style="height: 100%;">
|
||||
<a href="{{t.url}}" hreflang="{{t.language.code}}" class="card mb-4" style="color: inherit; text-decoration: none;">
|
||||
<div class="card-header">
|
||||
<div class="card-header-title">{{t.title}} </div>
|
||||
<div class="card-header-icon">{{t.language.flag}}</div>
|
||||
</div>
|
||||
|
||||
<div class="card-content">
|
||||
<p class="title">{{t.language.flag}} {{t.title}} </p>
|
||||
<p class="content">
|
||||
{{t.preview}}
|
||||
<br />
|
||||
<time datetime="{{iso_date}}">{{date}}</time>
|
||||
</p>
|
||||
</div>
|
||||
</a>
|
||||
<hr />
|
||||
<p class="content">
|
||||
{{t.preview}}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="card-footer">
|
||||
<time datetime="{{iso_date}}">{{date}}</time>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
@@ -49,7 +57,7 @@
|
||||
{% if children|length %}
|
||||
<div class="container content">
|
||||
<div class="column is-half is-offset-one-quarter">
|
||||
<h1>Child Articles</h1>
|
||||
<h1>Further reading</h1>
|
||||
</div>
|
||||
<div class="column is-half is-offset-one-quarter">
|
||||
{% for c in children %}
|
||||
@@ -57,9 +65,14 @@
|
||||
<a href="{{c.url}}" class="card mb-4" style="color: inherit; text-decoration: none;">
|
||||
<div class="card-content">
|
||||
<p class="title">{{c.slug}} </p>
|
||||
<p class="content">
|
||||
<time datetime="{{iso_date}}">{{date}}</time>
|
||||
<hr />
|
||||
<p class="content is-flex is-flex-direction-column" style="gap: 10px;">
|
||||
{% for ct in c.translations %}
|
||||
<a href="{{ct.url}}" hreflang="{{ct.language.code}}">{{ct.language.flag}}: {{ct.title}}</a>
|
||||
{% endfor %}
|
||||
</p>
|
||||
<hr />
|
||||
<time datetime="{{iso_date}}">{{date}}</time>
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -3,7 +3,8 @@ source_directory = "src"
|
||||
dist_directory = "dist"
|
||||
|
||||
[formatting]
|
||||
article_preview_length = 400
|
||||
preview_length = 400
|
||||
preview_header_shift = 2
|
||||
datetime_format = "%d. %B %Y"
|
||||
default_language = "de"
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@ class config:
|
||||
dist_directory = "dist"
|
||||
|
||||
class formatting:
|
||||
article_preview_length = 200
|
||||
datetime_format = "%d. %B %Y"
|
||||
fallback_language = "en"
|
||||
preview_length = 400
|
||||
preview_header_shift = 2
|
||||
|
||||
languages = {
|
||||
"af": {
|
||||
|
||||
106
stsg/build.py
106
stsg/build.py
@@ -4,7 +4,7 @@ import shutil
|
||||
from pathlib import Path
|
||||
import os
|
||||
import markdown
|
||||
from typing import Optional, Union, Dict, Generator, List, DefaultDict, Any
|
||||
from typing import Optional, Union, Dict, Generator, List, DefaultDict, Any, TypedDict, Set
|
||||
from bs4 import BeautifulSoup
|
||||
from collections import defaultdict
|
||||
import toml
|
||||
@@ -13,14 +13,6 @@ import jinja2
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
def replace_values(template: str, values: Dict[str, str]) -> str:
|
||||
for key, value in values.items():
|
||||
template = template.replace("{" + key + "}", value)
|
||||
|
||||
return template
|
||||
|
||||
|
||||
def get_first_header_content(content, fallback: str = ""):
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
for level in range(1, 7):
|
||||
@@ -31,7 +23,7 @@ def get_first_header_content(content, fallback: str = ""):
|
||||
return fallback
|
||||
|
||||
|
||||
def shorten_text_and_clean(html_string, max_length=config.formatting.article_preview_length):
|
||||
def shorten_text_and_clean(html_string, max_length=config.formatting.preview_length):
|
||||
soup = BeautifulSoup(html_string, 'html.parser')
|
||||
|
||||
# Keep track of total characters added
|
||||
@@ -69,6 +61,23 @@ def shorten_text_and_clean(html_string, max_length=config.formatting.article_pre
|
||||
return str(soup)
|
||||
|
||||
|
||||
def shift_headings(html_string, header_shift=config.formatting.preview_header_shift):
|
||||
soup = BeautifulSoup(html_string, 'html.parser')
|
||||
|
||||
for level in range(6, 0, -1): # Start from h6 to h1 to avoid overwriting
|
||||
old_tag = f'h{level}'
|
||||
for tag in soup.find_all(old_tag):
|
||||
new_level = min(level + header_shift, 6) # Cap at h6
|
||||
new_tag = f'h{new_level}'
|
||||
tag.name = new_tag
|
||||
|
||||
return str(soup)
|
||||
|
||||
|
||||
def get_preview_text(html_string: str):
|
||||
return shift_headings(shorten_text_and_clean(html_string))
|
||||
|
||||
|
||||
def stem_to_language_code(stem: str) -> str:
|
||||
language_code = stem.lower().replace("-", "_")
|
||||
|
||||
@@ -83,7 +92,6 @@ def stem_to_language_code(stem: str) -> str:
|
||||
exit(1)
|
||||
|
||||
|
||||
|
||||
class TemplateDict(dict):
|
||||
def __init__(self, folder: Path):
|
||||
self.folder = folder
|
||||
@@ -99,8 +107,10 @@ class TemplateDict(dict):
|
||||
self[name] = t
|
||||
return t
|
||||
|
||||
|
||||
TEMPLATE: Dict[str, jinja2.Template] = TemplateDict(Path(config.setup.source_directory, "templates"))
|
||||
|
||||
|
||||
class LanguageDict(dict):
|
||||
def __missing__(self, key: str):
|
||||
if key not in config.languages:
|
||||
@@ -116,9 +126,17 @@ class LanguageDict(dict):
|
||||
|
||||
return lang_dict
|
||||
|
||||
|
||||
LANGUAGES = LanguageDict()
|
||||
|
||||
|
||||
def compile_cross_article_context(cross_article_context):
|
||||
title = cross_article_context["title"]
|
||||
url = cross_article_context["url"]
|
||||
|
||||
cross_article_context["link"] = f'<a href="{url}">{title}</a>'
|
||||
|
||||
|
||||
class ArticleTranslation:
|
||||
def __init__(self, file: Path, article: Article):
|
||||
self.file = file
|
||||
@@ -131,32 +149,44 @@ class ArticleTranslation:
|
||||
self.location_in_tree = [self.language_code, *self.article.location_in_tree]
|
||||
self.url = "/" + "/".join(self.location_in_tree)
|
||||
self.dist_path = Path(config.setup.dist_directory, *self.location_in_tree)
|
||||
self.cross_article_context = TRANSLATED_CROSS_ARTICLE_CONTEXT[self.language_code][self.article.slug] = {}
|
||||
|
||||
|
||||
self.priority = LANGUAGES[self.language_code]["priority"]
|
||||
self.real_language_code = LANGUAGES[self.language_code]["code"]
|
||||
|
||||
# TODO remove
|
||||
self.article_content = self.file.read_text()
|
||||
self.article_preview = self.article_content[:config.formatting.article_preview_length] + "..."
|
||||
self.html_content = self.file.read_text()
|
||||
if self.file.suffix == ".md":
|
||||
self.article_content = markdown.markdown(self.article_content)
|
||||
self.article_preview = markdown.markdown(self.article_preview)
|
||||
self.html_content = markdown.markdown(self.html_content)
|
||||
|
||||
self.title = get_first_header_content(self.article_content, fallback="")
|
||||
|
||||
def __init_context__(self):
|
||||
self.context["meta"] = self.article.context_shared
|
||||
self.context["url"] = self.url
|
||||
self.context["language"] = LANGUAGES[self.language_code]
|
||||
self.context["article_url"] = self.article.url
|
||||
self.context["title"] = get_first_header_content(self.html_content, fallback=LANGUAGES[self.language_code]["native_name"])
|
||||
|
||||
html_content = self.file.read_text()
|
||||
if self.file.suffix == ".md":
|
||||
html_content = markdown.markdown(html_content)
|
||||
self.cross_article_context.update(self.article.context_shared)
|
||||
self.cross_article_context["title"] = self.context["title"]
|
||||
self.cross_article_context["article_url"] = self.article.url
|
||||
self.cross_article_context["url"] = self.url
|
||||
compile_cross_article_context(self.cross_article_context)
|
||||
|
||||
self.context["title"] = get_first_header_content(html_content, fallback=LANGUAGES[self.language_code]["native_name"])
|
||||
self.context["content"] = html_content
|
||||
self.context["preview"] = shorten_text_and_clean(html_string=html_content)
|
||||
# get children
|
||||
self.context["children"] = [
|
||||
c.article_translations_map[self.language_code].context for c in self.article.child_articles
|
||||
if self.language_code in c.article_translations_map
|
||||
]
|
||||
|
||||
def __init_content_context__(self):
|
||||
template = jinja2.Template(self.html_content)
|
||||
self.html_content = template.render({
|
||||
**CROSS_ARTICLE_CONTEXT,
|
||||
**TRANSLATED_CROSS_ARTICLE_CONTEXT[self.language_code],
|
||||
})
|
||||
|
||||
self.context["content"] = self.html_content
|
||||
self.context["preview"] = get_preview_text(html_string=self.html_content)
|
||||
|
||||
def build(self):
|
||||
self.dist_path.mkdir(parents=True, exist_ok=True)
|
||||
@@ -183,6 +213,7 @@ class Article:
|
||||
if self.slug in ARTICLE_LAKE:
|
||||
logger.error("two articles have the same name at %s and %r", ARTICLE_LAKE[self.slug].directory, self.directory)
|
||||
exit(1)
|
||||
self.cross_article_context = CROSS_ARTICLE_CONTEXT[self.slug] = {}
|
||||
ARTICLE_LAKE[self.slug] = self
|
||||
|
||||
self.location_in_tree: List[str] = location_in_tree or []
|
||||
@@ -225,6 +256,11 @@ class Article:
|
||||
|
||||
self.context.update(self.context_shared)
|
||||
|
||||
self.cross_article_context.update(self.context_shared)
|
||||
self.cross_article_context["title"] = self.context_shared["slug"]
|
||||
self.cross_article_context["article_url"] = self.context_shared["url"]
|
||||
compile_cross_article_context(self.cross_article_context)
|
||||
|
||||
# recursive context structures
|
||||
translation_list = self.context["translations"] = []
|
||||
child_article_list = self.context["children"] = []
|
||||
@@ -242,9 +278,15 @@ class Article:
|
||||
for a in self.child_articles:
|
||||
a.__init_context__()
|
||||
|
||||
def __init_content_context__(self):
|
||||
for at in self.article_translations_list:
|
||||
at.__init_content_context__()
|
||||
for a in self.child_articles:
|
||||
a.__init_content_context__()
|
||||
|
||||
def build(self):
|
||||
self.dist_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
with Path(self.dist_path, "index.html").open("w") as f:
|
||||
f.write(TEMPLATE["article"].render(self.context))
|
||||
|
||||
@@ -257,24 +299,32 @@ class Article:
|
||||
|
||||
# GLOBALS
|
||||
logger = logging.getLogger("stsg.build")
|
||||
CROSS_ARTICLE_CONTEXT: Dict[str, Dict[str, Any]] = {}
|
||||
TRANSLATED_CROSS_ARTICLE_CONTEXT: Dict[str, Dict[str, Dict[str, Any]]] = defaultdict(dict)
|
||||
ARTICLE_LAKE: Dict[str, Article] = {}
|
||||
ARTICLE_REFERENCE_VALUES: DefaultDict[str, Dict[str, str]] = defaultdict(dict)
|
||||
|
||||
|
||||
def build():
|
||||
logger.info("starting build process...")
|
||||
logger.info("starting build process...")
|
||||
|
||||
logger.info("copying static folder...")
|
||||
shutil.copytree(Path(config.setup.source_directory, "static"), Path(config.setup.dist_directory, "static"), dirs_exist_ok=True)
|
||||
|
||||
logger.info("building page tree...")
|
||||
tree = Article(directory=Path(config.setup.source_directory, "articles"), is_root=True)
|
||||
|
||||
|
||||
logger.info("compiling tree context...")
|
||||
tree.__init_context__()
|
||||
tree.__init_content_context__()
|
||||
|
||||
import json
|
||||
with Path("context.json").open("w") as f:
|
||||
json.dump(tree.context, f, indent=4)
|
||||
with Path("cross_article_context.json").open("w") as f:
|
||||
json.dump(CROSS_ARTICLE_CONTEXT, f, indent=4)
|
||||
with Path("t_cross_article_context.json").open("w") as f:
|
||||
json.dump(TRANSLATED_CROSS_ARTICLE_CONTEXT, f, indent=4)
|
||||
|
||||
logger.info("dumping page tree...")
|
||||
tree.build()
|
||||
tree.build()
|
||||
|
||||
Reference in New Issue
Block a user