Module: CortexReaver::Model::Renderer

Includes:
Innate::Helper::CGI, Ramaze::Helper::Attachments, Ramaze::Helper::Form, Ramaze::Helper::Pages
Included in:
Comment, Journal, Page
Defined in:
lib/cortex_reaver/support/renderer.rb

Overview

Some common rendering methods, wrapped up for your convenience. Use in your model with something like:

render :body, :with => :render_comment

See CortexReaver::Model::CachedRendering for more details.

Instance Method Summary collapse

Methods included from Ramaze::Helper::Form

#attr_h, #errors_list, #errors_on, #form_for, #form_p, #form_submit

Methods included from Ramaze::Helper::Attachments

#delete_attachment, included

Methods included from Ramaze::Helper::Pages

#page_navigation, #page_navigation_helper, #page_select, #subpage_navigation

Instance Method Details

#bluecloth(text, parse_code = true, increment_headers = true) ⇒ Object

Renders plain text and html to html. If parse_code isn’t true, only runs bluecloth on text outside any ... blocks.



24
25
26
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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cortex_reaver/support/renderer.rb', line 24

def bluecloth(text, parse_code = true, increment_headers = true)
  return text if text.nil?

  if parse_code
    return BlueCloth::new(text).to_html
  end

  text = text.dup
  out = ''
  level = 0
  until text.empty? do
    if level < 1
      # Find start of code block
      j = text.index(/<code.*?>/) || text.length
      j -= 1 if j != 0
      level += 1
     
      if j != 0
        # Convert to bluecloth
        blue = BlueCloth::new(text[0..j]).to_html
        if increment_headers
          # Increment headings by two (h1, h2 are page/entry headers)
          blue.gsub!(/<(\/)?h(\d)>/) { |match| "<#{$1}h#{$2.to_i + 2}>" }
        end
        out << blue
      end
    else
      # Find end of code block
      j = text.index('</code>') || text.length
      level -= 1
      j += 6

      # Output as is
      out << text[0..j]
    end

    # Excise parsed string
    text.slice! 0..j unless j == 0
  end

  out
end

#erubis_filter(text) ⇒ Object

Replace <% and %> to prevent Erubis injection.



68
69
70
71
72
73
74
75
# File 'lib/cortex_reaver/support/renderer.rb', line 68

def erubis_filter(text)
  return text if text.nil?
  
  t = text.dup
  t.gsub!('<%', '&lt;%')
  t.gsub!('%>', '%&rt;')
  t
end

#macro(text) ⇒ Object

Macro substitutions

Expands [[type:resource]] macros. Right now, resource is just an attachment.

Included types are:

url: returns the URL to an attachment image: returns an image tag link: returns a link to an attachment page_nav: A list of sub-pages attachments: A list of attachments

The default action is a link, so

[foo.jpg]

> <a href=“/data/…/foo.jpg”>foo.jpg</a>



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
# File 'lib/cortex_reaver/support/renderer.rb', line 93

def macro(text)
  return text if text.nil?

  copy = text.dup

  # Links
  # 
  # Example                         [[image:foo.png][name]]
  # 1. the prefix                   image
  # 2. the link, with colon         :foo.png
  # 3. the link itself              foo.png
  # 4. the second half of the link  [name]
  # 5. the name                     name 
#copy.gsub!(/\[\[(([^\]]+):)?([^\]]+)(\]\[([^\]]+))?\]\]/) do |match|
 copy.gsub!(/\[\[([^\]]+?)(:([^\]]+))?(\]\[([^\]]+))?\]\]/) do |match|
    prefix = $1
    path = $3
    name = $5

    # Name of the link
    name ||= path

    Ramaze::Log.debug prefix

    # Create link to this target
    case prefix
    when 'attachments'
      # A list of attachments
      attachment_list self
    when 'image'
      # Create an inline image
      target = attachment(path)
      "<img class=\"attachment\" src=\"#{target.public_path}\" alt=\"#{name.gsub('"', '&quot;')}\" title=\"#{name.gsub('"', '&quot')}\" />"
    when 'page_nav'
      # Create a page table of contents
      subpage_navigation self
    when 'url'
      # Create a URL
      target = attachment(path)
      target.public_path
    else
      # Create a full link
      target = attachment(path)
      "<a href=\"#{target.public_path}\">#{Rack::Utils.escape_html(name).gsub(/#([{@$]@?)/, '&#35;\1')}</a>"
    end
  end
 
  copy
end

#render(text) ⇒ Object

Default renderer



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cortex_reaver/support/renderer.rb', line 180

def render(text)
  bluecloth(
    macro(
      erubis_filter(
        syntax_highlight(
          text
        )
      )
    ), false
  ) # (((Feeling) LISPish yet)?)
end

#render_comment(text) ⇒ Object

Comments render



193
194
195
196
197
198
199
200
201
# File 'lib/cortex_reaver/support/renderer.rb', line 193

def render_comment(text)
  bluecloth(
    erubis_filter(
      sanitize_html(
        text
      )
    )
  )
end

#sanitize_html(html) ⇒ Object



173
174
175
176
177
# File 'lib/cortex_reaver/support/renderer.rb', line 173

def sanitize_html(html)
  return html if html.nil?

  Sanitize.clean(html, Sanitize::Config::BASIC)
end

#syntax_highlight(text) ⇒ Object



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
169
170
171
# File 'lib/cortex_reaver/support/renderer.rb', line 143

def syntax_highlight(text)
  return text if text.nil?

  text = text.gsub(/<cr:code([^>]+lang="([a-z0-9]+)".*?)?>(.*?)<\/cr:code>/m) do |match|
    lang = $2 || 'text'
    code = $3

    # Tempfile...
    Tempfile.open('cortex-reaver') do |f|
      f.write code.strip
      f.close

      system('vim -f +"set filetype=' + lang + '" +"syn on" +"let html_use_css = 1" +"let html_use_encoding = \"UTF-8\"" +"let use_xhtml = 1" +"run! syntax/2html.vim" +"wq" +"q" ' + f.path)

      code = File.read(f.path + '.html')
      File.delete(f.path + '.html')
      f.unlink
    end

    # Slice out preamble
    code.sub!(/^.*?<pre>/m, '')
    code.sub!(/<\/pre>.*$/m, '')

    # Wrap
    code = '<code class="block">' + code.strip + '</code>' 
    code
  end
  text
end