phlog

Source code for my blog/gemlog. It used to be on gopher, hence the name
git clone http://shtanton.xyz/git/repo/phlog
Log | Files | Refs

commit 790b508217beac94cda20add4e0f60fa9bca2c8a
parent cdc43cac0bfedce6ae786bef22c958cf73c6f938
Author: Charlie Stanton <charlie@shtanton.com>
Date:   Sun, 16 Aug 2020 17:41:33 +0100

Switches to tup and chevron, gopher is behind now

Also loads of stuff needs deleting

Diffstat:
M.gitignore | 1+
ATupfile | 4++++
Ahtml_src/colours.css | 12++++++++++++
Ahtml_src/index_template.html | 14++++++++++++++
Ahtml_src/post.css | 25+++++++++++++++++++++++++
Ahtml_src/post_template.html | 25+++++++++++++++++++++++++
Amd_to_json.py | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 136 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ target /html/ /gopher/ +.tup diff --git a/Tupfile b/Tupfile @@ -0,0 +1,4 @@ +: foreach html_src/*.css |> cp %f %o |> html/%B.css +: posts/ex.md |> ./md_to_json.py %f %o config.json |> posts/%B.json +: posts/ex.json |> bat %f | ./chevron html_src/post_template.html %o |> html/ex.html +: |> bat config.json | ./chevron html_src/index_template.html %o |> html/index.html diff --git a/html_src/colours.css b/html_src/colours.css @@ -0,0 +1,12 @@ +html { + background-color: #282828; + color: #ebdbb2; +} + +h1,h2,h3,h4,h5,h6 { + color: #98971a; +} + +a { + color: #fabd2f; +} diff --git a/html_src/index_template.html b/html_src/index_template.html @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> + <head> + <title>Charlie Stanton's Blog</title> + <link rel="stylesheet" href="colours.css"/> + </head> + <body> + <h2> Charlie Stanton's Blog</h2> + <p>RSS on the way</p> + <ul>{#posts} + <li><a href="/{file}.html">{title}</a> - <em>{date}</em></li>{/posts} + </ul> + </body> +</html> diff --git a/html_src/post.css b/html_src/post.css @@ -0,0 +1,25 @@ +@media only screen and (min-width: 640px) { + body { + display: grid; + grid-template-columns: auto auto; + grid-column-gap: 50px; + } + .center { + text-align: center; + } + .center > * { + text-align: left; + display: inline-block; + } + article { + max-width: 600px; + } + #nav { + grid-column-start: 2; + grid-row-start: 1; + grid-row-end: 3; + } +} +body { + padding: 20px; +} diff --git a/html_src/post_template.html b/html_src/post_template.html @@ -0,0 +1,25 @@ +<!DOCTYPE html> +<html> + <head> + <title>Charlie Stanton's Blog</title> + <link rel="stylesheet" href="post.css"/> + <link rel="stylesheet" href="colours.css"/> + </head> + <body> + <div id="nav" class="center"> + <nav> + Nav + </nav> + </div> + <div class="center"> + <header> + Header + </header> + </div> + <div class="center"> + <article>{#article} + {#paragraph}<p>{#lines}{#parts}{#code}<code>{text}</code>{/code}{#link}<a href="{link}">{text}</a>{/link}{#plain}{text}{/plain}{/parts} {/lines}</p>{/paragraph}{#header}<h{header}>{#lines}{#parts}{#code}<code>{text}</code>{/code}{#link}<a href="{link}">{text}</a>{/link}{#plain}{text}{/plain}{/parts} {/lines}</h{header}>{/header}{#list}<ul>{#lines}<li>{#parts}{#code}<code>{text}</code>{/code}{#link}<a href="{link}">{text}</a>{/link}{#plain}{text}{/plain}{/parts}</li>{/lines}</ul>{/list}{#quote}<quote>{#lines}{#parts}{#code}<code>{text}</code>{/code}{#link}<a href="{link}">{text}</a>{/link}{#plain}{text}{/plain}{/parts} {/lines}</quote>{/quote}{/article} + </article> + </div> + </body> +</html> diff --git a/md_to_json.py b/md_to_json.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3.8 +import argparse, json, re + +parser = argparse.ArgumentParser(description="Generate post json file from markdown") +parser.add_argument("markdown", help="Markdown input file") +parser.add_argument("output", help="Output destination") +parser.add_argument("config", help="Config json") +args = parser.parse_args() + +link_pattern = re.compile(r"([^[]*)\[([^]]+)\]\(([^)]+)\)") + +def split_part_into_links(part): + parts = [] + while len(part) > 0: + match = link_pattern.match(part) + if match: + parts.append({"text": match.group(1), "plain": True}) + parts.append({"text": match.group(2), "link": match.group(3)}) + part = part[match.end():] + else: + parts.append({"text": part, "plain": True}) + return parts + return parts + +def parse_line(line): + coded_parts = [({"text": part, "plain": True}, {"text": part, "code": True})[i%2] for i, part in enumerate(line.split("`"))] + linked_parts = [[part] if "code" in part else split_part_into_links(part["text"]) for part in coded_parts] + return {"parts": [part for parts in linked_parts for part in parts]} + +def parse_paragraph(paragraph): + markdown_lines = paragraph.split("\n") + if markdown_lines[0][0] == "#": + hashes = 1 + while markdown_lines[0][hashes] == "#": + hashes += 1 + lines = [parse_line(line[hashes+1:]) for line in markdown_lines] + return {"header": hashes, "lines": lines} + elif markdown_lines[0][:2] == "- ": + lines = [parse_line(line[2:]) for line in markdown_lines] + return {"list": True, "lines": lines} + elif markdown_lines[0][:2] == "> ": + lines = [parse_line(line[2:]) for line in markdown_lines] + return {"quote": True, "lines": lines} + else: + lines = [parse_line(line) for line in markdown_lines] + return {"paragraph": True, "lines": lines} + +with open(args.markdown, encoding="utf-8") as markdown: + markdown_paragraphs = markdown.read().split("\n\n") +with open(args.config, encoding="utf-8") as config_file: + config = json.load(config_file) +paragraphs = [parse_paragraph(para) for para in markdown_paragraphs] +config["article"] = paragraphs +with open(args.output, mode="w", encoding="utf-8") as output: + json.dump(config, output)