Module: Zena::Use::Zazen::ViewMethods

Included in:
TextDocument::AssetHelper
Defined in:
lib/zena/use/zazen.rb

Constant Summary collapse

@@_asset_methods =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.asset_method(opts) ⇒ Object

define an asset method (‘key’ => method_name).



11
12
13
14
15
# File 'lib/zena/use/zazen.rb', line 11

def self.asset_method(opts)
  opts.each do |k,v|
    @@_asset_methods[k] = v
  end
end

Instance Method Details

#list_nodes(ids = [], opts = {}) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/zena/use/zazen.rb', line 252

def list_nodes(ids=[], opts={})
  style = opts[:style] || ''
  node  = opts[:node] || @node
  case style.sub('.', '')
  when ">"
    prefix = "<div class='img_right'>"
    suffix = "</div>"
  when "<"
    prefix = "<div class='img_left'>"
    suffix = "</div>"
  when "="
    prefix = "<div class='img_center'>"
    suffix = "</div>"
  else
    prefix = suffix = ""
  end
  if ids == []
    docs = node.find(:all, 'documents')
  elsif ids[0] == "d"
    docs = node.find(:all, 'documents where kpath not like "NDI%"')
  elsif ids[0] == "i"
    docs = node.find(:all, 'images')
  else
    ids = ids.map{|i| i.to_i}
    docs = ids == [] ? nil : secure!(Document) { Document.find(:all, :conditions=>"zip IN (#{ids.join(',')})") }
    # order like ids :
    docs.sort! {|a,b| ids.index(a[:zip].to_i) <=> ids.index(b[:zip].to_i) } if docs
  end
  return '' unless docs
  prefix + render_to_string( :partial=>'nodes/list_nodes', :locals=>{:docs=>docs}) + suffix
rescue
  '[no document found]'
end

#make_asset(opts) ⇒ Object

Parse the text in the given context (used by zazen)



95
96
97
98
99
100
101
102
103
# File 'lib/zena/use/zazen.rb', line 95

def make_asset(opts)
  asset_tag = opts[:asset_tag]
  if asset_method = @@_asset_methods[asset_tag]
    self.send(asset_method, opts)
  else
    # Unknown tag. Ignore
    "[#{asset_tag}]#{opts[:content]}[/#{asset_tag}]"
  end
end

Create a gallery from a list of images. See ApplicationHelper#zazen for details.



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/zena/use/zazen.rb', line 238

def make_gallery(ids=[], opts={})
  node = opts[:node] || @node
  if ids == []
    images = node.find(:all, 'images')
  else
    ids = ids.map{|i| i.to_i}
    images = secure(Document) { Document.find(:all, :conditions=>"zip IN (#{ids.join(',')})") }
    # order like ids :
    images.sort! {|a,b| ids.index(a[:zip].to_i) <=> ids.index(b[:zip].to_i) } if images
  end

  render_to_string( :partial=>'nodes/gallery', :locals=>{:gallery=>images} )
end

#make_image(opts) ⇒ Object

Create an img tag for the given image. See ApplicationHelper#zazen for details.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/zena/use/zazen.rb', line 182

