From 4b866eafef204863cb27ffbed28f57acd541673f Mon Sep 17 00:00:00 2001 From: "Niels G. W. Serup" Date: Fri, 2 Sep 2016 23:32:55 +0200 Subject: [PATCH] Generate article sitemap thing. --- scripts/transform-file.py | 76 ++++++++++++++++++++++++++++++++- site/longpoem/index.md | 4 +- site/misc/index.md | 6 ++- site/myuniverse/index.md | 2 +- site/sound-programming/index.md | 3 +- site/unhappy/index.md | 3 +- 6 files changed, 84 insertions(+), 10 deletions(-) diff --git a/scripts/transform-file.py b/scripts/transform-file.py index 6598f15..c1984c8 100755 --- a/scripts/transform-file.py +++ b/scripts/transform-file.py @@ -14,6 +14,8 @@ import os import html import subprocess +import yaml + def read(path): with open(path) as f: @@ -29,12 +31,20 @@ base_dir = os.path.split(script_dir)[0] template_dir = os.path.join(base_dir, 'template') template_base_file = os.path.join(template_dir, 'base.html') template_base = read(template_base_file) +site_dir = os.path.join(base_dir, 'site') def pandoc(filename): - proc = subprocess.run(['pandoc', '--smart', filename], stdout=subprocess.PIPE) + proc = subprocess.run(['pandoc', '--smart', filename], + stdout=subprocess.PIPE) return proc.stdout.decode('utf-8').strip() +def pandoc_stdin(text, from_type): + out = subprocess.Popen( + ['pandoc', '--smart', '-f', from_type], + stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(text.encode('utf-8'))[0] + return out.decode('utf-8').strip() + def extract_markdown_title(filename): with open(filename) as f: for line in f: @@ -43,10 +53,72 @@ def extract_markdown_title(filename): if filename.endswith('.md'): return os.path.basename(filename)[:-3] +def extract_markdown_abstract(filename): + state = 0 + with open(filename) as f: + for line in f: + if state == 0 and line == '---\n': + state = 1 + yaml_block = '' + elif state == 1: + if line == '---\n': + y = yaml.load(yaml_block) + if y is None: + return None + else: + return y.get('abstract').strip().replace('\n', ' ') + else: + yaml_block += line + def markdown_to_html(input_file, output_dir): title = extract_markdown_title(input_file) title = html.escape(title) - content = pandoc(input_file) + + common = os.path.commonpath([input_file, output_dir]) + relpath = os.path.relpath(input_file, start=common) + + # Special cases + if relpath == 'site/misc/index.md': + top = read(input_file) + + pages = [] + for path, subdirs, subfiles in os.walk(site_dir): + dir = path + if dir.startswith('./'): + dir = dir[1:] + if dir.startswith('/site'): + dir = dir[5:] + if 'index.md' in subfiles: + pages.append((dir + '/', + os.path.normpath(os.path.join(base_dir, path, 'index.md')))) + for name in subfiles: + if name.endswith('.md') and name != 'index.md': + uri = name[:-3] + pages.append((dir + '/' + uri, + os.path.normpath(os.path.join(base_dir, path, name)))) + + builtins = ['/', '/about/', '/about/niels', '/misc/'] + pages = filter(lambda t: t[0] not in builtins, pages) + pages_new = [] + for page in pages: + url, path = page + ptitle = extract_markdown_title(path) + pabstract = extract_markdown_abstract(path) + pages_new.append((ptitle, pabstract, url, path)) + + pages_new.sort() + md = '' + for page in pages_new: + ptitle, pabstract, url, _ = page + if pabstract is None: + pabstract = '(No description)' + md += '[{}]({})\n ~ {}\n\n'.format(ptitle, url, pabstract) + + bottom = md + content = pandoc_stdin(top + '\n' + bottom, 'markdown') + else: + content = pandoc(input_file) + html_out = template_base.format(title=title, content=content) output_file = os.path.join( output_dir, diff --git a/site/longpoem/index.md b/site/longpoem/index.md index 7202783..2671549 100644 --- a/site/longpoem/index.md +++ b/site/longpoem/index.md @@ -1,8 +1,8 @@ -``` +--- abstract: | (A Long Poem.) Et langt digt jeg skrev for noget tid siden fordi jeg ikke kan lide digte. -``` +--- # Et langt digt diff --git a/site/misc/index.md b/site/misc/index.md index 1e1fe2e..dda6b84 100644 --- a/site/misc/index.md +++ b/site/misc/index.md @@ -1,4 +1,4 @@ -# Miscellaneous projects +# Miscellaneous items This page contains things that I couldn't fit into other pages. @@ -7,3 +7,7 @@ This page contains things that I couldn't fit into other pages. I have an inactive webcomic called SUUM. It's located at [suum.metanohi.name](https://suum.metanohi.name). + + +## Articles + diff --git a/site/myuniverse/index.md b/site/myuniverse/index.md index 364f565..36b32f0 100644 --- a/site/myuniverse/index.md +++ b/site/myuniverse/index.md @@ -1,6 +1,6 @@ --- abstract: I have attempted to define a universe. ----- +--- # My Universe diff --git a/site/sound-programming/index.md b/site/sound-programming/index.md index 6761e05..4131a0d 100644 --- a/site/sound-programming/index.md +++ b/site/sound-programming/index.md @@ -2,8 +2,7 @@ abstract: Programming sound in Live-Sequencer and ChucK --- -Sound Programming -================= +# Sound Programming Much can be programmed, and that includes sound. In the digital world, sound is typically represented by sequences of about 90 kB per second, diff --git a/site/unhappy/index.md b/site/unhappy/index.md index 044611b..fea83b5 100644 --- a/site/unhappy/index.md +++ b/site/unhappy/index.md @@ -5,8 +5,7 @@ abstract: Rant about Haskell's parser "Happy". ![](UNHAPPY.PNG) -Unhappy About Happy: A Reflection or Something Like That -======================================================== +# Unhappy About Happy: A Reflection or Something Like That For the purposes of my quiet rage, this document will be written in all caps. I also will not use proper language. I won't even give suggestions