@@ -77,6 +77,7 @@ function render(lexed, filename, template, cb) {
77
77
78
78
filename = path . basename ( filename , '.markdown' ) ;
79
79
80
+ parseText ( lexed ) ;
80
81
lexed = parseLists ( lexed ) ;
81
82
82
83
// generate the table of contents.
@@ -105,6 +106,15 @@ function render(lexed, filename, template, cb) {
105
106
} ) ;
106
107
}
107
108
109
+ // handle general body-text replacements
110
+ // for example, link man page references to the actual page
111
+ function parseText ( lexed ) {
112
+ lexed . forEach ( function ( tok ) {
113
+ if ( tok . text ) {
114
+ tok . text = linkManPages ( tok . text ) ;
115
+ }
116
+ } ) ;
117
+ }
108
118
109
119
// just update the list item text in-place.
110
120
// lists that come right after a heading are what we're after.
@@ -167,11 +177,33 @@ function parseLists(input) {
167
177
}
168
178
169
179
180
+ // Syscalls which appear in the docs, but which only exist in BSD / OSX
181
+ var BSD_ONLY_SYSCALLS = new Set ( [ 'lchmod' ] ) ;
182
+
183
+ // Handle references to man pages, eg "open(2)" or "lchmod(2)"
184
+ // Returns modified text, with such refs replace with HTML links, for example
185
+ // '<a href="http://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>'
186
+ function linkManPages ( text ) {
187
+ return text . replace ( / ( [ a - z ] + ) \( ( \d ) \) / gm, function ( match , name , number ) {
188
+ // name consists of lowercase letters, number is a single digit
189
+ var displayAs = name + '(' + number + ')' ;
190
+ if ( BSD_ONLY_SYSCALLS . has ( name ) ) {
191
+ return ' <a href="https://www.freebsd.org/cgi/man.cgi?query=' + name +
192
+ '&sektion=' + number + '">' + displayAs + '</a>' ;
193
+ } else {
194
+ return ' <a href="http://man7.org/linux/man-pages/man' + number +
195
+ '/' + name + '.' + number + '.html">' + displayAs + '</a>' ;
196
+ }
197
+ } ) ;
198
+ }
199
+
170
200
function parseListItem ( text ) {
171
201
var parts = text . split ( '`' ) ;
172
202
var i ;
173
203
var typeMatches ;
174
204
205
+ // Handle types, for example the source Markdown might say
206
+ // "This argument should be a {Number} or {String}"
175
207
for ( i = 0 ; i < parts . length ; i += 2 ) {
176
208
typeMatches = parts [ i ] . match ( / \{ ( [ ^ \} ] + ) \} / g) ;
177
209
if ( typeMatches ) {
0 commit comments