Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(865)

Side by Side Diff: grit/format/policy_templates/writers/doc_writer.py

Issue 911563002: Fixed bug with paragraphs in policy description (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | grit/format/policy_templates/writers/doc_writer_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 6
7 import json 7 import json
8 from xml.dom import minidom 8 from xml.dom import minidom
9 from grit import lazy_re 9 from grit import lazy_re
10 from grit.format.policy_templates.writers import xml_formatted_writer 10 from grit.format.policy_templates.writers import xml_formatted_writer
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 return ', '.join([item_map[x] for x in items]) 59 return ', '.join([item_map[x] for x in items])
60 60
61 def _AddTextWithLinks(self, parent, text): 61 def _AddTextWithLinks(self, parent, text):
62 '''Parse a string for URLs and add it to a DOM node with the URLs replaced 62 '''Parse a string for URLs and add it to a DOM node with the URLs replaced
63 with <a> HTML links. 63 with <a> HTML links.
64 64
65 Args: 65 Args:
66 parent: The DOM node to which the text will be added. 66 parent: The DOM node to which the text will be added.
67 text: The string to be added. 67 text: The string to be added.
68 ''' 68 '''
69 # A simple regexp to search for URLs. It is enough for now.
70 url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])')
71
69 # Iterate through all the URLs and replace them with links. 72 # Iterate through all the URLs and replace them with links.
70 out = []
71 while True: 73 while True:
72 # Look for the first URL. 74 # Look for the first URL.
73 res = self._url_matcher.search(text) 75 res = url_matcher.search(text)
74 if not res: 76 if not res:
75 break 77 break
76 # Calculate positions of the substring of the URL. 78 # Calculate positions of the substring of the URL.
77 url = res.group(0) 79 url = res.group(0)
78 start = res.start(0) 80 start = res.start(0)
79 end = res.end(0) 81 end = res.end(0)
80 # Add the text prior to the URL. 82 # Add the text prior to the URL.
81 self.AddText(parent, text[:start]) 83 self.AddText(parent, text[:start])
82 # Add a link for the URL. 84 # Add a link for the URL.
83 self.AddElement(parent, 'a', {'href': url}, url) 85 self.AddElement(parent, 'a', {'href': url}, url)
84 # Drop the part of text that is added. 86 # Drop the part of text that is added.
85 text = text[end:] 87 text = text[end:]
86 self.AddText(parent, text) 88 self.AddText(parent, text)
87 89
90 def _AddParagraphs(self, parent, text):
91 '''Break description into paragraphs and replace URLs with links.
92
93 Args:
94 parent: The DOM node to which the text will be added.
95 text: The string to be added.
96 '''
97 # Split text into list of paragraphs.
98 entries = text.split('\n\n')
99 for entry in entries:
100 # Create a new paragraph node.
101 paragraph = self.AddElement(parent, 'p')
102 # Insert text to the paragraph with processing the URLs.
103 self._AddTextWithLinks(paragraph, entry)
88 104
89 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None): 105 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None):
90 '''Adds an XML element to a parent, with CSS style-sheets included. 106 '''Adds an XML element to a parent, with CSS style-sheets included.
91 107
92 Args: 108 Args:
93 parent: The parent DOM node. 109 parent: The parent DOM node.
94 name: Name of the element to add. 110 name: Name of the element to add.
95 style_ids: A list of CSS style strings from self._STYLE[]. 111 style_ids: A list of CSS style strings from self._STYLE[].
96 attrs: Dictionary of attributes for the element. 112 attrs: Dictionary of attributes for the element.
97 text: Text content for the element. 113 text: Text content for the element.
98 ''' 114 '''
99 if attrs == None: 115 if attrs == None:
100 attrs = {} 116 attrs = {}
101 117
102 style = ''.join([self._STYLE[x] for x in style_ids]) 118 style = ''.join([self._STYLE[x] for x in style_ids])
103 if style != '': 119 if style != '':
104 # Apply the style specified by style_ids. 120 # Apply the style specified by style_ids.
105 attrs['style'] = style + attrs.get('style', '') 121 attrs['style'] = style + attrs.get('style', '')
106 return self.AddElement(parent, name, attrs, text) 122 return self.AddElement(parent, name, attrs, text)
107 123
108 def _AddDescription(self, parent, policy): 124 def _AddDescription(self, parent, policy):
109 '''Adds a string containing the description of the policy. URLs are 125 '''Adds a string containing the description of the policy. URLs are
110 replaced with links and the possible choices are enumerated in case 126 replaced with links and the possible choices are enumerated in case
111 of 'string-enum' and 'int-enum' type policies. 127 of 'string-enum' and 'int-enum' type policies.
112 128
113 Args: 129 Args:
114 parent: The DOM node for which the feature list will be added. 130 parent: The DOM node for which the feature list will be added.
115 policy: The data structure of a policy. 131 policy: The data structure of a policy.
116 ''' 132 '''
117 # Replace URLs with links in the description. 133 # Add description by paragraphs (URLs will be substituted by links).
118 self._AddTextWithLinks(parent, policy['desc']) 134 self._AddParagraphs(parent, policy['desc'])
119 # Add list of enum items. 135 # Add list of enum items.
120 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'): 136 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'):
121 ul = self.AddElement(parent, 'ul') 137 ul = self.AddElement(parent, 'ul')
122 for item in policy['items']: 138 for item in policy['items']:
123 if policy['type'] == 'int-enum': 139 if policy['type'] == 'int-enum':
124 value_string = str(item['value']) 140 value_string = str(item['value'])
125 else: 141 else:
126 value_string = '"%s"' % item['value'] 142 value_string = '"%s"' % item['value']
127 self.AddElement( 143 self.AddElement(
128 ul, 'li', {}, '%s = %s' % (value_string, item['caption'])) 144 ul, 'li', {}, '%s = %s' % (value_string, item['caption']))
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 a link for that page. 517 a link for that page.
502 518
503 Args: 519 Args:
504 policy: The data structure of the policy. 520 policy: The data structure of the policy.
505 ''' 521 '''
506 if 'problem_href' not in policy: 522 if 'problem_href' not in policy:
507 return 523 return
508 problem_href = policy['problem_href'] 524 problem_href = policy['problem_href']
509 div = self._AddStyledElement(parent, 'div', ['div.note']) 525 div = self._AddStyledElement(parent, 'div', ['div.note'])
510 note = self._GetLocalizedMessage('note').replace('$6', problem_href) 526 note = self._GetLocalizedMessage('note').replace('$6', problem_href)
511 self._AddTextWithLinks(div, note) 527 self._AddParagraphs(div, note)
512 528
513 def _AddPolicyRow(self, parent, policy): 529 def _AddPolicyRow(self, parent, policy):
514 '''Adds a row for the policy in the summary table. 530 '''Adds a row for the policy in the summary table.
515 531
516 Args: 532 Args:
517 parent: The DOM node of the summary table. 533 parent: The DOM node of the summary table.
518 policy: The data structure of the policy. 534 policy: The data structure of the policy.
519 ''' 535 '''
520 tr = self._AddStyledElement(parent, 'tr', ['tr']) 536 tr = self._AddStyledElement(parent, 'tr', ['tr'])
521 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14) 537 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 606
591 def BeginTemplate(self): 607 def BeginTemplate(self):
592 # Add a <div> for the summary section. 608 # Add a <div> for the summary section.
593 if self._GetChromiumVersionString() is not None: 609 if self._GetChromiumVersionString() is not None:
594 self.AddComment(self._main_div, self.config['build'] + \ 610 self.AddComment(self._main_div, self.config['build'] + \
595 ' version: ' + self._GetChromiumVersionString()) 611 ' version: ' + self._GetChromiumVersionString())
596 612
597 summary_div = self.AddElement(self._main_div, 'div') 613 summary_div = self.AddElement(self._main_div, 'div')
598 self.AddElement(summary_div, 'a', {'name': 'top'}) 614 self.AddElement(summary_div, 'a', {'name': 'top'})
599 self.AddElement(summary_div, 'br') 615 self.AddElement(summary_div, 'br')
600 self._AddTextWithLinks( 616 self._AddParagraphs(
601 summary_div, 617 summary_div,
602 self._GetLocalizedMessage('intro')) 618 self._GetLocalizedMessage('intro'))
603 self.AddElement(summary_div, 'br') 619 self.AddElement(summary_div, 'br')
604 self.AddElement(summary_div, 'br') 620 self.AddElement(summary_div, 'br')
605 self.AddElement(summary_div, 'br') 621 self.AddElement(summary_div, 'br')
606 # Add the summary table of policies. 622 # Add the summary table of policies.
607 summary_table = self._AddStyledElement(summary_div, 'table', ['table']) 623 summary_table = self._AddStyledElement(summary_div, 'table', ['table'])
608 # Add the first row. 624 # Add the first row.
609 thead = self.AddElement(summary_table, 'thead') 625 thead = self.AddElement(summary_table, 'thead')
610 tr = self._AddStyledElement(thead, 'tr', ['tr']) 626 tr = self._AddStyledElement(thead, 'tr', ['tr'])
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 'td.right': 'width: 100%;', 699 'td.right': 'width: 100%;',
684 'dt': 'font-weight: bold;', 700 'dt': 'font-weight: bold;',
685 'dd dl': 'margin-top: 0px; margin-bottom: 0px;', 701 'dd dl': 'margin-top: 0px; margin-bottom: 0px;',
686 '.monospace': 'font-family: monospace;', 702 '.monospace': 'font-family: monospace;',
687 '.pre': 'white-space: pre;', 703 '.pre': 'white-space: pre;',
688 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;', 704 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;',
689 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;', 705 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;',
690 'ul': 'padding-left: 0px; margin-left: 0px;' 706 'ul': 'padding-left: 0px; margin-left: 0px;'
691 } 707 }
692 708
693 # A simple regexp to search for URLs. It is enough for now.
694 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])')
695 709
696 def GetTemplateText(self): 710 def GetTemplateText(self):
697 # Return the text representation of the main <div> tag. 711 # Return the text representation of the main <div> tag.
698 return self._main_div.toxml() 712 return self._main_div.toxml()
699 # To get a complete HTML file, use the following. 713 # To get a complete HTML file, use the following.
700 # return self._doc.toxml() 714 # return self._doc.toxml()
OLDNEW
« no previous file with comments | « no previous file | grit/format/policy_templates/writers/doc_writer_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698