Chromium Code Reviews| Index: grit/format/policy_templates/writers/doc_writer.py |
| diff --git a/grit/format/policy_templates/writers/doc_writer.py b/grit/format/policy_templates/writers/doc_writer.py |
| index e90e16cb63388e9e2b9f5f642a91f5c06e07d445..6b5ae172e73bde4854074c2b3c37a768abc180d7 100644 |
| --- a/grit/format/policy_templates/writers/doc_writer.py |
| +++ b/grit/format/policy_templates/writers/doc_writer.py |
| @@ -58,6 +58,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| ''' |
| return ', '.join([item_map[x] for x in items]) |
| + |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
Nit: Don't introduce a blank line here (two blank
|
| def _AddTextWithLinks(self, parent, text): |
| '''Parse a string for URLs and add it to a DOM node with the URLs replaced |
| with <a> HTML links. |
| @@ -66,11 +67,13 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| parent: The DOM node to which the text will be added. |
| text: The string to be added. |
| ''' |
| + # A simple regexp to search for URLs. It is enough for now. |
| + url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])') |
| + |
| # Iterate through all the URLs and replace them with links. |
| - out = [] |
| while True: |
| # Look for the first URL. |
| - res = self._url_matcher.search(text) |
| + res = url_matcher.search(text) |
| if not res: |
| break |
| # Calculate positions of the substring of the URL. |
| @@ -85,6 +88,37 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| text = text[end:] |
| self.AddText(parent, text) |
| + def _AddParagraphs(self, parent, text): |
| + '''Parse a string for paragraphs (/n/n) and add them to a DOM node with |
| + the URLs replaced with <a> HTML links (done by _AddTextWithLinks). |
| + |
| + Args: |
| + parent: The DOM node to which the text will be added. |
| + text: The string to be added.''' |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
nit: Other docstrings in this file place a newline
|
| + if len(text) == 0: |
| + return |
| + |
| + # A regexp to search for line breaks. |
| + new_line_matcher = lazy_re.compile('\n\n') |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
You can simplify this a lot by just doing text.spl
|
| + |
| + # Open paragraph |
| + paragraph = self.AddElement(parent, 'p') |
| + while True: |
| + # Look for the first break line. |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
nit: s/break line/line break/.
|
| + res = new_line_matcher.search(text) |
| + if not res: |
| + break |
| + # Get postiotons of begin and end of new line charecters. |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
nit: spelling
|
| + start = res.start(0) |
| + end = res.end(0) |
| + # Insert text to the paragraph with processing the urls |
| + self._AddTextWithLinks(paragraph, text[:start]) |
| + #Create a new paragraph. The old one will be automatically closed. |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
nit: space after #
|
| + paragraph = self.AddElement(parent, 'p') |
| + # Drop the part of text that is added. |
| + text = text[end:] |
| + self._AddTextWithLinks(paragraph, text) # Adding the rest of the text |
| + |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
nit: remove extra blank line.
|
| def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None): |
| '''Adds an XML element to a parent, with CSS style-sheets included. |
| @@ -114,8 +148,9 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| parent: The DOM node for which the feature list will be added. |
| policy: The data structure of a policy. |
| ''' |
| - # Replace URLs with links in the description. |
| - self._AddTextWithLinks(parent, policy['desc']) |
| + |
| + # Replace URLs with links and /n with <p>...</p> in the description. |
|
Mattias Nissler (ping if slow)
2015/02/10 12:54:13
nit: I think "Break description into paragraphs an
|
| + self._AddParagraphs(parent, policy['desc']) |
| # Add list of enum items. |
| if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'): |
| ul = self.AddElement(parent, 'ul') |
| @@ -508,7 +543,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| problem_href = policy['problem_href'] |
| div = self._AddStyledElement(parent, 'div', ['div.note']) |
| note = self._GetLocalizedMessage('note').replace('$6', problem_href) |
| - self._AddTextWithLinks(div, note) |
| + self._AddParagraphs(div, note) |
| def _AddPolicyRow(self, parent, policy): |
| '''Adds a row for the policy in the summary table. |
| @@ -597,7 +632,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| summary_div = self.AddElement(self._main_div, 'div') |
| self.AddElement(summary_div, 'a', {'name': 'top'}) |
| self.AddElement(summary_div, 'br') |
| - self._AddTextWithLinks( |
| + self._AddParagraphs( |
| summary_div, |
| self._GetLocalizedMessage('intro')) |
| self.AddElement(summary_div, 'br') |
| @@ -690,8 +725,6 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): |
| 'ul': 'padding-left: 0px; margin-left: 0px;' |
| } |
| - # A simple regexp to search for URLs. It is enough for now. |
| - self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])') |
| def GetTemplateText(self): |
| # Return the text representation of the main <div> tag. |