Skip to content

Commit 9fb0db4

Browse files
author
wushuiyong
committed
正式引入markdown的git存储备份
1 parent c9c194c commit 9fb0db4

File tree

6 files changed

+53
-137
lines changed

6 files changed

+53
-137
lines changed

Bootstrap.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public function setConfig($config) {
7676
public function run() {
7777
$route = static::getSafeFile(urldecode($_SERVER['REQUEST_URI']));
7878
$file = static::MARKDOWN_ROOT . '/' . $route;
79+
// 先创建markdown作为文档仓库,可以保存至git
80+
if (!file_exists(static::MARKDOWN_ROOT)) {
81+
$this->actionInit();
82+
}
7983
// html渲染
80-
if ($file && static::isHtmlFile($file)) {
84+
elseif ($file && static::isHtmlFile($file)) {
8185
$this->actionReadHtml($file);
8286
}
8387
// 编辑md文件(md文件不存在则先创建) /docx/usage/start.md
@@ -127,6 +131,7 @@ public function render($view, $params) {
127131
include($tpl);
128132
ob_flush();
129133
}
134+
130135
/**
131136
* 输出json
132137
*
@@ -144,6 +149,7 @@ public function renderJson($data, $code = 0, $msg = '') {
144149
echo json_encode($ret, 0);
145150
die;
146151
}
152+
147153
/**
148154
* 文档路径过滤
149155
*
@@ -160,6 +166,7 @@ public static function getSafeFile($file) {
160166
}
161167
return $file;
162168
}
169+
163170
/**
164171
* markdown文件路径转化为html文件路径
165172
*
@@ -171,10 +178,12 @@ public static function md2HtmlFile($file) {
171178
if (!static::isMarkDownFile($file)) return false;
172179
return substr($file, 0, strlen($file) - strlen(static::TYPE_MD)) . static::TYPE_HTML;
173180
}
181+
174182
public static function html2MdFile($file) {
175183
if (!static::isHtmlFile($file)) return false;
176184
return substr($file, 0, strlen($file) - strlen(static::TYPE_HTML)) . static::TYPE_MD;
177185
}
186+
178187
/**
179188
* 判断是否为html文件
180189
*
@@ -195,8 +204,16 @@ public static function isHtmlFile($file) {
195204
public static function isMarkDownFile($file) {
196205
return substr($file, 0 - strlen(static::TYPE_MD)) === static::TYPE_MD;
197206
}
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+
198214
public function exceptionHandle() {
199215
}
216+
200217
/**
201218
* html渲染
202219
* @param $file
@@ -206,7 +223,7 @@ public function actionReadHtml($file) {
206223
$mdFile = static::html2MdFile($file);
207224
if ($mdFile && file_exists($mdFile) && is_file($mdFile)) {
208225
// 目录索引
209-
$index = DirectoryIndex::listDirectory(static::MARKDOWN_ROOT . '/docx', DirectoryIndex::MODE_READ);
226+
$index = DirectoryIndex::listDirectory(static::getProject($file), DirectoryIndex::MODE_READ);
210227
// 标题
211228
$title = DirectoryIndex::trimFileExtension(basename($file));
212229
// 文档预览
@@ -240,6 +257,8 @@ public function actionEditMarkdown($file, $route) {
240257
$content = $_POST['content'];
241258
$ret = file_put_contents($file, $content);
242259
$htmlFile = static::md2HtmlFile($route);
260+
$markdown = sprintf("%s/%s", rtrim(dirname(__FILE__), '/'), static::MARKDOWN_ROOT);
261+
Command::gitPush($markdown);
243262
$this->redirect('/' . $htmlFile);
244263
}
245264
$time = time();
@@ -258,7 +277,7 @@ public function actionEditMarkdown($file, $route) {
258277
* @throws Exception
259278
*/
260279
public function actionListDir($route) {
261-
$title = DirectoryIndex::trimFileExtension(basename($file));
280+
$title = DirectoryIndex::trimFileExtension(basename($route));
262281
// 当前目录索引
263282
$currentIndex = DirectoryIndex::listDirectory($route, DirectoryIndex::MODE_READ);
264283
// 如果是ajax请求,则以json返回
@@ -267,7 +286,7 @@ public function actionListDir($route) {
267286
return;
268287
}
269288
// 顶层目录索引
270-
$TopIndex = DirectoryIndex::listDirectory(static::MARKDOWN_ROOT . '/docx', DirectoryIndex::MODE_READ);
289+
$TopIndex = DirectoryIndex::listDirectory(static::getProject($route), DirectoryIndex::MODE_READ);
271290
$this->render(static::VIEW_DETAIL, [
272291
'index' => $TopIndex,
273292
'currentIndex' => $currentIndex,
@@ -301,4 +320,9 @@ public function actionUploadAttached() {
301320
}
302321
}
303322
}
323+
324+
public function actionInit() {
325+
$git = new Command();
326+
$ret = $git->initGit($this->_config['git'], dirname(__FILE__), static::MARKDOWN_ROOT);
327+
}
304328
}

Command.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,27 @@ public static function execute($command) {
2727
return !$return;
2828
}
2929

30-
public static function gitPush() {
30+
public static function gitPush($markdown) {
31+
// 存在git目录,直接push
32+
if (!file_exists($markdown)) return false;
33+
$cmd[] = sprintf('cd %s ', $markdown);
34+
$cmd[] = sprintf('/usr/bin/env git add .');
35+
$cmd[] = sprintf('/usr/bin/env git commit -m"%s"', date("Y-m-d H:i:s", time()));
36+
$cmd[] = sprintf('/usr/bin/env git push origin master');
37+
$command = join(' && ', $cmd);
38+
$log = '';
39+
return static::execute($command, $log);
40+
}
41+
42+
public function initGit($gitRepo, $webroot, $markdown) {
43+
$gitDir = sprintf("%s/%s", rtrim($webroot, '/'), $markdown);
44+
if (file_exists($gitDir) && file_exists(rtrim($gitDir, '/') . '/.git')) return true;
3145

46+
$cmd[] = sprintf('cd %s ', $webroot);
47+
$cmd[] = sprintf('/usr/bin/env git clone %s %s', $gitRepo, $markdown);
48+
$command = join(' && ', $cmd);
49+
$log = '';
50+
return static::execute($command, $log);
3251
}
3352

3453

config.php Config.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
* *****************************************************************/
99
return [
1010
'template' => 'default',
11-
'validationKey' => 'PdXWDAfV5-gPJJWRar5sEN71DN0JcDRV'
12-
'git' => ''
11+
'validationKey' => 'PdXWDAfV5-gPJJWRar5sEN71DN0JcDRV',
12+
'git' => '[email protected]:meolu/docx-markdown-demo.git',
1313
];

index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ function d($var) {
1616
var_dump($var);
1717
}
1818

19-
$config = include('config.php');
19+
$config = include('Config.php');
2020
$bootstrap = new Bootstrap();
2121
$bootstrap->setConfig($config)->run();

templates/default/markdown-detail-view.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</li>
3737
</ul>
3838
<ul class="nav navbar-nav navbar-right">
39-
<li><a href="<?= $editUrl?>">编辑</a></li>
39+
<?php if (isset($editUrl)) { ?><li><a href="<?= $editUrl?>">编辑</a></li><?php } ?>
4040
</ul>
4141
</div><!-- /.navbar-collapse -->
4242
</div>
@@ -64,7 +64,7 @@
6464
<?php foreach ($index as $item) { ?>
6565
<?php if ($item['type'] == DirectoryIndex::TYPE_FILE) { ?>
6666
<li class="level_1">
67-
<a href="<?= $link ?>"><?= $item['title'] ?><i class="icon-chevron-right"></i></a>
67+
<a href="<?= $item['link'] ?>"><?= $item['title'] ?><i class="icon-chevron-right"></i></a>
6868
</li>
6969
<?php } else { ?>
7070
<li class="level_1">

templates/default/markdown-list-view.php

-127
This file was deleted.

0 commit comments

Comments
 (0)