Add bin and edit workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s

This commit is contained in:
2026-09-16 13:11:16 -06:00
parent c8ac4fcae5
commit 4cee170d66
17576 changed files with 895740 additions and 2 deletions
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
# Transforms a token stream into HTML output.
class HTML < Formatter
TABLE_FOR_ESCAPE_HTML = {
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
}.freeze
ESCAPE_REGEX = /[&<>]/.freeze
tag 'html'
# @yield the html output.
def stream(tokens, &b)
tokens.each { |tok, val| yield span(tok, val) }
end
def span(tok, val)
return val if escape?(tok)
safe_span(tok, escape_special_html_chars(val))
end
def safe_span(tok, safe_val)
if tok == Token::Tokens::Text
safe_val
else
shortname = tok.shortname or raise "unknown token: #{tok.inspect} for #{safe_val.inspect}"
"<span class=\"#{shortname}\">#{safe_val}</span>"
end
end
private
# A performance-oriented helper method to escape `&`, `<` and `>` for the rendered
# HTML from this formatter.
#
# `String#gsub` will always return a new string instance irrespective of whether
# a substitution occurs. This method however invokes `String#gsub` only if
# a substitution is imminent.
#
# Returns either the given `value` argument string as is or a new string with the
# special characters replaced with their escaped counterparts.
def escape_special_html_chars(value)
return value unless value =~ ESCAPE_REGEX
value.gsub(ESCAPE_REGEX, TABLE_FOR_ESCAPE_HTML)
end
end
end
end
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class HTMLInline < HTML
tag 'html_inline'
def initialize(theme)
if theme.is_a?(Class) && theme < Rouge::Theme
@theme = theme.new
elsif theme.is_a?(Rouge::Theme)
@theme = theme
elsif theme.is_a?(String)
@theme = Rouge::Theme.find(theme).new
else
raise ArgumentError, "invalid theme: #{theme.inspect}"
end
end
def safe_span(tok, safe_val)
return safe_val if tok == Token::Tokens::Text
rules = @theme.style_for(tok).rendered_rules
"<span style=\"#{rules.to_a.join(';')}\">#{safe_val}</span>"
end
end
end
end
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
# stdlib
require 'cgi'
module Rouge
module Formatters
# Transforms a token stream into HTML output.
class HTMLLegacy < Formatter
tag 'html_legacy'
# @option opts [String] :css_class ('highlight')
# @option opts [true/false] :line_numbers (false)
# @option opts [Rouge::CSSTheme] :inline_theme (nil)
# @option opts [true/false] :wrap (true)
#
# Initialize with options.
#
# If `:inline_theme` is given, then instead of rendering the
# tokens as <span> tags with CSS classes, the styles according to
# the given theme will be inlined in "style" attributes. This is
# useful for formats in which stylesheets are not available.
#
# Content will be wrapped in a tag (`div` if tableized, `pre` if
# not) with the given `:css_class` unless `:wrap` is set to `false`.
def initialize(opts={})
@formatter = opts[:inline_theme] ? HTMLInline.new(opts[:inline_theme])
: HTML.new
@formatter = HTMLTable.new(@formatter, opts) if opts[:line_numbers]
if opts.fetch(:wrap, true)
@formatter = HTMLPygments.new(@formatter, opts.fetch(:css_class, 'codehilite'))
end
end
# @yield the html output.
def stream(tokens, &b)
@formatter.stream(tokens, &b)
end
end
end
end
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class HTMLLineHighlighter < Formatter
tag 'html_line_highlighter'
def initialize(delegate, opts = {})
@delegate = delegate
@highlight_line_class = opts.fetch(:highlight_line_class, 'hll')
@highlight_lines = opts[:highlight_lines] || []
end
def stream(tokens)
token_lines(tokens).with_index(1) do |line_tokens, lineno|
line = %(#{@delegate.format(line_tokens)}\n)
line = %(<span class="#{@highlight_line_class}">#{line}</span>) if @highlight_lines.include? lineno
yield line
end
end
end
end
end
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class HTMLLineTable < Formatter
tag 'html_line_table'
# @param [Rouge::Formatters::Formatter] formatter An instance of a
# `Rouge::Formatters::HTML` or `Rouge::Formatters::HTMLInline`
# @param [Hash] opts options for HTMLLineTable instance.
# @option opts [Integer] :start_line line number to start from. Defaults to `1`.
# @option opts [String] :table_class Class name for the table.
# Defaults to `"rouge-line-table"`.
# @option opts [String] :line_id a `sprintf` template for generating an `id`
# attribute for each table row corresponding to current line number.
# Defaults to `"line-%i"`.
# @option opts [String] :line_class Class name for each table row.
# Defaults to `"lineno"`.
# @option opts [String] :gutter_class Class name for rendered line-number cell.
# Defaults to `"rouge-gutter"`.
# @option opts [String] :code_class Class name for rendered code cell.
# Defaults to `"rouge-code"`.
def initialize(formatter, opts={})
@formatter = formatter
@start_line = opts.fetch :start_line, 1
@table_class = opts.fetch :table_class, 'rouge-line-table'
@gutter_class = opts.fetch :gutter_class, 'rouge-gutter'
@code_class = opts.fetch :code_class, 'rouge-code'
@line_class = opts.fetch :line_class, 'lineno'
@line_id = opts.fetch :line_id, 'line-%i'
end
def stream(tokens, &b)
buffer = [%(<table class="#@table_class"><tbody>)]
token_lines(tokens).with_index(@start_line) do |line_tokens, lineno|
buffer << %(<tr id="#{sprintf @line_id, lineno}" class="#@line_class">)
buffer << %(<td class="#@gutter_class gl" )
buffer << %(style="-moz-user-select: none;-ms-user-select: none;)
buffer << %(-webkit-user-select: none;user-select: none;">)
buffer << %(<pre>#{lineno}</pre></td>)
buffer << %(<td class="#@code_class"><pre>)
@formatter.stream(line_tokens) { |formatted| buffer << formatted }
buffer << "\n</pre></td></tr>"
end
buffer << %(</tbody></table>)
yield buffer.join
end
end
end
end
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class HTMLLinewise < Formatter
def initialize(formatter, opts={})
@formatter = formatter
@tag_name = opts.fetch(:tag_name, 'div')
@class_format = opts.fetch(:class, 'line-%i')
end
def stream(tokens, &b)
token_lines(tokens).with_index(1) do |line_tokens, lineno|
yield %(<#{@tag_name} class="#{sprintf @class_format, lineno}">)
@formatter.stream(line_tokens) {|formatted| yield formatted }
yield %(\n</#{@tag_name}>)
end
end
end
end
end
@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Rouge
module Formatters
class HTMLPygments < Formatter
def initialize(inner, css_class='codehilite')
@inner = inner
@css_class = css_class
end
def stream(tokens, &b)
yield %(<div class="highlight"><pre class="#{@css_class}"><code>)
@inner.stream(tokens, &b)
yield "</code></pre></div>"
end
end
end
end
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class HTMLTable < Formatter
tag 'html_table'
def initialize(inner, opts={})
@inner = inner
@start_line = opts.fetch(:start_line, 1)
@line_format = opts.fetch(:line_format, '%i')
@table_class = opts.fetch(:table_class, 'rouge-table')
@gutter_class = opts.fetch(:gutter_class, 'rouge-gutter')
@code_class = opts.fetch(:code_class, 'rouge-code')
end
def style(scope)
yield %(#{scope} .rouge-table { border-spacing: 0 })
yield %(#{scope} .rouge-gutter { text-align: right })
end
def stream(tokens, &b)
last_val = nil
num_lines = tokens.reduce(0) {|count, (_, val)| count + (last_val = val).count(?\n) }
formatted = @inner.format(tokens)
unless last_val && last_val.end_with?(?\n)
num_lines += 1
formatted << ?\n
end
# generate a string of newline-separated line numbers for the gutter>
formatted_line_numbers = (@start_line..(@start_line + num_lines - 1)).map do |i|
sprintf(@line_format, i)
end.join(?\n) << ?\n
buffer = [%(<table class="#@table_class"><tbody><tr>)]
# the "gl" class applies the style for Generic.Lineno
buffer << %(<td class="#@gutter_class gl">)
buffer << %(<pre class="lineno">#{formatted_line_numbers}</pre>)
buffer << '</td>'
buffer << %(<td class="#@code_class"><pre>)
buffer << formatted
buffer << '</pre></td>'
buffer << '</tr></tbody></table>'
yield buffer.join
end
end
end
end
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
# A formatter which renders nothing.
class Null < Formatter
tag 'null'
def initialize(*)
end
def stream(tokens, &b)
tokens.each do |tok, val|
yield "#{tok.qualname} #{val.inspect}\n"
end
end
end
end
end
@@ -0,0 +1,198 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
# A formatter for 256-color terminals
class Terminal256 < Formatter
tag 'terminal256'
# @private
attr_reader :theme
# @param [Hash,Rouge::Theme] theme
# the theme to render with.
def initialize(theme = Themes::ThankfulEyes.new)
if theme.is_a?(Rouge::Theme)
@theme = theme
elsif theme.is_a?(Hash)
@theme = theme[:theme] || Themes::ThankfulEyes.new
else
raise ArgumentError, "invalid theme: #{theme.inspect}"
end
end
def stream(tokens, &b)
tokens.each do |tok, val|
escape_sequence(tok).stream_value(val, &b)
end
end
class EscapeSequence
attr_reader :style
def initialize(style)
@style = style
end
def self.xterm_colors
@xterm_colors ||= [].tap do |out|
# colors 0..15: 16 basic colors
out << [0x00, 0x00, 0x00] # 0
out << [0xcd, 0x00, 0x00] # 1
out << [0x00, 0xcd, 0x00] # 2
out << [0xcd, 0xcd, 0x00] # 3
out << [0x00, 0x00, 0xee] # 4
out << [0xcd, 0x00, 0xcd] # 5
out << [0x00, 0xcd, 0xcd] # 6
out << [0xe5, 0xe5, 0xe5] # 7
out << [0x7f, 0x7f, 0x7f] # 8
out << [0xff, 0x00, 0x00] # 9
out << [0x00, 0xff, 0x00] # 10
out << [0xff, 0xff, 0x00] # 11
out << [0x5c, 0x5c, 0xff] # 12
out << [0xff, 0x00, 0xff] # 13
out << [0x00, 0xff, 0xff] # 14
out << [0xff, 0xff, 0xff] # 15
# colors 16..232: the 6x6x6 color cube
valuerange = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]
217.times do |i|
r = valuerange[(i / 36) % 6]
g = valuerange[(i / 6) % 6]
b = valuerange[i % 6]
out << [r, g, b]
end
# colors 233..253: grayscale
1.upto 22 do |i|
v = 8 + i * 10
out << [v, v, v]
end
end
end
def fg
return @fg if instance_variable_defined? :@fg
@fg = style.fg && self.class.color_index(style.fg)
end
def bg
return @bg if instance_variable_defined? :@bg
@bg = style.bg && self.class.color_index(style.bg)
end
def stream_value(val, &b)
yield style_string
yield val.gsub("\e", "\\e")
.gsub("\n", "#{reset_string}\n#{style_string}")
yield reset_string
end
def style_string
@style_string ||= begin
attrs = []
attrs << ['38', '5', fg.to_s] if fg
attrs << ['48', '5', bg.to_s] if bg
attrs << '01' if style[:bold]
attrs << '04' if style[:italic] # underline, but hey, whatevs
escape(attrs)
end
end
def reset_string
@reset_string ||= begin
attrs = []
attrs << '39' if fg # fg reset
attrs << '49' if bg # bg reset
attrs << '00' if style[:bold] || style[:italic]
escape(attrs)
end
end
private
def escape(attrs)
return '' if attrs.empty?
"\e[#{attrs.join(';')}m"
end
def self.color_index(color)
@color_index_cache ||= {}
@color_index_cache[color] ||= closest_color(*get_rgb(color))
end
def self.get_rgb(color)
color = $1 if color =~ /#([0-9a-f]+)/i
hexes = case color.size
when 3
color.chars.map { |c| "#{c}#{c}" }
when 6
color.scan(/../)
else
raise "invalid color: #{color}"
end
hexes.map { |h| h.to_i(16) }
end
# max distance between two colors, #000000 to #ffffff
MAX_DISTANCE = 257 * 257 * 3
def self.closest_color(r, g, b)
@@colors_cache ||= {}
key = (r << 16) + (g << 8) + b
@@colors_cache.fetch(key) do
distance = MAX_DISTANCE
match = 0
xterm_colors.each_with_index do |(cr, cg, cb), i|
d = (r - cr)**2 + (g - cg)**2 + (b - cb)**2
next if d >= distance
match = i
distance = d
end
match
end
end
end
class Unescape < EscapeSequence
def initialize(*) end
def style_string(*) '' end
def reset_string(*) '' end
def stream_value(val) yield val end
end
# private
def escape_sequence(token)
return Unescape.new if escape?(token)
@escape_sequences ||= {}
@escape_sequences[token.qualname] ||=
make_escape_sequence(get_style(token))
end
def make_escape_sequence(style)
EscapeSequence.new(style)
end
def get_style(token)
return text_style if token.ancestors.include? Token::Tokens::Text
theme.get_own_style(token) || text_style
end
def text_style
style = theme.get_style(Token['Text'])
# don't highlight text backgrounds
style.delete :bg
style
end
end
end
end
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class TerminalTruecolor < Terminal256
tag 'terminal_truecolor'
class TruecolorEscapeSequence < Terminal256::EscapeSequence
def style_string
@style_string ||= begin
out = String.new('')
out << escape(['48', '2', *get_rgb(style.bg)]) if style.bg
out << escape(['38', '2', *get_rgb(style.fg)]) if style.fg
out << escape(['1']) if style[:bold] || style[:italic]
out
end
end
def get_rgb(color)
color = $1 if color =~ /#(\h+)/
case color.size
when 3 then color.chars.map { |c| c.to_i(16) * 2 }
when 6 then color.scan(/../).map { |cc| cc.to_i(16) }
else
raise "invalid color: #{color.inspect}"
end
end
end
# @override
def make_escape_sequence(style)
TruecolorEscapeSequence.new(style)
end
end
end
end
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Formatters
class Tex < Formatter
tag 'tex'
# A map of TeX escape characters.
# Newlines are handled specially by using #token_lines
# spaces are preserved as long as they aren't at the beginning
# of a line. see #tag_first for our initial-space strategy
ESCAPE = {
'&' => '\&',
'%' => '\%',
'$' => '\$',
'#' => '\#',
'_' => '\_',
'{' => '\{',
'}' => '\}',
'~' => '{\textasciitilde}',
'^' => '{\textasciicircum}',
'|' => '{\textbar}',
'\\' => '{\textbackslash}',
'`' => '{\textasciigrave}',
"'" => "'{}",
'"' => '"{}',
"\t" => '{\tab}',
}
ESCAPE_REGEX = /[#{ESCAPE.keys.map(&Regexp.method(:escape)).join}]/om
def initialize(opts={})
@prefix = opts.fetch(:prefix) { 'RG' }
end
def escape_tex(str)
str.gsub(ESCAPE_REGEX, ESCAPE)
end
def stream(tokens, &b)
# surround the output with \begin{RG*}...\end{RG*}
yield "\\begin{#{@prefix}*}%\n"
# we strip the newline off the last line to avoid
# an extra line being rendered. we do this by yielding
# the \newline tag *before* every line group except
# the first.
first = true
token_lines tokens do |line|
if first
first = false
else
yield "\\newline%\n"
end
render_line(line, &b)
end
yield "%\n\\end{#{@prefix}*}%\n"
end
def render_line(line, &b)
line.each do |(tok, val)|
hphantom_tag(tok, val, &b)
end
end
# Special handling for leading spaces, since they may be gobbled
# by a previous command. We replace all initial spaces with
# \hphantom{xxxx}, which renders an empty space equal to the size
# of the x's.
def hphantom_tag(tok, val)
leading = nil
val.sub!(/^[ ]+/) { leading = $&.size; '' }
yield "\\hphantom{#{'x' * leading}}" if leading
yield tag(tok, val) unless val.empty?
end
def tag(tok, val)
if escape?(tok)
val
elsif tok == Token::Tokens::Text
escape_tex(val)
else
"\\#@prefix{#{tok.shortname}}{#{escape_tex(val)}}"
end
end
end
end
end