fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / khtml / misc / gennames.py
blobe7d1851b9c1687b96da9ed7620270e3e90c64256
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 cache = {}
5 namesList = []
7 additionalCaseSensitiveAttrs = []
8 lastCaseInsensitiveAttr = 0
11 def constName(prefix, i):
12 return (prefix + "_%s") % i.upper().replace("-", "_");
14 def parseFile(fname, isAttrs, isHtmlAttrs):
15 global cache
16 global namesList
18 global additionalCaseSensitiveAttrs
19 global lastCaseInsensitiveAttr
21 prefix = "ID"
22 if isAttrs:
23 prefix = "ATTR"
25 f = open(fname, "r")
26 for i in f.xreadlines():
27 i = i.strip()
28 if i.startswith("#") or i == "": continue
29 if isHtmlAttrs and i.startswith("END_CI"):
30 lastCaseInsensitiveAttr = len(cache)
31 continue
33 # don't add it twice to names list
34 cons = constName(prefix, i)
35 alreadyDefined = [k for k in namesList if k[2] == cons]
36 if len(alreadyDefined) > 0:
37 continue
39 if i.upper() == "TEXT" and isAttrs: # hack to get ID_TEXT and ATTR_TEXT be different
40 key = len(cache) + 1
41 cache["TEXT#"] = -1
42 else:
43 key = cache.get(i, len(cache) + 1)
45 if isHtmlAttrs and lastCaseInsensitiveAttr > 0 and key <= len(cache):
46 additionalCaseSensitiveAttrs.append(cons)
48 cache[i] = key
49 namesList.append([key, i, cons])
50 f.close();
52 def parseTags(fname):
53 parseFile(fname, False, False)
55 def parseAttrs(fname, isHtml):
56 parseFile(fname, True, isHtml)
58 # parse htmltags.in
59 parseTags("htmltags.in");
60 lastTagId = len(cache)
62 # parse htmlattrs.in file
63 parseAttrs("htmlattrs.in", True)
64 lastAttrId = len(cache)
66 # parse SVG tags, attrs
67 parseTags("../svg/svgtags.in")
68 parseAttrs("../svg/svgattrs.in", False)
70 # parse XLink attrs
71 parseAttrs("../svg/xlinkattrs.in", False)
73 # sort the list
74 def func(a, b):
75 return cmp(a[0], b[0]) or -cmp(a[2], b[2])
76 namesList.sort(func)
78 ########################################
79 # END OF PARSING, START WRITING FILES
80 ########################################
82 out = open("htmlnames.h", "w")
83 out.write("/* This file is automatically generated from htmltags.in and htmlattrs.in by gennames.py, do not edit */\n")
84 out.write("/* Copyright 2008 Vyacheslav Tokarev */\n")
85 out.write("\n")
86 out.write("#ifndef HTMLNames_h\n")
87 out.write("#define HTMLNames_h\n")
88 out.write("\n")
89 out.write("#include \"misc/idstring.h\"\n")
90 out.write("\n")
91 out.write("#define XHTML_NAMESPACE \"http://www.w3.org/1999/xhtml\"\n")
92 out.write("#define SVG_NAMESPACE \"http://www.w3.org/2000/svg\"\n")
93 out.write("#define XLINK_NAMESPACE \"http://www.w3.org/1999/xlink\"\n")
94 out.write("\n")
95 for i in namesList:
96 if i[2].startswith("ATTR"):
97 out.write("#define %s %d\n" % (i[2], 65536 + i[0]))
98 else:
99 out.write("#define %s %d\n" % (i[2], i[0]))
100 out.write("#define ID_LAST_TAG %d\n" % (lastTagId))
101 out.write("#define ID_CLOSE_TAG 16384\n")
102 out.write("#define ATTR_LAST_ATTR %d\n" % (lastAttrId))
103 out.write("#define ATTR_LAST_CI_ATTR %d\n" % (lastCaseInsensitiveAttr))
104 out.write("\n")
105 s = "((localNamePart(id)) > ATTR_LAST_CI_ATTR"
106 for i in additionalCaseSensitiveAttrs:
107 s = s + " || (id) == %s" % i
108 s = s + ")"
109 out.write("#define caseSensitiveAttr(id) (%s)\n" % s)
110 out.write("\n")
111 out.write("namespace DOM {\n\
113 #define NodeImpl_IdNSMask 0xffff0000\n\
114 #define NodeImpl_IdLocalMask 0x0000ffff\n\
116 const quint16 xhtmlNamespace = 0;\n\
117 const quint16 emptyNamespace = 1;\n\
118 const quint16 svgNamespace = 2;\n\
119 const quint16 xlinkNamespace = 3;\n\
120 const quint16 anyNamespace = 0xffff;\n\
121 const quint16 anyLocalName = 0xffff;\n\
122 const quint16 emptyPrefix = 0;\n\
124 inline quint16 localNamePart(quint32 id) { return id & NodeImpl_IdLocalMask; }\n\
125 inline quint16 namespacePart(quint32 id) { return (((unsigned int)id) & NodeImpl_IdNSMask) >> 16; }\n\
126 inline quint32 makeId(quint16 n, quint16 l) { return (n << 16) | l; }\n\
128 const quint32 anyQName = makeId(anyNamespace, anyLocalName);\n\
130 }\n")
131 out.write("\n")
132 out.write("namespace khtml {\n\
134 class NamespaceFactory {\n\
135 public:\n\
136 static IDTable<NamespaceFactory>* idTable() {\n\
137 return s_idTable;\n\
138 }\n\
139 static IDTable<NamespaceFactory>* initIdTable();\n\
140 protected:\n\
141 static IDTable<NamespaceFactory>* s_idTable;\n\
142 };\n\
144 class LocalNameFactory {\n\
145 public:\n\
146 static IDTable<LocalNameFactory>* idTable() {\n\
147 return s_idTable;\n\
148 }\n\
149 static IDTable<LocalNameFactory>* initIdTable();\n\
150 protected:\n\
151 static IDTable<LocalNameFactory>* s_idTable;\n\
152 };\n\
154 class PrefixFactory {\n\
155 public:\n\
156 static IDTable<PrefixFactory>* idTable() {\n\
157 return s_idTable;\n\
158 }\n\
159 static IDTable<PrefixFactory>* initIdTable();\n\
160 protected:\n\
161 static IDTable<PrefixFactory>* s_idTable;\n\
162 };\n")
163 out.write("\n")
164 out.write("}\n")
165 out.write("\n")
166 out.write("namespace DOM {\n\
168 typedef khtml::IDString<khtml::NamespaceFactory> NamespaceName;\n\
169 typedef khtml::IDString<khtml::LocalNameFactory> LocalName;\n\
170 typedef khtml::IDString<khtml::PrefixFactory> PrefixName;\n\
171 extern PrefixName emptyPrefixName;\n\
172 extern LocalName emptyLocalName;\n\
173 extern NamespaceName emptyNamespaceName;\n\
175 QString getPrintableName(int id);\n\n")
176 out.write("}\n\n")
177 out.write("#endif\n")
178 out.close()
180 temp = ""
181 prev = 0
182 for i in namesList:
183 if prev and prev[0] == i[0]: continue
184 if i[2] == "ID_TEXT" or i[2] == "ID_COMMENT":
185 temp = temp + (" s_idTable->addHiddenMapping(%s, \"%s\");\n" % (i[2], i[1]))
186 else:
187 temp = temp + (" s_idTable->addStaticMapping(localNamePart(%s), \"%s\");\n" % (i[2], i[1]))
188 prev = i
190 out = open("htmlnames.cpp", "w")
191 out.write("#include \"misc/htmlnames.h\"\n")
192 out.write("#include \"dom/dom_string.h\"\n")
193 out.write("\nusing namespace DOM;\n")
194 out.write("\n")
195 out.write("namespace khtml {\n\n")
196 out.write("IDTable<NamespaceFactory>* NamespaceFactory::s_idTable;\n\
197 IDTable<NamespaceFactory>* NamespaceFactory::initIdTable()\n\
198 {\n\
199 s_idTable = new IDTable<NamespaceFactory>();\n\
200 s_idTable->addStaticMapping(DOM::xhtmlNamespace, XHTML_NAMESPACE);\n\
201 s_idTable->addStaticMapping(DOM::emptyNamespace, DOMString());\n\
202 s_idTable->addStaticMapping(DOM::svgNamespace, SVG_NAMESPACE);\n\
203 s_idTable->addStaticMapping(DOM::xlinkNamespace, XLINK_NAMESPACE);\n\
204 return s_idTable;\n\
205 }\n\
207 IDTable<LocalNameFactory>* LocalNameFactory::s_idTable;\n\
208 IDTable<LocalNameFactory>* LocalNameFactory::initIdTable()\n\
209 {\n\
210 s_idTable = new IDTable<LocalNameFactory>();\n\
211 s_idTable->addStaticMapping(0, DOMString());\n\
213 return s_idTable;\n\
214 }\n\
216 IDTable<PrefixFactory>* PrefixFactory::s_idTable;\n\
217 IDTable<PrefixFactory>* PrefixFactory::initIdTable()\n\
218 {\n\
219 s_idTable = new IDTable<PrefixFactory>();\n\
220 s_idTable->addStaticMapping(DOM::emptyPrefix, DOMString());\n\
221 return s_idTable;\n\
222 }\n" % temp)
223 out.write("\n}\n")
224 out.write("\n")
225 out.write("namespace DOM {\n\n")
226 out.write("LocalName emptyLocalName;// = LocalName::fromId(0);\n")
227 out.write("PrefixName emptyPrefixName;// = PrefixName::fromId(0);\n")
228 out.write("NamespaceName emptyNamespaceName;// = NamespaceName::fromId(0);\n")
229 out.write("\n")
230 out.write("""QString getPrintableName(int id) {
231 QString local = QString("null");
232 QString namespacename = QString("null");
234 if (localNamePart(id) != anyLocalName) {
235 DOMString localName = LocalName::fromId(localNamePart(id)).toString();
236 if (localName.implementation())
237 local = localName.string();
238 } else {
239 local = "*";
242 if (namespacePart(id) != anyNamespace) {
243 DOMString namespaceName = NamespaceName::fromId(namespacePart(id)).toString();
244 if (namespaceName.implementation())
245 namespacename = namespaceName.string();
246 } else {
247 namespacename = "*";
249 return "{ns:" + QString::number(namespacePart(id)) + ",[" + namespacename + "] local:" + QString::number(localNamePart(id)) + ",[" + local + "]}";
250 }\n""")
251 out.write("\n}\n")
252 out.close()
254 # kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;