JSON formatter

This commit is contained in:
Roberto Alsina 2024-08-09 16:58:15 -03:00
parent 89d212b71c
commit 84ee7e6934
5 changed files with 39 additions and 23 deletions

View File

@ -15,6 +15,8 @@ dependencies:
github: crystal-china/base58.cr github: crystal-china/base58.cr
sixteen: sixteen:
github: ralsina/sixteen github: ralsina/sixteen
docopt:
github: chenkovsky/docopt.cr
crystal: ">= 1.13.0" crystal: ">= 1.13.0"

View File

@ -2,23 +2,18 @@ require "../formatter"
module Tartrazine module Tartrazine
class Html < Formatter class Html < Formatter
# Not all of these options are implemented # property line_number_in_table : Bool = false
property? standalone : Bool = false
property class_prefix : String = ""
# property with_classes : Bool = true # 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 tab_width = 8
property? line_numbers : Bool = false
property? linkable_line_numbers : Bool = true
property? standalone : Bool = false
property? surrounding_pre : Bool = true property? surrounding_pre : Bool = true
property? wrap_long_lines : Bool = false 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 def format(text : String, lexer : Lexer, theme : Theme) : String
text = format_text(text, lexer, theme) text = format_text(text, lexer, theme)

11
src/formatters/json.cr Normal file
View File

@ -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

View File

@ -23,12 +23,12 @@ HELP
lexer = Tartrazine.lexer("crystal") lexer = Tartrazine.lexer("crystal")
theme = Tartrazine.theme(ARGV[1]) theme = Tartrazine.theme(ARGV[1])
formatter = Tartrazine::Html.new formatter = Tartrazine::Json.new
formatter.standalone = true # formatter.standalone = true
formatter.class_prefix = "hl-" # formatter.class_prefix = "hl-"
formatter.line_number_id_prefix = "ln-" # formatter.line_number_id_prefix = "ln-"
formatter.line_numbers = true # formatter.line_numbers = true
formatter.highlight_lines = [3..7, 20..30] # formatter.highlight_lines = [3..7, 20..30]
formatter.linkable_line_numbers = false # formatter.linkable_line_numbers = false
formatter.wrap_long_lines = false # formatter.wrap_long_lines = false
puts formatter.format(File.read(ARGV[0]), lexer, theme) puts formatter.format(File.read(ARGV[0]), lexer, theme)

View File

@ -11,8 +11,16 @@ module Tartrazine
alias Color = Sixteen::Color alias Color = Sixteen::Color
def self.theme(name : String) : Theme def self.theme(name : String) : Theme
return Theme.from_base16(name[7..]) if name.starts_with? "base16_" begin
Theme.from_xml(ThemeFiles.get("/#{name}.xml").gets_to_end) 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 end
class ThemeFiles class ThemeFiles