From f98f44365fe6fe10d719910e42bf59544ff00377 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 9 Aug 2024 14:00:42 -0300 Subject: [PATCH] HTML formatter option: line_numbers / highlight_lines --- scripts/token_abbrevs.py | 2 +- src/constants.cr | 1 + src/formatters/html.cr | 14 ++++++++++---- src/main.cr | 3 ++- src/styles.cr | 1 + 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/scripts/token_abbrevs.py b/scripts/token_abbrevs.py index 2b0c91b..e8b7f11 100644 --- a/scripts/token_abbrevs.py +++ b/scripts/token_abbrevs.py @@ -8,7 +8,7 @@ def abbr(line): return "".join(c for c in line if c in string.ascii_uppercase).lower() abbrevs = {} -tokens = set([]) +tokens = {"Highlight"} for line in sys.stdin: if " "b", + "Highlight" => "h", "Text" => "t", "CommentSingle" => "cs", "CommentSpecial" => "cs", diff --git a/src/formatters/html.cr b/src/formatters/html.cr index e552d50..d477d56 100644 --- a/src/formatters/html.cr +++ b/src/formatters/html.cr @@ -12,13 +12,13 @@ module Tartrazine # property surrounding_pre : Bool = true # property wrap_long_lines : Bool = false - # property line_numbers : Bool = false + property? line_numbers : Bool = false property line_number_start : Int32 = 1 property line_number_id_prefix : String = "line-" # property line_number_in_table : Bool = false # property linkable_line_numbers : Bool = false - # property highlight_lines : Array(Range(Int32, Int32)) = [] of Range(Int32, Int32) + 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) @@ -45,12 +45,14 @@ module Tartrazine output = String.build do |outp| outp << "
"
         lines.each_with_index(offset: line_number_start - 1) do |line, i|
-          outp << ""
+          line_label = line_numbers? ? "#{i + 1}".rjust(4).ljust(5) : ""
+
+          line_class = highlighted?(i + 1) ? "class=\"#{get_css_class("Highlight", theme)}\"" : ""
+          outp << "#{line_label}"
           line.each do |token|
             fragment = "#{token[:value]}"
             outp << fragment
           end
-          outp << ""
         end
         outp << "
" end @@ -96,6 +98,10 @@ module Tartrazine }] end + def highlighted?(line : Int) : Bool + highlight_lines.any?(&.includes?(line)) + end + def group_tokens_in_lines(tokens : Array(Token)) : Array(Array(Token)) split_tokens = [] of Token tokens.each do |token| diff --git a/src/main.cr b/src/main.cr index 949aa0e..fecdcf8 100644 --- a/src/main.cr +++ b/src/main.cr @@ -6,5 +6,6 @@ formatter = Tartrazine::Html.new formatter.standalone = true formatter.class_prefix = "hl-" formatter.line_number_id_prefix = "ln-" -formatter.line_number_start = 3 +formatter.line_numbers = true +formatter.highlight_lines = [3..7, 20..30] puts formatter.format(File.read(ARGV[0]), lexer, theme) diff --git a/src/styles.cr b/src/styles.cr index f9c2a9e..2120223 100644 --- a/src/styles.cr +++ b/src/styles.cr @@ -104,6 +104,7 @@ module Tartrazine # https://github.com/mohd-akram/base16-pygments/ theme.styles["Background"] = Style.new(color: t["base05"], background: t["base00"]) + theme.styles["Highlight"] = Style.new(color: t["base0D"], background: t["base01"]) theme.styles["Text"] = Style.new(color: t["base05"]) theme.styles["Error"] = Style.new(color: t["base08"]) theme.styles["Comment"] = Style.new(color: t["base03"])