OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''A gatherer for the TotalRecall brand of HTML templates with replaceable | 6 '''A gatherer for the TotalRecall brand of HTML templates with replaceable |
7 portions. We wanted to reuse extern.tclib.api.handlers.html.TCHTMLParser | 7 portions. We wanted to reuse extern.tclib.api.handlers.html.TCHTMLParser |
8 but this proved impossible due to the fact that the TotalRecall HTML templates | 8 but this proved impossible due to the fact that the TotalRecall HTML templates |
9 are in general quite far from parseable HTML and the TCHTMLParser derives | 9 are in general quite far from parseable HTML and the TCHTMLParser derives |
10 from HTMLParser.HTMLParser which requires relatively well-formed HTML. Some | 10 from HTMLParser.HTMLParser which requires relatively well-formed HTML. Some |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 msg = self.skeleton_[ix].GetMessage() | 686 msg = self.skeleton_[ix].GetMessage() |
687 for item in msg.GetContent(): | 687 for item in msg.GetContent(): |
688 if (isinstance(item, types.StringTypes) and _NON_WHITESPACE.search(ite
m) | 688 if (isinstance(item, types.StringTypes) and _NON_WHITESPACE.search(ite
m) |
689 and item != ' '): | 689 and item != ' '): |
690 got_text = True | 690 got_text = True |
691 break | 691 break |
692 if not got_text: | 692 if not got_text: |
693 self.skeleton_[ix] = msg.GetRealContent() | 693 self.skeleton_[ix] = msg.GetRealContent() |
694 | 694 |
695 | 695 |
696 # Static method | 696 @staticmethod |
697 def FromFile(html, extkey=None, encoding = 'utf-8'): | 697 def FromFile(html, extkey=None, encoding = 'utf-8'): |
698 '''Creates a TrHtml object from the contents of 'html' which are decoded | 698 '''Creates a TrHtml object from the contents of 'html' which are decoded |
699 using 'encoding'. Returns a new TrHtml object, upon which Parse() has not | 699 using 'encoding'. Returns a new TrHtml object, upon which Parse() has not |
700 been called. | 700 been called. |
701 | 701 |
702 Args: | 702 Args: |
703 html: file('') | 'filename.html' | 703 html: file('') | 'filename.html' |
704 extkey: ignored | 704 extkey: ignored |
705 encoding: 'utf-8' (note that encoding is ignored if 'html' is not a file | 705 encoding: 'utf-8' (note that encoding is ignored if 'html' is not a file |
706 name but instead an open file or file-like object) | 706 name but instead an open file or file-like object) |
707 | 707 |
708 Return: | 708 Return: |
709 TrHtml(text_of_file) | 709 TrHtml(text_of_file) |
710 ''' | 710 ''' |
711 if isinstance(html, types.StringTypes): | 711 if isinstance(html, types.StringTypes): |
712 html = util.WrapInputStream(file(html, 'r'), encoding) | 712 html = util.WrapInputStream(file(html, 'r'), encoding) |
713 doc = html.read() | 713 doc = html.read() |
714 | 714 |
715 # Ignore the BOM character if the document starts with one. | 715 # Ignore the BOM character if the document starts with one. |
716 if len(doc) and doc[0] == u'\ufeff': | 716 if len(doc) and doc[0] == u'\ufeff': |
717 doc = doc[1:] | 717 doc = doc[1:] |
718 | 718 |
719 return TrHtml(doc) | 719 return TrHtml(doc) |
720 FromFile = staticmethod(FromFile) | |
721 | 720 |
| 721 def SubstituteMessages(self, substituter): |
| 722 '''Applies substitutions to all messages in the tree. |
| 723 |
| 724 Goes through the skeleton and finds all MessageCliques. |
| 725 |
| 726 Args: |
| 727 substituter: a grit.util.Substituter object. |
| 728 ''' |
| 729 new_skel = [] |
| 730 for chunk in self.skeleton_: |
| 731 if isinstance(chunk, clique.MessageClique): |
| 732 old_message = chunk.GetMessage() |
| 733 new_message = substituter.SubstituteMessage(old_message) |
| 734 if new_message is not old_message: |
| 735 new_skel.append(self.uberclique.MakeClique(new_message)) |
| 736 continue |
| 737 new_skel.append(chunk) |
| 738 self.skeleton_ = new_skel |
| 739 |
OLD | NEW |