Class: O2Html

Inherits:
Object
  • Object
show all
Defined in:
lib/o2html.rb

Defined Under Namespace

Classes: Ajax, Div, InputBox, Sound, SrButton

Instance Method Summary collapse

Constructor Details

#initialize(s, debug: false) ⇒ O2Html

Returns a new instance of O2Html.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/o2html.rb', line 65

def initialize(s, debug: false)

  @s, @debug = s, debug

  @lookup = {
    div: Div,
    inputbox: InputBox,
    ajax: Ajax,
    sr_button: SrButton,
    sound: Sound
  }

end

Instance Method Details

#to_cssObject



79
80
81
# File 'lib/o2html.rb', line 79

def to_css()
  @css
end

#to_htmlObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/o2html.rb', line 83

def to_html()
  
  userdoc = Rexle.new(@s)
  e = userdoc.root.element('body/controls')
  e.delete if e
  doc = Rexle.new(scan2html(Rexle.new(userdoc.xml).to_a))
  
  puts 'before htmltocss: ' + doc.xml.inspect if @debug
  htc = HtmlToCss.new(doc.xml)
  puts 'before to_css' if @debug
  @css = htc.to_css
  puts 'after htmltocss' if @debug
  
  doc.root.each_recursive {|e| e.attributes.delete :style }
  
  head = doc.root.element('head')
  
  if not head then
  
    head = Rexle::Element.new('head')
    doc.root.element('body').insert_before head 
    
  end
  
  style = Rexle::Element.new('style').add_text("\n" + @css + "\n\n")
  head.add style
  
  doc.xml pretty: true
  
end

#to_jsObject



114
115
# File 'lib/o2html.rb', line 114

def to_js()
end

#validateObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/o2html.rb', line 117

def validate()

  doc = Rexle.new(@s)
  puts doc.root.element('body/script').xml

  a = []
  doc.root.each_recursive do |e|

    type = e.attributes[:type]
    next unless type
    puts 'type: ' + type.inspect
    puts 'e: ' + e.name  if type
    a << "#{e.name} = @lookup[:#{type.to_sym}].new"
  end

  scripts = doc.root.xpath('//script').map(&:text).join
  a << scripts.gsub!(/_on/,'.on')

  a << 'sr1_onresponse("fun")'.gsub(/_on/,'.on')
  eval a.join("\n")
end