1#!/usr/bin/env python
2# Copyright (c) 2010 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7# 
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17# 
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import csv
31import os.path
32import string
33import sys
34
35ENTITY = 0
36VALUE = 1
37
38def convert_entity_to_cpp_name(entity):
39    postfix = "EntityName"
40    if entity[-1] == ";":
41        return "%sSemicolon%s" % (entity[:-1], postfix)
42    return "%s%s" % (entity, postfix)
43
44
45def convert_entity_to_uchar_array(entity):
46    return "{'%s'}" % "', '".join(entity)
47
48
49def convert_value_to_int(value):
50    assert(value[0] == "U")
51    assert(value[1] == "+")
52    return "0x" + value[2:]
53
54
55def offset_table_entry(offset):
56    return "    &staticEntityTable[%s]," % offset
57
58
59program_name = os.path.basename(__file__)
60if len(sys.argv) < 4 or sys.argv[1] != "-o":
61    # Python 3, change to: print("Usage: %s -o OUTPUT_FILE INPUT_FILE" % program_name, file=sys.stderr)
62    sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name)
63    exit(1)
64
65output_path = sys.argv[2]
66input_path = sys.argv[3]
67
68html_entity_names_file = open(input_path)
69entries = list(csv.reader(html_entity_names_file))
70html_entity_names_file.close()
71
72entries.sort(key = lambda entry: entry[ENTITY])
73entity_count = len(entries)
74
75output_file = open(output_path, "w")
76
77output_file.write("""/*
78 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
79 *
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
82 * are met:
83 * 1. Redistributions of source code must retain the above copyright
84 *    notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 *    notice, this list of conditions and the following disclaimer in the
87 *    documentation and/or other materials provided with the distribution.
88 *
89 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
90 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
92 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
93 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
94 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
95 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
96 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
97 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
98 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
99 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
100 */
101
102// THIS FILE IS GENERATED BY WebCore/html/parser/create-html-entity-table
103// DO NOT EDIT (unless you are a ninja)!
104
105#include "config.h"
106#include "HTMLEntityTable.h"
107
108namespace WebCore {
109
110namespace {
111""")
112
113for entry in entries:
114    output_file.write("const UChar %sEntityName[] = %s;" % (
115        convert_entity_to_cpp_name(entry[ENTITY]),
116        convert_entity_to_uchar_array(entry[ENTITY])))
117
118output_file.write("""
119HTMLEntityTableEntry staticEntityTable[%s] = {""" % entity_count)
120
121index = {}
122offset = 0
123for entry in entries:
124    letter = entry[ENTITY][0]
125    if not index.get(letter):
126        index[letter] = offset
127    output_file.write('    { %sEntityName, %s, %s },' % (
128        convert_entity_to_cpp_name(entry[ENTITY]),
129        len(entry[ENTITY]),
130        convert_value_to_int(entry[VALUE])))
131    offset += 1
132
133output_file.write("""};
134""")
135
136output_file.write("const HTMLEntityTableEntry* uppercaseOffset[] = {")
137for letter in string.ascii_uppercase:
138    output_file.write(offset_table_entry(index[letter]))
139output_file.write(offset_table_entry(index['a']))
140output_file.write("""};
141
142const HTMLEntityTableEntry* lowercaseOffset[] = {""")
143for letter in string.ascii_lowercase:
144    output_file.write(offset_table_entry(index[letter]))
145output_file.write(offset_table_entry(entity_count))
146output_file.write("""};
147
148}
149
150const HTMLEntityTableEntry* HTMLEntityTable::firstEntryStartingWith(UChar c)
151{
152    if (c >= 'A' && c <= 'Z')
153        return uppercaseOffset[c - 'A'];
154    if (c >= 'a' && c <= 'z')
155        return lowercaseOffset[c - 'a'];
156    return 0;
157}
158
159const HTMLEntityTableEntry* HTMLEntityTable::lastEntryStartingWith(UChar c)
160{
161    if (c >= 'A' && c <= 'Z')
162        return uppercaseOffset[c - 'A' + 1] - 1;
163    if (c >= 'a' && c <= 'z')
164        return lowercaseOffset[c - 'a' + 1] - 1;
165    return 0;
166}
167
168const HTMLEntityTableEntry* HTMLEntityTable::firstEntry()
169{
170    return &staticEntityTable[0];
171}
172
173const HTMLEntityTableEntry* HTMLEntityTable::lastEntry()
174{
175    return &staticEntityTable[%s - 1];
176}
177
178}
179""" % entity_count)
180