-
Notifications
You must be signed in to change notification settings - Fork 2
/
IRWebAPIKitEntityDefinesGenerator.rb
executable file
·120 lines (63 loc) · 2.56 KB
/
IRWebAPIKitEntityDefinesGenerator.rb
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
113
#!/usr/bin/env ruby
# IRWebAPIKit Entity Defines Generator
#
# Evadne Wu at Iridia, 2010
#
# This script downloads DTDs from w3.org, then parses them, then makes a special header for inclusion into IRWebAPIKit for the entity replacing helper method to consume.
require 'open-uri'
# Retrieve definitions
retrievedDefinitions = {}
EntityDefinitionURIPrefix = "http://www.w3.org/TR/xhtml1/DTD" # replace this with localhost to use predownloaded copies
# EntityDefinitionURIPrefix = "http://localhost/~evadne/"
LineDefinition = /\<!ENTITY ([^\s]+)\s+\"(&)(#\d+;)(#\d+;)*\"/ # <!ENTITY agrave "à"> or <!ENTITY lt "&#60;">
([
"#{EntityDefinitionURIPrefix}xhtml-lat1.ent",
"#{EntityDefinitionURIPrefix}xhtml-symbol.ent",
"#{EntityDefinitionURIPrefix}xhtml-special.ent"
]).each { | remoteURI |
puts "Downloading DTD from #{remoteURI}"
open (remoteURI) { | contents | contents.each_line { |line|
next unless (matches = LineDefinition.match line)
entityName = "&#{matches[1]};"
entityNumber = matches[2] + ((matches[4]) ? matches[4] : matches[3]) # amp, lt are culprits
puts "Adding #{entityName} => #{entityNumber}"
retrievedDefinitions[entityName] = entityNumber
}}
puts "\n"
}
# Dump everything out
HeaderHeaderFilename = "IRWebAPIKitEntityDefinesHeader.h"
HeaderFilename = "IRWebAPIKitEntityDefines.h"
HeaderFooterFilename = "IRWebAPIKitEntityDefinesFooter.h"
headerHeader = File.open(HeaderHeaderFilename, 'r')
headerFile = File.open(HeaderFilename, 'w')
headerFooter = File.open(HeaderFooterFilename, 'r')
now = Time.new
headerHeader.each_line { |line|
headerFile.write line
.gsub("#YEAR", "#{now.year}")
.gsub("#DATE", "#{now.year}/#{now.month}/#{now.day}")
.gsub("#AUTHOR", "Iridia Automata")
}
headerHeader.close
def makeDictionary (methodName, hash, keysToValues)
returnedString = <<-END
static inline NSDictionary* #{methodName} () {
return [NSDictionary dictionaryWithObjectsAndKeys:
END
hash.each { | key, value | returnedString << <<-END
@"#{keysToValues ? value : key}", @"#{keysToValues ? key : value}",
END
}
returnedString << <<-END
nil];
}
END
return returnedString.gsub(/^\t\t\t/, "")
end
headerFile.write makeDictionary("IRWebAPIKitXMLEntityNumbersFromNames", retrievedDefinitions, true)
2.times { headerFile.write "\n" }
headerFile.write makeDictionary("IRWebAPIKitXMLEntityNamesFromNumbers", retrievedDefinitions, false)
headerFooter.each_line { |line| headerFile.write line }
headerFooter.close
headerFile.close