-
Notifications
You must be signed in to change notification settings - Fork 29
/
make-common
96 lines (79 loc) · 2.4 KB
/
make-common
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'fileutils'
require 'open3'
require 'erb'
require 'yaml'
include FileUtils, Open3
alias :pipe :popen3
$root = $here = File.expand_path(File.dirname(__FILE__))
def figures(&block)
begin
# Dir["#$root/figures-png/*"].each do |file|
# cp(file, file.sub(/(figures-png)/, '\1.\2'))
# end
block.call
ensure
# Dir["#$root/figures/18333*.png"].each do |file|
# rm(file.gsub(/18333fig0(\d)0?(\d+)\-tn/, '\1.\2'))
# end
end
end
def command_exists?(command)
ENV['PATH'].split(/:/).map do |path|
File.executable?("#{path}/#{command}")
end.inject{|a, b| a || b}
end
def replace(string, &block)
string.instance_eval do
alias :s :gsub!
instance_eval(&block)
end
string
end
def verbatim_sanitize(string)
string.gsub('\\', '{\textbackslash}').
gsub('~', '{\textasciitilde}').
gsub(/([\$\#\_\^\%])/, '\\\\' + '\1{}')
end
def pre_pandoc(string, config)
replace(string) do
# Ignore comments
s /^%.*/, ''
# Pandoc discards #### subsubsections #### - this hack recovers them
s /\#\#\#\# (.*?) \#\#\#\#/, 'SUBSUBSECTION: \1'
# Turns URLs into clickable links
s /\`(http:\/\/[A-Za-z0-9\/\%\&\=\-\_\\\.]+)\`/, '<\1>'
s /(\n\n)\t(http:\/\/[A-Za-z0-9\/\%\&\=\-\_\\\.]+)\n([^\t]|\t\n)/, '\1<\2>\1'
# Process figures
s /Insert\s.+\.png\s*\n.*?\d{1,2}-\d{1,2}\. (.*)/, 'FIG: \1'
end
end
def html_mtime(chap)
mtime = `git log -1 --format=format:"%cD" #$here/chap#{chap}/doc.md`
return DateTime.parse(mtime).strftime("%B %d, %Y")
end
# check deps
missing = ['pandoc',
'xelatex',
'runhaskell',
'pygmentize'].reject{|command| command_exists?(command)}
unless missing.empty?
puts "Missing dependencies: #{missing.join(', ')}."
puts "Install these and try again."
exit
end
# include configuration file
$config = YAML.load_file("#$here/latex/config.yml")
# include templates
$base_template = ERB.new(File.read("#$here/html/template.html"))
$chap_template = ERB.new(File.read("#$here/html/template-chap.html"))
$index_template = ERB.new(File.read("#$here/html/template-index.html"))
$latex_template = ERB.new(File.read("#$here/latex/template.tex"))
# common debug flag
ARGV.delete_if{|arg| $DEBUG = true if arg == '-d' or arg == '--debug'}
# check pandoc version
unless `pandoc --version | head -1`.include? "1.9"
puts "Install the latest version of pandoc"
exit
end