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

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 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 if len(text) == 0:
Mattias Nissler (ping if slow) 2015/02/10 14:09:55 nit: This check is no longer needed. The loop belo
peletskyi 2015/02/10 16:12:56 Done.
98 return
99 # Split text into list of paragraphs
Mattias Nissler (ping if slow) 2015/02/10 14:09:55 nit: missing period
peletskyi 2015/02/10 16:12:56 Done.
100 entries = text.split('\n\n')
101 for entry in entries:
102 # Create a new paragraph node
Mattias Nissler (ping if slow) 2015/02/10 14:09:55 nit: period
peletskyi 2015/02/10 16:12:56 Done.
103 paragraph = self.AddElement(parent, 'p')
104 # Insert text to the paragraph with processing the urls
Mattias Nissler (ping if slow) 2015/02/10 14:09:55 nit: period
peletskyi 2015/02/10 16:12:56 Done.
105 self._AddTextWithLinks(paragraph, entry)
88 106
89 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None): 107 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None):
90 '''Adds an XML element to a parent, with CSS style-sheets included. 108 '''Adds an XML element to a parent, with CSS style-sheets included.
91 109
92 Args: 110 Args:
93 parent: The parent DOM node. 111 parent: The parent DOM node.
94 name: Name of the element to add. 112 name: Name of the element to add.
95 style_ids: A list of CSS style strings from self._STYLE[]. 113 style_ids: A list of CSS style strings from self._STYLE[].
96 attrs: Dictionary of attributes for the element. 114 attrs: Dictionary of attributes for the element.
97 text: Text content for the element. 115 text: Text content for the element.
98 ''' 116 '''
99 if attrs == None: 117 if attrs == None:
100 attrs = {} 118 attrs = {}
101 119
102 style = ''.join([self._STYLE[x] for x in style_ids]) 120 style = ''.join([self._STYLE[x] for x in style_ids])
103 if style != '': 121 if style != '':
104 # Apply the style specified by style_ids. 122 # Apply the style specified by style_ids.
105 attrs['style'] = style + attrs.get('style', '') 123 attrs['style'] = style + attrs.get('style', '')
106 return self.AddElement(parent, name, attrs, text) 124 return self.AddElement(parent, name, attrs, text)
107 125
108 def _AddDescription(self, parent, policy): 126 def _AddDescription(self, parent, policy):
109 '''Adds a string containing the description of the policy. URLs are 127 '''Adds a string containing the description of the policy. URLs are
110 replaced with links and the possible choices are enumerated in case 128 replaced with links and the possible choices are enumerated in case
111 of 'string-enum' and 'int-enum' type policies. 129 of 'string-enum' and 'int-enum' type policies.
112 130
113 Args: 131 Args:
114 parent: The DOM node for which the feature list will be added. 132 parent: The DOM node for which the feature list will be added.
115 policy: The data structure of a policy. 133 policy: The data structure of a policy.
116 ''' 134 '''
117 # Replace URLs with links in the description. 135
Mattias Nissler (ping if slow) 2015/02/10 14:09:55 nit: no reason for a blank line here AFAICT?
peletskyi 2015/02/10 16:12:56 Done.
118 self._AddTextWithLinks(parent, policy['desc']) 136 # Replace URLs with links and /n with <p>...</p> in the description.
Mattias Nissler (ping if slow) 2015/02/10 14:09:55 nit: I suggested to reword the comment to be clear
peletskyi 2015/02/10 16:12:56 I have "Break description into paragraphs and repl
137 self._AddParagraphs(parent, policy['desc'])
119 # Add list of enum items. 138 # Add list of enum items.
120 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'): 139 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'):
121 ul = self.AddElement(parent, 'ul') 140 ul = self.AddElement(parent, 'ul')
122 for item in policy['items']: 141 for item in policy['items']:
123 if policy['type'] == 'int-enum': 142 if policy['type'] == 'int-enum':
124 value_string = str(item['value']) 143 value_string = str(item['value'])
125 else: 144 else:
126 value_string = '"%s"' % item['value'] 145 value_string = '"%s"' % item['value']
127 self.AddElement( 146 self.AddElement(
128 ul, 'li', {}, '%s = %s' % (value_string, item['caption'])) 147 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. 520 a link for that page.
502 521
503 Args: 522 Args:
504 policy: The data structure of the policy. 523 policy: The data structure of the policy.
505 ''' 524 '''
506 if 'problem_href' not in policy: 525 if 'problem_href' not in policy:
507 return 526 return
508 problem_href = policy['problem_href'] 527 problem_href = policy['problem_href']
509 div = self._AddStyledElement(parent, 'div', ['div.note']) 528 div = self._AddStyledElement(parent, 'div', ['div.note'])
510 note = self._GetLocalizedMessage('note').replace('$6', problem_href) 529 note = self._GetLocalizedMessage('note').replace('$6', problem_href)
511 self._AddTextWithLinks(div, note) 530 self._AddParagraphs(div, note)
512 531
513 def _AddPolicyRow(self, parent, policy): 532 def _AddPolicyRow(self, parent, policy):
514 '''Adds a row for the policy in the summary table. 533 '''Adds a row for the policy in the summary table.
515 534
516 Args: 535 Args:
517 parent: The DOM node of the summary table. 536 parent: The DOM node of the summary table.
518 policy: The data structure of the policy. 537 policy: The data structure of the policy.
519 ''' 538 '''
520 tr = self._AddStyledElement(parent, 'tr', ['tr']) 539 tr = self._AddStyledElement(parent, 'tr', ['tr'])
521 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14) 540 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 609
591 def BeginTemplate(self): 610 def BeginTemplate(self):
592 # Add a <div> for the summary section. 611 # Add a <div> for the summary section.
593 if self._GetChromiumVersionString() is not None: 612 if self._GetChromiumVersionString() is not None:
594 self.AddComment(self._main_div, self.config['build'] + \ 613 self.AddComment(self._main_div, self.config['build'] + \
595 ' version: ' + self._GetChromiumVersionString()) 614 ' version: ' + self._GetChromiumVersionString())
596 615
597 summary_div = self.AddElement(self._main_div, 'div') 616 summary_div = self.AddElement(self._main_div, 'div')
598 self.AddElement(summary_div, 'a', {'name': 'top'}) 617 self.AddElement(summary_div, 'a', {'name': 'top'})
599 self.AddElement(summary_div, 'br') 618 self.AddElement(summary_div, 'br')
600 self._AddTextWithLinks( 619 self._AddParagraphs(
601 summary_div, 620 summary_div,
602 self._GetLocalizedMessage('intro')) 621 self._GetLocalizedMessage('intro'))
603 self.AddElement(summary_div, 'br') 622 self.AddElement(summary_div, 'br')
604 self.AddElement(summary_div, 'br') 623 self.AddElement(summary_div, 'br')
605 self.AddElement(summary_div, 'br') 624 self.AddElement(summary_div, 'br')
606 # Add the summary table of policies. 625 # Add the summary table of policies.
607 summary_table = self._AddStyledElement(summary_div, 'table', ['table']) 626 summary_table = self._AddStyledElement(summary_div, 'table', ['table'])
608 # Add the first row. 627 # Add the first row.
609 thead = self.AddElement(summary_table, 'thead') 628 thead = self.AddElement(summary_table, 'thead')
610 tr = self._AddStyledElement(thead, 'tr', ['tr']) 629 tr = self._AddStyledElement(thead, 'tr', ['tr'])
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 'td.right': 'width: 100%;', 702 'td.right': 'width: 100%;',
684 'dt': 'font-weight: bold;', 703 'dt': 'font-weight: bold;',
685 'dd dl': 'margin-top: 0px; margin-bottom: 0px;', 704 'dd dl': 'margin-top: 0px; margin-bottom: 0px;',
686 '.monospace': 'font-family: monospace;', 705 '.monospace': 'font-family: monospace;',
687 '.pre': 'white-space: pre;', 706 '.pre': 'white-space: pre;',
688 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;', 707 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;',
689 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;', 708 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;',
690 'ul': 'padding-left: 0px; margin-left: 0px;' 709 'ul': 'padding-left: 0px; margin-left: 0px;'
691 } 710 }
692 711
693 # A simple regexp to search for URLs. It is enough for now.
694 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])')
695 712
696 def GetTemplateText(self): 713 def GetTemplateText(self):
697 # Return the text representation of the main <div> tag. 714 # Return the text representation of the main <div> tag.
698 return self._main_div.toxml() 715 return self._main_div.toxml()
699 # To get a complete HTML file, use the following. 716 # To get a complete HTML file, use the following.
700 # return self._doc.toxml() 717 # 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