Method: Comet::Generator#generate

Defined in:
lib/comet-html/generator.rb

#generateObject



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
180
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
# File 'lib/comet-html/generator.rb', line 129

def generate
  src = File.read @filepath
  src.gsub! /<template/i,  "<html"
  src.gsub! /<\/template/i, "</html>"

  document = Nokogiri::HTML.parse(src) do |config| config.strict end
  
  html = document.xpath("html").first
  head = document.xpath("//head")
  body = document.xpath("//body")
  raise LoadError.new "could not load html template `#{@filepath}`" if html.nil?
  raise LoadError.new "missing body element in `#{@filepath}`" if body.first.nil?

  Context.reset
  Context.filename = @filepath
  if !@config.nil? && !@config["elements"].nil?
    Context.load_global_element_types @config["elements"]
  end
  
  load_includes head
  
  fatal_error = nil
  document.errors.each do |error|
    if is_error_fatal? error
      puts "[comet-html] #{@filepath}:#{error.line}:#{error.column}: #{error.message}"
      fatal_error ||= error
      error_count += 1
    end
  end
  raise HtmlParseError.new fatal_error unless fatal_error.nil?

  main_element = Comet::Class.new body
  main_element.typename = if html["classname"].nil?
    File.basename(@filepath, ".html").to_s.camelize
  else
    html["classname"]
  end
  main_element.superclass = html["extends"] unless html["extends"].nil?
  main_element.probe

  main_element.inline_code = ""
  head.css("script").each do |script|
    main_element.inline_code += script.text.strip + "\n"
  end

  head.css("attribute").each do |attribute|
    main_element.refs << (CppReference.new attribute, attribute["type"], attribute["name"], attribute["value"])
  end

  define_name = "__COMET_CUSTOM_ELEMENT_#{main_element.typename.upcase}__"
  header_code = ""
  source_code = ""
  Context.classes.reverse.each do |object|
    next if object.should_skip?
    header_code += HeaderGenerator.new(object).generate + "\n"
    source_code += SourceGenerator.new(object).generate + "\n"
  end

  includes_str = prepare_includes head
  
  header = "#ifndef  \#{define_name}\n# define \#{define_name}\n\n# include <comet/bindable.hpp>\n# include <comet/repeater.hpp>\n# include <comet/slot_element.hpp>\n# include <comet/comment_element.hpp>\n# include <comet/custom_element.hpp>\n# include <comet/signal.hpp>\n\#{includes_str}\n\nnamespace HtmlTemplate\n{\n\#{append_classes_predeclaration}\n\#{header_code}\n}\n\n#endif\n"

  source = "#include <comet/lexical_cast.hpp>\n#include \"\#{compiled_header_path}\"\n\n\#{source_code}\n"

  { header: header, source: source }
end