From ad910dafcc38f7d4aad5c9c28dc0be9dae8c3b5c Mon Sep 17 00:00:00 2001 From: "Niels G. W. Serup" Date: Fri, 26 Aug 2016 15:26:30 +0200 Subject: [PATCH] Add basic site generation scripts. --- scripts/transform-file.py | 100 ++++++++++++++++++++++++++++++++++++++ scripts/transform-site.sh | 18 +++++++ 2 files changed, 118 insertions(+) create mode 100755 scripts/transform-file.py create mode 100755 scripts/transform-site.sh diff --git a/scripts/transform-file.py b/scripts/transform-file.py new file mode 100755 index 0000000..82c4dae --- /dev/null +++ b/scripts/transform-file.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 + +''' +Transforms a file into a web-servable file. + +In most cases this entails either + + + just symlinking, or + + transforming a page to html +''' + +import sys +import os +import html +import subprocess + + +def read(path): + with open(path) as f: + return f.read() + +def write(path, text): + with open(path, 'w') as f: + f.write(text) + + +script_dir = os.path.dirname(__file__) +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) + + +def pandoc(filename): + proc = subprocess.run(['pandoc', filename], stdout=subprocess.PIPE) + return proc.stdout.decode('utf-8').strip() + +def indent_block(t, block): + return '\n'.join(t + line for line in block.split('\n')) + +def extract_markdown_title(filename): + with open(filename) as f: + for line in f: + if line.startswith('# '): + return line[2:].strip() + if filename.endswith('.md'): + return os.path.basename(filename)[:-3] + +def markdown_to_html(input_file, output_dir): + title = extract_markdown_title(input_file) + title = html.escape(title) + content = indent_block(8 * ' ', pandoc(input_file)) + html_out = template_base.format(title=title, content=content) + output_file = os.path.join( + output_dir, + os.path.splitext(os.path.basename(input_file))[0] + + '.html') + write(output_file, html_out) + +def symlink_relative(input_file, output_dir): + in_path_abs = os.path.abspath(input_file) + os.chdir(output_dir) + in_path_rel = os.path.relpath(in_path_abs) + output_file = os.path.basename(input_file) + os.symlink(in_path_rel, output_file) + +def get_extension(filename): + _, extension = os.path.splitext(filename) + extension = extension[1:] + return extension + +def transform(input_file, output_dir): + extension = get_extension(input_file) + + actions = { + 'md': lambda: markdown_to_html(input_file, output_dir) + } + try: + action = actions[extension] + except KeyError: + action = lambda: symlink_relative(input_file, output_dir) + action() + return 0 + +def main(args): + try: + [input_file, output_dir] = args + except ValueError: + print_usage() + return 1 + + transform(input_file, output_dir) + +def print_usage(): + print('''\ +usage: transform-file.py INPUT_FILE OUTPUT_DIRECTORY +''') + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/scripts/transform-site.sh b/scripts/transform-site.sh new file mode 100755 index 0000000..b1bda95 --- /dev/null +++ b/scripts/transform-site.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# +# Transform the entire site into something suitable for a web server. + +set -e # Exit on first error. + +cd "$(dirname "$0")/.." + +rm -rf web-serve +mkdir web-serve + +for file in $(cd site; find -type f); do + file="$(echo $file | sed -r 's/^\.\/?//')" + dir="$(dirname $file)" + outdir="web-serve/$dir" + mkdir -p "$outdir" + ./scripts/transform-file.py "site/$file" "$outdir" +done