Class: Kramdown::Converter::SeHtml

Inherits:
Html
  • Object
show all
Defined in:
lib/zine_brewer/kramdown/converter/sehtml.rb

Instance Method Summary collapse

Instance Method Details

#convert_a(el, indent) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 113

def convert_a(el, indent)
  res = inner(el, indent)
  attr = el.attr.dup
  if attr['href'].start_with?('mailto:')
    mail_addr = attr['href'][7..-1]
    attr['href'] = obfuscate('mailto') << ":" << obfuscate(mail_addr)
    res = obfuscate(res) if res == mail_addr
  else
    attr['target'] = '_blank' unless attr['href'].start_with?('#')
  end
  format_as_span_html(el.type, attr, res)
end

#convert_codeblock(el, indent) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 27

def convert_codeblock(el, indent)
  raw_caption = el.attr.delete('caption')
  caption = if raw_caption.nil?
    ''
  else
    el_caption = Document.new(raw_caption, {:auto_ids => false, :input => 'sekd'}).root.children[0]
    format_as_block_html('div', {'class' => 'caption'}, inner(el_caption, indent), indent + 2)
  end

  result = escape_html(el.value)
  result.chomp!
  if el.attr['class'].to_s =~ /\bshow-whitespaces\b/
    result.gsub!(/(?:(^[ \t]+)|([ \t]+$)|([ \t]+))/) do |m|
      suffix = ($1 ? '-l' : ($2 ? '-r' : ''))
      m.scan(/./).map do |c|
        case c
        when "\t" then "<span class=\"ws-tab#{suffix}\">\t</span>"
        when " " then "<span class=\"ws-space#{suffix}\">&#8901;</span>"
        end
      end.join('')
    end
  end

  format_as_indented_block_html('div', {'class' => 'src_frame'},
    caption + '  ' + format_as_indented_block_html('pre', {'class' => el.attr['class']}, result, indent),
    indent)
end

#convert_column(el, indent) ⇒ Object



55
56
57
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 55

def convert_column(el, indent)
  format_as_indented_block_html('section', el.attr, inner(el, indent), indent)
end

#convert_definition_table(el, indent) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 77

def convert_definition_table(el, indent)
  th_width = el.attr.delete('th-width')
  th = th_width.nil? ? "<th>" : "<th width=\"#{th_width}\">"
  raw_caption = el.attr.delete('caption')
  rows = raw_caption.nil? ? '' : table_caption(raw_caption)
  inner(el.children.first, indent).scan(/(<dt>.+?<\/dt>|<dd>.+?<\/dd>)/m).
    map{|x| x.first.sub(/\A<dt>(.+)<\/dt>\z/m, "#{th}\\1</th>")}.
    map{|x| x.sub(/\A<dd>(.+)<\/dd>\z/m, "<td>\\1</td>")}.
    each_slice(2) do |x|
      row = x.map{|c| ('  '*(indent + 2)) + c}.join("\n")
      rows += format_as_indented_block_html('tr', {}, "#{row}\n", indent + 2)
    end
  format_as_indented_block_html('table', el.attr, rows, indent)
end

#convert_div(el, indent) ⇒ Object



97
98
99
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 97

def convert_div(el, indent)
  format_as_indented_block_html('div', el.attr, inner(el, indent), indent)
end

#convert_dt(el, indent) ⇒ Object



92
93
94
95
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 92

def convert_dt(el, indent)
  result = super
  result.sub(/<dt>\\/, '<dt>')
end

#convert_em(el, indent) ⇒ Object



126
127
128
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 126

def convert_em(el, indent)
  format_as_span_html('strong', el.attr, inner(el, indent))
end

#convert_footnote_definition_sekd(el, indent) ⇒ Object



109
110
111
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 109

def convert_footnote_definition_sekd(el, indent)
  format_as_block_html('section', el.attr, "\n  <h4>注</h4>\n" + inner(el, indent), indent)
end

#convert_footnote_marker_sekd(el, indent) ⇒ Object



105
106
107
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 105

def convert_footnote_marker_sekd(el, indent)
  %!<sup id="fnref:#{el.value}"><a href="#fn:#{el.value}">[#{el.value}]</a></sup>!
end

#convert_img(el, indent) ⇒ Object



20
21
22
23
24
25
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 20

def convert_img(el, indent)
  if %r{^common} =~ File.dirname(el.attr['src'])
    el.attr['src'] = "/static/images/article/common/#{File.basename(el.attr['src'])}"
  end
  "<img#{html_attributes(el.attr)} />"
end

#convert_math(el, indent) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 130

def convert_math(el, indent)
  if (result = format_math(el, :indent => indent))
    result
  elsif el.options[:category] == :block
    format_as_block_html('pre', el.attr, "$$\n#{el.value}\n$$", indent)
  else
    format_as_span_html('math', el.attr, "$#{el.value}$")
  end
end

#convert_page(el, indent) ⇒ Object



101
102
103
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 101

def convert_page(el, indent)
  el.value + "\n"
end

#convert_root(el, indent) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 140

def convert_root(el, indent)
  result = inner(el, indent)
  if @toc_code
    toc_tree = generate_toc_tree(@toc, @toc_code[0], @toc_code[1] || {})
    text = if toc_tree.children.size > 0
             convert(toc_tree, 0)
           else
             ''
           end
    result.sub!(/#{@toc_code.last}/, text.gsub(/\\/, "\\\\\\\\"))
  end
  result
end

#convert_table(el, indent) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 65

def convert_table(el, indent)
  raw_caption = el.attr.delete('caption')
  table = super(el, indent)
  if !raw_caption.nil?
    table.sub!(/\A(<table.*?>)/, "\\1\n#{table_caption(raw_caption)}\n")
  end
  table.gsub(/^\s*<\/?t(head|body|foot)>\n/, '')
       .gsub(/ style="text-align: left"/, '')
       .gsub(/ style="text-align: center"/, ' class="txtC"')
       .gsub(/ style="text-align: right"/, ' class="txtR"')
end

#table_caption(raw_caption) ⇒ Object

Makes caption element from caption attr of table element.



60
61
62
63
# File 'lib/zine_brewer/kramdown/converter/sehtml.rb', line 60

def table_caption(raw_caption)
  el_caption = Document.new(raw_caption, {:auto_ids => false, :input => 'sekd'}).root.children[0]
  format_as_block_html('caption', {}, inner(el_caption, indent), indent)
end