@@ -76,8 +76,12 @@ public function setConfig($config) {
76
76
public function run () {
77
77
$ route = static ::getSafeFile (urldecode ($ _SERVER ['REQUEST_URI ' ]));
78
78
$ file = static ::MARKDOWN_ROOT . '/ ' . $ route ;
79
+ // 先创建markdown作为文档仓库,可以保存至git
80
+ if (!file_exists (static ::MARKDOWN_ROOT )) {
81
+ $ this ->actionInit ();
82
+ }
79
83
// html渲染
80
- if ($ file && static ::isHtmlFile ($ file )) {
84
+ elseif ($ file && static ::isHtmlFile ($ file )) {
81
85
$ this ->actionReadHtml ($ file );
82
86
}
83
87
// 编辑md文件(md文件不存在则先创建) /docx/usage/start.md
@@ -127,6 +131,7 @@ public function render($view, $params) {
127
131
include ($ tpl );
128
132
ob_flush ();
129
133
}
134
+
130
135
/**
131
136
* 输出json
132
137
*
@@ -144,6 +149,7 @@ public function renderJson($data, $code = 0, $msg = '') {
144
149
echo json_encode ($ ret , 0 );
145
150
die;
146
151
}
152
+
147
153
/**
148
154
* 文档路径过滤
149
155
*
@@ -160,6 +166,7 @@ public static function getSafeFile($file) {
160
166
}
161
167
return $ file ;
162
168
}
169
+
163
170
/**
164
171
* markdown文件路径转化为html文件路径
165
172
*
@@ -171,10 +178,12 @@ public static function md2HtmlFile($file) {
171
178
if (!static ::isMarkDownFile ($ file )) return false ;
172
179
return substr ($ file , 0 , strlen ($ file ) - strlen (static ::TYPE_MD )) . static ::TYPE_HTML ;
173
180
}
181
+
174
182
public static function html2MdFile ($ file ) {
175
183
if (!static ::isHtmlFile ($ file )) return false ;
176
184
return substr ($ file , 0 , strlen ($ file ) - strlen (static ::TYPE_HTML )) . static ::TYPE_MD ;
177
185
}
186
+
178
187
/**
179
188
* 判断是否为html文件
180
189
*
@@ -195,8 +204,16 @@ public static function isHtmlFile($file) {
195
204
public static function isMarkDownFile ($ file ) {
196
205
return substr ($ file , 0 - strlen (static ::TYPE_MD )) === static ::TYPE_MD ;
197
206
}
207
+
208
+ public static function getProject ($ route ) {
209
+ $ pattern = sprintf ('#%s/.*?/# ' , static ::MARKDOWN_ROOT );
210
+ preg_match ($ pattern , static ::getSafeFile ($ route ), $ match );
211
+ return $ match ? current ($ match ) : static ::MARKDOWN_ROOT ;
212
+ }
213
+
198
214
public function exceptionHandle () {
199
215
}
216
+
200
217
/**
201
218
* html渲染
202
219
* @param $file
@@ -206,7 +223,7 @@ public function actionReadHtml($file) {
206
223
$ mdFile = static ::html2MdFile ($ file );
207
224
if ($ mdFile && file_exists ($ mdFile ) && is_file ($ mdFile )) {
208
225
// 目录索引
209
- $ index = DirectoryIndex::listDirectory (static ::MARKDOWN_ROOT . ' /docx ' , DirectoryIndex::MODE_READ );
226
+ $ index = DirectoryIndex::listDirectory (static ::getProject ( $ file ) , DirectoryIndex::MODE_READ );
210
227
// 标题
211
228
$ title = DirectoryIndex::trimFileExtension (basename ($ file ));
212
229
// 文档预览
@@ -240,6 +257,8 @@ public function actionEditMarkdown($file, $route) {
240
257
$ content = $ _POST ['content ' ];
241
258
$ ret = file_put_contents ($ file , $ content );
242
259
$ htmlFile = static ::md2HtmlFile ($ route );
260
+ $ markdown = sprintf ("%s/%s " , rtrim (dirname (__FILE__ ), '/ ' ), static ::MARKDOWN_ROOT );
261
+ Command::gitPush ($ markdown );
243
262
$ this ->redirect ('/ ' . $ htmlFile );
244
263
}
245
264
$ time = time ();
@@ -258,7 +277,7 @@ public function actionEditMarkdown($file, $route) {
258
277
* @throws Exception
259
278
*/
260
279
public function actionListDir ($ route ) {
261
- $ title = DirectoryIndex::trimFileExtension (basename ($ file ));
280
+ $ title = DirectoryIndex::trimFileExtension (basename ($ route ));
262
281
// 当前目录索引
263
282
$ currentIndex = DirectoryIndex::listDirectory ($ route , DirectoryIndex::MODE_READ );
264
283
// 如果是ajax请求,则以json返回
@@ -267,7 +286,7 @@ public function actionListDir($route) {
267
286
return ;
268
287
}
269
288
// 顶层目录索引
270
- $ TopIndex = DirectoryIndex::listDirectory (static ::MARKDOWN_ROOT . ' /docx ' , DirectoryIndex::MODE_READ );
289
+ $ TopIndex = DirectoryIndex::listDirectory (static ::getProject ( $ route ) , DirectoryIndex::MODE_READ );
271
290
$ this ->render (static ::VIEW_DETAIL , [
272
291
'index ' => $ TopIndex ,
273
292
'currentIndex ' => $ currentIndex ,
@@ -301,4 +320,9 @@ public function actionUploadAttached() {
301
320
}
302
321
}
303
322
}
323
+
324
+ public function actionInit () {
325
+ $ git = new Command ();
326
+ $ ret = $ git ->initGit ($ this ->_config ['git ' ], dirname (__FILE__ ), static ::MARKDOWN_ROOT );
327
+ }
304
328
}
0 commit comments