tartrazine/src/main.cr

98 lines
2.7 KiB
Crystal
Raw Normal View History

2024-08-11 14:49:42 +00:00
require "docopt"
require "./tartrazine"
2024-08-09 19:53:24 +00:00
HELP = <<-HELP
tartrazine: a syntax highlighting tool
Usage:
2024-08-11 14:49:42 +00:00
tartrazine (-h, --help)
tartrazine FILE -f html [-t theme][--standalone][--line-numbers]
[-l lexer][-o output]
2024-08-11 14:49:42 +00:00
tartrazine -f html -t theme --css
tartrazine FILE -f terminal [-t theme][-l lexer][--line-numbers]
[-o output]
2024-08-09 19:53:24 +00:00
tartrazine FILE -f json [-o output]
tartrazine --list-themes
tartrazine --list-lexers
2024-08-11 14:49:42 +00:00
tartrazine --list-formatters
tartrazine --version
2024-08-09 19:53:24 +00:00
2024-08-11 14:49:42 +00:00
Options:
-f <formatter> Format to use (html, terminal, json)
-t <theme> Theme to use, see --list-themes [default: default-dark]
-l <lexer> Lexer (language) to use, see --list-lexers. Use more than
one lexer with "+" (e.g. jinja+yaml) [default: autodetect]
2024-08-11 14:49:42 +00:00
-o <output> Output file. Default is stdout.
--standalone Generate a standalone HTML file, which includes
all style information. If not given, it will generate just
a HTML fragment ready to include in your own page.
--css Generate a CSS file for the theme called <theme>.css
--line-numbers Include line numbers in the output
-h, --help Show this screen
-v, --version Show version number
2024-08-09 19:53:24 +00:00
HELP
2024-08-11 14:49:42 +00:00
options = Docopt.docopt(HELP, ARGV)
# Handle version manually
if options["--version"]
puts "tartrazine #{Tartrazine::VERSION}"
exit 0
end
if options["--list-themes"]
puts Tartrazine.themes.join("\n")
exit 0
end
if options["--list-lexers"]
puts Tartrazine.lexers.join("\n")
exit 0
end
if options["--list-formatters"]
puts "html\njson\nterminal"
exit 0
end
theme = Tartrazine.theme(options["-t"].as(String))
2024-08-11 14:49:42 +00:00
if options["-f"]
formatter = options["-f"].as(String)
case formatter
when "html"
formatter = Tartrazine::Html.new
formatter.standalone = options["--standalone"] != nil
formatter.line_numbers = options["--line-numbers"] != nil
formatter.theme = theme
2024-08-11 14:49:42 +00:00
when "terminal"
formatter = Tartrazine::Ansi.new
formatter.line_numbers = options["--line-numbers"] != nil
formatter.theme = theme
2024-08-11 14:49:42 +00:00
when "json"
formatter = Tartrazine::Json.new
else
puts "Invalid formatter: #{formatter}"
exit 1
end
if formatter.is_a?(Tartrazine::Html) && options["--css"]
File.open("#{options["-t"].as(String)}.css", "w") do |outf|
2024-08-15 20:05:03 +00:00
outf << formatter.style_defs
2024-08-11 14:49:42 +00:00
end
exit 0
end
2024-08-11 16:04:35 +00:00
lexer = Tartrazine.lexer(name: options["-l"].as(String), filename: options["FILE"].as(String))
2024-08-24 22:59:05 +00:00
2024-08-11 14:49:42 +00:00
input = File.open(options["FILE"].as(String)).gets_to_end
if options["-o"].nil?
2024-08-16 20:25:33 +00:00
outf = STDOUT
2024-08-11 14:49:42 +00:00
else
2024-08-16 20:25:33 +00:00
outf = File.open(options["-o"].as(String), "w")
2024-08-11 14:49:42 +00:00
end
2024-08-16 20:25:33 +00:00
formatter.format(input, lexer, outf)
2024-08-22 18:00:17 +00:00
outf.close
2024-08-11 14:49:42 +00:00
end