Module: Bike::Parser

Defined in:
lib/_parser.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Class Method Summary collapse

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
83
# 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?
    out << skip_comment(s)
    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).to_s
    end
  end
  out
end

.gsub_scalar(html, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/_parser.rb', line 85

def gsub_scalar(html, &block)
  out = ''
  s = StringScanner.new html
  until s.eos?
    out << skip_comment(s)
    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(/.+?(?=\$|<!|\z)/m).to_s
    end
  end
  out
end

.parse_block(open_tag, inner_html, close_tag, action = :index, xml = false) ⇒ Object



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
185
186
187
188
189
190
191
192
193
194
# File 'lib/_parser.rb', line 154

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

  item_meta = Bike::Parser.parse_html(item_html, action, xml)
  supplement_ss(item_meta[:tmpl][action], action) unless xml || workflow == 'attachment'

  sd = {
    :klass    => 'set-dynamic',
    :workflow => workflow.downcase,
    :tmpl     => tmpl,
    :item     => {
      'default' => item_meta,
    },
  }
  (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, meta|
    m = Proc.new {|a| html.include?("$(#{id}.#{a})") || meta[:tmpl][action].include?("$(.#{a})") }
    supplement_sd(meta[:tmpl][action], meta[:workflow], m)
    html.sub!("$(#{id})", "$(#{id}.message)\\&") unless (
      meta[:workflow] == 'attachment' || m.call('message')
    )
  } unless xml

  html = gsub_scalar(html) {|id, meta|
    item[id] = meta
    "$(#{id})"
  }
  html.gsub!(/\s*class=".*?"/,'') if xml

  meta = scrape_meta html
  meta.merge(
    :label => meta[:label] || scrape_label(html),
    :item  => item,
    :tmpl  => {action => html}
  )
end

.parse_token(prefix, token, meta = {}) ⇒ Object



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
# File 'lib/_parser.rb', line 196

def parse_token(prefix, token, meta = {})
  case prefix
    when ':'
      meta[:default] = token
    when ';'
      meta[:defaults] ||= []
      meta[:defaults] << token
    when ','
      meta[:options] ||= []
      meta[:options] << token
    else
      case token
        when /^(-?\d+)?\.\.(-?\d+)?$/
          meta[:min] = $1.to_i if $1
          meta[:max] = $2.to_i if $2
        when /^(\d+)\*(\d+)$/
          meta[:width]  = $1.to_i
          meta[:height] = $2.to_i
        else
          meta[:tokens] ||= []
          meta[:tokens] << token
      end
  end
  meta
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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/_parser.rb', line 222

def scan_inner_html(s, name)
  open_tag  = "<#{name}"
  close_tag = {'!--' => '-->', '<![CDATA[' => ']]>'}[name] || "</#{name}>"
  contents = ''
  rex = /(.*?)(#{Regexp.quote(open_tag)}|#{Regexp.quote(close_tag)}|\z)/m
  gen = 1
  until s.eos? || (gen < 1)
    contents << s.scan(rex)
    gen += 1 if s[2] == open_tag
    gen -= 1 if s[2] == close_tag
  end
  contents.gsub!(/\A\n+/, '')
  contents.gsub!(/[\t ]*#{Regexp.quote(close_tag)}\z/, '')
  [contents, $&]
end

.scan_tokens(s) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/_parser.rb', line 238

def scan_tokens(s)
  meta = {}
  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, meta)
    s.scan /\s+/
  end
  meta
end

.scrape_label(html) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/_parser.rb', line 116

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
    Bike::I18n.msg[label] ||= label_plural
    label
  end
end

.scrape_meta(html) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/_parser.rb', line 107

def scrape_meta(html)
  meta = {}
  html.gsub!(/(?:^\s+)?<meta[^>]*name="bike-([^"]+)[^>]*content="([^"]+).*?>\n?/i) {
    meta[$1.intern] = $2.include?(',') ? $2.split(/\s*,\s*/) : $2
    ''
  }
  meta
end

.skip_comment(s) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/_parser.rb', line 99

def skip_comment(s)
  if s.scan /\s*<(!--|!\[CDATA\[)\n*/
    s[0] + scan_inner_html(s, s[1]).join + (s.scan(/\n/) || '')
  else
    ''
  end
end

.supplement_sd(tmpl, workflow, m) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/_parser.rb', line 126

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/_parser.rb', line 135

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