forked from spox/ultraviolet
/
uv
executable file
·140 lines (105 loc) · 3.35 KB
/
uv
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env ruby
begin
require 'rubygems'
require 'uv'
rescue LoadError
$:.unshift File.join( File.dirname(__FILE__), '..', 'lib' )
require 'uv'
end
require 'optparse'
require 'ostruct'
o = OpenStruct.new
o.copy_files = false
o.output = "xhtml"
o.headers = false
o.theme = "espresso_libre"
o.lines = true
options = OptionParser.new
options.banner =<<END
Usage: uv [options] input_file
Parses input_file (or stdin, if no input_file is given) and
outputs the coresponding HTML to stdout. If no syntax is specified,
it tries to guess the best one. If no theme is specified, defaults
to espresso_libre.
Example:
uv -t amy -h ~/.bashrc > bashrc.html
Renders the contents of ~/.bashrc to file bashrc.html
as a standalone web page.
All options are non-mandatory
Options:
END
options.on( "-c DIR", "--copy-files DIR", <<DESCRIPTION ) {|val| o.copy_files = val}
Copy the required files to the specified output directory (css, images, etc).
DESCRIPTION
options.on( "-s SYNTAX", "--syntax SYNTAX", <<DESCRIPTION, Uv.syntaxes) {|val| o.syntax = val}
The file's syntax (e.g. ruby, c++, etc.)
DESCRIPTION
options.on( "-o OUTPUT", "--output OUTPUT", <<DESCRIPTION) {|val| o.output = val}
The output format (xhtml, latex, etc.) default to xhtml
DESCRIPTION
options.on( "-t THEME", "--theme THEME", <<DESCRIPTION, Uv.themes) {|val| o.theme = val}
The theme to be used (e.g. amy, espresso_libre, etc.)
DESCRIPTION
options.on( "-n", "--no-lines", <<DESCRIPTION ) {|val| o.lines = false}
Produces output without line numbers
DESCRIPTION
options.on( "-h", "--headers", <<DESCRIPTION ) {|val| o.headers = val}
Include headers, outputs a self-contained web page/document
DESCRIPTION
options.on( "-d", "--debug", <<DESCRIPTION ) {|val| o.debug = val}
Outputs debug information instead of normal page rendering
DESCRIPTION
options.on( "-l [syntaxes|themes]", "--list [syntaxes|themes]", ['syntaxes', 'themes'], <<DESCRIPTION ) {|val| o.list = val}
Lists all the available syntaxes/themes
DESCRIPTION
options.on( "-?", "--help", <<DESCRIPTION ) {|val| o.help = val}
Show this message
DESCRIPTION
rest = options.parse( ARGV )
if o.help
puts options
exit
elsif o.list
if o.list == 'syntaxes'
puts "Available syntaxes:\n"
Uv.syntaxes.sort.each{ |s| puts " - #{s}"}
elsif o.list == 'themes'
puts "Available themes:\n"
Uv.themes.sort.each{ |t| puts " - #{t}"}
else
STDERR.puts "Option #{o.list} is not valid should be one of [syntaxes, themes]"
end
exit
end
if o.copy_files
unless File.exists?(o.copy_files)
STDERR.puts "The specified output directory: #{o.copy_files} does not exist."
exit -1
end
Uv.copy_files o.output, o.copy_files
end
o.filename = rest[0]
if o.filename && ! o.syntax
candidates = Uv.syntax_for_file o.filename
if candidates.size > 1
STDERR.puts "Many syntaxes match, please specify"
STDERR.puts "\nMatching syntaxes:"
candidates.sort.each { |name, syntax| STDERR.puts "\t - " + name}
exit -1
end
o.syntax = candidates.first.first unless candidates.size == 0
end
unless o.syntax
STDERR.puts "No default syntax found, please specify"
exit -1
end
if o.filename
o.text = File.read(o.filename)
else
o.text = STDIN
end
if o.debug
Uv.debug( o.text, o.syntax )
exit
end
puts Uv.parse( o.text, o.output, o.syntax, o.lines, o.theme, o.headers )