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_value_to_int(value): 46 if not value: 47 return "0"; 48 assert(value[0] == "U") 49 assert(value[1] == "+") 50 return "0x" + value[2:] 51 52 53def offset_table_entry(offset): 54 return " &staticEntityTable[%s]," % offset 55 56 57program_name = os.path.basename(__file__) 58if len(sys.argv) < 4 or sys.argv[1] != "-o": 59 # Python 3, change to: print("Usage: %s -o OUTPUT_FILE INPUT_FILE" % program_name, file=sys.stderr) 60 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) 61 exit(1) 62 63output_path = sys.argv[2] 64input_path = sys.argv[3] 65 66html_entity_names_file = open(input_path) 67entries = list(csv.reader(html_entity_names_file)) 68html_entity_names_file.close() 69 70entries.sort(key = lambda entry: entry[ENTITY]) 71entity_count = len(entries) 72 73output_file = open(output_path, "w") 74 75output_file.write("""/* 76 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 77 * Copyright (C) 2013 Apple 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 output_file.write("static const LChar %s[] = \"%s\";\n" % (convert_entity_to_cpp_name(entry[ENTITY]), entry[ENTITY])) 114 115output_file.write(""" 116static const HTMLEntityTableEntry staticEntityTable[%s] = {\n""" % entity_count) 117 118index = {} 119offset = 0 120for entry in entries: 121 letter = entry[ENTITY][0] 122 if letter not in index: 123 index[letter] = offset 124 values = entry[VALUE].split(' ') 125 assert len(values) <= 2, values 126 output_file.write(' { %s, %s, %s, %s },\n' % ( 127 convert_entity_to_cpp_name(entry[ENTITY]), 128 len(entry[ENTITY]), 129 convert_value_to_int(values[0]), 130 convert_value_to_int(values[1] if len(values) >= 2 else ""))) 131 offset += 1 132 133output_file.write("""}; 134 135""") 136 137output_file.write("static const HTMLEntityTableEntry* uppercaseOffset[] = {\n") 138for letter in string.ascii_uppercase: 139 output_file.write("%s\n" % offset_table_entry(index[letter])) 140output_file.write("%s\n" % offset_table_entry(index['a'])) 141output_file.write("""}; 142 143static const HTMLEntityTableEntry* lowercaseOffset[] = {\n""") 144for letter in string.ascii_lowercase: 145 output_file.write("%s\n" % offset_table_entry(index[letter])) 146output_file.write("%s\n" % offset_table_entry(entity_count)) 147output_file.write("""}; 148 149} 150 151const HTMLEntityTableEntry* HTMLEntityTable::firstEntryStartingWith(UChar c) 152{ 153 if (c >= 'A' && c <= 'Z') 154 return uppercaseOffset[c - 'A']; 155 if (c >= 'a' && c <= 'z') 156 return lowercaseOffset[c - 'a']; 157 return 0; 158} 159 160const HTMLEntityTableEntry* HTMLEntityTable::lastEntryStartingWith(UChar c) 161{ 162 if (c >= 'A' && c <= 'Z') 163 return uppercaseOffset[c - 'A' + 1] - 1; 164 if (c >= 'a' && c <= 'z') 165 return lowercaseOffset[c - 'a' + 1] - 1; 166 return 0; 167} 168 169const HTMLEntityTableEntry* HTMLEntityTable::firstEntry() 170{ 171 return &staticEntityTable[0]; 172} 173 174const HTMLEntityTableEntry* HTMLEntityTable::lastEntry() 175{ 176 return &staticEntityTable[%s - 1]; 177} 178 179} 180""" % entity_count) 181