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

Side by Side Diff: tools/json_schema_compiler/idl_schema.py

Issue 404203002: Extension docs: Make comments in IDL files generate paragraphs rather than (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, bump yaml files Created 6 years, 5 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
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 import itertools 6 import itertools
7 import json 7 import json
8 import os.path 8 import os.path
9 import re 9 import re
10 import sys 10 import sys
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 49
50 Returns: A tuple that looks like: 50 Returns: A tuple that looks like:
51 ( 51 (
52 "The processed comment, minus all |parameter| mentions.", 52 "The processed comment, minus all |parameter| mentions.",
53 { 53 {
54 'parameter_name_1': "The comment that followed |parameter_name_1|:", 54 'parameter_name_1': "The comment that followed |parameter_name_1|:",
55 ... 55 ...
56 } 56 }
57 ) 57 )
58 ''' 58 '''
59 def add_paragraphs(content):
60 paragraphs = content.split('\n\n')
61 if len(paragraphs) < 2:
62 return content
63 return '<p>' + '</p><p>'.join(p.strip() for p in paragraphs) + '</p>'
64
59 # Find all the parameter comments of the form '|name|: comment'. 65 # Find all the parameter comments of the form '|name|: comment'.
60 parameter_starts = list(re.finditer(r' *\|([^|]*)\| *: *', comment)) 66 parameter_starts = list(re.finditer(r' *\|([^|]*)\| *: *', comment))
61 67
62 # Get the parent comment (everything before the first parameter comment. 68 # Get the parent comment (everything before the first parameter comment.
63 first_parameter_location = (parameter_starts[0].start() 69 first_parameter_location = (parameter_starts[0].start()
64 if parameter_starts else len(comment)) 70 if parameter_starts else len(comment))
65 parent_comment = comment[:first_parameter_location] 71 parent_comment = (add_paragraphs(comment[:first_parameter_location].strip())
66 72 .replace('\n', ''))
67 # We replace \n\n with <br/><br/> here and below, because the documentation
68 # needs to know where the newlines should be, and this is easier than
69 # escaping \n.
70 parent_comment = (parent_comment.strip().replace('\n\n', '<br/><br/>')
71 .replace('\n', ''))
72 73
73 params = OrderedDict() 74 params = OrderedDict()
74 for (cur_param, next_param) in itertools.izip_longest(parameter_starts, 75 for (cur_param, next_param) in itertools.izip_longest(parameter_starts,
75 parameter_starts[1:]): 76 parameter_starts[1:]):
76 param_name = cur_param.group(1) 77 param_name = cur_param.group(1)
77 78
78 # A parameter's comment goes from the end of its introduction to the 79 # A parameter's comment goes from the end of its introduction to the
79 # beginning of the next parameter's introduction. 80 # beginning of the next parameter's introduction.
80 param_comment_start = cur_param.end() 81 param_comment_start = cur_param.end()
81 param_comment_end = next_param.start() if next_param else len(comment) 82 param_comment_end = next_param.start() if next_param else len(comment)
82 params[param_name] = (comment[param_comment_start:param_comment_end 83 params[param_name] = (
83 ].strip().replace('\n\n', '<br/><br/>') 84 add_paragraphs(comment[param_comment_start:param_comment_end].strip())
84 .replace('\n', '')) 85 .replace('\n', ''))
86
85 return (parent_comment, params) 87 return (parent_comment, params)
86 88
87 89
88 class Callspec(object): 90 class Callspec(object):
89 ''' 91 '''
90 Given a Callspec node representing an IDL function declaration, converts into 92 Given a Callspec node representing an IDL function declaration, converts into
91 a tuple: 93 a tuple:
92 (name, list of function parameters, return type) 94 (name, list of function parameters, return type)
93 ''' 95 '''
94 def __init__(self, callspec_node, comment): 96 def __init__(self, callspec_node, comment):
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 print json.dumps(schema, indent=2) 479 print json.dumps(schema, indent=2)
478 else: 480 else:
479 contents = sys.stdin.read() 481 contents = sys.stdin.read()
480 idl = idl_parser.IDLParser().ParseData(contents, '<stdin>') 482 idl = idl_parser.IDLParser().ParseData(contents, '<stdin>')
481 schema = IDLSchema(idl).process() 483 schema = IDLSchema(idl).process()
482 print json.dumps(schema, indent=2) 484 print json.dumps(schema, indent=2)
483 485
484 486
485 if __name__ == '__main__': 487 if __name__ == '__main__':
486 Main() 488 Main()
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/dart_test/comments.idl ('k') | tools/json_schema_compiler/idl_schema_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698