Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
修复了中文markdown文件名页面404的问题
  • Loading branch information
zhoujing committed Aug 21, 2015
1 parent 721171c commit 16879d3
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 97 deletions.
146 changes: 73 additions & 73 deletions app/controllers/Gitblog.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,46 @@ function __construct() {
$this->load->driver('cache');

$this->configPath = str_replace("\\", "/", dirname(APPPATH)) . '/' . GB_CONF_FILE;
}
//导出网站
public function exportSite() {
//非命令行访问,返回404
if (!$this->input->is_cli_request()) return $this->go404();
$pageNo = 0;
$this->export = true;
//初始化
$this->init();
echo "\nexport index page\n";
//首页所有页面
}

//导出网站
public function exportSite() {

//非命令行访问,返回404
if (!$this->input->is_cli_request()) return $this->go404();

$pageNo = 0;
$this->export = true;

//初始化
$this->init();

echo "\nexport index page\n";

//首页所有页面
$pageSize = $this->confObj['blog']['pageSize'];
$pages = $this->markdown->getTotalPages($pageSize);
for ($pageNo = 1; $pageNo <= $pages; $pageNo++) {
$fileContent = $this->page($pageNo);
$filePath = GB_SITE_DIR . "/page/";
if (!file_exists($filePath)) mkdir($filePath, 0755, true);
write_file($filePath . $pageNo . ".html", $fileContent);
if ($pageNo == 1) {
if (!file_exists($filePath)) mkdir($filePath, 0755, true);
write_file(GB_SITE_DIR . "/index.html", $fileContent);
}
}
echo "export index page success\n";
echo "\nexport category page\n";
//分类下所有页面
$categoryList = $this->markdown->getAllCategorys();
foreach ($categoryList as $idx => $category) {
$categoryId = $category['id'];
$pages = $this->markdown->getCategoryTotalPages($categoryId, $pageSize);
for ($pageNo = 1; $pageNo <= $pages; $pageNo++) {
$pages = $this->markdown->getTotalPages($pageSize);
for ($pageNo = 1; $pageNo <= $pages; $pageNo++) {
$fileContent = $this->page($pageNo);
$filePath = GB_SITE_DIR . "/page/";
if (!file_exists($filePath)) mkdir($filePath, 0755, true);
write_file($filePath . $pageNo . ".html", $fileContent);

if ($pageNo == 1) {
if (!file_exists($filePath)) mkdir($filePath, 0755, true);
write_file(GB_SITE_DIR . "/index.html", $fileContent);
}
}
echo "export index page success\n";

echo "\nexport category page\n";
//分类下所有页面
$categoryList = $this->markdown->getAllCategorys();
foreach ($categoryList as $idx => $category) {
$categoryId = $category['id'];
$pages = $this->markdown->getCategoryTotalPages($categoryId, $pageSize);

for ($pageNo = 1; $pageNo <= $pages; $pageNo++) {
$fileContent = $this->category($categoryId, $pageNo);
$filePath = GB_SITE_DIR . "/category/$categoryId/page/";
if (!file_exists($filePath)) mkdir($filePath, 0755, true);
Expand Down Expand Up @@ -98,11 +98,11 @@ public function exportSite() {
echo "\nexport archive page\n";
//归档下所有页面
$yearMonthList = $this->markdown->getAllYearMonths();
foreach ($yearMonthList as $idx => $yearMonth) {
$yearMonthId = $yearMonth['id'];
$pages = $this->markdown->getYearMonthTotalPages($yearMonthId, $pageSize);
for ($pageNo = 1; $pageNo <= $pages; $pageNo++) {
foreach ($yearMonthList as $idx => $yearMonth) {
$yearMonthId = $yearMonth['id'];
$pages = $this->markdown->getYearMonthTotalPages($yearMonthId, $pageSize);

for ($pageNo = 1; $pageNo <= $pages; $pageNo++) {
$fileContent = $this->archive($yearMonthId, $pageNo);
$filePath = GB_SITE_DIR . "/archive/$yearMonthId/page/";
if (!file_exists($filePath)) mkdir($filePath, 0755, true);
Expand Down Expand Up @@ -179,20 +179,20 @@ public function exportSite() {
copy("favicon.ico", GB_SITE_DIR . "/favicon.ico");

echo "\nexport site success!!!\n";
}
//首页
public function index() {
$this->page(1);
}
private function init() {
}

//首页
public function index() {
$this->page(1);
}

private function init() {
//加载必要的类库
$this->load->library('Yaml');
$this->load->library('Markdown');
$this->load->library('Pager');

//加载配置文件
//加载配置文件
$this->confObj = $this->yaml->getConfObject($this->configPath);

//侧边栏最近博客条数
Expand Down Expand Up @@ -236,9 +236,9 @@ private function init() {

//配置名件对象别名
$this->setData("site", $this->confObj);
}
//加载缓存文件
}

//加载缓存文件
private function loadOutCache() {
$fag = false;
$cacheKey = $this->getCacheKey();
Expand Down Expand Up @@ -444,27 +444,27 @@ public function go404() {

//设置渲染数据
private function setData($key, $dataObj) {
$this->data[$key] = $dataObj;
}
//渲染页面
private function render($tpl) {
$htmlPage = $this->twig->render($tpl, $this->data, TRUE);
if (!$this->export) {
//生产模式下才会缓存
$this->data[$key] = $dataObj;
}

//渲染页面
private function render($tpl) {
$htmlPage = $this->twig->render($tpl, $this->data, TRUE);

if (!$this->export) {
//生产模式下才会缓存
if (ENVIRONMENT == "production") {
$cacheKey = $this->getCacheKey();
$this->cache->file->save($cacheKey, $htmlPage, GB_PAGE_CACHE_TIME);
}

$this->output->set_output($htmlPage);
}
return $htmlPage;
}
//计算缓存Key
private function getCacheKey() {
return md5(uri_string()) . ".html"; //category/1460001917
}
}
return $htmlPage;
}

//计算缓存Key
private function getCacheKey() {
return md5(uri_string()) . ".html"; //category/1460001917
}
}
60 changes: 36 additions & 24 deletions app/libraries/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ private function readPostBaseInfo($serverPath) {
$noteBlockArr = array();
$noteTmpArr = array();
$pattern1 = '/<\!\-\-(.*?)\-\->/is';
$pattern2 = '/^\s*(author|head|date|title|summary|images|tags|category|status)\s*:(.*?)$/im';
$pattern2 = '/^\s*(author|head|date|title|summary|images|tags|category|status)\s*:(.*?)$/im';

$subject = file_get_contents($serverPath);
$subject = file_get_contents($serverPath);

$blogProp = array(
$blogProp = array(
"author" => "",
"head" => "",
"date" => "",
Expand All @@ -348,21 +348,21 @@ private function readPostBaseInfo($serverPath) {
"content" => (string)$this->parseMarkdown($subject)
);

preg_match($pattern1, $subject, $matches);
preg_match($pattern1, $subject, $matches);

if (isset($matches[1])) {
$procontent = trim($matches[1]);
$proarr = explode("\n", $procontent);
if (isset($matches[1])) {
$procontent = trim($matches[1]);
$proarr = explode("\n", $procontent);

foreach($proarr as $proline) {
$proline = trim($proline);
if ($proline) {
preg_match($pattern2, $proline, $matches);
if(isset($matches[2])) {
$propName = trim($matches[1]);
$propVal = trim($matches[2]);
//echo $proName . " --> " . $proVal . "\n";
switch($propName) {
foreach($proarr as $proline) {
$proline = trim($proline);
if ($proline) {
preg_match($pattern2, $proline, $matches);
if(isset($matches[2])) {
$propName = trim($matches[1]);
$propVal = trim($matches[2]);
//echo $proName . " --> " . $proVal . "\n";
switch($propName) {
case "author":
$blogProp['author'] = $propVal;
break;
Expand Down Expand Up @@ -393,10 +393,10 @@ private function readPostBaseInfo($serverPath) {
$blogProp['status'] = $propVal == "draft" ? $propVal : "publish";
break;
}
}
}
}
}
}
}
}
}

$keywrodsArr = array_merge($tagsArr, $cateArr);

Expand Down Expand Up @@ -475,7 +475,7 @@ private function cleanKeywords2Arr($keywordsStr) {
private function readAllPostInfo($mdfiles) {
foreach ($mdfiles as $idx => $fileProp) {

$fileName = $fileProp['name'];
$fileName = $fileProp['name'];

//非markdown文件,不处理,直接过滤
if (!$this->checkFileExt($fileName)) continue;
Expand All @@ -488,6 +488,7 @@ private function readAllPostInfo($mdfiles) {
$sitePath = $this->changeFileExt($relativePath);
$siteURL = "/blog/" . $this->changeFileExt($relativePath);

$siteURL = $this->urlencodeFileName($siteURL);
$blogId = md5($siteURL);

$blog = array(
Expand All @@ -499,7 +500,7 @@ private function readAllPostInfo($mdfiles) {
"ctime" => $ctime,
"siteURL" => $siteURL
);
//读取自定义博客属性信息
$blogProp = $this->readPostBaseInfo($serverPath);

Expand Down Expand Up @@ -612,12 +613,23 @@ private function checkFileExt($fileName) {
//修改后缀名
public function changeFileExt($fileName, $ext="html") {
$pics = explode('.' , $fileName);
if (count($pics) > 1) {
$pics[count($pics) -1] = $ext;
$len = count($pics);
if ($len > 1) {
$pics[$len - 1] = $ext;
}

return implode(".", $pics);
}

//对URL中的中文编码
private function urlencodeFileName($fileName) {
$pics = explode('/' , $fileName);
$len = count($pics);
if ($len > 0) {
$pics[$len - 1] = urlencode($pics[$len - 1]);
}
return implode("/", $pics);
}

//将tags, category字符串转成数组
private function converStrArr($tags, $type) {
Expand Down

0 comments on commit 16879d3

Please sign in to comment.