Created
June 10, 2015 14:37
-
-
Save tryone144/8534b9533d9e2b457e42 to your computer and use it in GitHub Desktop.
Wrapper to run the markdown renderer gimli
This file contains hidden or 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/ruby | |
# | |
# Wrapper to run gimli | |
# | |
# (c) 2015 Bernd Busse | |
require 'rubygems' | |
version = ">= 0" | |
if ARGV.first | |
str = ARGV.first | |
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding | |
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then | |
version = $1 | |
ARGV.shift | |
end | |
end | |
_GIMLI_CONFIG = Dir.home + "/.config/gimli/" | |
_GIMLI_CSS = "./gimli.css" | |
_GIMLI_HEADER = "./gimli_header.html" | |
_GIMLI_FOOTER = "./gimli_footer.html" | |
if not File.file?(_GIMLI_CSS) | |
_GIMLI_CSS = File.join(_GIMLI_CONFIG, _GIMLI_CSS) | |
end | |
if File.file?(_GIMLI_HEADER) | |
_abs = Dir.pwd | |
else | |
_abs = _GIMLI_CONFIG | |
end | |
_GIMLI_HEADER = "file:///" + File.join(_abs, _GIMLI_HEADER) | |
if File.file?(_GIMLI_FOOTER) | |
_abs = Dir.pwd | |
else | |
_abs = _GIMLI_CONFIG | |
end | |
_GIMLI_FOOTER = "file:///" + File.join(_abs, _GIMLI_FOOTER) | |
if ARGV.length == 0 or ARGV.length > 1 | |
abort "ERROR: filename missing\n" | |
end | |
_infile = ARGV.first | |
ARGV.shift | |
if not File.file?(_infile) | |
abort "ERROR: \"" + _infile + "\" is not a file" | |
end | |
_infile = File.absolute_path(_infile) | |
_filename = File.basename(_infile, File.extname(_infile)) + ".pdf" | |
_title = "" | |
_subtitle = "" | |
_toc = false | |
File.open(_infile) do |f| | |
readheader = false | |
f.each_line do |line| | |
if line.match(/^(---\s*\n)/) | |
if not readheader | |
readheader = true | |
else | |
break | |
end | |
elsif readheader | |
if line.match(/^(title:\s?)/) | |
_title = line.match(/'(.*)'\s*$/)[1] | |
elsif line.match(/^(subtitle:\s?)/) | |
_subtitle = line.match(/'(.*)'\s*$/)[1] | |
elsif line.match(/^(toc:\s?)/) | |
if line.match(/'(true)'\s*$/i) or line.match(/'(yes)'\s*$/i) | |
_toc = true | |
end | |
end | |
end | |
end | |
end | |
_WKHTMLTOPDF_ARGUMENTS = "?filename=" + _filename + "&title=" + _title + "&subtitle=" + _subtitle | |
_WKHTMLTOPDF_HEADER = "--header-html \"" + _GIMLI_HEADER + _WKHTMLTOPDF_ARGUMENTS + "\"" | |
_WKHTMLTOPDF_FOOTER = "--footer-html \"" + _GIMLI_FOOTER + _WKHTMLTOPDF_ARGUMENTS + "\"" | |
_WKHTMLTOPDF_OPTIONS = _WKHTMLTOPDF_HEADER + " " + _WKHTMLTOPDF_FOOTER + " -B 20 -L 20 -R 20 -T 20" | |
if _toc | |
_WKHTMLTOPDF_OPTIONS += " -t" | |
end | |
ARGV.push("-w", _WKHTMLTOPDF_OPTIONS) | |
ARGV.push("-s", _GIMLI_CSS) | |
ARGV.push("-y") | |
ARGV.push("-f", _infile) | |
gem 'gimli', version | |
load Gem.bin_path('gimli', 'gimli', version) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment