Skip to content

Commit 2c63d09

Browse files
committed
coderay binary: list filetypes, styles
1 parent a06a8df commit 2c63d09

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

bin/coderay

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,35 @@ end
5454
def commands
5555
puts <<-COMMANDS
5656
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)
5959
6060
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)
6262
commands print this list
6363
help show some help
6464
version print CodeRay version
6565
COMMANDS
6666
end
6767

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+
6886
if option? '-v', '--version'
6987
version
7088
end
@@ -87,28 +105,32 @@ when 'highlight', nil
87105
when /^ff?$/
88106
input_file, output_file, = *names
89107
when /^f-f?$/
90-
input_file, output_filetype, output_file, = *names
108+
input_file, output_format, output_file, = *names
91109
when /^-ff?$/
92-
input_filetype, input_file, output_file, = *names
110+
input_lang, input_file, output_file, = *names
93111
when /^-f-f?$/
94-
input_filetype, input_file, output_filetype, output_file, = *names
112+
input_lang, input_file, output_format, output_file, = *names
95113
when /^--?f?$/
96-
input_filetype, output_filetype, output_file, = *names
114+
input_lang, output_format, output_file, = *names
97115
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
99121
end
100122

101123
if input_file
102-
input_filetype ||= CodeRay::FileType.fetch input_file, :text, true
124+
input_lang ||= CodeRay::FileType.fetch input_file, :text, true
103125
end
104126

105127
if output_file
106-
output_filetype ||= CodeRay::FileType[output_file]
128+
output_format ||= CodeRay::FileType[output_file]
107129
else
108-
output_filetype ||= :terminal
130+
output_format ||= :terminal
109131
end
110132

111-
output_filetype = :page if output_filetype.to_s == 'html'
133+
output_format = :page if output_format.to_s == 'html'
112134

113135
if input_file
114136
input = File.read input_file
@@ -124,42 +146,57 @@ when 'highlight', nil
124146
$stdout.sync = true
125147
$stdout
126148
end
127-
CodeRay.encode(input, input_filetype, output_filetype, :out => file)
149+
CodeRay.encode(input, input_lang, output_format, :out => file)
128150
file.puts
129151
rescue CodeRay::PluginHost::PluginNotFound => boom
152+
$stdout = $stderr
130153
if boom.message[/CodeRay::(\w+)s could not load plugin :?(.*?): /]
131154
puts "I don't know the #$1 \"#$2\"."
132155
else
133156
puts boom.message
134157
end
135158
# puts "I don't know this plugin: #{boom.message[/Could not load plugin (.*?): /, 1]}."
136159
rescue CodeRay::Scanners::Scanner::ScanError # FIXME: rescue Errno::EPIPE
137-
# ignore
160+
# this is sometimes raised by pagers; ignore [TODO: wtf?]
138161
ensure
139162
file.close
140163
end
141164
end
142-
when 'list'
165+
when 'li', 'list'
143166
arg = args.first && args.first.downcase
144167
if [nil, 's', 'sc', 'scanner', 'scanners'].include? arg
145168
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
152170
end
153171

154172
if [nil, 'e', 'en', 'enc', 'encoder', 'encoders'].include? arg
155173
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(', ')}"
159197
end
160-
puts encoders.sort
161198
end
162-
when 'stylesheet'
199+
when 'stylesheet', 'style', 'css'
163200
puts CodeRay::Encoders[:html]::CSS.new(args.first).stylesheet
164201
when 'commands'
165202
commands

0 commit comments

Comments
 (0)