Skip to content

Instantly share code, notes, and snippets.

@kindfulkirby
Last active August 29, 2015 14:15
Show Gist options
  • Save kindfulkirby/12f952f66e81ec6816c3 to your computer and use it in GitHub Desktop.
Save kindfulkirby/12f952f66e81ec6816c3 to your computer and use it in GitHub Desktop.
Git hook to publish things
#!/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:]])
#!/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)
#!/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:])
@kindfulkirby
Copy link
Author

EDIT: Don't forget to edit the first line to point to your local python 3! It's just python on Arch, python3 on Ubuntu and apparently python3.2 on Debian. Probably similar on most other distros.

@kindfulkirby
Copy link
Author

I release this code in the public domain, or - where not applicable - under a CC0 license.

@kindfulkirby
Copy link
Author

Turns out, I didn't read the pandoc manual very carefully... now updated & much better ;)

@kindfulkirby
Copy link
Author

Thanks timo for some significant improvements on the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment