Browse Source

Fix critical bugs

Fix critical bugs.
pull/4/merge
Danang Probo Sayekti 12 years ago
parent
commit
e666497cf0
2 changed files with 66 additions and 52 deletions
  1. +10
    -4
      system/htmly.php
  2. +56
    -48
      system/includes/functions.php

+ 10
- 4
system/htmly.php View File

@ -21,6 +21,9 @@ get('/index', function () {
$posts = get_posts($page); $posts = get_posts($page);
// Extract a specific page with results
$posts = array_slice($posts, ($page-1) * $perpage, $perpage);
$total = ''; $total = '';
if(empty($posts) || $page < 1){ if(empty($posts) || $page < 1){
@ -122,13 +125,16 @@ get('/archive/:req',function($req){
// The blog post page // The blog post page
get('/:year/:month/:name', function($year, $month, $name){ get('/:year/:month/:name', function($year, $month, $name){
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = 1;
$post = find_post($year, $month, $name); $post = find_post($year, $month, $name);
$current = $post['current'];
// Extract a specific page with results
$post = array_slice($post, 0, $perpage);
if(!$current){
not_found();
}
$current = $post[0];
if (array_key_exists('prev', $post)) { if (array_key_exists('prev', $post)) {
$prev = $post['prev']; $prev = $post['prev'];


+ 56
- 48
system/includes/functions.php View File

@ -56,17 +56,14 @@ function get_author_names(){
return $_cache; return $_cache;
} }
function cmp($a, $b) {
return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
}
// Return blog post // Return blog post
function get_posts($page = 1, $perpage = 0){ function get_posts($page = 1, $perpage = 0){
if($perpage == 0){
$perpage = config('posts.perpage');
}
$posts = get_post_names(); $posts = get_post_names();
// Extract a specific page with results
$posts = array_slice($posts, ($page-1) * $perpage, $perpage);
$tmp = array(); $tmp = array();
@ -116,8 +113,8 @@ function get_posts($page = 1, $perpage = 0){
$tmp[] = $post; $tmp[] = $post;
} }
krsort($tmp);
usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -125,47 +122,59 @@ function get_posts($page = 1, $perpage = 0){
function find_post($year, $month, $name){ function find_post($year, $month, $name){
$posts = get_post_names(); $posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){ foreach($posts as $index => $v){
if( strpos($v, "$year-$month") !== false && strpos($v, $name.'.md') !== false){ if( strpos($v, "$year-$month") !== false && strpos($v, $name.'.md') !== false){
// Use the get_posts method to return
// a properly parsed object
$post = new stdClass;
$ar = get_posts($index+1,1);
$nx = get_posts($index,1);
$pr = get_posts($index+2,1);
// Extract the date
$arr = explode('_', $v);
if ($index == 0) {
if(isset($pr[0])) {
return array(
'current'=> $ar[0],
'prev'=> $pr[0]
);
}
else {
return array(
'current'=> $ar[0],
'prev'=> null
);
}
}
elseif (count($posts) == $index+1) {
return array(
'current'=> $ar[0],
'next'=> $nx[0]
);
}
else {
return array(
'current'=> $ar[0],
'next'=> $nx[0],
'prev'=> $pr[0]
);
}
// Replaced string
$replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/';
// Author string
$str = explode('/', $replaced);
$author = $str[count($str)-3];
// The post author + author url
$post->author = $author;
$post->authorurl = site_url() . 'author/' . $author;
// The post date
$post->date = strtotime(str_replace($replaced,'',$arr[0]));
// The archive per day
$post->archive = site_url(). 'archive/' . date('Y-m-d', $post->date) ;
// The post URL
$post->url = site_url().date('Y/m', $post->date).'/'.str_replace('.md','',$arr[2]);
// The post tag
$post->tag = str_replace($replaced,'',$arr[1]);
// The post tag url
$post->tagurl = site_url(). 'tag/' . $arr[1];
// Get the contents and convert it to HTML
$content = $md->transformMarkdown(file_get_contents($v));
// Extract the title and body
$arr = explode('</h1>', $content);
$post->title = str_replace('<h1>','',$arr[0]);
$post->body = $arr[1];
$tmp[] = $post;
} }
} }
return false;
usort($tmp,'cmp');
return $tmp;
} }
// Return tag page // Return tag page
@ -227,7 +236,7 @@ function get_tag($tag){
} }
} }
krsort($tmp);
usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -283,7 +292,7 @@ function get_archive($req){
} }
} }
krsort($tmp);
usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -403,8 +412,7 @@ function get_spage($posts, $spage){
$tmp[] = $post; $tmp[] = $post;
} }
} }
krsort($tmp);
return $tmp; return $tmp;
} }
@ -484,7 +492,7 @@ function get_profile($profile){
} }
} }
krsort($tmp);
usort($tmp,'cmp');
return $tmp; return $tmp;
} }
@ -602,7 +610,7 @@ function get_keyword($keyword){
} }
} }
krsort($tmp);
usort($tmp,'cmp');
return $tmp; return $tmp;
} }


Loading…
Cancel
Save