@@ -8,6 +8,8 @@ var htmlEscape = require('./dom.js').htmlEscape;
8
8
var Example = require ( './example.js' ) . Example ;
9
9
var NEW_LINE = / \n \r ? / ;
10
10
var globalID = 0 ;
11
+ var fs = require ( 'fs' ) ;
12
+ var fspath = require ( 'path' ) ;
11
13
12
14
exports . trim = trim ;
13
15
exports . metadata = metadata ;
@@ -113,6 +115,19 @@ Doc.prototype = {
113
115
return id ;
114
116
}
115
117
118
+ function extractInlineDocCode ( text , tag ) {
119
+ if ( tag == 'all' ) {
120
+ //use a greedy operator to match the last </docs> tag
121
+ regex = / \/ \/ < d o c s .* ?> ( [ . \s \S ] + ) \/ \/ < \/ d o c s > / im;
122
+ }
123
+ else {
124
+ //use a non-greedy operator to match the next </docs> tag
125
+ regex = new RegExp ( "\/\/<docs\\s*tag=\"" + tag + "\".*?>([.\\s\\S]+?)\/\/<\/docs>" , "im" ) ;
126
+ }
127
+ var matches = regex . exec ( text . toString ( ) ) ;
128
+ return matches && matches . length > 1 ? matches [ 1 ] : "" ;
129
+ }
130
+
116
131
parts . forEach ( function ( text , i ) {
117
132
parts [ i ] = ( text || '' ) .
118
133
replace ( / < e x a m p l e (?: \s + m o d u l e = " ( [ ^ " ] * ) " ) ? (?: \s + d e p s = " ( [ ^ " ] * ) " ) ? > ( [ \s \S ] * ?) < \/ e x a m p l e > / gmi, function ( _ , module , deps , content ) {
@@ -123,8 +138,30 @@ Doc.prototype = {
123
138
content . replace ( / < f i l e \s + n a m e = " ( [ ^ " ] * ) " \s * > ( [ \s \S ] * ?) < \/ f i l e > / gmi, function ( _ , name , content ) {
124
139
example . addSource ( name , content ) ;
125
140
} ) ;
141
+ content . replace ( / < f i l e \s + s r c = " ( [ ^ " ] + ) " (?: \s + t a g = " ( [ ^ " ] + ) " ) ? (?: \s + n a m e = " ( [ ^ " ] + ) " ) ? \s * \/ ? > / gmi, function ( _ , file , tag , name ) {
142
+ if ( fspath . existsSync ( file ) ) {
143
+ var content = fs . readFileSync ( file , 'utf8' ) ;
144
+ if ( content && content . length > 0 ) {
145
+ if ( tag && tag . length > 0 ) {
146
+ content = extractInlineDocCode ( content , tag ) ;
147
+ }
148
+ name = name && name . length > 0 ? name : fspath . basename ( file ) ;
149
+ example . addSource ( name , content ) ;
150
+ }
151
+ }
152
+ return '' ;
153
+ } )
126
154
return placeholder ( example . toHtml ( ) ) ;
127
155
} ) .
156
+ replace ( / (?: \* \s + ) ? < f i l e .+ ?s r c = " ( [ ^ " ] + ) " (?: \s + t a g = " ( [ ^ " ] + ) " ) ? \s * \/ ? > / i, function ( _ , file , tag ) {
157
+ if ( fspath . existsSync ( file ) ) {
158
+ var content = fs . readFileSync ( file , 'utf8' ) ;
159
+ if ( tag && tag . length > 0 ) {
160
+ content = extractInlineDocCode ( content , tag ) ;
161
+ }
162
+ return content ;
163
+ }
164
+ } ) .
128
165
replace ( / ^ < d o c : e x a m p l e ( \s + [ ^ > ] * ) ? > ( [ \s \S ] * ) < \/ d o c : e x a m p l e > / mi, function ( _ , attrs , content ) {
129
166
var html , script , scenario ,
130
167
example = new Example ( self . scenarios ) ;
0 commit comments