@@ -48,14 +48,50 @@ export function extractSignatureAndDocumentation(definition: proxy.IAutoComplete
4848 return [ signature , lines . join ( EOL ) . trim ( ) . replace ( / ^ \s + | \s + $ / g, '' ) . trim ( ) ] ;
4949}
5050
51- export function highlightCode ( documentation : string ) : string {
52- let lines = documentation . split ( / \r ? \n / ) ;
53- lines = lines . map ( line => {
54- if ( line . trim ( ) . startsWith ( '>>> ' ) ) {
55- return '```python\n' + line . substring ( 4 ) . trim ( ) + '\n```' ;
51+ export function highlightCode ( docstring : string ) : string {
52+ // Add blank line
53+ docstring = EOL + EOL + docstring + EOL + EOL ;
54+ // Section title -> heading level 2
55+ docstring = docstring . replace ( / ( .+ \r ? \n ) [ - = ] + \r ? \n / g, '## $1' + EOL ) ;
56+ // Directives: '.. directive::' -> '**directive**'
57+ docstring = docstring . replace ( / \. \. ( .* ) : : / g, '**$1**' ) ;
58+ // Field lists: ':field:' -> '**field**'
59+ docstring = docstring . replace ( / : ( .+ ?) : / g, '**$1** ' ) ;
60+ // Pattern of 'var : description'
61+ let paramLinePattern = '[\\*\\w_]+ ?:[^:\r\n]+' ;
62+ // Add new line after and before param line
63+ docstring = docstring . replace ( new RegExp ( `(${ EOL + paramLinePattern } )` , 'g' ) , `$1${ EOL } ` ) ;
64+ docstring = docstring . replace ( new RegExp ( `(${ EOL + paramLinePattern + EOL } )` , 'g' ) , `${ EOL } $1` ) ;
65+ // 'var : description' -> '`var` description'
66+ docstring = docstring . replace ( / \r ? \n ( [ \* \w ] + ) ? : ? ( [ ^ : \r \n ] + \r ? \n ) / g, `${ EOL } \`$1\` $2` ) ;
67+ // Doctest blocks: begin with `>>>` and end with blank line
68+ docstring = docstring . replace ( / ( > > > [ \w \W ] + ?\r ? \n ) \r ? \n / g, `${ '```python' + EOL } $1${ '```' + EOL + EOL } ` ) ;
69+ // Literal blocks: begin with `::` (literal blocks are indented or quoted; for simplicity, we end literal blocks with blank line)
70+ docstring = docstring . replace ( / ( \r ? \n [ ^ \. ] * ) : : \r ? \n \r ? \n ( [ \w \W ] + ?\r ? \n ) \r ? \n / g, `$1${ EOL + '```' + EOL } $2${ '```' + EOL + EOL } ` ) ;
71+ // Remove indentation in Field lists and Literal blocks
72+ let inCodeBlock = false ;
73+ let codeIndentation = 0 ;
74+ let lines = docstring . split ( / \r ? \n / ) ;
75+ for ( let i = 0 ; i < lines . length ; i ++ ) {
76+ let line = lines [ i ] ;
77+ if ( line . startsWith ( '```' ) ) {
78+ inCodeBlock = ! inCodeBlock ;
79+ if ( inCodeBlock ) {
80+ codeIndentation = lines [ i + 1 ] . match ( / ^ * / ) [ 0 ] . length ;
81+ }
82+ continue ;
5683 }
57- return line ;
58- } ) ;
59-
60- return lines . join ( EOL ) . trim ( ) . replace ( / ^ \s + | \s + $ / g, '' ) . trim ( ) ;
84+ if ( ! inCodeBlock ) {
85+ lines [ i ] = line . replace ( / ^ { 4 , 8 } / , '' ) ;
86+ } else {
87+ if ( codeIndentation != 0 ) {
88+ lines [ i ] = line . substring ( codeIndentation ) ;
89+ }
90+ }
91+ }
92+ docstring = lines . join ( EOL ) ;
93+ // Grid Tables
94+ docstring = docstring . replace ( / \r ? \n [ \+ - ] + \r ? \n / g, EOL ) ;
95+ docstring = docstring . replace ( / \r ? \n [ \+ = ] + \r ? \n / g, s => s . replace ( / \+ / g, '|' ) . replace ( / = / g, '-' ) ) ;
96+ return docstring ;
6197}
0 commit comments