From bb952a44b8f52884e195af419a6bf3005655e163 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 16 Aug 2024 17:25:33 -0300 Subject: [PATCH] Use IO for output --- src/formatters/ansi.cr | 30 ++++++-------------------- src/formatters/html.cr | 49 ++++++++++++++++++------------------------ src/formatters/json.cr | 5 +++-- src/main.cr | 8 +++---- 4 files changed, 34 insertions(+), 58 deletions(-) diff --git a/src/formatters/ansi.cr b/src/formatters/ansi.cr index ea86790..c1bea9a 100644 --- a/src/formatters/ansi.cr +++ b/src/formatters/ansi.cr @@ -11,35 +11,19 @@ module Tartrazine "#{i + 1}".rjust(4).ljust(5) end - def format(text : String, lexer : Lexer) : String + def format(text : String, lexer : Lexer, outp : IO) : Nil tokenizer = Tokenizer.new(lexer, text) i = 0 - output = String.build do |outp| - outp << line_label(i) if line_numbers? - tokenizer.each do |token| - outp << colorize(token[:value], token[:type]) - if token[:value].includes?("\n") - i += 1 - outp << line_label(i) if line_numbers? - end + outp << line_label(i) if line_numbers? + tokenizer.each do |token| + outp << colorize(token[:value], token[:type]) + if token[:value].includes?("\n") + i += 1 + outp << line_label(i) if line_numbers? end end - output end - # def format(text : String, lexer : Lexer) : String - # output = String.build do |outp| - # lexer.group_tokens_in_lines(lexer.tokenize(text)).each_with_index do |line, i| - # label = line_numbers? ? "#{i + 1}".rjust(4).ljust(5) : "" - # outp << label - # line.each do |token| - # outp << colorize(token[:value], token[:type]) - # end - # end - # end - # output - # end - def colorize(text : String, token : String) : String style = theme.styles.fetch(token, nil) return text if style.nil? diff --git a/src/formatters/html.cr b/src/formatters/html.cr index 87105d3..3b994d1 100644 --- a/src/formatters/html.cr +++ b/src/formatters/html.cr @@ -34,24 +34,21 @@ module Tartrazine @weight_of_bold : Int32 = 600) end - def format(text : String, lexer : Lexer) : String - text = format_text(text, lexer) - if standalone? - text = wrap_standalone(text) - end - text + def format(text : String, lexer : Lexer, dst : IO) : Nil + pre, post = wrap_standalone + dst << pre if standalone? + format_text(text, lexer, dst) + dst << post if standalone? end # Wrap text into a full HTML document, including the CSS for the theme - def wrap_standalone(text) : String + def wrap_standalone output = String.build do |outp| outp << "" - outp << text - outp << "" end - output + {output.to_s, ""} end private def line_label(i : Int32) : String @@ -61,27 +58,23 @@ module Tartrazine "#{line_label} " end - def format_text(text : String, lexer : Lexer) : String - # lines = lexer.group_tokens_in_lines(lexer.tokenize(text)) + def format_text(text : String, lexer : Lexer, outp : IO) : Nil tokenizer = Tokenizer.new(lexer, text) i = 0 - output = String.build do |outp| - if surrounding_pre? - pre_style = wrap_long_lines? ? "style=\"white-space: pre-wrap; word-break: break-word;\"" : "" - outp << "
"
-        end
-        outp << ""
-        outp << line_label(i) if line_numbers?
-        tokenizer.each do |token|
-          outp << "#{HTML.escape(token[:value])}"
-          if token[:value].ends_with? "\n"
-            i += 1
-            outp << line_label(i) if line_numbers?
-          end
-        end
-        outp << "
" + if surrounding_pre? + pre_style = wrap_long_lines? ? "style=\"white-space: pre-wrap; word-break: break-word;\"" : "" + outp << "
"
       end
-      output
+      outp << ""
+      outp << line_label(i) if line_numbers?
+      tokenizer.each do |token|
+        outp << "#{HTML.escape(token[:value])}"
+        if token[:value].ends_with? "\n"
+          i += 1
+          outp << line_label(i) if line_numbers?
+        end
+      end
+      outp << "
" end # ameba:disable Metrics/CyclomaticComplexity diff --git a/src/formatters/json.cr b/src/formatters/json.cr index 25604b5..9e03d21 100644 --- a/src/formatters/json.cr +++ b/src/formatters/json.cr @@ -4,8 +4,9 @@ module Tartrazine class Json < Formatter property name = "json" - def format(text : String, lexer : Lexer, _theme : Theme) : String - lexer.tokenize(text).to_json + def format(text : String, lexer : Lexer, outp : IO) : Nil + tokenizer = Tokenizer.new(lexer, text) + outp << Tartrazine::Lexer.collapse_tokens(tokenizer.to_a).to_json end end end diff --git a/src/main.cr b/src/main.cr index 4b517c6..116768e 100644 --- a/src/main.cr +++ b/src/main.cr @@ -85,13 +85,11 @@ if options["-f"] lexer = Tartrazine.lexer(name: options["-l"].as(String), filename: options["FILE"].as(String)) input = File.open(options["FILE"].as(String)).gets_to_end - output = formatter.format(input, lexer) if options["-o"].nil? - puts output + outf = STDOUT else - File.open(options["-o"].as(String), "w") do |outf| - outf << output - end + outf = File.open(options["-o"].as(String), "w") end + formatter.format(input, lexer, outf) end