-
Notifications
You must be signed in to change notification settings - Fork 0
/
verkilo
executable file
·79 lines (67 loc) · 2.4 KB
/
verkilo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env ruby
require 'fileutils'
def epub_image(images_dir, action)
fname = "#{images_dir}epub-cover.png"
raise "Epub Cover Missing (#{fname})" if (action == 'epub' and !File.exist?(fname))
end
def makeProductionFile(action, target)
book_basename = File.basename(target)
basename = "#{File.basename(Dir.pwd)}-#{book_basename}"
build_dir = (action == "html") ? "build/html/" : "build/"
chapters = Dir["./#{target}/**/*.md"].sort.join(" ")
output_filename = "#{build_dir}#{basename}.#{action}"
args = @build_flags[:all] + @build_flags[action.to_sym] + "-o #{output_filename}"
cmd = "pandoc #{args.gsub(/\s+/, " ")} #{chapters}"
FileUtils.mkdir_p(build_dir)
puts `#{cmd}`
end
action = ARGV.shift.gsub('/','')
target = ARGV.shift unless ARGV.empty?
images_dir = "./#{target}/images/"
templates_dir = "#{Dir.home}/.verkilorc/templates/"
version = `git describe --abbrev=0 --tags` || 'none'
# Build Flags are a series of pandoc build arguments that are tailored to
# a specific output format. :all describes the commonly used arguments.
@build_flags = { }
@build_flags[:all] = <<-HEREDOC
--lua-filter #{templates_dir}latex.lua \
--metadata=version:#{@version} \
--metadata-file=metadata.yml
HEREDOC
@build_flags[:epub] = <<-HEREDOC
--css=#{templates_dir}style.css \
--epub-cover-image=#{epub_image(images_dir, action)} \
--template=#{templates_dir}epub.html \
--webtex
HEREDOC
@build_flags[:html] = <<-HEREDOC
--css=#{templates_dir}style.css \
--self-contained \
--standalone --to=html5 \
--webtex
HEREDOC
@build_flags[:pdf] = <<-HEREDOC
--pdf-engine=xelatex \
--template=#{templates_dir}pdf.tex \
-V documentclass=book \
--webtex
HEREDOC
@build_flags[:docx] = <<-HEREDOC
--reference-doc=#{templates_dir}reference.docx
HEREDOC
# Actions are the various output formats that we create.
actions = %w(pdf docx epub html)
if action == 'clean'
puts "Removing Build files."
FileUtils.rm_rf Dir.glob("build/*") if Dir.exist?('build')
elsif actions.include?(action)
makeProductionFile(action, target)
elsif (action == 'book')
makeProductionFile('epub', target)
makeProductionFile('pdf', target)
elsif (action == 'clean')
FileUtils.remove_dir(build_dir)
else
puts "Usage: verkilo {#{actions}} {target-directory}"
puts " verkilo needs one of the listed formats as an action, and a target."
end