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 if not value: 51 return "0"; 52 assert(value[0] == "U") 53 assert(value[1] == "+") 54 return "0x" + value[2:] 55 56 57def offset_table_entry(offset): 58 return " &staticEntityTable[%s]," % offset 59 60 61program_name = os.path.basename(__file__) 62if len(sys.argv) < 4 or sys.argv[1] != "-o": 63 # Python 3, change to: print("Usage: %s -o OUTPUT_FILE INPUT_FILE" % program_name, file=sys.stderr) 64 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) 65 exit(1) 66 67output_path = sys.argv[2] 68input_path = sys.argv[3] 69 70html_entity_names_file = open(input_path) 71entries = list(csv.reader(html_entity_names_file)) 72html_entity_names_file.close() 73 74entries.sort(key = lambda entry: entry[ENTITY]) 75entity_count = len(entries) 76 77output_file = open(output_path, "w") 78 79output_file.write("""/* 80 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 81 * 82 * Redistribution and use in source and binary forms, with or without 83 * modification, are permitted provided that the following conditions 84 * are met: 85 * 1. Redistributions of source code must retain the above copyright 86 * notice, this list of conditions and the following disclaimer. 87 * 2. Redistributions in binary form must reproduce the above copyright 88 * notice, this list of conditions and the following disclaimer in the 89 * documentation and/or other materials provided with the distribution. 90 * 91 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 92 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 95 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 96 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 97 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 98 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 99 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 100 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 101 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 102 */ 103 104// THIS FILE IS GENERATED BY WebCore/html/parser/create-html-entity-table 105// DO NOT EDIT (unless you are a ninja)! 106 107#include "config.h" 108#include "HTMLEntityTable.h" 109 110namespace WebCore { 111 112namespace { 113""") 114 115for entry in entries: 116 output_file.write("static const UChar %s[] = %s;\n" % ( 117 convert_entity_to_cpp_name(entry[ENTITY]), 118 convert_entity_to_uchar_array(entry[ENTITY]))) 119 120output_file.write(""" 121static const HTMLEntityTableEntry staticEntityTable[%s] = {\n""" % entity_count) 122 123index = {} 124offset = 0 125for entry in entries: 126 letter = entry[ENTITY][0] 127 if letter not in index: 128 index[letter] = offset 129 values = entry[VALUE].split(' ') 130 assert len(values) <= 2, values 131 output_file.write(' { %s, %s, %s, %s },\n' % ( 132 convert_entity_to_cpp_name(entry[ENTITY]), 133 len(entry[ENTITY]), 134 convert_value_to_int(values[0]), 135 convert_value_to_int(values[1] if len(values) >= 2 else ""))) 136 offset += 1 137 138output_file.write("""}; 139 140""") 141 142output_file.write("static const HTMLEntityTableEntry* uppercaseOffset[] = {\n") 143for letter in string.ascii_uppercase: 144 output_file.write("%s\n" % offset_table_entry(index[letter])) 145output_file.write("%s\n" % offset_table_entry(index['a'])) 146output_file.write("""}; 147 148static const HTMLEntityTableEntry* lowercaseOffset[] = {\n""") 149for letter in string.ascii_lowercase: 150 output_file.write("%s\n" % offset_table_entry(index[letter])) 151output_file.write("%s\n" % offset_table_entry(entity_count)) 152output_file.write("""}; 153 154} 155 156const HTMLEntityTableEntry* HTMLEntityTable::firstEntryStartingWith(UChar c) 157{ 158 if (c >= 'A' && c <= 'Z') 159 return uppercaseOffset[c - 'A']; 160 if (c >= 'a' && c <= 'z') 161 return lowercaseOffset[c - 'a']; 162 return 0; 163} 164 165const HTMLEntityTableEntry* HTMLEntityTable::lastEntryStartingWith(UChar c) 166{ 167 if (c >= 'A' && c <= 'Z') 168 return uppercaseOffset[c - 'A' + 1] - 1; 169 if (c >= 'a' && c <= 'z') 170 return lowercaseOffset[c - 'a' + 1] - 1; 171 return 0; 172} 173 174const HTMLEntityTableEntry* HTMLEntityTable::firstEntry() 175{ 176 return &staticEntityTable[0]; 177} 178 179const HTMLEntityTableEntry* HTMLEntityTable::lastEntry() 180{ 181 return &staticEntityTable[%s - 1]; 182} 183 184} 185""" % entity_count) 186