diff options
author | Philip Chimento <philip@endlessm.com> | 2015-06-04 13:53:57 -0700 |
---|---|---|
committer | Philip Chimento <philip@endlessm.com> | 2015-06-04 14:28:18 -0700 |
commit | de707ee95941b63bf0ed3fdbbe206e06ea97475a (patch) | |
tree | 98ee02368f5bab8b01f0ffd9c0b2a0e5e4e36b6a /tools | |
parent | def18b63d07a570ec5732a36f2eaabc6b10b14d5 (diff) |
Avoid global state
Another minor cleanup; TranslatableHTMLParser shouldn't use global state.
[endlessm/eos-sdk#3245]
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/eos-html-extractor | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/tools/eos-html-extractor b/tools/eos-html-extractor index 23802dc..879d048 100755 --- a/tools/eos-html-extractor +++ b/tools/eos-html-extractor @@ -11,12 +11,18 @@ from HTMLParser import HTMLParser # Parser that adds line numbers to the HTML strings that need translating class TranslatableHTMLParser(HTMLParser): + def __init__(self, translatable_strings): + HTMLParser.__init__(self) + self.all_translatable_data = [] + self._comments_with_line_numbers = [] + self._translatable_strings = translatable_strings + def handle_data(self, data): - if data not in translatable_strings: + if data not in self._translatable_strings: return # Determine if comment should be included - most_recent_comment = comments_with_line_numbers[-1] + most_recent_comment = self._comments_with_line_numbers[-1] comment_string, comment_line = most_recent_comment code_line = self.getpos()[0] @@ -27,10 +33,10 @@ class TranslatableHTMLParser(HTMLParser): # If the comment immediately preceded this string, include it if comment_line + comment_length == code_line: optional_comment = ' '.join(comment_string.split()) - all_translatable_data.append((data.strip(), code_line, optional_comment)) + self.all_translatable_data.append((data.strip(), code_line, optional_comment)) def handle_comment(self, comment): - comments_with_line_numbers.append((comment, self.getpos()[0])) + self._comments_with_line_numbers.append((comment, self.getpos()[0])) parser = argparse.ArgumentParser(description='Extract translatable strings ' + 'from HTML files. This is xgettext for HTML.') @@ -56,13 +62,11 @@ translatable_strings = map(lambda div: div.contents[0].encode('utf-8'), translatable_divs) # Find the line numbers for those strings -all_translatable_data = [] -comments_with_line_numbers = [] -parser = TranslatableHTMLParser() +parser = TranslatableHTMLParser(translatable_strings) parser.feed(page) # Write out all info about the translatable strings found in this file -for string, line_num, optional_comment in all_translatable_data: +for string, line_num, optional_comment in parser.all_translatable_data: print('#line {line} "{path}"'.format(line=line_num, path=final_path)) if optional_comment != '': print('// ' + optional_comment) |