/
theme2latexrender
executable file
·122 lines (104 loc) · 3.56 KB
/
theme2latexrender
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /usr/bin/env ruby
begin
require 'plist'
rescue LoadError
require 'rubygems'
require 'plist'
end
require 'uv/utility'
base_dir = File.join( File.dirname(__FILE__), '..', 'render' )
def settings
unless @settings
@settings = @theme["settings"].find { |s| ! s["name"] }["settings"]
end
@settings
end
@theme = Plist::parse_xml( ARGV[0] )
render = {"name" => @theme["name"]}
codecolumn = ""
numbercolumn = ""
standard_name = File.basename( ARGV[0] ).downcase.gsub(/\s+/, '_').gsub('.tmtheme', '').gsub(/\W/, '').gsub(/_+/, '_')
render["tags"] = []
@theme["settings"].each do |t|
if t["scope"]
class_name = t["name"].downcase.gsub(/\W/, ' ').gsub('.tmtheme', '').split(' ').collect{|s| s.capitalize}.join
if class_name == ""
class_name = "x" * t["name"].size
end
tag = {}
tag["selector"] = t["scope"]
render["tags"] << tag
begin_string = ""
pcount = 0
if s = t["settings"]
if s["background"]
begin_string << "\\setlength{\\fboxsep}{0ex}\\colorbox[HTML]{#{Uv.normalize_color(settings, s["background"])[1..-1]}}{\\rule[-0.5ex]{0pt}{2.0ex}"
else
begin_string << "{"
end
pcount += 1
if s["foreground"]
begin_string << "\\color[HTML]{#{Uv.normalize_color(settings, s["foreground"], true)[1..-1]}}"
end
case s["fontStyle"]
when /bold/
begin_string << "\\textbf{"
pcount += 1
when /italic/
begin_string << "\\textit{"
pcount += 1
when /underline/
begin_string << "\\underline{"
pcount += 1
end
tag["begin"] = begin_string
tag["end"] = "}" * pcount
end
elsif ! t["name"]
if s = t["settings"]
codecolumn = "\\newcolumntype{C}{>{"
codecolumn << "\\color[HTML]{#{Uv.normalize_color(settings, s["foreground"], true)[1..-1]}}" if s["foreground"]
codecolumn << "\\columncolor[HTML]{#{Uv.alpha_blend(s["background"], s["background"])[1..-1]}}" if s["background"]
codecolumn << "}l}"
bg = Uv.alpha_blend(s["selection"], s["selection"]) if s["selection"]
numbercolumn = "\\newcolumntype{N}{>{"
numbercolumn << "\\color[HTML]{#{Uv.foreground(bg)[1..-1]}}" if bg
numbercolumn << "\\columncolor[HTML]{#{bg[1..-1]}}" if s["selection"]
numbercolumn << "}l}"
tag = {}
tag["begin"] = "\\texttt{"
tag["end"] = "}&\\mbox{\\texttt{"
render["line-numbers"] = tag end
end
end
render["filter"] = '@escaped.gsub(/(\$)/, \'\\\\\\\\\1\').gsub(/\\\\(?!\$)/, \'$\\\\\\\\backslash$\').gsub(/(_|\{|\}|&|\#|%)/, \'\\\\\\\\\1\').gsub(/~/, \'\\\\textasciitilde \').gsub(/ /,\'\\\\hspace{1ex}\').gsub(/\t| /,\'\\\\hspace{3ex}\').gsub(/\"/, "\'\'").gsub(/(\^)/,\'\\\\\\\\\1{}\')'
tag = {}
tag["begin"] = ""
tag["end"] = "}}\\\\"
render["line"] = tag
tag = {}
tag["begin"] = <<END
#{codecolumn}
#{numbercolumn}
\\begin{longtable}{NC}
END
tag["end"] = <<END
\\end{longtable}
END
render["listing"] = tag
tag = {}
tag["begin"] = <<END
\\documentclass[a4paper,landscape]{article}
\\usepackage{xcolor}
\\usepackage{colortbl}
\\usepackage{longtable}
\\usepackage[left=2cm,top=1cm,right=3cm,nohead,nofoot]{geometry}
\\usepackage[T1]{fontenc}
\\usepackage[scaled]{beramono}
\\begin{document}
END
tag["end"] = <<END
\\end{document}
END
render["document"] = tag
File.open( File.join( base_dir, "latex", "#{standard_name}.render" ), "w" ) {|f| YAML.dump( render, f ) }