@@ -50,6 +50,44 @@ var getPath = function(req){ |
||
50 | 50 |
return path; |
51 | 51 |
} |
52 | 52 |
|
53 |
+var getIndexFilePath = function(path, req, res){ |
|
54 |
+ if(req.params['file'] != undefined){ |
|
55 |
+ path = path + req.params['file'] + "/"; |
|
56 |
+ } |
|
57 |
+ console.log("Looking for index file in " + path); |
|
58 |
+ fs.readFile(path + "index.html", 'utf8', function (err,data) { |
|
59 |
+ if (err) { |
|
60 |
+ fs.readFile(path + "index.md", 'utf8', function (err,data) { |
|
61 |
+ if (err) { |
|
62 |
+ error_404(req, res); |
|
63 |
+ } else { |
|
64 |
+ req.params['file'] = buildFilename(req.params['file'], "index.md"); |
|
65 |
+ get_file(req, res); |
|
66 |
+ } |
|
67 |
+ }); |
|
68 |
+ } else { |
|
69 |
+ req.params['file'] = buildFilename(req.params['file'], "index.html"); |
|
70 |
+ get_file(req, res); |
|
71 |
+ } |
|
72 |
+ }); |
|
73 |
+} |
|
74 |
+ |
|
75 |
+var isFile = function(filename){ |
|
76 |
+ var re = /(?:\.([^.]+))?$/; |
|
77 |
+ var ext = re.exec(filename)[1]; |
|
78 |
+ if(ext != undefined){ |
|
79 |
+ return true; |
|
80 |
+ } |
|
81 |
+ return false; |
|
82 |
+} |
|
83 |
+ |
|
84 |
+var buildFilename = function(path, filename){ |
|
85 |
+ if(path == undefined){ |
|
86 |
+ return filename; |
|
87 |
+ } |
|
88 |
+ return path + "/" + filename |
|
89 |
+} |
|
90 |
+ |
|
53 | 91 |
var get_file = function(req, res){ |
54 | 92 |
|
55 | 93 |
var path = getPath(req); |
@@ -58,7 +96,7 @@ var get_file = function(req, res){ |
||
58 | 96 |
var extension; |
59 | 97 |
|
60 | 98 |
// Load file or look for index? |
61 |
- if(req.params['file'] != undefined){ |
|
99 |
+ if(isFile(req.params['file'])){ |
|
62 | 100 |
// Set filetype |
63 | 101 |
path = path + req.params['file']; |
64 | 102 |
parts = req.params['file'].split("."); |
@@ -71,6 +109,8 @@ var get_file = function(req, res){ |
||
71 | 109 |
} |
72 | 110 |
} else { |
73 | 111 |
// return index file if it exists |
112 |
+ path = getIndexFilePath(path, req, res) |
|
113 |
+ return; |
|
74 | 114 |
} |
75 | 115 |
// Action for each filetype |
76 | 116 |
if(file_type == "markdown"){ |
@@ -108,7 +148,7 @@ var write_file = function(req, res){ |
||
108 | 148 |
var path = getPath(req); |
109 | 149 |
var save_data; |
110 | 150 |
// Write file |
111 |
- if(req.params['file'] != undefined){ |
|
151 |
+ if(isFile(req.params['file'])){ |
|
112 | 152 |
path = path + req.params['file']; |
113 | 153 |
if(path == ""){ |
114 | 154 |
console.log("Error: path not specified") |
@@ -134,6 +174,10 @@ var write_file = function(req, res){ |
||
134 | 174 |
} |
135 | 175 |
} |
136 | 176 |
|
177 |
+var error_404 = function(req, res){ |
|
178 |
+ res.render('error-404', extra_data()); |
|
179 |
+} |
|
180 |
+ |
|
137 | 181 |
var markdown_parser = function(data){ |
138 | 182 |
var md = require('markdown-it')({ |
139 | 183 |
html: true, |
@@ -164,6 +208,7 @@ var extra_data = function(){ |
||
164 | 208 |
} |
165 | 209 |
|
166 | 210 |
var url_paths = [ |
211 |
+ '/', |
|
167 | 212 |
'/:file', |
168 | 213 |
'/:folder_1/:file', |
169 | 214 |
'/:folder_2/:folder_1/:file', |