| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Formats as a .C file for compilation. |
| 7 """ |
| 8 |
| 9 import os |
| 10 import re |
| 11 import types |
| 12 |
| 13 from grit import util |
| 14 from grit.format import interface |
| 15 |
| 16 |
| 17 class TopLevel(interface.ItemFormatter): |
| 18 """Writes out the required preamble for C files.""" |
| 19 |
| 20 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 21 assert isinstance(lang, types.StringTypes) |
| 22 if not begin_item: |
| 23 return '' |
| 24 else: |
| 25 # Find the location of the resource header file, so that we can include |
| 26 # it. |
| 27 resource_header = 'resource.h' # fall back to this |
| 28 for child in item.GetRoot().children: |
| 29 if child.name == 'outputs': |
| 30 for output in child.children: |
| 31 if output.attrs['type'] == 'rc_header': |
| 32 resource_header = os.path.abspath(output.GetOutputFilename()) |
| 33 resource_header = util.MakeRelativePath(output_dir, |
| 34 resource_header) |
| 35 return """// Copyright %d Google Inc. All Rights Reserved. |
| 36 // This file is automatically generated by GRIT. Do not edit. |
| 37 |
| 38 #include "%s" |
| 39 |
| 40 // All strings are UTF-8 |
| 41 """ % (util.GetCurrentYear(), resource_header) |
| 42 # end Format() function |
| 43 |
| 44 |
| 45 class StringTable(interface.ItemFormatter): |
| 46 """Outputs a C switch statement representing the string table.""" |
| 47 |
| 48 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 49 assert isinstance(lang, types.StringTypes) |
| 50 if begin_item: |
| 51 return 'const char* GetString(int id) {\n switch (id) {' |
| 52 else: |
| 53 return '\n default:\n return 0;\n }\n}' |
| 54 |
| 55 def _HexToOct(match): |
| 56 "Return the octal form of the hex numbers" |
| 57 hex = match.group("hex") |
| 58 result = "" |
| 59 while len(hex): |
| 60 next_num = int(hex[2:4], 16) |
| 61 result += "\\" + '%03d' % int(oct(next_num), 10) |
| 62 hex = hex[4:] |
| 63 return match.group("escaped_backslashes") + result |
| 64 |
| 65 class Message(interface.ItemFormatter): |
| 66 """Writes out a single message as part of the switch.""" |
| 67 |
| 68 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 69 from grit.node import message |
| 70 if not begin_item: |
| 71 return '' |
| 72 |
| 73 assert isinstance(lang, types.StringTypes) |
| 74 assert isinstance(item, message.MessageNode) |
| 75 |
| 76 message = item.ws_at_start + item.Translate(lang) + item.ws_at_end |
| 77 # output message with non-ascii chars escaped as octal numbers |
| 78 # C's grammar allows escaped hexadecimal numbers to be infinite, |
| 79 # but octal is always of the form \OOO |
| 80 message = message.encode('utf-8').encode('string_escape') |
| 81 # an escaped char is (\xHH)+ but only if the initial |
| 82 # backslash is not escaped. |
| 83 not_a_backslash = r"(^|[^\\])" # beginning of line or a non-backslash char |
| 84 escaped_backslashes = not_a_backslash + r"(\\\\)*" |
| 85 hex_digits = r"((\\x)[0-9a-f]{2})+" |
| 86 two_digit_hex_num = re.compile( |
| 87 r"(?P<escaped_backslashes>%s)(?P<hex>%s)" |
| 88 % (escaped_backslashes, hex_digits)) |
| 89 message = two_digit_hex_num.sub(_HexToOct, message) |
| 90 # unescape \ (convert \\ back to \) |
| 91 message = message.replace('\\\\', '\\') |
| 92 message = message.replace('"', '\\"') |
| 93 message = util.LINEBREAKS.sub(r'\\n', message) |
| 94 |
| 95 name_attr = item.GetTextualIds()[0] |
| 96 |
| 97 return '\n case %s:\n return "%s";' % (name_attr, message) |
| OLD | NEW |