@@ -14,88 +14,97 @@ exports.view = function (aReq, aRes, aNext) {
14
14
var options = { } ;
15
15
var tasks = [ ] ;
16
16
17
-
18
- var matches = null ;
19
17
var page = null ;
20
-
21
18
var documentPath = null ;
22
-
23
- //---
24
- function preRender ( ) { } ;
25
- function render ( ) { aRes . render ( 'pages/documentPage' , options ) ; }
26
- function asyncComplete ( err ) { if ( err ) { return aNext ( ) ; } else { preRender ( ) ; render ( ) ; } } ;
19
+ var document = aReq . route . params . document ;
27
20
28
21
// Session
29
22
authedUser = options . authedUser = modelParser . parseUser ( authedUser ) ;
30
23
options . isMod = authedUser && authedUser . isMod ;
31
24
options . isAdmin = authedUser && authedUser . isAdmin ;
32
25
33
- //--- Tasks
34
-
35
- var document = aReq . route . params . document ;
36
26
37
27
if ( document ) {
38
28
documentPath = 'views/includes/documents' ;
39
29
40
- fs . readdir ( documentPath , function ( aErr , aFiles ) {
41
- if ( ! aErr ) {
42
- var file = null ;
43
-
44
- // Dynamically create a file listing of the pages
45
- options . files = [ ] ;
46
- for ( file in aFiles ) {
47
- if ( / \. m d $ / . test ( aFiles [ file ] ) ) {
48
- options . files . push ( {
49
- href : aFiles [ file ] . replace ( / \. m d $ / , '' ) ,
50
- textContent : aFiles [ file ] . replace ( / \. m d $ / , '' ) . replace ( / - / g, ' ' )
51
- } ) ;
52
- }
53
- }
54
- }
55
- } ) ;
56
-
57
- fs . readFile ( documentPath + '/' + document + '.md' , 'UTF8' , function ( aErr , aData ) {
58
- if ( aErr ) {
59
- return statusCodePage ( aReq , aRes , aNext , {
60
- statusCode : 404 ,
61
- statusMessage : 'Page not found.'
62
- } ) ;
63
- }
64
-
65
- var lines = null ;
66
- var heading = null ;
67
- var content = null ;
68
-
69
- // Check if first line is h2 and use for title/heading if present
70
- lines = aData . match ( / .* / gm) ;
71
- if ( lines ) {
72
- matches = lines [ 0 ] . match ( / ^ # # \s ( .* ) $ / ) ;
73
- if ( matches ) {
74
- heading = lines . shift ( ) . replace ( / ^ # # \s + / , "" ) ;
75
- } else {
76
- heading = page ;
30
+ //--- Tasks
31
+ tasks . push ( function ( aCallback ) {
32
+ async . waterfall ( [
33
+
34
+ // Read file listing
35
+ function ( aCallback ) {
36
+ fs . readdir ( documentPath , function ( aErr , aFiles ) {
37
+ if ( aErr ) { aCallback ( 'Error retrieving page list' ) ; return ; }
38
+
39
+ var file = null ;
40
+
41
+ // Dynamically create a file listing of the pages
42
+ options . files = [ ] ;
43
+ for ( file in aFiles ) {
44
+ if ( / \. m d $ / . test ( aFiles [ file ] ) ) {
45
+ options . files . push ( {
46
+ href : aFiles [ file ] . replace ( / \. m d $ / , '' ) ,
47
+ textContent : aFiles [ file ] . replace ( / \. m d $ / , '' ) . replace ( / - / g, ' ' )
48
+ } ) ;
49
+ }
50
+ }
51
+
52
+ aCallback ( null ) ;
53
+ } ) ;
54
+ } ,
55
+
56
+ // Read md file contents
57
+ function ( aCallback ) {
58
+ fs . readFile ( documentPath + '/' + document + '.md' , 'UTF8' , function ( aErr , aData ) {
59
+ if ( aErr ) { aCallback ( 'Error retrieving page' ) ; return ; }
60
+
61
+ var lines = null ;
62
+ var matches = null ;
63
+ var heading = null ;
64
+ var content = null ;
65
+
66
+ // Check if first line is h2 and use for title/heading if present
67
+ lines = aData . match ( / .* / gm) ;
68
+ if ( lines ) {
69
+ matches = lines [ 0 ] . match ( / ^ # # \s ( .* ) $ / ) ;
70
+ if ( matches ) {
71
+ heading = lines . shift ( ) . replace ( / ^ # # \s + / , "" ) ;
72
+ } else {
73
+ heading = page ;
74
+ }
75
+ content = lines . join ( '\n' ) ;
76
+ } else {
77
+ heading = page ;
78
+ content = aData ;
79
+ }
80
+
81
+ // Page metadata
82
+ pageMetadata ( options , [ heading , 'About' ] ) ;
83
+
84
+ options . pageHeading = heading ;
85
+ options . pageData = renderMd ( content ) ;
86
+
87
+ aCallback ( null ) ;
88
+ } ) ;
77
89
}
78
- content = lines . join ( '\n' ) ;
79
- } else {
80
- heading = page ;
81
- content = aData ;
82
- }
83
-
84
- // Page metadata
85
- pageMetadata ( options , [ heading , 'About' ] ) ;
86
-
87
- options . pageHeading = heading ;
88
- options . pageData = renderMd ( content ) ;
89
-
90
- async . parallel ( tasks , asyncComplete ) ;
90
+ ] , aCallback )
91
91
} ) ;
92
92
}
93
93
else {
94
94
// Page metadata
95
95
pageMetadata ( options , [ 'About' , 'About' ] ) ;
96
96
97
97
options . isAbout = true ;
98
-
99
- async . parallel ( tasks , asyncComplete ) ;
100
98
}
99
+
100
+ //---
101
+ async . parallel ( tasks , function ( aErr ) {
102
+ if ( aErr ) {
103
+ return statusCodePage ( aReq , aRes , aNext , {
104
+ statusMessage : aErr
105
+ } )
106
+ }
107
+
108
+ aRes . render ( 'pages/documentPage' , options ) ;
109
+ } ) ;
101
110
} ;
0 commit comments