54
54
def commands
55
55
puts <<-COMMANDS
56
56
general:
57
- highlight code highlighting (default command)
58
- stylesheet print the CSS stylesheet with the given name
57
+ highlight code highlighting (default command, optional )
58
+ stylesheet print the CSS stylesheet with the given name (aliases: style, css)
59
59
60
60
about:
61
- list [of] list all available plugins (or just the scanners|encoders)
61
+ list [of] list all available plugins (or just the scanners|encoders|styles|filetypes )
62
62
commands print this list
63
63
help show some help
64
64
version print CodeRay version
65
65
COMMANDS
66
66
end
67
67
68
+ def print_list_of plugin_host
69
+ plugins = plugin_host . all_plugins . map do |plugin |
70
+ info = " #{ plugin . plugin_id } : #{ plugin . title } "
71
+
72
+ aliases = ( plugin . aliases - [ :default ] ) . map { |key | "-#{ key } " } . sort_by { |key | key . size }
73
+ if plugin . respond_to? ( :file_extension ) || !aliases . empty?
74
+ additional_info = [ ]
75
+ additional_info << aliases . join ( ', ' ) unless aliases . empty?
76
+ info << " (#{ additional_info . join ( '; ' ) } )"
77
+ end
78
+
79
+ info << ' <-- default' if plugin . aliases . include? :default
80
+
81
+ info
82
+ end
83
+ puts plugins . sort
84
+ end
85
+
68
86
if option? '-v' , '--version'
69
87
version
70
88
end
@@ -87,28 +105,32 @@ when 'highlight', nil
87
105
when /^ff?$/
88
106
input_file , output_file , = *names
89
107
when /^f-f?$/
90
- input_file , output_filetype , output_file , = *names
108
+ input_file , output_format , output_file , = *names
91
109
when /^-ff?$/
92
- input_filetype , input_file , output_file , = *names
110
+ input_lang , input_file , output_file , = *names
93
111
when /^-f-f?$/
94
- input_filetype , input_file , output_filetype , output_file , = *names
112
+ input_lang , input_file , output_format , output_file , = *names
95
113
when /^--?f?$/
96
- input_filetype , output_filetype , output_file , = *names
114
+ input_lang , output_format , output_file , = *names
97
115
else
98
- raise signature
116
+ $stdout = $stderr
117
+ help
118
+ puts
119
+ puts "Unknown parameter order: #{ args . join ' ' } , expected: [-language] [input] [-format] [output]"
120
+ exit 1
99
121
end
100
122
101
123
if input_file
102
- input_filetype ||= CodeRay ::FileType . fetch input_file , :text , true
124
+ input_lang ||= CodeRay ::FileType . fetch input_file , :text , true
103
125
end
104
126
105
127
if output_file
106
- output_filetype ||= CodeRay ::FileType [ output_file ]
128
+ output_format ||= CodeRay ::FileType [ output_file ]
107
129
else
108
- output_filetype ||= :terminal
130
+ output_format ||= :terminal
109
131
end
110
132
111
- output_filetype = :page if output_filetype . to_s == 'html'
133
+ output_format = :page if output_format . to_s == 'html'
112
134
113
135
if input_file
114
136
input = File . read input_file
@@ -124,42 +146,57 @@ when 'highlight', nil
124
146
$stdout. sync = true
125
147
$stdout
126
148
end
127
- CodeRay . encode ( input , input_filetype , output_filetype , :out => file )
149
+ CodeRay . encode ( input , input_lang , output_format , :out => file )
128
150
file . puts
129
151
rescue CodeRay ::PluginHost ::PluginNotFound => boom
152
+ $stdout = $stderr
130
153
if boom . message [ /CodeRay::(\w +)s could not load plugin :?(.*?): / ]
131
154
puts "I don't know the #$1 \" #$2\" ."
132
155
else
133
156
puts boom . message
134
157
end
135
158
# puts "I don't know this plugin: #{boom.message[/Could not load plugin (.*?): /, 1]}."
136
159
rescue CodeRay ::Scanners ::Scanner ::ScanError # FIXME: rescue Errno::EPIPE
137
- # ignore
160
+ # this is sometimes raised by pagers; ignore [TODO: wtf?]
138
161
ensure
139
162
file . close
140
163
end
141
164
end
142
- when 'list'
165
+ when 'li' , ' list'
143
166
arg = args . first && args . first . downcase
144
167
if [ nil , 's' , 'sc' , 'scanner' , 'scanners' ] . include? arg
145
168
puts 'input languages (Scanners):'
146
- scanners = CodeRay ::Scanners . all_plugins . map do |plugin |
147
- aliases = ( plugin . aliases - [ nil ] ) . map { |key | "-#{ key } " } . sort_by { |key | key . size }
148
- " #{ plugin . lang } : #{ plugin . title } #{ " (.#{ plugin . file_extension } ; #{ aliases . join ( ', ' ) } )" unless aliases . empty? } "
149
- end
150
- puts scanners . sort
151
- puts
169
+ print_list_of CodeRay ::Scanners
152
170
end
153
171
154
172
if [ nil , 'e' , 'en' , 'enc' , 'encoder' , 'encoders' ] . include? arg
155
173
puts 'output formats (Encoders):'
156
- encoders = CodeRay ::Encoders . all_plugins . map do |plugin |
157
- aliases = ( plugin . aliases - [ nil ] ) . map { |key | "-#{ key } " } . sort_by { |key | key . size }
158
- " #{ plugin . plugin_id } : #{ plugin . title } #{ " (.#{ plugin . file_extension } ; #{ aliases . join ( ', ' ) } )" unless aliases . empty? } "
174
+ print_list_of CodeRay ::Encoders
175
+ end
176
+
177
+ if [ nil , 'st' , 'style' , 'styles' ] . include? arg
178
+ puts 'CSS themes for HTML output (Styles):'
179
+ print_list_of CodeRay ::Styles
180
+ end
181
+
182
+ if [ nil , 'f' , 'ft' , 'file' , 'filetype' , 'filetypes' ] . include? arg
183
+ puts 'recognized file types:'
184
+
185
+ filetypes = Hash . new { |h , k | h [ k ] = [ ] }
186
+ CodeRay ::FileType ::TypeFromExt . inject filetypes do |types , ( ext , type ) |
187
+ types [ type . to_s ] << ".#{ ext } "
188
+ types
189
+ end
190
+ CodeRay ::FileType ::TypeFromName . inject filetypes do |types , ( name , type ) |
191
+ types [ type . to_s ] << name
192
+ types
193
+ end
194
+
195
+ filetypes . sort . each do |type , exts |
196
+ puts " #{ type } : #{ exts . sort_by { |ext | ext . size } . join ( ', ' ) } "
159
197
end
160
- puts encoders . sort
161
198
end
162
- when 'stylesheet'
199
+ when 'stylesheet' , 'style' , 'css'
163
200
puts CodeRay ::Encoders [ :html ] ::CSS . new ( args . first ) . stylesheet
164
201
when 'commands'
165
202
commands
0 commit comments