Class: VivlioPack::Renderer
- Inherits:
-
Redcarpet::Render::HTML
- Object
- Redcarpet::Render::HTML
- VivlioPack::Renderer
- Includes:
- Rouge::Plugins::Redcarpet
- Defined in:
- lib/vivlio_pack/renderer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #block_code(code, language) ⇒ Object
- #header(c, level) ⇒ Object
- #hrule ⇒ Object
- #image(link, title, alt_text) ⇒ Object
-
#initialize ⇒ Renderer
constructor
A new instance of Renderer.
- #onepoint(text) ⇒ Object
- #paragraph(text) ⇒ Object
Constructor Details
#initialize ⇒ Renderer
Returns a new instance of Renderer.
17 18 19 20 21 22 23 24 |
# File 'lib/vivlio_pack/renderer.rb', line 17 def initialize super @sec = 0 @ch = 0 @para = 0 @state = [] @ring_buffer = [] end |
Class Method Details
.add_plugin(name, mod) ⇒ Object
9 10 11 |
# File 'lib/vivlio_pack/renderer.rb', line 9 def self.add_plugin(name, mod) (@plugins ||= {})[name] = mod end |
.plugin(name) ⇒ Object
13 14 15 |
# File 'lib/vivlio_pack/renderer.rb', line 13 def self.plugin(name) @plugins[name] end |
Instance Method Details
#block_code(code, language) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/vivlio_pack/renderer.rb', line 96 def block_code(code, language) if @in_file @in_file = false lineno = @file_lineno || 1 lexer = Rouge::Lexer.find_fancy(language) || Rouge::Lexers::PlainText.new formatter = Rouge::Formatters::HTML.new lines = code.split("\n") numbered_lines = lines.each_with_index.map do |line, i| highlighted_line = formatter.format(lexer.lex(line)) "<span class='code-line'><span class='line-number'>#{(lineno + i).to_s.rjust(4)}</span>#{highlighted_line}</span>" end "<div class='file'><div class='filename'>#{@file_filename}:#{lineno}</div><pre class='highlight'><code>#{numbered_lines.join}</code></pre></div>\n" else super end end |
#header(c, level) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vivlio_pack/renderer.rb', line 26 def header(c, level) case level when 1 @sec += 1 @ch = 0 @para = 0 "<h1 id='sec#{@sec}'>#{c}</h1>\n\n" when 2 @ch += 1 @para = 0 "<h2 id='ch#{@sec}-#{@ch}'>#{c}</h2>\n\n" when 3 @para += 1 "<h3 id='para#{@sec}-#{@ch}-#{@para}'>#{c}</h3>\n\n" when 4 "<h4>#{c}</h4>\n\n" end end |
#hrule ⇒ Object
130 131 132 |
# File 'lib/vivlio_pack/renderer.rb', line 130 def hrule "<div class='page-break'></div>\n" end |
#image(link, title, alt_text) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/vivlio_pack/renderer.rb', line 113 def image(link, title, alt_text) name = File.basename(link, ".*") size_class = "" caption = alt_text if alt_text =~ /\A(.+?)\s*"(\d+)%"\s*\z/ caption = $1.strip size_class = "img-#{$2}" end figure_attr = size_class.empty? ? "" : " class=\"#{size_class}\"" <<~HTML <figure#{figure_attr}> <img src="contents/images/#{link}" alt="#{caption}" /> <figcaption id="#{name}">#{caption}</figcaption> </figure> HTML end |
#onepoint(text) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/vivlio_pack/renderer.rb', line 134 def onepoint(text) case text.strip when "[begin one-point]" return "<img src='images/onepoint_before.png' class='onepoint'>\n" when "[end one-point]" return "<img src='images/onepoint_after.png' class='onepoint'>\n" end end |
#paragraph(text) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vivlio_pack/renderer.rb', line 45 def paragraph(text) @ring_buffer.push text @ring_buffer.shift if @ring_buffer.length > 5 case text.strip when /\A\[file (?<filename>(\w+\/)*(\w+)\.(\w+))(:(?<lineno>\d+))?\]$/ @in_file = true filename = $~[:filename] @file_lineno = $~[:lineno] ? $~[:lineno].to_i : 1 @file_filename = filename return "" when "[page break]" return "<div class='page-break'></div>\n" when /\A%([a-z][a-z0-9\-]*):(.*)/m return "<div class='#{$1}'>#{$2}</div>\n" when /\A\[begin\s+(\w+)(\s+(.+))?\]\z/ plugin_name = $~[1] plugin = Renderer.plugin(plugin_name) if plugin @state.push plugin_name result = plugin.process_begin($~[3]) return result if result end when /\A\[end\s+(\w+)\]\z/ plugin_name = $~[1] plugin = Renderer.plugin(plugin_name) if plugin if @state.last == plugin_name @state.pop else @ring_buffer.each do |line| puts line end raise RuntimeError.new("plugin: #{plugin_name} の対応が合っていません") end result = plugin.process_end return result if result end end @state.reverse.each do |plugin_name| plugin = Renderer.plugin(plugin_name) if plugin result = plugin.process_inner(text) return result if result end end text = text.strip.gsub(/%([a-z][a-z0-9\-]*){(.*)}/, "<span class='\\1'>\\2</span>") "<p>#{text.gsub(/ $/, "<br>\n")}</p>\n" end |