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

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 def _AddTextWithLinks(self, parent, text): 61 def _AddTextWithLinksAndParagraphs(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 paragraphs and
63 with <a> HTML links. 63 add it to a DOM node with the URLs replaced
64 with <a> HTML links and paragraphs replaced by <p>.
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 '''
69 # Iterate through all the URLs and replace them with links. 70
70 out = [] 71 # Open paragraph
72 paragraph = self.AddElement(parent, 'p')
71 while True: 73 while True:
72 # Look for the first URL. 74 # Look for the first URL or break line.
73 res = self._url_matcher.search(text) 75 res = self._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 or new line.
77 url = res.group(0) 79 group = res.group(0) # Either "/n/n" or "http://some.url"
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 or break line.
81 self.AddText(parent, text[:start]) 83 self.AddText(paragraph, text[:start])
82 # Add a link for the URL. 84 # Either add new paragraph or add a link for the URL.
83 self.AddElement(parent, 'a', {'href': url}, url) 85 if group == "\n\n": #have we found a break line?
86 paragraph = self.AddElement(parent, 'p')
87 else:
88 self.AddElement(paragraph, 'a', {'href': group}, group)
84 # Drop the part of text that is added. 89 # Drop the part of text that is added.
85 text = text[end:] 90 text = text[end:]
86 self.AddText(parent, text) 91 self.AddText(paragraph, text)
87 92
88 93
89 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None): 94 def _AddStyledElement(self, parent, name, style_ids, attrs=None, text=None):
90 '''Adds an XML element to a parent, with CSS style-sheets included. 95 '''Adds an XML element to a parent, with CSS style-sheets included.
91 96
92 Args: 97 Args:
93 parent: The parent DOM node. 98 parent: The parent DOM node.
94 name: Name of the element to add. 99 name: Name of the element to add.
95 style_ids: A list of CSS style strings from self._STYLE[]. 100 style_ids: A list of CSS style strings from self._STYLE[].
96 attrs: Dictionary of attributes for the element. 101 attrs: Dictionary of attributes for the element.
(...skipping 10 matching lines...) Expand all
107 112
108 def _AddDescription(self, parent, policy): 113 def _AddDescription(self, parent, policy):
109 '''Adds a string containing the description of the policy. URLs are 114 '''Adds a string containing the description of the policy. URLs are
110 replaced with links and the possible choices are enumerated in case 115 replaced with links and the possible choices are enumerated in case
111 of 'string-enum' and 'int-enum' type policies. 116 of 'string-enum' and 'int-enum' type policies.
112 117
113 Args: 118 Args:
114 parent: The DOM node for which the feature list will be added. 119 parent: The DOM node for which the feature list will be added.
115 policy: The data structure of a policy. 120 policy: The data structure of a policy.
116 ''' 121 '''
117 # Replace URLs with links in the description. 122
118 self._AddTextWithLinks(parent, policy['desc']) 123 # Replace URLs with links and /n with <p>...</p> in the description.
124 self._AddTextWithLinksAndParagraphs(parent, policy['desc'])
119 # Add list of enum items. 125 # Add list of enum items.
120 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'): 126 if policy['type'] in ('string-enum', 'int-enum', 'string-enum-list'):
121 ul = self.AddElement(parent, 'ul') 127 ul = self.AddElement(parent, 'ul')
122 for item in policy['items']: 128 for item in policy['items']:
123 if policy['type'] == 'int-enum': 129 if policy['type'] == 'int-enum':
124 value_string = str(item['value']) 130 value_string = str(item['value'])
125 else: 131 else:
126 value_string = '"%s"' % item['value'] 132 value_string = '"%s"' % item['value']
127 self.AddElement( 133 self.AddElement(
128 ul, 'li', {}, '%s = %s' % (value_string, item['caption'])) 134 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. 507 a link for that page.
502 508
503 Args: 509 Args:
504 policy: The data structure of the policy. 510 policy: The data structure of the policy.
505 ''' 511 '''
506 if 'problem_href' not in policy: 512 if 'problem_href' not in policy:
507 return 513 return
508 problem_href = policy['problem_href'] 514 problem_href = policy['problem_href']
509 div = self._AddStyledElement(parent, 'div', ['div.note']) 515 div = self._AddStyledElement(parent, 'div', ['div.note'])
510 note = self._GetLocalizedMessage('note').replace('$6', problem_href) 516 note = self._GetLocalizedMessage('note').replace('$6', problem_href)
511 self._AddTextWithLinks(div, note) 517 self._AddTextWithLinksAndParagraphs(div, note)
512 518
513 def _AddPolicyRow(self, parent, policy): 519 def _AddPolicyRow(self, parent, policy):
514 '''Adds a row for the policy in the summary table. 520 '''Adds a row for the policy in the summary table.
515 521
516 Args: 522 Args:
517 parent: The DOM node of the summary table. 523 parent: The DOM node of the summary table.
518 policy: The data structure of the policy. 524 policy: The data structure of the policy.
519 ''' 525 '''
520 tr = self._AddStyledElement(parent, 'tr', ['tr']) 526 tr = self._AddStyledElement(parent, 'tr', ['tr'])
521 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14) 527 indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 596
591 def BeginTemplate(self): 597 def BeginTemplate(self):
592 # Add a <div> for the summary section. 598 # Add a <div> for the summary section.
593 if self._GetChromiumVersionString() is not None: 599 if self._GetChromiumVersionString() is not None:
594 self.AddComment(self._main_div, self.config['build'] + \ 600 self.AddComment(self._main_div, self.config['build'] + \
595 ' version: ' + self._GetChromiumVersionString()) 601 ' version: ' + self._GetChromiumVersionString())
596 602
597 summary_div = self.AddElement(self._main_div, 'div') 603 summary_div = self.AddElement(self._main_div, 'div')
598 self.AddElement(summary_div, 'a', {'name': 'top'}) 604 self.AddElement(summary_div, 'a', {'name': 'top'})
599 self.AddElement(summary_div, 'br') 605 self.AddElement(summary_div, 'br')
600 self._AddTextWithLinks( 606 self._AddTextWithLinksAndParagraphs(
601 summary_div, 607 summary_div,
602 self._GetLocalizedMessage('intro')) 608 self._GetLocalizedMessage('intro'))
603 self.AddElement(summary_div, 'br') 609 self.AddElement(summary_div, 'br')
604 self.AddElement(summary_div, 'br') 610 self.AddElement(summary_div, 'br')
605 self.AddElement(summary_div, 'br') 611 self.AddElement(summary_div, 'br')
606 # Add the summary table of policies. 612 # Add the summary table of policies.
607 summary_table = self._AddStyledElement(summary_div, 'table', ['table']) 613 summary_table = self._AddStyledElement(summary_div, 'table', ['table'])
608 # Add the first row. 614 # Add the first row.
609 thead = self.AddElement(summary_table, 'thead') 615 thead = self.AddElement(summary_table, 'thead')
610 tr = self._AddStyledElement(thead, 'tr', ['tr']) 616 tr = self._AddStyledElement(thead, 'tr', ['tr'])
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 'dt': 'font-weight: bold;', 690 'dt': 'font-weight: bold;',
685 'dd dl': 'margin-top: 0px; margin-bottom: 0px;', 691 'dd dl': 'margin-top: 0px; margin-bottom: 0px;',
686 '.monospace': 'font-family: monospace;', 692 '.monospace': 'font-family: monospace;',
687 '.pre': 'white-space: pre;', 693 '.pre': 'white-space: pre;',
688 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;', 694 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;',
689 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;', 695 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;',
690 'ul': 'padding-left: 0px; margin-left: 0px;' 696 'ul': 'padding-left: 0px; margin-left: 0px;'
691 } 697 }
692 698
693 # A simple regexp to search for URLs. It is enough for now. 699 # A simple regexp to search for URLs. It is enough for now.
694 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])') 700 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])|\n\n')
Mattias Nissler (ping if slow) 2015/02/10 08:30:08 It's rather confusing to do both URL matching and
695 701
696 def GetTemplateText(self): 702 def GetTemplateText(self):
697 # Return the text representation of the main <div> tag. 703 # Return the text representation of the main <div> tag.
698 return self._main_div.toxml() 704 return self._main_div.toxml()
699 # To get a complete HTML file, use the following. 705 # To get a complete HTML file, use the following.
700 # return self._doc.toxml() 706 # 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