def make_image(opts)
  id, style, link, mode, title = opts[:id], opts[:style], opts[:link], opts[:mode], opts[:title]
  mode ||= 'std' # default mode
  img = opts[:node] || secure(Node) { Node.find_by_zip(id) }

  return "<span class='unknownLink'>#{_('unknown document')}</span>" unless img

  if !opts[:images].nil? && !opts[:images]
    return "[#{_('image')}: #{img.title}]"
  end
  title = img.summary if title == ""

  image = img_tag(img, :mode=>mode, :host=>opts[:host])
  
  return "<span class='unknownLink'>#{_('invalid document')}</span>" unless image

  unless link
    if id[0..0] == "0" || (!img.kind_of?(Image) && !(image =~ /ZAZENBLOCKCODE/))
      # if the id starts with '0' or it is not an Image, link to data
      link = zen_path(img, :format => img.prop['ext'])
      link_tag = "<a class='popup' href='#{link}' target='_blank'>"
    end
  end

  style ||= ''
  case style.sub('.', '')
  when ">"
    prefix = "<div class='img_right'>"
    suffix = "</div>"
  when "<"
    prefix = "<div class='img_left'>"
    suffix = "</div>"
  when "="
    prefix = "<div class='img_center'>"
    suffix = "</div>"
  else
    prefix = suffix = ""
  end

  if title
    prefix = "#{prefix}<div class='img_with_title'>"
    suffix = "<div class='img_title'>#{ZazenParser.new(title,:helper=>self).render(:images=>false)}</div></div>#{suffix}"
  end

  if link.nil? || image[0..3] == '[:::' # do not link on placeholders
    prefix + image + suffix
  elsif link =~ /^\d+/
    prefix + make_link(:id=>link,:title=>image) + suffix
  else
    link = "http://#{link}" unless link =~ %r{(^/|.+://.+)}
    link_tag ||= "<a href='#{link}'>"
    prefix + link_tag + image + "</a>" + suffix
  end
end

Creates a link to the node referenced by zip (used by zazen)



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
163
164
165
# File 'lib/zena/use/zazen.rb', line 106

def make_link(opts)
  # for latex refs, see http://www.tug.org/applications/hyperref/manual.html
  anchor = opts[:anchor]
  if anchor && anchor =~ /\[(.+?)\/(.*)\]/
    anchor_in, anchor = $1, $2
    anchor = anchor == '' ? 'true' : "[#{anchor}]"
  end

  link_opts = {}
  link_opts[:host] = opts[:host]

  if opts[:id] =~ /(\d+)(_\w+|)(\.\w+|)/
    id = $1
    link_opts[:mode]   = ($2 != '') ? $2[1..-1] : nil
    link_opts[:format] = ($3 != '') ? $3[1..-1] : nil

    node = opts[:node] || secure(Node) { Node.find_by_zip(id) }
  else
    node = opts[:node]
  end

  if %w{true [id] [zip]}.include?(anchor)
    return "<span class='unknownLink'>#{_('unknown link')}</span>" unless node
    anchor_value = "node#{node.zip}"
  end

  if !node && (
     opts[:id]           ||
     opts[:title].blank? ||
     anchor_in           ||
     anchor == 'true'    ||
     anchor =~ /^\[(.+)\]/)
    return "<span class='unknownLink'>#{_('unknown link')}</span>"
  end

  title = opts[:title].blank? ? node.title : opts[:title]

  link_opts[:format] = node.ext if node.kind_of?(Document) && link_opts[:format] == 'data'

  if anchor
    if %w{true [id] [zip]}.include?(anchor)
      anchor_value = "node#{node.zip}"
    elsif anchor =~ /\[(.+)\]/
      anchor_value = node.prop[$1].try(:url_name)
    else
      anchor_value = anchor
    end

    if anchor_in
      node = node.find(:first, anchor_in)
      link_to title, "#{zen_path(node, link_opts)}##{anchor_value}"
    else
      link_to title, "##{anchor_value}"
    end
  elsif opts[:id] && opts[:id][0..0] == '0'
    link_to title, zen_path(node, link_opts), :popup => true
  else
    link_to title, zen_path(node, link_opts), :target => opts[:target]
  end
end

TODO: test



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/zena/use/zazen.rb', line 168

def make_wiki_link(opts)
  l = opts[:node] ? opts[:node].version.lang : visitor.lang
  if opts[:url]
    if opts[:url][0..3] == 'http'
      "<a href='#{opts[:url]}' class='wiki'>#{opts[:title]}</a>"
    else
      "<a href='http://#{l}.wikipedia.org/wiki/#{opts[:url]}' class='wiki'>#{opts[:title]}</a>"
    end
  else
    "<a href='http://#{l}.wikipedia.org/wiki/Special:Search?search=#{CGI::escape(opts[:title])}' class='wiki'>#{opts[:title]}</a>"
  end
end

#raw_content(txt) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/zena/use/zazen.rb', line 81

def raw_content(txt)
  if @zazen
    @zazen.raw_content(txt)
  else
    txt
  end
end

#zazen(text, opt = {}) ⇒ Object

This method renders the Textile text contained in an object as html. It also renders the zena additions :

Zena additions

all these additions are replaced by the traduction of ‘unknown link’ or ‘unknown image’ if the user does not have read access to the linked node.

  • “”:34

    creates a link to node 34 with node’s title.

  • “title”:34

    creates a link to node 34 with the given title.

  • “”:034

    if the node id starts with ‘0’, creates a popup link.

  • !14!

    inline image 14 or link to document 14 with icon. (default format for images is ‘std’ defined in #ImageBuilder). Options are :

** [!014!] if the id starts with ‘0’, the image becomes a link to the full image. ** [!<.14!] or [!<14!] inline image surrounded with <p class=‘img_left’></p> ** [!>.14!] or [!>14!] inline image surrounded with <p class=‘img_right’></p> ** [!=.14!] or [!=14!] inline image with <p class=‘img_center’></p> ** [!14_pv!] inline image transformed to format ‘pv’ (class is also set to ‘pv’). Formats are defined in #ImageBuilder. ** all the options above can be used together as in [!>.14.med!] : inline image on the right, size ‘med’. ** [![2,3,5]!] gallery : inline preview with javascript inline viewer ** [![]!] gallery with all images contained in the current node

  • {7,9}!

    documents listing for documents 7 and 9

  • !{}!

    list all documents (with images) for the current node

  • {d}!

    list all documents (without images) for the current node

  • {i}!

    list all images for the current node

  • !14!:37

    you can use an image as the source for a link

  • !14!:www.example.com

    use an image for an outgoing link



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zena/use/zazen.rb', line 39

def zazen(text, opt={})
  Timeout.timeout(ZAZEN_TIMEOUT) do
    return '' unless text
    code_lang = opt[:code]
    if code_lang == '' && opt[:node].kind_of?(TextDocument)
      # do not parse. This is to make sure we do not accidently parse
      # TextDocument content as Zazen.
      return text
    end

    if !code_lang.blank?
      return Zena::CodeSyntax.new(text, code_lang).to_html(opt)
    end

    opt = {:images=>true, :pretty_code=>true, :output=>'html'}.merge(opt)
    no_p = opt.delete(:no_p)
    img = opt[:images]
    if opt[:limit]
      opt[:limit] -= 1 unless opt[:limit] <= 0
      paragraphs = text.split(/\n\n|\r\n\r\n/)
      if paragraphs.size > (opt[:limit]+1) && opt[:limit] != -1
        text = paragraphs[0..opt[:limit]].join("\r\n\r\n") + " &#8230;"
      end
    end
    opt[:node] ||= @node
    # Bug in Textile when text starts with ' '
    if text[0..0] == ' '
      text = "\n\n#{text}"
    end
    @zazen = ZazenParser.new(text, :helper=>self)
    res = @zazen.render(opt)
    @zazen = nil
    if no_p && !text.include?("\n")
      res.gsub(%r{\A<p>|</p>\Z},'')
    else
      res
    end
  end
rescue Timeout::Error
  return %Q{<span class='parser_error'>#{_('Could not render text (Timeout error)')}</span>}
end

#zazen_diff(text1, text2, opt = {}) ⇒ Object

TODO: test



90
91
92
# File 'lib/zena/use/zazen.rb', line 90

def zazen_diff(text1, text2, opt={})
  HTMLDiff::diff(zazen(text1), zazen(text2))
end