23
23
24
24
25
25
_directive_regex = re .compile (r'\.\. \S+::' )
26
- _google_untyped_arg_regex = re .compile (r'(.+)\s*(?<!:):(?!: )\s*(.* )' )
27
- _google_typed_arg_regex = re .compile (r'(.+)\((.+)\)\s*(?<!:):(?!:)\s*(.* )' )
26
+ _google_typed_arg_regex = re .compile (r'\s* (.+? )\s*\(\s*(.+? )\s*\ )' )
27
+ _xref_regex = re .compile (r'(:\w+:\S+:`.+?`|:\S+:`.+?`|`.+?` )' )
28
28
29
29
30
30
class GoogleDocstring (UnicodeMixin ):
@@ -202,20 +202,14 @@ def _consume_empty(self):
202
202
def _consume_field (self , parse_type = True , prefer_type = False ):
203
203
line = next (self ._line_iter )
204
204
205
- match = None
206
- _name , _type , _desc = line .strip (), '' , ''
207
- if parse_type :
208
- match = _google_typed_arg_regex .match (line )
209
- if match :
210
- _name = match .group (1 ).strip ()
211
- _type = match .group (2 ).strip ()
212
- _desc = match .group (3 ).strip ()
205
+ before , colon , after = self ._partition_field_on_colon (line )
206
+ _name , _type , _desc = before , '' , after
213
207
214
- if not match :
215
- match = _google_untyped_arg_regex .match (line )
208
+ if parse_type :
209
+ match = _google_typed_arg_regex .match (before )
216
210
if match :
217
- _name = match .group (1 ). strip ()
218
- _desc = match .group (2 ). strip ( )
211
+ _name = match .group (1 )
212
+ _type = match .group (2 )
219
213
220
214
if _name [:2 ] == '**' :
221
215
_name = r'\*\*' + _name [2 :]
@@ -241,20 +235,21 @@ def _consume_fields(self, parse_type=True, prefer_type=False):
241
235
def _consume_returns_section (self ):
242
236
lines = self ._dedent (self ._consume_to_next_section ())
243
237
if lines :
238
+ before , colon , after = self ._partition_field_on_colon (lines [0 ])
244
239
_name , _type , _desc = '' , '' , lines
245
- match = _google_typed_arg_regex .match (lines [0 ])
246
- if match :
247
- _name = match .group (1 ).strip ()
248
- _type = match .group (2 ).strip ()
249
- _desc = match .group (3 ).strip ()
250
- else :
251
- match = _google_untyped_arg_regex .match (lines [0 ])
240
+
241
+ if colon :
242
+ if after :
243
+ _desc = [after ] + lines [1 :]
244
+ else :
245
+ _desc = lines [1 :]
246
+
247
+ match = _google_typed_arg_regex .match (before )
252
248
if match :
253
- _type = match .group (1 ).strip ()
254
- _desc = match .group (2 ).strip ()
255
- if match :
256
- lines [0 ] = _desc
257
- _desc = lines
249
+ _name = match .group (1 )
250
+ _type = match .group (2 )
251
+ else :
252
+ _type = before
258
253
259
254
_desc = self .__class__ (_desc , self ._config ).lines ()
260
255
return [(_name , _type , _desc ,)]
@@ -593,6 +588,27 @@ def _parse_yields_section(self, section):
593
588
fields = self ._consume_returns_section ()
594
589
return self ._format_fields ('Yields' , fields )
595
590
591
+ def _partition_field_on_colon (self , line ):
592
+ before_colon = []
593
+ after_colon = []
594
+ colon = ''
595
+ found_colon = False
596
+ for i , source in enumerate (_xref_regex .split (line )):
597
+ if found_colon :
598
+ after_colon .append (source )
599
+ else :
600
+ if (i % 2 ) == 0 and ":" in source :
601
+ found_colon = True
602
+ before , colon , after = source .partition (":" )
603
+ before_colon .append (before )
604
+ after_colon .append (after )
605
+ else :
606
+ before_colon .append (source )
607
+
608
+ return ("" .join (before_colon ).strip (),
609
+ colon ,
610
+ "" .join (after_colon ).strip ())
611
+
596
612
def _strip_empty (self , lines ):
597
613
if lines :
598
614
start = - 1
@@ -719,7 +735,7 @@ def __init__(self, docstring, config=None, app=None, what='', name='',
719
735
def _consume_field (self , parse_type = True , prefer_type = False ):
720
736
line = next (self ._line_iter )
721
737
if parse_type :
722
- _name , _ , _type = line . partition ( ':' )
738
+ _name , _ , _type = self . _partition_field_on_colon ( line )
723
739
if not _name :
724
740
_type = line
725
741
else :
0 commit comments