Update writing site generator.

This commit is contained in:
voussoir 2023-07-20 22:24:48 -07:00
parent dead581d22
commit bcf102f11f
2 changed files with 48 additions and 40 deletions

View file

@ -44,6 +44,10 @@ article
padding: 16px; padding: 16px;
background-color: var(--color_bodybg); background-color: var(--color_bodybg);
} }
article address
{
font-style: normal;
}
@media not print @media not print
{ {
article article
@ -252,7 +256,7 @@ https://github.com/richleland/pygments-css
.highlight .nf { color: var(--color_monokai_green) } /* Name.Function */ .highlight .nf { color: var(--color_monokai_green) } /* Name.Function */
.highlight .nl { color: var(--color_monokai_white) } /* Name.Label */ .highlight .nl { color: var(--color_monokai_white) } /* Name.Label */
.highlight .nn { color: var(--color_monokai_white) } /* Name.Namespace */ .highlight .nn { color: var(--color_monokai_white) } /* Name.Namespace */
.highlight .nx { color: var(--color_monokai_green) } /* Name.Other */ .highlight .nx { color: var(--color_monokai_white) } /* Name.Other */
.highlight .py { color: var(--color_monokai_white) } /* Name.Property */ .highlight .py { color: var(--color_monokai_white) } /* Name.Property */
.highlight .nt { color: var(--color_monokai_pink) } /* Name.Tag */ .highlight .nt { color: var(--color_monokai_pink) } /* Name.Tag */
.highlight .nv { color: var(--color_monokai_white) } /* Name.Variable */ .highlight .nv { color: var(--color_monokai_white) } /* Name.Variable */

View file

@ -10,6 +10,7 @@ import re
import subprocess import subprocess
import vmarkdown import vmarkdown
from voussoirkit import dotdict
from voussoirkit import pathclass from voussoirkit import pathclass
from voussoirkit import spinal from voussoirkit import spinal
from voussoirkit import winwhich from voussoirkit import winwhich
@ -20,20 +21,6 @@ WRITING_ROOTDIR = pathclass.Path(__file__).parent
GIT = winwhich.which('git') GIT = winwhich.which('git')
ARTICLE_TEMPLATE = '''
[Back to writing](/writing)
{body}
---
[View this document's history]({github_history})
{commits}
Contact me: writing@voussoir.net
'''
# HELPERS # HELPERS
################################################################################ ################################################################################
def check_output(command): def check_output(command):
@ -87,7 +74,7 @@ def git_file_edited_date(path) -> datetime.datetime:
date = check_output(command) date = check_output(command)
if date: if date:
date = dateutil.parser.parse(date) date = dateutil.parser.parse(date)
date = date.astimezone(datetime.timezone.utc) # date = date.astimezone(datetime.timezone.utc)
return date return date
else: else:
return datetime.datetime.now(datetime.timezone.utc) return datetime.datetime.now(datetime.timezone.utc)
@ -107,16 +94,24 @@ def git_file_commit_history(path):
'-C', repo.absolute_path, '-C', repo.absolute_path,
'log', 'log',
'--follow', '--follow',
'--pretty=format:%H %ad %s', '--pretty=format:%H///%ad///%s',
'--date=short', '--date=iso',
'--', '--',
path, path,
] ]
output = check_output(command) output = check_output(command)
lines = [line for line in output.splitlines() if line.strip()] #'*' in line] lines = [line for line in output.splitlines() if line.strip()] #'*' in line]
lines = [re.sub(r'([\*\_\[\]\(\)\^])', r'\\\1', line) for line in lines] lines = [line.split('///', 2) for line in lines]
lines = [line.split(' ', 1) for line in lines] commits = []
return lines for (commit_hash, date, title) in lines:
commit = dotdict.DotDict({
'hash': commit_hash,
'date': dateutil.parser.parse(date),
'title': title,
})
commits.append(commit)
commits.sort(key=lambda commit: commit.date, reverse=True)
return commits
def git_file_published_date(path) -> datetime.datetime: def git_file_published_date(path) -> datetime.datetime:
''' '''
@ -139,7 +134,7 @@ def git_file_published_date(path) -> datetime.datetime:
date = check_output(command) date = check_output(command)
if date: if date:
date = dateutil.parser.parse(date) date = dateutil.parser.parse(date)
date = date.astimezone(datetime.timezone.utc) # date = date.astimezone(datetime.timezone.utc)
return date return date
else: else:
return datetime.datetime.now(datetime.timezone.utc) return datetime.datetime.now(datetime.timezone.utc)
@ -221,25 +216,33 @@ class Article:
github_history = f'https://github.com/voussoir/voussoir.net/commits/master/{relative_path}' github_history = f'https://github.com/voussoir/voussoir.net/commits/master/{relative_path}'
commits = git_file_commit_history(self.md_file) commits = git_file_commit_history(self.md_file)
self.publication_id = f'{commits[-1][0]}/{self.md_file.parent.basename}' if commits else None self.publication_id = f'{commits[-1].hash}/{self.md_file.parent.basename}' if commits else None
commits = [
f'- [{html.escape(line)}](https://github.com/voussoir/voussoir.net/commit/{hash})'
for (hash, line) in commits
]
commits = '\n'.join(commits)
md = vmarkdown.cat_file(self.md_file.absolute_path)
md = ARTICLE_TEMPLATE.format(
body=md,
github_history=github_history,
commits=commits,
)
self.soup = vmarkdown.markdown( self.soup = vmarkdown.markdown(
md, self.md_file.read('r', encoding='utf-8'),
css=WRITING_ROOTDIR.with_child('dark.css').absolute_path, css=WRITING_ROOTDIR.with_child('dark.css').absolute_path,
return_soup=True, return_soup=True,
) )
header = jinja2.Template('''
<p><a href="/writing">Back to writing</a></p>
''').render()
footer = jinja2.Template('''
<hr/>
<p><a href="{{github_history}}">View this document's history</a></p>
<ul>
{% for commit in commits %}
<li><a href="https://github.com/voussoir/voussoir.net/commit/{{commit.hash}}"><time datetime="{{commit.date.isoformat()}}">{{commit.date.strftime('%Y-%m-%d')}}</time> {{commit.title}}</a></li>
{% endfor %}
</ul>
<address>Contact me: writing@voussoir.net</address>
''').render(
github_history=github_history,
commits=commits,
)
self.soup.article.insert(0, bs4.BeautifulSoup(header, 'html.parser'))
self.soup.article.append(bs4.BeautifulSoup(footer, 'html.parser'))
if self.soup.head.title: if self.soup.head.title:
self.title = self.soup.head.title.get_text() self.title = self.soup.head.title.get_text()
else: else:
@ -346,6 +349,7 @@ def make_tag_page(index, path):
path = '/'.join(path) path = '/'.join(path)
page = jinja2.Template(''' page = jinja2.Template('''
<!DOCTYPE html>
<html> <html>
<head> <head>
<link rel="icon" href="/favicon.png" type="image/png"/> <link rel="icon" href="/favicon.png" type="image/png"/>
@ -375,7 +379,7 @@ def make_tag_page(index, path):
<ol class="article_list"> <ol class="article_list">
{% for article in articles %} {% for article in articles %}
<li> <li>
<a href="{{article.web_path}}">{{article.published_date}} - {{article.title|e}}</a> <a href="{{article.web_path}}"><time datetime="{{article.published_iso}}">{{article.published_date}}</time> - {{article.title|e}}</a>
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>
@ -442,7 +446,7 @@ def write_writing_index():
<ol class="article_list"> <ol class="article_list">
{% for article in articles %} {% for article in articles %}
<li> <li>
<a href="{{article.web_path}}">{{article.published_date}} - {{article.title|e}}</a> <a href="{{article.web_path}}"><time datetime="{{article.published_iso}}">{{article.published_date}}</time> - {{article.title|e}}</a>
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>
@ -457,7 +461,7 @@ def write_writing_index():
{% for article in articles_edited %} {% for article in articles_edited %}
{% if article.edited and article.edited != article.published %} {% if article.edited and article.edited != article.published %}
<li> <li>
<a href="{{article.web_path}}">{{article.edited_date}} - {{article.title|e}} ({{article.published_date}})</a> <a href="{{article.web_path}}"><time datetime="{{article.edited_iso}}">{{article.edited_date}}</time> - {{article.title|e}} ({{article.published_date}})</a>
</li> </li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
@ -538,7 +542,7 @@ def write_rss():
ARTICLES = { ARTICLES = {
file: Article(file) file: Article(file)
for file in spinal.walk(WRITING_ROOTDIR) for file in spinal.walk(WRITING_ROOTDIR)
if file.extension == 'md' and file.parent != WRITING_ROOTDIR if file.extension == 'md' and file.parent != WRITING_ROOTDIR and '_unpublished' not in file.absolute_path
} }
ARTICLES_PUBLISHED = {file: article for (file, article) in ARTICLES.items() if article.publication_id} ARTICLES_PUBLISHED = {file: article for (file, article) in ARTICLES.items() if article.publication_id}