From 84ee7e6934544c1ec963c758df24f0c23d943d82 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 9 Aug 2024 16:58:15 -0300 Subject: [PATCH] JSON formatter --- shard.yml | 2 ++ src/formatters/html.cr | 21 ++++++++------------- src/formatters/json.cr | 11 +++++++++++ src/main.cr | 16 ++++++++-------- src/styles.cr | 12 ++++++++++-- 5 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 src/formatters/json.cr diff --git a/shard.yml b/shard.yml index 41cdd3a..4fa51e7 100644 --- a/shard.yml +++ b/shard.yml @@ -15,6 +15,8 @@ dependencies: github: crystal-china/base58.cr sixteen: github: ralsina/sixteen + docopt: + github: chenkovsky/docopt.cr crystal: ">= 1.13.0" diff --git a/src/formatters/html.cr b/src/formatters/html.cr index 216e7e0..8c4abf5 100644 --- a/src/formatters/html.cr +++ b/src/formatters/html.cr @@ -2,23 +2,18 @@ require "../formatter" module Tartrazine class Html < Formatter - # Not all of these options are implemented - - property? standalone : Bool = false - property class_prefix : String = "" - + # property line_number_in_table : Bool = false # property with_classes : Bool = true + property class_prefix : String = "" + property highlight_lines : Array(Range(Int32, Int32)) = [] of Range(Int32, Int32) + property line_number_id_prefix : String = "line-" + property line_number_start : Int32 = 1 property tab_width = 8 - + property? line_numbers : Bool = false + property? linkable_line_numbers : Bool = true + property? standalone : Bool = false property? surrounding_pre : Bool = true property? wrap_long_lines : Bool = false - property? line_numbers : Bool = false - property line_number_start : Int32 = 1 - property line_number_id_prefix : String = "line-" - property? linkable_line_numbers : Bool = true - - # property line_number_in_table : Bool = false - property highlight_lines : Array(Range(Int32, Int32)) = [] of Range(Int32, Int32) def format(text : String, lexer : Lexer, theme : Theme) : String text = format_text(text, lexer, theme) diff --git a/src/formatters/json.cr b/src/formatters/json.cr new file mode 100644 index 0000000..25604b5 --- /dev/null +++ b/src/formatters/json.cr @@ -0,0 +1,11 @@ +require "../formatter" + +module Tartrazine + class Json < Formatter + property name = "json" + + def format(text : String, lexer : Lexer, _theme : Theme) : String + lexer.tokenize(text).to_json + end + end +end diff --git a/src/main.cr b/src/main.cr index c7369bc..515ad49 100644 --- a/src/main.cr +++ b/src/main.cr @@ -23,12 +23,12 @@ HELP lexer = Tartrazine.lexer("crystal") theme = Tartrazine.theme(ARGV[1]) -formatter = Tartrazine::Html.new -formatter.standalone = true -formatter.class_prefix = "hl-" -formatter.line_number_id_prefix = "ln-" -formatter.line_numbers = true -formatter.highlight_lines = [3..7, 20..30] -formatter.linkable_line_numbers = false -formatter.wrap_long_lines = false +formatter = Tartrazine::Json.new +# formatter.standalone = true +# formatter.class_prefix = "hl-" +# formatter.line_number_id_prefix = "ln-" +# formatter.line_numbers = true +# formatter.highlight_lines = [3..7, 20..30] +# formatter.linkable_line_numbers = false +# formatter.wrap_long_lines = false puts formatter.format(File.read(ARGV[0]), lexer, theme) diff --git a/src/styles.cr b/src/styles.cr index 9b2686d..31921a5 100644 --- a/src/styles.cr +++ b/src/styles.cr @@ -11,8 +11,16 @@ module Tartrazine alias Color = Sixteen::Color def self.theme(name : String) : Theme - return Theme.from_base16(name[7..]) if name.starts_with? "base16_" - Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end) + begin + return Theme.from_base16(name) + rescue ex : Exception + raise ex unless ex.message.try &.includes? "Theme not found" + end + begin + return Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end) + rescue + raise Exception.new("Theme #{name} not found") + end end class ThemeFiles