@@ -77,6 +77,7 @@ function render(lexed, filename, template, cb) {
7777
7878 filename = path . basename ( filename , '.markdown' ) ;
7979
80+ parseText ( lexed ) ;
8081 lexed = parseLists ( lexed ) ;
8182
8283 // generate the table of contents.
@@ -105,6 +106,15 @@ function render(lexed, filename, template, cb) {
105106 } ) ;
106107}
107108
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+ }
108118
109119// just update the list item text in-place.
110120// lists that come right after a heading are what we're after.
@@ -167,11 +177,33 @@ function parseLists(input) {
167177}
168178
169179
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+
170200function parseListItem ( text ) {
171201 var parts = text . split ( '`' ) ;
172202 var i ;
173203 var typeMatches ;
174204
205+ // Handle types, for example the source Markdown might say
206+ // "This argument should be a {Number} or {String}"
175207 for ( i = 0 ; i < parts . length ; i += 2 ) {
176208 typeMatches = parts [ i ] . match ( / \{ ( [ ^ \} ] + ) \} / g) ;
177209 if ( typeMatches ) {
0 commit comments