Last active
August 29, 2015 14:15
-
-
Save kindfulkirby/12f952f66e81ec6816c3 to your computer and use it in GitHub Desktop.
Git hook to publish things
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.2 | |
import subprocess, re | |
matchers = { | |
'../scripts/public-add': re.compile('[AM]\tpublic/[\w.]'), | |
'../scripts/public-del': re.compile('D\tpublic/[\w.]'), | |
} | |
for line in subprocess.check_output(["git", "show", "--name-status"]).decode().split('\n')[4:-1]: | |
for k, v in matchers.items(): | |
if v.match(line): | |
subprocess.call([k, line[2:]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.2 | |
import sys, subprocess, tempfile, shutil | |
# directory to publish the result to | |
to_dir = '../../public/serve/' | |
# metadata, with defaults | |
muformat = 'markdown' | |
date = '' | |
categories = tuple() | |
tags = tuple() | |
title = 'Paste' | |
if (len(sys.argv) > 1): | |
filename = sys.argv[1] | |
# if it is a wiki page | |
if (filename.endswith('.page')): | |
file = open(filename) | |
lines = file.readlines() | |
file.close() | |
last_yaml_line = 0 | |
#if it is has YAML metadata, process it | |
if (lines[0] == '---\n'): | |
for i, line in enumerate(lines): | |
if (line == '...\n'): | |
last_yaml_line = i+1 | |
break | |
key, _, value = line[:-1].partition(": ") | |
if (key == 'format'): | |
muformat = value | |
elif (key == 'date'): | |
date = value | |
elif (key == 'categories'): | |
categories = tuple(value.split(' ')) | |
elif (key =='tags'): | |
tags = tuple(value.split(' ')) | |
elif (key == 'title'): | |
title = value | |
# write content without metadate to temporary file | |
temp = tempfile.NamedTemporaryFile(mode='w+t', delete=False) | |
temp.writelines(lines[last_yaml_line:]) | |
temp.close() | |
# convert the temporary file, pass metadata to converter | |
subprocess.call([ | |
'pandoc', '-s', '-V', 'pagetitle=' + title, '-f', muformat, '-t', 'html', | |
'-o', to_dir + filename[7:-5] + '.html', temp.name | |
]) | |
# if there's not metadata just convert the original | |
else: | |
subprocess.call([ | |
'pandoc', '-s', '-V', 'pagetitle=Paste', '-f', 'markdown', '-t', 'html', | |
'-o', to_dir + filename[7:-5] + '.html', filename | |
]) | |
#if it is not a wiki page, just copy it | |
else: | |
shutil.copy(filename, to_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.2 | |
import sys, os | |
to_dir = '../../public/serve/' | |
if (len(sys.argv) > 1): | |
filename = sys.argv[1] | |
if (filename.endswith('.page')): | |
os.remove(to_dir + filename[7:-5] + '.html') | |
else: | |
os.remove(to_dir + filename[7:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks timo for some significant improvements on the code!