Added HTML parser with embedded CSS and Javascript.
[ohcount] / ext / ohcount_native / ragel_parsers / construct_embedded.rb
1 =begin
2   construct_embedded written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 =end
4
5 def has_embedded?(parser_file)
6   return IO.read(parser_file).include?('#EMBED')
7 end
8
9 def construct_language(parser_file)
10   parser_text = IO.read(parser_file).gsub(/#EMBED\([\w_]+\)/) do |elang|
11     lang = elang.scan(/^#EMBED\(([\w_]+)\)/)[0][0]
12     eparser_file = lang + '.rl'
13     if File.exists?(eparser_file)
14       eparser = IO.read(eparser_file)
15       ragel = eparser.scan(/%%\{(.+?)\}%%/m)[0][0]
16       # eliminate machine definition, writes, and includes
17       ragel.gsub!(/^\s*machine[^;]+;\s+write[^;]+;\s+include[^;]+;\s+/, '')
18       "}%%\n%%{\n#{ragel}"
19     else
20       ''
21     end
22   end
23   f = File.new(parser_file + '.tmp', 'w')
24   f.write(parser_text)
25   f.close
26 end