File: ent2rb.rb

package info (click to toggle)
rabbit 3.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,220 kB
  • sloc: ruby: 29,637; lisp: 309; makefile: 43; sh: 7
file content (65 lines) | stat: -rwxr-xr-x 1,289 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
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
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby

require "rexml/document"

def expand_ref(str)
  REXML::Text.unnormalize(str)
end

def expand_ext_ref(str, table)
  str.gsub(/%([^\s;]+);/) do |x|
    if table.has_key?($1)
      expand_ref(table[$1])
    else
      p table
      raise $1
    end
  end
end

external_params = {}
entities = {}
ARGF.each do |line|
  case line
  when /^<!ENTITY\s+%\s+(\w+)\s+"(\S+)"\s*>/
    # p ["%", $1, $2]
    external_params[$1] = expand_ref($2)
  when /^<!ENTITY\s+(\w+)\s+"(\S+)"\s*>\s*<!--\s*(.+)\s*-->/
    key = $1
    comment = $3.strip
    value = expand_ext_ref($2.gsub(/&#38;/, '&'), external_params)
    next if entities.key?(key)
    entities[key] = {
      :value   => value,
      :comment => comment,
    }
  end
end

File.open(File.join(*%W(lib rabbit parser ext entity)) + ".rb", "w") do |out|
  out.print <<-HEADER
require 'rabbit/element'

module Rabbit
  module Parser
    module Ext
      module Entity
        TABLE = {
  HEADER

  entities.keys.sort.each do |key|
    attributes = entities[key]
    value   = attributes[:value]
    comment = attributes[:comment]
    out.puts("          # #{comment}") unless comment.empty?
    out.puts("          #{key.dump} => #{value.dump},")
  end

  out.print <<-FOOTER
        }
      end
    end
  end
end
  FOOTER
end