Module: Runo::Parser
- Defined in:
- lib/_parser.rb
Overview
- Author
-
Akira FUNAI
- Copyright
-
Copyright © 2009 Akira FUNAI
Class Method Summary collapse
- .gsub_action_tmpl(html, &block) ⇒ Object
- .gsub_block(html, class_name, &block) ⇒ Object
- .gsub_scalar(html, &block) ⇒ Object
- .parse_block(open_tag, inner_html, close_tag, action = :index, xml = false) ⇒ Object
- .parse_html(html, action = :index, xml = false) ⇒ Object
- .parse_token(prefix, token, meta = {}) ⇒ Object
- .parse_xml(html, action = :index) ⇒ Object
- .scan_inner_html(s, name) ⇒ Object
- .scan_tokens(s) ⇒ Object
- .scrape_label(html) ⇒ Object
- .scrape_meta(html) ⇒ Object
- .supplement_sd(tmpl, workflow, m) ⇒ Object
- .supplement_ss(tmpl, action) ⇒ Object
Class Method Details
.gsub_action_tmpl(html, &block) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/_parser.rb', line 58 def gsub_action_tmpl(html, &block) rex_klass = /(?:\w+\-)?(?:action|view|navi|submit|done)\w*/ gsub_block(html, rex_klass) {|open, inner, close| klass = open[/class=(?:"|"[^"]*?\s)(#{rex_klass})(?:"|\s)/, 1] id, action = (klass =~ /-/) ? klass.split('-', 2) : [nil, klass] block.call(id, action.intern, open, inner, close) } end |
.gsub_block(html, class_name, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/_parser.rb', line 67 def gsub_block(html, class_name, &block) rex_open_tag = /\s*<(\w+)[^>]+?class=(?:"|"[^"]*?\s)#{class_name}(?:"|\s).*?>\n?/i out = '' s = StringScanner.new html until s.eos? if s.scan rex_open_tag open_tag = s[0] inner_html, close_tag = scan_inner_html(s, s[1]) close_tag << "\n" if s.scan /\n/ out << block.call(open_tag, inner_html, close_tag) else out << s.scan(/.+?(?=\t| |<|\z)/m) end end out end |
.gsub_scalar(html, &block) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/_parser.rb', line 84 def gsub_scalar(html, &block) out = '' s = StringScanner.new html until s.eos? if s.scan /\$\((\w+)(?:\s+|\s*=\s*)([\w\-]+)\s*/m out << block.call(s[1], {:klass => s[2]}.merge(scan_tokens(s))) else out << s.scan(/.+?(?=\$|\w|<|\z)/m) end end out end |
.parse_block(open_tag, inner_html, close_tag, action = :index, xml = false) ⇒ Object
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 180 181 182 183 184 |
# File 'lib/_parser.rb', line 144 def parse_block(open_tag, inner_html, close_tag, action = :index, xml = false) open_tag.sub!(/id=".*?"/i, 'id="@(name)"') workflow = open_tag[/class=(?:"|".*?\s)(?:app|runo)-(\w+)/, 1] # "runo" is obsolete at 0.1.1 if inner_html =~ /<(?:\w+).+?class=(?:"|"[^"]*?\s)(model|body)(?:"|\s)/i # "body" is obsolete at 0.1.1 item_html = '' sd_tmpl = gsub_block(inner_html, Regexp.last_match(1)) {|open, inner, close| item_html = open + inner + close '$()' } else item_html = inner_html sd_tmpl = '$()' end tmpl = {} sd_tmpl = gsub_action_tmpl(sd_tmpl) {|id, act, open, inner, close| inner = gsub_action_tmpl(inner) {|i, a, *t| tmpl[a] = t.join "$(.#{a})" } tmpl[act] = open + inner + close "$(.#{act})" } tmpl[action] = "#{open_tag}#{sd_tmpl}#{close_tag}" tmpl[action].gsub!(/\s*class=".*?"/,'') if xml = Runo::Parser.parse_html(item_html, action, xml) supplement_ss([:tmpl][action], action) unless xml || workflow == 'attachment' sd = { :klass => 'set-dynamic', :workflow => workflow.downcase, :tmpl => tmpl, :item => { 'default' => , }, } (inner_html =~ /\A\s*<!--(.+?)-->/m) ? sd.merge(scan_tokens(StringScanner.new($1))) : sd end |
.parse_html(html, action = :index, xml = false) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 |
# File 'lib/_parser.rb', line 10 def parse_html(html, action = :index, xml = false) item = {} html = gsub_block(html, '(?:app|runo)-\w+') {|open, inner, close| # "runo" is obsolete at 0.1.1 id = open[/id="(.+?)"/i, 1] || 'main' item[id] = parse_block(open, inner, close, action, xml) "$(#{id})" } html = gsub_action_tmpl(html) {|id, act, open, inner, close| id ||= 'main' if item[id] item[id][:tmpl] ||= {} inner = gsub_action_tmpl(inner) {|i, a, *t| item[id][:tmpl][a] = t.join "$(.#{a})" } item[id][:tmpl][act] = open + inner + close "$(#{id}.#{act})" else open + inner + close end } item.each {|id, | m = Proc.new {|a| html.include?("$(#{id}.#{a})") || [:tmpl][action].include?("$(.#{a})") } supplement_sd([:tmpl][action], [:workflow], m) html.sub!("$(#{id})", "$(#{id}.message)\\&") unless ( [:workflow] == 'attachment' || m.call('message') ) } unless xml html = gsub_scalar(html) {|id, | item[id] = "$(#{id})" } html.gsub!(/\s*class=".*?"/,'') if xml = html .merge( :label => [:label] || scrape_label(html), :item => item, :tmpl => {action => html} ) end |
.parse_token(prefix, token, meta = {}) ⇒ Object
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 |
# File 'lib/_parser.rb', line 186 def parse_token(prefix, token, = {}) case prefix when ':' [:default] = token when ';' [:defaults] ||= [] [:defaults] << token when ',' [:options] ||= [] [:options] << token else case token when /^(-?\d+)?\.\.(-?\d+)?$/ [:min] = $1.to_i if $1 [:max] = $2.to_i if $2 when /^(\d+)\*(\d+)$/ [:width] = $1.to_i [:height] = $2.to_i else [:tokens] ||= [] [:tokens] << token end end end |
.parse_xml(html, action = :index) ⇒ Object
54 55 56 |
# File 'lib/_parser.rb', line 54 def parse_xml(html, action = :index) parse_html(html, action, xml = true) end |
.scan_inner_html(s, name) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/_parser.rb', line 212 def scan_inner_html(s, name) contents = '' gen = 1 until s.eos? || (gen < 1) contents << s.scan(/(.*?)(<#{name}|<\/#{name}>|\z)/m) gen += 1 if s[2] == "<#{name}" gen -= 1 if s[2] == "</#{name}>" end contents.gsub!(/\A\n+/, '') contents.gsub!(/[\t ]*<\/#{name}>\z/, '') [contents, $&] end |
.scan_tokens(s) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/_parser.rb', line 225 def scan_tokens(s) = {} until s.eos? || s.scan(/\)/) prefix = s[1] if s.scan /([:;,])?\s*/ if s.scan /(["'])(.*?)(\1|$)/ token = s[2] elsif s.scan /[^\s\):;,]+/ token = s[0] end prefix ||= ',' if s.scan /(?=,)/ # 1st element of options prefix ||= ';' if s.scan /(?=;)/ # 1st element of defaults parse_token(prefix, token, ) s.scan /\s+/ end end |
.scrape_label(html) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/_parser.rb', line 106 def scrape_label(html) if html.sub!(/\A((?:[^<]*<!--)?[^<]*<[^>]*title=")([^"]+)/, '\\1') label_plural = $2.to_s.split(/,/).collect {|s| s.strip } label_plural *= 4 if label_plural.size == 1 label = label_plural.first Runo::I18n.msg[label] ||= label_plural label end end |
.scrape_meta(html) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/_parser.rb', line 97 def (html) = {} html.gsub!(/(?:^\s+)?<meta[^>]*name="runo-([^"]+)[^>]*content="([^"]+).*?>\n?/i) { [$1.intern] = $2.include?(',') ? $2.split(/\s*,\s*/) : $2 '' } end |
.supplement_sd(tmpl, workflow, m) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/_parser.rb', line 116 def supplement_sd(tmpl, workflow, m) tmpl << '$(.navi)' unless m.call 'navi' unless workflow == 'attachment' tmpl << '$(.submit)' unless m.call 'submit' tmpl << '$(.action_create)' unless m.call 'action_create' end tmpl end |
.supplement_ss(tmpl, action) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/_parser.rb', line 125 def supplement_ss(tmpl, action) if action == :summary tmpl.sub!( /\$\(.*?\)/m, '<a href="$(.uri_detail)">\&</a>' ) unless tmpl.include? '$(.uri_detail)' else tmpl.sub!( /\$\([^\.]*?\)/m, '$(.a_update)\&</a>' ) unless tmpl.include? '$(.action_update)' tmpl.sub!( /.*\$\(.*?\)/m, '\&$(.hidden)' ) unless tmpl.include? '$(.hidden)' end tmpl end |