Module: PostModern::Render::ClassMethods

Defined in:
lib/post-modern/render.rb

Instance Method Summary collapse

Instance Method Details

#filter_code!(content, pretty = true) ⇒ Object



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
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
# File 'lib/post-modern/render.rb', line 70

def filter_code!(content, pretty = true)
  html = Nokogiri::HTML(content)#::fragment(content, 'utf-8')
  html.encoding = "utf-8"
  
  index = 0
  
  # code
  all_lines = 0
  html.css("pre").each do |code|
    index += 1
    liners      = ""
    source      = ""
    lines       = code.text.strip.split("\n")
    language    = code["lang"].to_s
    language    = language.present? ? language : "text"
    language = case language
    when "yml"
      :yaml
    when "coffee", "coffee-script", "coffeescript"
      :coffeescript
    else # sass, scss, css, rbcon, irb
      language.to_sym
    end
    total_lines = lines.size
    
    total_lines.times do |ln|
      liners += "<span id='L#{all_lines + ln+1}' rel='#LC#{all_lines + ln+1}'>#{ln+1}</span>\n"
    end
    
    lines.each_with_index do |line, i|
      code_line = line.blank? ? line.to_s : Albino.new(line, language).to_s
      code_line.gsub!(/<div class="highlight"><pre>|\n<\/pre>|\n<\/div>/,'')
      #code_line.gsub!(/^([ ])+/){|m| "&nbsp;"*m.size}
      code_line = "<br/>" if code_line.strip == ''
      source += "<div class='line' id='LC#{all_lines + i+1}'>#{code_line}</div>"
    end

    all_lines += total_lines

    liners_block = %{<td>
    <pre class='line-numbers'>#{liners}</pre>
  </td>}

    source_block = %{<td>
    <pre class='highlight'>#{source}</pre>
  </td>}

    output = %{
  <table cellpadding='0' cellspacing='0'>
  <tr>
  #{liners_block}
  #{source_block}
  </tr>
  </table>
  }
    code.name = "div"
    code.set_attribute "class", "data type-#{language}"
    code.set_attribute "id", "code-block-#{index}"
    #code.inner_html = "<div class='highlight'><pre>#{source}</pre></div>"#output
    code.inner_html = output#.gsub(/^([ ])+/){|m| "&nbsp;"*m.size}.gsub(/\n/, "")
  end
  
  if pretty
    pretty_html(html)#.gsub(/^([ ])+/){|m| "&nbsp;"*m.size}.gsub(/\n/, "")
  else
    html.to_html.gsub(/^([ ])+/){|m| "&nbsp;"*m.size}.gsub(/\n/, "")
  end
end

#filter_code_faster_todo!(content, pretty = true) ⇒ Object

pygmentize -f html -O style=colorful,linenos=table,anchorlinenos,lineanchors=LC -l ruby tmp.rb



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/post-modern/render.rb', line 140

def filter_code_faster_todo!(content, pretty = true)
  index = 0

  # code
  all_lines = 0
  content.scan(/\`{3}\s*(\w+)?\n*(?:([^\`]{3}*))\n*\`{3}/m).each do |lang, code|
    match = $&
    html = Nokogiri::HTML::fragment(code, "utf-8")#::fragment(content, 'utf-8')

    index += 1
    language    = lang
    language    = language.present? ? language : "text"
    language = case language
    when "yml"
      :yaml
    when "coffee", "coffee-script", "coffeescript"
      :coffeescript
    else # sass, scss, css, rbcon, irb
      language.to_sym
    end

    output = Albino.new(code.strip, language).to_s

    output = "<div class='data type-#{language}' id='code-block-#{index}' lang='#{language}'>\n#{output}\n</div>"
    content.gsub!(match, output)
  end

  content
end

#filter_haml!(code, locals = {}) ⇒ Object



66
67
68
# File 'lib/post-modern/render.rb', line 66

def filter_haml!(code, locals = {})
  Haml::Engine.new(code).render(Object.new, locals).gsub(/^\n/, "").gsub(/\n$/, "")
end

#pretty_html(html) ⇒ Object

can check prev/next elements to make it even prettier with correct ā€œnā€ do that in xslt.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/post-modern/render.rb', line 40

def pretty_html(html)
  return "" if html.nil?
  raise "will throw segmentation error if not an Nokogiri::XML::Document" unless html.is_a?(Nokogiri::XML::Document)
  pretty_printer = File.join(File.dirname(__FILE__), "pretty-print.xsl")
  xsl = Nokogiri::XSLT(IO.read(pretty_printer))
  xsl.transform(html).css("body").children.map do |child|
    if child["class"].to_s.split(/\s+/).include?("data")
      "\n" + html.css("##{child["id"]}").to_html.gsub(/^([ ])+/){|m| "&nbsp;"*m.size}.gsub(/\n/, "") + "\n"
    else
      child.to_html.gsub(/\t/, "").gsub(/^    /, "").gsub(/^\n/, "").gsub(/\n?  $/, "\n")
    end
  end.join("\n").gsub(/\n+/m, "\n").gsub(/^\n/, "")
end

#pretty_html_with_xslt(html) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/post-modern/render.rb', line 54

def pretty_html_with_xslt(html)
  return "" if html.nil?
  raise "will throw segmentation error if not an Nokogiri::XML::Document" unless html.is_a?(Nokogiri::XML::Document)
  html.css("body").children.map do |child|
    if child["class"].to_s.split(/\s+/).include?("data")
      "\n" + html.css("##{child["id"]}").to_html.gsub(/^([ ])+/){|m| "&nbsp;"*m.size}.gsub(/\n/, "") + "\n"
    else
      child.to_html.gsub(/\t/, "").gsub(/^    /, "").gsub(/^\n/, "").gsub(/\n?  $/, "\n")
    end
  end.join("\n").gsub(/\n+/m, "\n").gsub(/^\n/, "")
end

#render(content, locals = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/post-modern/render.rb', line 19

def render(content, locals = {})
  # filter haml, scss
  if locals[:filters] && Array(locals[:filters]).include?(:haml)
    content.scan(/@@@ (\w+)\s*(.*)\n@@@/m).each do |lang, code|  
      content.gsub!($&, send("filter_#{lang}!", code, locals))
    end
  end
  
  # convert to markdown
  content = Redcarpet.new(content, :autolink, :fenced_code, :gh_blockcode).to_html
  
  # filter code
  if locals[:filters] && Array(locals[:filters]).include?(:highlight)
    content = filter_code!(content, locals[:filters].include?(:pretty))
  end
  
  content
end