Class: CodeRay::Encoders::HTML2
- Inherits:
-
HTML
- Object
- HTML
- CodeRay::Encoders::HTML2
- Defined in:
- lib/milkode/cdweb/lib/coderay_html2.rb
Class Method Summary collapse
- .create_anchor(options) ⇒ Object
-
.number!(output, mode = :table, options = {}) ⇒ Object
- ref
-
CodeRay::Encoders::Numberling#number! (coderay-1.0.5/lib/coderay/encoders/numbering.rb:8).
Instance Method Summary collapse
-
#finish(options) ⇒ Object
- ref
-
CodeRay::Encoders::HTML#finish (coderay-1.0.5/lib/coderay/encoders/html.rb:219).
- #line_attr(line, no, options) ⇒ Object
- #ornament_line_attr(options) ⇒ Object
- #text_token(text, kind) ⇒ Object
Class Method Details
.create_anchor(options) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/milkode/cdweb/lib/coderay_html2.rb', line 164 def self.create_anchor() anchor_prefix = [:line_number_anchors] anchor_prefix = 'line' if anchor_prefix == true anchor_prefix = anchor_prefix.to_s[/\w+/] if anchor_prefix if anchor_prefix anchor_url = [:line_number_anchor_url] || "" proc do |line| line = line.to_s anchor = anchor_prefix + line "<a href=\"#{anchor_url}##{anchor}\" name=\"#{anchor}\">#{line}</a>" end elsif [:onclick_copy_line_number] prefix = [:onclick_copy_prefix] || "" proc do |line| "<a href=\"#lineno-modal\" data-toggle=\"modal\" onclick=\"lineno_setup('#{prefix}', '#{line.to_s}');\" title=\"Display line number\">#{line.to_s}</a>" # "<a onclick=\"lineno_setup('#{prefix}', '#{line.to_s}');\" title=\"Display line number\">#{line.to_s}</a>" end else proc { |line| line.to_s } # :to_s.to_proc in Ruby 1.8.7+ end end |
.number!(output, mode = :table, options = {}) ⇒ Object
- ref
-
CodeRay::Encoders::Numberling#number! (coderay-1.0.5/lib/coderay/encoders/numbering.rb:8)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/milkode/cdweb/lib/coderay_html2.rb', line 74 def self.number! output, mode = :table, = {} return self unless mode = DEFAULT_OPTIONS.merge start = [:line_number_start] unless start.is_a? Integer raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start end anchoring = create_anchor() bold_every = [:bold_every] highlight_lines = [:highlight_lines] bolding = if bold_every == false && highlight_lines == nil anchoring elsif highlight_lines.is_a? Enumerable highlight_lines = highlight_lines.to_set proc do |line| if highlight_lines.include? line "<strong class=\"highlighted\">#{anchoring[line]}</strong>" # highlighted line numbers in bold else anchoring[line] end end elsif bold_every.is_a? Integer raise ArgumentError, ":bolding can't be 0." if bold_every == 0 proc do |line| if line % bold_every == 0 "<strong>#{anchoring[line]}</strong>" # every bold_every-th number in bold else anchoring[line] end end else raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every end line_count = output.count("\n") position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n) if position_of_last_newline after_last_newline = output[position_of_last_newline + 1 .. -1] ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/] line_count += 1 if not ends_with_newline end case mode when :inline max_width = (start + line_count).to_s.size line_number = start nesting = [] output.gsub!(/^.*$\n?/) do |line| line.chomp! open = nesting.join line.scan(%r!<(/)?span[^>]*>?!) do |close,| if close nesting.pop else nesting << $& end end close = '</span>' * nesting.size line_number_text = bolding.call line_number indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x) line_number += 1 "<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{open}#{line}#{close}\n" end when :table line_numbers = (start ... start + line_count).map(&bolding).join("\n") line_numbers << "\n" line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers) output.gsub!(/<\/div>\n/, '</div>') output.wrap_in! line_numbers_table_template output.wrapped_in = :div when :list raise NotImplementedError, 'The :list option is no longer available. Use :table.' else raise ArgumentError, 'Unknown value %p for mode: expected one of %p' % [mode, [:table, :inline]] end output end |
Instance Method Details
#finish(options) ⇒ Object
- ref
-
CodeRay::Encoders::HTML#finish (coderay-1.0.5/lib/coderay/encoders/html.rb:219)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/milkode/cdweb/lib/coderay_html2.rb', line 23 def finish @out = ornament_line_attr() unless @opened.empty? warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG @out << '</span>' while @opened.pop @last_opened = nil end @out.extend Output @out.css = @css if [:line_numbers] # Numbering.number! @out, options[:line_numbers], options HTML2::number! @out, [:line_numbers], end @out.wrap! [:wrap] @out.apply_title! [:title] if defined?(@real_out) && @real_out @real_out << @out @out = @real_out end @out end |
#line_attr(line, no, options) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/milkode/cdweb/lib/coderay_html2.rb', line 59 def line_attr(line, no, ) # p options is_highlight = true if [:highlight_lines].include?(no) r = [] r << "id=\"n#{no}\"" r << "class=\"highlight-line\"" if is_highlight attr = r.join(" ") line = Milkode::Util.highlight_keywords(line, [:keywords], 'highlight-filename') if is_highlight "<span #{attr}>#{line}</span>" end |
#ornament_line_attr(options) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/milkode/cdweb/lib/coderay_html2.rb', line 49 def ornament_line_attr() line_number = [:line_number_start] lines = @out.split("\n") lines.map{|l| line_number += 1 line_attr(l, line_number - 1, ) }.join("\n") + "\n" end |
#text_token(text, kind) ⇒ Object
17 18 19 20 |
# File 'lib/milkode/cdweb/lib/coderay_html2.rb', line 17 def text_token text, kind # p "#{kind}: #{text}" super end |