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