Skip to content

Commit 0e8008a

Browse files
author
Etienne Massip
committed
Extracted code making HTML lines independent from numbering code to a specific option in encoder.
1 parent 185c422 commit 0e8008a

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

lib/coderay/encoders/html.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ module Encoders
4747
#
4848
# Default: 'CodeRay output'
4949
#
50+
# === :independent_lines
51+
# Split multilines blocks into line-wide blocks.
52+
# Forced to true if :line_numbers option is set to :inline.
53+
#
54+
# Default: false
55+
#
5056
# === :line_numbers
5157
# Include line numbers in :table, :inline, or nil (no line numbers)
5258
#
@@ -99,7 +105,8 @@ class HTML < Encoder
99105
:style => :alpha,
100106
:wrap => nil,
101107
:title => 'CodeRay output',
102-
108+
109+
:independent_lines => false,
103110
:line_numbers => nil,
104111
:line_number_anchors => 'n',
105112
:line_number_start => 1,
@@ -167,7 +174,11 @@ def setup options
167174
@real_out = @out
168175
@out = ''
169176
end
170-
177+
178+
options[:independent_lines] = true if options[:line_numbers] == :inline
179+
180+
@independent_lines = (options[:independent_lines] == true)
181+
171182
@HTML_ESCAPE = HTML_ESCAPE.dup
172183
@HTML_ESCAPE["\t"] = ' ' * options[:tab_width]
173184

@@ -245,13 +256,21 @@ def text_token text, kind
245256
if text =~ /#{HTML_ESCAPE_PATTERN}/o
246257
text = text.gsub(/#{HTML_ESCAPE_PATTERN}/o) { |m| @HTML_ESCAPE[m] }
247258
end
259+
if @independent_lines && @opened.any? && text.end_with?("\n")
260+
text.chomp!
261+
close_eol_reopen = "#{'</span>' * @opened.size}\n"
262+
@opened.each_with_index do |k, index|
263+
close_eol_reopen << (@span_for_kind[index > 0 ? [k, *@opened[0 ... index ]] : k] || '<span>')
264+
end
265+
end
248266
if style = @span_for_kind[@last_opened ? [kind, *@opened] : kind]
249267
@out << style << text << '</span>'
250268
else
251269
@out << text
252270
end
271+
@out << close_eol_reopen if close_eol_reopen
253272
end
254-
273+
255274
# token groups, eg. strings
256275
def begin_group kind
257276
@out << (@span_for_kind[@last_opened ? [kind, *@opened] : kind] || '<span>')
@@ -299,4 +318,4 @@ def end_line kind
299318
end
300319

301320
end
302-
end
321+
end

lib/coderay/encoders/html/numbering.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,11 @@ def self.number! output, mode = :table, options = {}
6868
when :inline
6969
max_width = (start + line_count).to_s.size
7070
line_number = start
71-
nesting = []
7271
output.gsub!(/^.*$\n?/) do |line|
73-
line.chomp!
74-
open = nesting.join
75-
line.scan(%r!<(/)?span[^>]*>?!) do |close,|
76-
if close
77-
nesting.pop
78-
else
79-
nesting << $&
80-
end
81-
end
82-
close = '</span>' * nesting.size
83-
8472
line_number_text = bolding.call line_number
8573
indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
8674
line_number += 1
87-
"<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{open}#{line}#{close}\n"
75+
"<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{line}"
8876
end
8977

9078
when :table
@@ -112,4 +100,4 @@ def self.number! output, mode = :table, options = {}
112100
end
113101

114102
end
115-
end
103+
end

0 commit comments

Comments
 (0)