Skip to content

Instantly share code, notes, and snippets.

@memeplex
Last active June 12, 2016 22:04
Show Gist options
  • Save memeplex/95398ea4e00a975a8398d32487f2a2a7 to your computer and use it in GitHub Desktop.
Save memeplex/95398ea4e00a975a8398d32487f2a2a7 to your computer and use it in GitHub Desktop.
Liftup
#!/bin/python3
from gcd.nix import sh, sh_quote, path, os, cmd, env
from urllib.parse import quote
from pandocfilters import toJSONFilter, RawInline
def codecogs(key, value, format, meta):
if key == 'Math':
inline = value[0]['t'] == 'InlineMath'
latex = '\\inline ' + value[1] if inline else value[1]
url = 'http://latex.codecogs.com/gif.latex?' + quote(latex)
if format.startswith('markdown'):
return RawInline('markdown', '![](%s)' % url)
elif format == 'html':
return RawInline('html', '<img src="%s"></img>' % url)
def tmpout(args):
output_dir = '/tmp/liftup'
output_name = path.splitext(
path.abspath(args.input).replace('/', '$'))[0]
output_ext = 'pdf' if args.type == 'latex' else 'html'
if not path.exists(output_dir):
os.mkdir(output_dir)
return '%s/%s.%s' % (output_dir, output_name, output_ext)
@cmd.run
def main():
cmd.arg('--output', '-o',
help='Output file, by default a temporary file uniquely '
'associated with the input file.')
cmd.arg('--type', '-t', default='html',
choices=('html', 'latex', 'github', 'snippet'),
help='Output type, by default html.')
cmd.arg('--mathimg', '-m', action='store_true',
help='Render math as image url. Always true for -t github.')
cmd.arg('--subst', '-s', action='store_true',
help='Prints the output path to stdout in order to facilitate '
'command substitution.')
cmd.arg('input')
args = cmd.args
if env.get('LIFTUP_FILTER'):
toJSONFilter(globals()[env.get('LIFTUP_FILTER')])
return
output = args.output if args.output else tmpout(args)
pandoc = 'pandoc -S -f markdown -o ' + sh_quote(output)
if args.type == 'snippet':
pandoc += ' -t html --template=snippet'
elif args.type == 'github':
pandoc += ' -s -t markdown_github'
args.mathimg = True
else:
pandoc += ' -s -t ' + args.type
if args.mathimg:
pandoc = 'LIFTUP_FILTER=codecogs ' + pandoc + ' --filter liftup'
else:
pandoc += ' --mathjax'
sh(pandoc + ' ' + sh_quote(args.input))
if args.subst:
print(output)
<style type="text/css">code{white-space: pre;}</style>
$if(quotes)$
<style type="text/css">q { quotes: "“" "”" "‘" "’"; }</style>
$endif$
$if(highlighting-css)$
<style type="text/css">
$highlighting-css$
</style>
$endif$
$if(math)$
$math$
$endif$
$for(include-before)$
$include-before$
$endfor$
$if(toc)$
<div id="$idprefix$TOC">
$toc$
</div>
$endif$
$body$
$for(include-after)$
$include-after$
$endfor$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment