@@ -42,7 +42,6 @@ program
4242 . parse ( )
4343const debug = program . opts ( ) . debug
4444
45-
4645const doInclude = ( content , includes , f ) => {
4746 const result = content . replace ( includesRE , ( m , m1 ) => {
4847 if ( ! m1 . length ) return m
@@ -91,33 +90,80 @@ const processDovecotMdPost = () => {
9190 }
9291}
9392
93+ const DovecotMd = ( md ) => {
94+ const parts = md . split ( ',' ) . map ( ( x ) => x . trim ( ) )
95+ switch ( parts [ 0 ] ) {
96+ case 'doveadm' :
97+ return [
98+ {
99+ type :'strong' ,
100+ children : [
101+ {
102+ type :'text' , value :'doveadm(1)'
103+ }
104+ ]
105+ } ,
106+ {
107+ type :'emphasis' ,
108+ children : [
109+ {
110+ type :'text' , value :' ' + parts . slice ( 1 ) . join ( ' ' )
111+ }
112+ ]
113+ }
114+ ]
115+ case 'man' :
116+ return [ { type :'text' , value :parts [ 1 ] + '(' + ( parts [ 3 ] ? parts [ 3 ] : '1' ) + ')' } ]
117+ case 'plugin' :
118+ return [ { type :'text' , value :parts [ 1 ] + ' plugin documentation' } ]
119+ case 'rfc' :
120+ return [ { type :'text' , value :'RFC ' + parts [ 1 ] } ]
121+ case 'setting' :
122+ return [
123+ {
124+ type :'strong' ,
125+ children : [
126+ {
127+ type :'text' , value :parts [ 1 ]
128+ }
129+ ]
130+ } ,
131+ {
132+ type :'text' , value :' setting'
133+ }
134+ ]
135+ case 'link' :
136+ return [ { type :'text' , value :parts [ 1 ] } ]
137+ default :
138+ throw new Error ( 'unknown dovecot markdown command: ' + parts [ 0 ] )
139+ }
140+ }
141+
94142const processDovecotMdPre = ( ) => {
95143 return tree => {
96144 /* Go through and replace Dovecot markdown items with man-friendly
97145 * textual equivalents. */
98- return map ( tree , ( node ) => {
99- if ( node . value ) {
100- node . value = node . value . replace ( includesDM , ( m , m1 ) => {
101- if ( ! m1 . length ) return m
102-
103- const parts = m1 . split ( ',' ) . map ( ( x ) => x . trim ( ) )
104- switch ( parts [ 0 ] ) {
105- case 'man' :
106- return parts [ 1 ] + '(' + ( parts [ 3 ] ? parts [ 3 ] : '1' ) + ')'
107- case 'plugin' :
108- return parts [ 1 ] + ' plugin documentation'
109- case 'rfc' :
110- return 'RFC ' + parts [ 1 ]
111- case 'setting' :
112- return '`' + parts [ 1 ] + '`'
113- case 'link' :
114- return parts [ 1 ]
115- default :
116- throw new Error ( 'unknown dovecot markdown command: ' + parts [ 0 ] )
117- }
118- } )
119- }
120- return node
146+ return map ( tree , ( para ) => {
147+ visit ( para , 'text' , ( node , index , para ) => {
148+ /* if the text contains at least one tag, we need to convert
149+ * this into an array of children that replace the parent's children. */
150+ var result ;
151+ var newChildren = [ ]
152+ var pos = 0 ;
153+ while ( ( result = includesDM . exec ( node . value ) ) ) {
154+ const pre = node . value . substr ( pos , result . index - pos )
155+ pos = result . index + result [ 0 ] . length
156+ if ( pre != "" )
157+ newChildren . push ( { type : 'text' , value : pre } )
158+ newChildren . push ( ...DovecotMd ( result [ 1 ] ) )
159+ }
160+ if ( newChildren . length != 0 ) {
161+ if ( pos < node . value . length )
162+ newChildren . push ( { type : 'text' , value : node . value . substr ( pos ) } )
163+ para . children = newChildren
164+ }
165+ } )
166+ return para
121167 } )
122168 }
123169}
0 commit comments