From 635a607a0360645b8b61faea482a310624a36e04 Mon Sep 17 00:00:00 2001 From: Collin Lefeber Date: Fri, 22 Nov 2024 15:39:52 -0500 Subject: [PATCH] public dir --- .build.yml | 17 --- .gitignore | 3 + main.py | 121 ++++++++++-------- avatar.png => public/avatar.png | Bin chalk.min.css => public/chalk.min.css | 0 public/grayscale-dark.min.css | 7 + .../highlight.js.min.css | 0 highlight.min.js => public/highlight.min.js | 0 {img => public/img}/mse_first_play.png | Bin style.css => public/style.css | 0 10 files changed, 81 insertions(+), 67 deletions(-) delete mode 100644 .build.yml rename avatar.png => public/avatar.png (100%) rename chalk.min.css => public/chalk.min.css (100%) create mode 100644 public/grayscale-dark.min.css rename highlight.js.min.css => public/highlight.js.min.css (100%) rename highlight.min.js => public/highlight.min.js (100%) rename {img => public/img}/mse_first_play.png (100%) rename style.css => public/style.css (100%) diff --git a/.build.yml b/.build.yml deleted file mode 100644 index 8b3b3fb..0000000 --- a/.build.yml +++ /dev/null @@ -1,17 +0,0 @@ ---- -image: alpine/edge -oauth: pages.sr.ht/PAGES:RW -packages: - - hut - - python3 - - py3-markdown -environment: - site: cfebs.srht.site - dest: cfebs.com -tasks: - - package: | - cd $site - python3 ./main.py - tar -cvz . > ../site.tar.gz - - upload: | - hut pages publish -d $dest site.tar.gz diff --git a/.gitignore b/.gitignore index 6824ce8..81118ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ index.html posts/*.html index.xml +public/posts/*.html +public/*.html +public/*.xml __pycache__/ diff --git a/main.py b/main.py index 2ea80cb..656ae98 100644 --- a/main.py +++ b/main.py @@ -11,18 +11,20 @@ from string import Template import markdown from markdown.extensions.toc import TocExtension -destpath_re = re.compile(r'\.md$') -logging.basicConfig(encoding='utf-8', level=logging.INFO) +destpath_re = re.compile(r"\.md$") +logging.basicConfig(encoding="utf-8", level=logging.INFO) cpu_count = os.cpu_count() + def convert(text): - md = markdown.Markdown(extensions=['extra', 'meta', TocExtension(anchorlink=True)]) + md = markdown.Markdown(extensions=["extra", "meta", TocExtension(anchorlink=True)]) res = md.convert(text) return res, md.Meta + def render_post(fpath): - destpath = destpath_re.sub('.html', fpath) + destpath = destpath_re.sub(".html", fpath) logging.info("opening %s for parsing, dest %s", fpath, destpath) # from: https://python-markdown.github.io/reference/ with open(fpath, "r", encoding="utf-8") as input_file: @@ -32,37 +34,41 @@ def render_post(fpath): logging.info("parsing %s", fpath) out, meta = convert(text) - title = meta.get('title')[0] - date = meta.get('date')[0] + title = meta.get("title")[0] + date = meta.get("date")[0] draft = False - if meta.get('draft'): + if meta.get("draft"): draft = True - title_out, _ = convert('# ' + title) + title_out, _ = convert("# " + title) out = title_out + out logging.info("writing to %s", destpath) - render_template('index.html.tmpl', destpath, {'content': out, 'more_title': ' - ' + title}) + render_template( + "index.html.tmpl", destpath, {"content": out, "more_title": " - " + title} + ) return { - 'title': title, - 'date': date, - 'fpath': fpath, - 'destpath': destpath, - 'draft': draft, + "title": title, + "date": date, + "fpath": fpath, + "destpath": destpath, + "draft": draft, } + def render_posts(): - files = glob.glob('posts/*.md') - logging.info('found post files %s', files) + files = glob.glob("posts/*.md") + logging.info("found post files %s", files) posts = [] - logging.info('starting render posts with cpu_count: %d', cpu_count) + logging.info("starting render posts with cpu_count: %d", cpu_count) with Pool(processes=cpu_count) as pool: posts = pool.map(render_post, files) logging.info("render_posts result: %s", posts) return posts + def posts_list_html(posts): post_tpl = """
  • {title} @@ -70,25 +76,36 @@ def posts_list_html(posts):
  • """ out = '' + disp_date = datetime.datetime.fromisoformat(post.get("date")).strftime( + "%Y-%m-%d" + ) + out += post_tpl.format( + href=post.get("destpath"), + title=post.get("title"), + date=post.get("date"), + disp_date=disp_date, + ) + return out + "" + def render_template(tpl_fname, out_fname, subs): - with open(tpl_fname, 'r', encoding='utf-8') as inf: + with open(tpl_fname, "r", encoding="utf-8") as inf: tmpl = Template(inf.read()) out = tmpl.substitute(subs) - with open(out_fname, 'w', encoding='utf-8') as outf: + out_fname = os.path.join("public/", out_fname) + + with open(out_fname, "w", encoding="utf-8") as outf: outf.write(out) + def render_index(posts): content_html = posts_list_html(posts) - render_template('index.html.tmpl', 'index.html', {'content': content_html, 'more_title': ''}) + render_template( + "index.html.tmpl", "index.html", {"content": content_html, "more_title": ""} + ) + def rss_post_xml(post): tpl = """ @@ -100,53 +117,57 @@ def rss_post_xml(post): {description} """ - link = "https://cfebs.com/" + post['destpath'] + link = "https://cfebs.com/" + post["destpath"] - with open(post['fpath'], 'r', encoding='utf-8') as inf: + with open(post["fpath"], "r", encoding="utf-8") as inf: text = inf.read() - converted, _ = convert(text) - pubdate = email.utils.format_datetime(datetime.datetime.fromisoformat(post['date'])) + pubdate = email.utils.format_datetime(datetime.datetime.fromisoformat(post["date"])) subs = { - 'title': post['title'], - 'link': link, - 'pubdate': pubdate, - 'description': converted - } + "title": post["title"], + "link": link, + "pubdate": pubdate, + "description": converted, + } - for k,v in subs.items(): + for k, v in subs.items(): subs[k] = html.escape(v) return tpl.format(**subs) + def render_rss_index(posts): - items = '' + items = "" for post in posts[:5]: items += rss_post_xml(post) subs = { - 'site_title': 'cfebs.com', - 'site_link': 'https://cfebs.com', - 'self_full_link': 'https://cfebs.com/index.xml', - 'description': 'Recent content from cfebs.com', - 'last_build_date': email.utils.format_datetime(datetime.datetime.now()), + "site_title": "cfebs.com", + "site_link": "https://cfebs.com", + "self_full_link": "https://cfebs.com/index.xml", + "description": "Recent content from cfebs.com", + "last_build_date": email.utils.format_datetime(datetime.datetime.now()), } - for k,v in subs.items(): + for k, v in subs.items(): subs[k] = html.escape(v) - subs['items'] = items - render_template('index.xml.tmpl', 'index.xml', subs) + subs["items"] = items + render_template("index.xml.tmpl", "index.xml", subs) + def main(): + os.makedirs("public/posts/", exist_ok=True) posts = render_posts() - logging.info('rendered posts: %s', posts) - posts = filter(lambda p: not p['draft'], posts) - sorted_posts = sorted(posts, - key=lambda p: datetime.datetime.fromisoformat(p['date']), reverse=True) + logging.info("rendered posts: %s", posts) + posts = filter(lambda p: not p["draft"], posts) + sorted_posts = sorted( + posts, key=lambda p: datetime.datetime.fromisoformat(p["date"]), reverse=True + ) render_index(sorted_posts) render_rss_index(sorted_posts) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/avatar.png b/public/avatar.png similarity index 100% rename from avatar.png rename to public/avatar.png diff --git a/chalk.min.css b/public/chalk.min.css similarity index 100% rename from chalk.min.css rename to public/chalk.min.css diff --git a/public/grayscale-dark.min.css b/public/grayscale-dark.min.css new file mode 100644 index 0000000..54c9a4c --- /dev/null +++ b/public/grayscale-dark.min.css @@ -0,0 +1,7 @@ +/*! + Theme: Grayscale Dark + Author: Alexandre Gavioli (https://github.com/Alexx2/) + License: ~ MIT (or more permissive) [via base16-schemes-source] + Maintainer: @highlightjs/core-team + Version: 2021.09.0 +*/pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{color:#b9b9b9;background:#101010}.hljs ::selection,.hljs::selection{background-color:#464646;color:#b9b9b9}.hljs-comment{color:#525252}.hljs-tag{color:#ababab}.hljs-operator,.hljs-punctuation,.hljs-subst{color:#b9b9b9}.hljs-operator{opacity:.7}.hljs-bullet,.hljs-deletion,.hljs-name,.hljs-selector-tag,.hljs-template-variable,.hljs-variable{color:#7c7c7c}.hljs-attr,.hljs-link,.hljs-literal,.hljs-number,.hljs-symbol,.hljs-variable.constant_{color:#999}.hljs-class .hljs-title,.hljs-title,.hljs-title.class_{color:#a0a0a0}.hljs-strong{font-weight:700;color:#a0a0a0}.hljs-addition,.hljs-code,.hljs-string,.hljs-title.class_.inherited__{color:#8e8e8e}.hljs-built_in,.hljs-doctag,.hljs-keyword.hljs-atrule,.hljs-quote,.hljs-regexp{color:#868686}.hljs-attribute,.hljs-function .hljs-title,.hljs-section,.hljs-title.function_,.ruby .hljs-property{color:#686868}.diff .hljs-meta,.hljs-keyword,.hljs-template-tag,.hljs-type{color:#747474}.hljs-emphasis{color:#747474;font-style:italic}.hljs-meta,.hljs-meta .hljs-keyword,.hljs-meta .hljs-string{color:#5e5e5e}.hljs-meta .hljs-keyword,.hljs-meta-keyword{font-weight:700} \ No newline at end of file diff --git a/highlight.js.min.css b/public/highlight.js.min.css similarity index 100% rename from highlight.js.min.css rename to public/highlight.js.min.css diff --git a/highlight.min.js b/public/highlight.min.js similarity index 100% rename from highlight.min.js rename to public/highlight.min.js diff --git a/img/mse_first_play.png b/public/img/mse_first_play.png similarity index 100% rename from img/mse_first_play.png rename to public/img/mse_first_play.png diff --git a/style.css b/public/style.css similarity index 100% rename from style.css rename to public/style.css