Module: Zena::Parser::ZazenRules
- Includes:
- Acts::Secure
- Defined in:
- lib/zena/parser/zazen_rules.rb
Constant Summary collapse
- PSEUDO_ID_REGEXP =
":[0-9a-zA-Z-]+\\+*|\\([^\\)]*\\)"
Instance Method Summary collapse
- #extract_code(fulltext) ⇒ Object
- #find_node_by_pseudo(id, base_node = ) ⇒ Object
- #flush(str = @text) ⇒ Object
- #parse_document_ids(str) ⇒ Object
- #raw_content(txt) ⇒ Object
- #render_code(text) ⇒ Object
- #scan ⇒ Object
- #scan_bracket ⇒ Object
- #scan_exclam ⇒ Object
- #scan_pipe ⇒ Object
- #scan_quote ⇒ Object
- #scan_wiki ⇒ Object
- #scan_wiki_link ⇒ Object
- #start(mode) ⇒ Object
-
#store(str) ⇒ Object
rewrite store to optimize for our ‘text only’ parser.
Methods included from Acts::Secure
#secure_scope, #secure_write_scope, #visitor=
Methods included from Acts::Secure::SecureResult
#construct_id_map, #secure_result
Instance Method Details
#extract_code(fulltext) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/zena/parser/zazen_rules.rb', line 334 def extract_code(fulltext) @escaped_code = [] @block_counter = -1 fulltext.gsub!( /<(code|notextile|html)([^>]*)>(.*?)<\/\1>/m ) do if @translate_ids raw_content([nil, $&]) elsif $1 == 'notextile' || $1 == 'html' # FIXME: SECURITY. How to avoid the use of notextile in comments and such ? if @context[:notextile] == 'true' raw_content($3) else '' end else params, text = $2, $3 pre_params = [] if params =~ /\A(.*)lang\s*=\s*("|')([^"']+)\2(.*)\Z/m pre, lang, post = $1.strip, $3, $4.strip pre_params << pre if pre && pre != "" pre_params << post if post && post != "" else pre_params << params.strip if params != '' lang = '' end #pre_params << "class='code'" unless params =~ /class\s*=/ pre_params = pre_params.blank? ? nil : pre_params.join(' ') raw_content([lang, text, pre_params]) end end @escaped_at = [] block_counter = -1 fulltext.gsub!( /(\A|[^\w])@(.*?)@(\Z|[^\w])/m ) do @escaped_at << $2 block_counter += 1 "#{$1}\\ZAZENBLOCKAT#{block_counter}ZAZENBLOCKAT\\#{$3}" end end |
#find_node_by_pseudo(id, base_node = ) ⇒ Object
402 403 404 |
# File 'lib/zena/parser/zazen_rules.rb', line 402 def find_node_by_pseudo(id, base_node = @context[:node]) secure(Node) { Node.find_node_by_pseudo(id, base_node) } end |
#flush(str = @text) ⇒ Object
28 29 30 31 |
# File 'lib/zena/parser/zazen_rules.rb', line 28 def flush(str=@text) @blocks << str @text = @text[str.length..-1] end |
#parse_document_ids(str) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/zena/parser/zazen_rules.rb', line 406 def parse_document_ids(str) meth = @translate_ids || :zip str.split(',').map do |id| if id.strip =~ /\A(\d+|#{PSEUDO_ID_REGEXP})/ if node = find_node_by_pseudo($1) # replace shortcut node.pseudo_id(@context[:node], meth) else id # keep end else id end end.compact end |
#raw_content(txt) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/zena/parser/zazen_rules.rb', line 11 def raw_content(txt) txt = [:raw, txt] if txt.kind_of?(String) @escaped_code << txt @block_counter += 1 return "<pre>\\ZAZENBLOCKCODE#{@block_counter}ZAZENBLOCKCODE\\</pre>" end |
#render_code(text) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/zena/parser/zazen_rules.rb', line 373 def render_code(text) text.gsub!( /<pre>\\ZAZENBLOCKCODE(\d+)ZAZENBLOCKCODE\\<\/pre>/ ) do if @translate_ids @escaped_code[$1.to_i] else code_lang, code, pre_params = @escaped_code[$1.to_i] # FIXME: How to not parse with Textile ? if code_lang == :raw then # SECURITY: symbol cannot be set from zazen. code else Zena::CodeSyntax.new(code, @context[:pretty_code] ? code_lang : nil).to_html(@context.merge(:pre_params => pre_params)) end end end text.gsub!( /\\ZAZENBLOCKAT(\d+)ZAZENBLOCKAT\\/ ) do code = @escaped_at[$1.to_i] if @translate_ids '@'+code+'@' else if code =~ /^(\w+)\|(.*)$/ code_lang, code = $1, $2 end Zena::CodeSyntax.new(code, @context[:pretty_code] ? code_lang : nil).to_html(@context.merge(:inline => true)) end end end |
#scan ⇒ Object
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/zena/parser/zazen_rules.rb', line 33 def scan #puts "SCAN:[#{@text}]" if @text =~ /\A([^\|!"<\n\[]*)/m flush $& if @text[0..0] == '!' scan_exclam elsif @text[0..0] == '"' scan_quote elsif @text[0..0] == '[' scan_bracket elsif @text[0..0] == '|' scan_pipe elsif @text[0..4] == '<code' # FIXME: implement <code..> and @@ instead of "extract" flush # implement !! scan_code elsif @text[0..0] == '<' flush '<' elsif !@in_space_pre && @text[0..2] == "\n\n " && !@translate_ids # space preserving paragraphe @in_space_pre = true store "\n\n<pre>" eat 3 elsif @in_space_pre && @text[0..1] == "\n\n" && !@translate_ids store "</pre>" while @text[0..0] == "\n" flush "\n" end @in_space_pre = false elsif @text[0..1] == "\n\n" while @text[0..0] == "\n" flush "\n" end elsif @text[0..1] == "\n " && @in_space_pre && !@translate_ids store "\n" eat 2 elsif @text[0..1] == "\n|" flush "\n|" elsif @text[0..0] == "\n" && !@translate_ids if @in_space_pre || @text == "\n" || @text[1..1] == '*' || @text[1..1] == '#' flush "\n" else # forced line break store "\n<br/>" eat 1 end elsif @text[0..0] == "\n" flush "\n" else # error flush end else # nothing interesting flush end end |
#scan_bracket ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/zena/parser/zazen_rules.rb', line 236 def scan_bracket # puts "BRACKET:[#{@text}]" if @text =~ /\A\[(\w+)\](.*?)\[\/\1\]/m if @translate_ids flush $& else eat $& # [math]....[/math] (we do not use <math> to avoid confusion with mathml) store @helper.make_asset(:asset_tag => $1, :content => $2, :node => @context[:node], :preview => @context[:preview], :output => @context[:output]) end else # nothing interesting flush '[' end end |
#scan_exclam ⇒ Object
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/zena/parser/zazen_rules.rb', line 91 def scan_exclam #puts "EXCL:[#{@text}]" if @text =~ /\A\!\[([^\]]*)\]\!/m # create a gallery ![...]! #puts "GALLERY:[#{$&}]" eat $& if @context[:images] ids = parse_document_ids($1) if @translate_ids store "![#{ids.join(',')}]!" else store @helper.make_gallery(ids, :node=>@context[:node]) end else store @helper._('[gallery]') end elsif @text =~ /\A\!([^0-9]{0,2})\{([^\}]*)\}\!/m # list of documents !<.{...}! #puts "DOCS:[#{$&}]" eat $& style, ids = $1, $2 if @context[:images] ids = parse_document_ids(ids) if @translate_ids store "!#{style}{#{ids.join(',')}}!" else store @helper.list_nodes(ids, :style=>style, :node=>@context[:node]) end else store @helper._('[documents]') end elsif @text =~ /\A\!([^0-9:]{0,2})(#{PSEUDO_ID_REGEXP})(_([^\/\!]+)|)(\/([^\!]*)|)\!(:([^:\(][^\s]*|#{PSEUDO_ID_REGEXP}(_[a-zA-Z]+|))|)/m # image !<.:art++_pv/blah blah!:12 #puts "SHORCUT IMAGE:#{$~.to_a.inspect}" eat $& style, id, other_opts, mode, title_opts, title, link = $1, $2, $3, $4, $5, $6, $8 if node = find_node_by_pseudo(id) if link && link =~ /^(#{PSEUDO_ID_REGEXP})(.*)$/ rest = $2 if link_node = find_node_by_pseudo($1) link = link_node.pseudo_id(@context[:node], @translate_ids || :zip).to_s + rest end end if @translate_ids if node.kind_of?(Document) # replace shortcut store "!#{style}#{node.pseudo_id(@context[:node], @translate_ids || :zip)}#{other_opts}#{title_opts}!#{link ? ':' + link : ''}" else store $& end else if node.kind_of?(Document) store @helper.make_image(:style=>style, :id=>node[:zip].to_s, :node=>node, :mode=>mode, :title=>title, :link=>link, :images=>@context[:images], :host => @context[:host],:target=>@context[:target]) else store "[#{node.fullpath_as_title.join('/')} is not a document]" end end elsif @translate_ids store $& else store "[#{id} not found]" end elsif @text =~ /\A\!([^0-9]{0,2})([0-9]+)(_([^\/\!]+)|)(\/([^\!]*)|)\!(:([^:\(][^\s]*|#{PSEUDO_ID_REGEXP}(_[a-zA-Z]+|))|)/m # image !<.12_pv/blah blah!:12 #puts "IMAGE:[#{$&}]" eat $& style, id, other_opts, mode, title_opts, title, link = $1, $2, $3, $4, $5, $6, $8 if link && link =~ /^(#{PSEUDO_ID_REGEXP})(.*)$/ rest = $2 if link_node = find_node_by_pseudo($1) link = link_node[:zip].to_s + rest end end if @translate_ids if @translate_ids != :zip node = find_node_by_pseudo(id) id = node.pseudo_id(@context[:node], @translate_ids) if node end store "!#{style}#{id}#{other_opts}#{title_opts}!#{link ? ':' + link : ''}" else store @helper.make_image(:style=>style, :id=>id, :mode=>mode, :title=>title, :link=>link, :images=>@context[:images], :host => @context[:host],:target=>@context[:target]) end else #puts "EAT:[#{$&}]" # eat marker and continue scan flush @text[0..0] end end |
#scan_pipe ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/zena/parser/zazen_rules.rb', line 286 def scan_pipe #puts "PIPE:[#{@text}]" if @text =~ /\A\|([<=>]\.|)([0-9]+\.|)([a-zA-Z_]+)(\/([^\|]*)|)\|/m # table |<.34.shopping_list/blah blah| # table |shopping_list| #puts "TABLE:#{$~.to_a.inspect}" eat $& style, id, attribute, title_opts, title = $1, $2, $3, $4, $5 id = id[0..-2] if id != '' if @translate_ids if @translate_ids != :zip node = find_node_by_pseudo(id) id = node.pseudo_id(@context[:node], @translate_ids) if node end store "|#{style}#{id == '' ? '' : "#{id}."}#{attribute}#{title}|" else node = id == '' ? @context[:node] : find_node_by_pseudo(id) store @helper.make_table(:style=>style, :node=>node, :attribute=>attribute, :title=>title) end elsif @text =~ /\A\|([<=>]\.|)(#{PSEUDO_ID_REGEXP})\.([a-zA-Z_]+)(\/([^\|]*)|)\|/m # table |<.:art++.shopping_list/blah blah| # table |shopping_list| #puts "TABLE SHORTCUT:#{$~.to_a.inspect}" eat $& text = $& style, id, attribute, title_opts, title = $1, $2, $3, $4, $5 if node = find_node_by_pseudo(id) if @translate_ids # replace shortcut store "|#{style}#{node.pseudo_id(@context[:node], @translate_ids || :zip)}.#{attribute}#{title}|" else # write table store @helper.make_table(:style=>style, :node=>node, :attribute=>attribute, :title=>title) end elsif @translate_ids # node not found, ignore store text else # node not found store "[#{id} not found]" end else #puts "EAT:[#{$&}]" # eat marker and continue scan flush @text[0..0] end end |
#scan_quote ⇒ Object
181 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 |
# File 'lib/zena/parser/zazen_rules.rb', line 181 def scan_quote if @text =~ /\A"([^"]*)":([0-9]+(_[a-z]+|)(\.[a-z]+|)(#[a-z_\/\[\]]*|))/m #puts "LINK:[#{$&}]" if @translate_ids == :zip flush $& elsif @translate_ids eat $& title, id = $1, $2 node = find_node_by_pseudo(id) id = node.pseudo_id(@context[:node], @translate_ids) if node store "\"#{title}\":#{id}" else eat $& # link inside the cms "":34 title, id = $1, $2 if id =~ /(.*?)#(.*)/ id, anchor = $1, $2 anchor = 'true' if anchor == '' end store @helper.make_link(:title=>title,:id=>id,:anchor=>anchor,:host=>@context[:host],:target=>@context[:target]) end elsif @text =~ /\A"([^"]*)":(#{PSEUDO_ID_REGEXP})((_[a-z]+|)(\.[a-z]+|)(#[a-z_\/\[\]]*|))/m #puts "SHORTCUT_LINK:[#{$&}]" eat $& title, pseudo_id, mode_format, mode, format, dash = $1, $2, $3, $4, $5, $6 if node = find_node_by_pseudo(pseudo_id) if @translate_ids id = "#{node.pseudo_id(@context[:node], @translate_ids)}#{mode_format}" # replace shortcut store "\"#{title}\":#{id}" else id = "#{node.zip}#{mode_format}" if format == '.data' && node.kind_of?(Document) title = "#{node.fullpath_as_title.join('/')}#{mode}.#{node.ext}#{dash}" else title = "#{node.fullpath_as_title.join('/')}#{mode_format}" end if id =~ /(.*?)#(.*)/ id, anchor = $1, $2 anchor = 'true' if anchor == '' end store @helper.make_link(:title=>title,:id=>id,:anchor=>anchor,:node=>node,:host=>@context[:host],:target=>@context[:target]) end elsif @translate_ids store $& else pseudo_id = pseudo_id[1..-1] if pseudo_id[0..0] == ':' store "[#{pseudo_id} not found]" end else #puts "NOT A ZAZEN LINK" flush @text[0..0] end end |
#scan_wiki ⇒ Object
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/zena/parser/zazen_rules.rb', line 252 def scan_wiki #puts "WIKI:[#{@text}]" if @text =~ /\A([^\?])*/m flush $& scan_wiki_link else # nothing interesting flush end end |
#scan_wiki_link ⇒ Object
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/parser/zazen_rules.rb', line 263 def scan_wiki_link if @text =~ /\A\?(\w[^\?]*?\w)\?([^\w:]|:([^\s<]+))/m #puts "WIKI:[#{$&}]" eat $& title = $1 url = $3 # wiki reference ?zafu? or ?zafu?:http://... if url if url =~ /[^\w0-9]$/ # keep trailing punctuation store @helper.make_wiki_link(:title=>title, :url=>url[0..-2], :node=>@context[:node]) + $& else store @helper.make_wiki_link(:title=>title, :url=>url, :node=>@context[:node]) end else store @helper.make_wiki_link(:title=>title, :node=>@context[:node]) + $2 end else # false alert flush @text[0..0] end end |
#start(mode) ⇒ Object
18 19 20 21 |
# File 'lib/zena/parser/zazen_rules.rb', line 18 def start(mode) @helper = @options[:helper] # we do nothing, everything is done when 'render' is called end |
#store(str) ⇒ Object
rewrite store to optimize for our ‘text only’ parser
24 25 26 |
# File 'lib/zena/parser/zazen_rules.rb', line 24 def store(str) @blocks << str end |