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

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 | no next file » | 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 item_map: A dictionary containing all the elements of 'items' as 51 item_map: A dictionary containing all the elements of 'items' as
52 keys. 52 keys.
53 items: A list of arbitrary items. 53 items: A list of arbitrary items.
54 54
55 Returns: 55 Returns:
56 Looks up each item of 'items' in 'item_maps' and concatenates the 56 Looks up each item of 'items' in 'item_maps' and concatenates the
57 resulting items into a comma-separated list. 57 resulting items into a comma-separated list.
58 ''' 58 '''
59 return ', '.join([item_map[x] for x in items]) 59 return ', '.join([item_map[x] for x in items])
60 60
61
Mattias Nissler (ping if slow) 2015/02/10 12:54:13 Nit: Don't introduce a blank line here (two blank
61 def _AddTextWithLinks(self, parent, text): 62 def _AddTextWithLinks(self, parent, text):
62 '''Parse a string for URLs and add it to a DOM node with the URLs replaced 63 '''Parse a string for URLs and add it to a DOM node with the URLs replaced
63 with <a> HTML links. 64 with <a> HTML links.
64 65
65 Args: 66 Args:
66 parent: The DOM node to which the text will be added. 67 parent: The DOM node to which the text will be added.
67 text: The string to be added. 68 text: The string to be added.
68 ''' 69 '''
70 # A simple regexp to search for URLs. It is enough for now.
71 url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])')
72
69 # Iterate through all the URLs and replace them with links. 73 # Iterate through all the URLs and replace them with links.
70 out = []
71 while True: 74 while True:
72 # Look for the first URL. 75 # Look for the first URL.
73 res = self._url_matcher.search(text) 76 res = url_matcher.search(text)
74 if not res: 77 if not res:
75 break 78 break
76 # Calculate positions of the substring of the URL. 79 # Calculate positions of the substring of the URL.
77 url = res.group(0) 80 url = res.group(0)
78 start = res.start(0) 81 start = res.start(0)
79 end = res.end(0) 82 end = res.end(0)
80 # Add the text prior to the URL. 83 # Add the text prior to the URL.
81 self.AddText(parent, text[:start]) 84 self.AddText(parent, text[:start])
82 # Add a link for the URL. 85 # Add a link for the URL.
83 self.AddElement(parent, 'a', {'href': url}, url) 86 self.AddElement(parent, 'a', {'href': url}, url)
84 # Drop the part of text that is added. 87 # Drop the part of text that is added.
85 text = text[end:] 88 text = text[end:]
86 self.AddText(parent, text) 89 self.AddText(parent, text)
87 90
91 def _AddParagraphs(self, parent, text):
92 '''Parse a string for paragraphs (/n/n) and add them to a DOM node with
93 the URLs replaced with <a> HTML links (done by _AddTextWithLinks).
94
95 Args:
96 parent: The DOM node to which the text will be added.
97 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
98 if len(text) == 0:
99 return
100
101 # A regexp to search for line breaks.
102 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
103
104 # Open paragraph
105 paragraph = self.AddElement(parent, 'p')
106 while True:
107 # Look for the first break line.
Mattias Nissler (ping if slow) 2015/02/10 12:54:13 nit: s/break line/line break/.
108 res = new_line_matcher.search(text)
109 if not res:
110 break
111 # Get postiotons of begin and end of new line charecters.
Mattias Nissler (ping if slow) 2015/02/10 12:54:13 nit: spelling
112 start = res.start(0)
113 end = res.end(0)
114 # Insert text to the paragraph with processing the urls
115 self._AddTextWithLinks(paragraph, text[:start])
116 #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 #
117 paragraph = self.AddElement(parent, 'p')
118 # Drop the part of text that is added.
119 text = text[end:]
120 self._AddTextWithLinks(paragraph, text) # Adding the rest of the text
121
Mattias Nissler (ping if slow) 2015/02/10 12:54:13 nit: remove extra blank line.
88 122
89 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None): 123 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None):
90 '''Adds an XML element to a parent, with CSS style-sheets included. 124 '''Adds an XML element to a parent, with CSS style-sheets included.
91 125
92 Args: 126 Args:
93 parent: The parent DOM node. 127 parent: The parent DOM node.
94 name: Name of the element to add. 128 name: Name of the element to add.
95 style_ids: A list of CSS style strings from self._STYLE[]. 129 style_ids: A list of CSS style strings from self._STYLE[].
96 attrs: Dictionary of attributes for the element. 130 attrs: Dictionary of attributes for the element.
97 text: Text content for the element. 131 text: Text content for the element.
98 ''' 132 '''
99 if attrs == None: 133 if attrs == None:
100 attrs = {} 134 attrs = {}
101 135
102 style = ''.join([self._STYLE[x] for x in style_ids]) 136 style = ''.join([self._STYLE[x] for x in style_ids])
103 if style != '': 137 if style != '':
104 # Apply the style specified by style_ids. 138 # Apply the style specified by style_ids.
105 attrs['style'] = style + attrs.get('style', '') 139 attrs['style'] = style + attrs.get('style', '')
106 return self.AddElement(parent, name, attrs, text) 140 return self.AddElement(parent, name, attrs, text)
107 141
108 def _AddDescription(self, parent, policy): 142 def _AddDescription(self, parent, policy):
109 '''Adds a string containing the description of the policy. URLs are 143 '''Adds a string containing the description of the policy. URLs are
110 replaced with links and the possible choices are enumerated in case 144 replaced with links and the possible choices are enumerated in case
111 of 'string-enum' and 'int-enum' type policies. 145 of 'string-enum' and 'int-enum' type policies.
112 146
113 Args: 147 Args:
114 parent: The DOM node for which the feature list will be added. 148 parent: The DOM node for which the feature list will be added.
115 policy: The data structure of a policy. 149 policy: The data structure of a policy.
116 ''' 150 '''
117 # Replace URLs with links in the description. 151
118 self._AddTextWithLinks(parent, policy['desc']) 152 # 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
153 self._AddParagraphs(parent, policy['desc'])
119 # Add list of enum items. 154 # Add list of enum items.
120 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'): 155 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'):
121 ul = self.AddElement(parent, 'ul') 156 ul = self.AddElement(parent, 'ul')
122 for item in policy['items']: 157 for item in policy['items']:
123 if policy['type'] == 'int-enum': 158 if policy['type'] == 'int-enum':
124 value_string = str(item['value']) 159 value_string = str(item['value'])
125 else: 160 else:
126 value_string = '"%s"' % item['value'] 161 value_string = '"%s"' % item['value']
127 self.AddElement( 162 self.AddElement(
128 ul, 'li', {}, '%s = %s' % (value_string, item['caption'])) 163 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. 536 a link for that page.
502 537
503 Args: 538 Args:
504 policy: The data structure of the policy. 539 policy: The data structure of the policy.
505 ''' 540 '''
506 if 'problem_href' not in policy: 541 if 'problem_href' not in policy:
507 return 542 return
508 problem_href = policy['problem_href'] 543 problem_href = policy['problem_href']
509 div = self._AddStyledElement(parent, 'div', ['div.note']) 544 div = self._AddStyledElement(parent, 'div', ['div.note'])
510 note = self._GetLocalizedMessage('note').replace('$6', problem_href) 545 note = self._GetLocalizedMessage('note').replace('$6', problem_href)
511 self._AddTextWithLinks(div, note) 546 self._AddParagraphs(div, note)
512 547
513 def _AddPolicyRow(self, parent, policy): 548 def _AddPolicyRow(self, parent, policy):
514 '''Adds a row for the policy in the summary table. 549 '''Adds a row for the policy in the summary table.
515 550
516 Args: 551 Args:
517 parent: The DOM node of the summary table. 552 parent: The DOM node of the summary table.
518 policy: The data structure of the policy. 553 policy: The data structure of the policy.
519 ''' 554 '''
520 tr = self._AddStyledElement(parent, 'tr', ['tr']) 555 tr = self._AddStyledElement(parent, 'tr', ['tr'])
521 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14) 556 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 625
591 def BeginTemplate(self): 626 def BeginTemplate(self):
592 # Add a <div> for the summary section. 627 # Add a <div> for the summary section.
593 if self._GetChromiumVersionString() is not None: 628 if self._GetChromiumVersionString() is not None:
594 self.AddComment(self._main_div, self.config['build'] + \ 629 self.AddComment(self._main_div, self.config['build'] + \
595 ' version: ' + self._GetChromiumVersionString()) 630 ' version: ' + self._GetChromiumVersionString())
596 631
597 summary_div = self.AddElement(self._main_div, 'div') 632 summary_div = self.AddElement(self._main_div, 'div')
598 self.AddElement(summary_div, 'a', {'name': 'top'}) 633 self.AddElement(summary_div, 'a', {'name': 'top'})
599 self.AddElement(summary_div, 'br') 634 self.AddElement(summary_div, 'br')
600 self._AddTextWithLinks( 635 self._AddParagraphs(
601 summary_div, 636 summary_div,
602 self._GetLocalizedMessage('intro')) 637 self._GetLocalizedMessage('intro'))
603 self.AddElement(summary_div, 'br') 638 self.AddElement(summary_div, 'br')
604 self.AddElement(summary_div, 'br') 639 self.AddElement(summary_div, 'br')
605 self.AddElement(summary_div, 'br') 640 self.AddElement(summary_div, 'br')
606 # Add the summary table of policies. 641 # Add the summary table of policies.
607 summary_table = self._AddStyledElement(summary_div, 'table', ['table']) 642 summary_table = self._AddStyledElement(summary_div, 'table', ['table'])
608 # Add the first row. 643 # Add the first row.
609 thead = self.AddElement(summary_table, 'thead') 644 thead = self.AddElement(summary_table, 'thead')
610 tr = self._AddStyledElement(thead, 'tr', ['tr']) 645 tr = self._AddStyledElement(thead, 'tr', ['tr'])
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 'td.right': 'width: 100%;', 718 'td.right': 'width: 100%;',
684 'dt': 'font-weight: bold;', 719 'dt': 'font-weight: bold;',
685 'dd dl': 'margin-top: 0px; margin-bottom: 0px;', 720 'dd dl': 'margin-top: 0px; margin-bottom: 0px;',
686 '.monospace': 'font-family: monospace;', 721 '.monospace': 'font-family: monospace;',
687 '.pre': 'white-space: pre;', 722 '.pre': 'white-space: pre;',
688 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;', 723 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;',
689 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;', 724 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;',
690 'ul': 'padding-left: 0px; margin-left: 0px;' 725 'ul': 'padding-left: 0px; margin-left: 0px;'
691 } 726 }
692 727
693 # A simple regexp to search for URLs. It is enough for now.
694 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])')
695 728
696 def GetTemplateText(self): 729 def GetTemplateText(self):
697 # Return the text representation of the main <div> tag. 730 # Return the text representation of the main <div> tag.
698 return self._main_div.toxml() 731 return self._main_div.toxml()
699 # To get a complete HTML file, use the following. 732 # To get a complete HTML file, use the following.
700 # return self._doc.toxml() 733 # return self._doc.toxml()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698