date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
}
// Return blog posts.
function get_posts($posts, $page = 1, $perpage = 0){
if(empty($posts)) {
$posts = get_post_sorted();
}
$tmp = array();
// Extract a specific page with results
$posts = array_slice($posts, ($page-1) * $perpage, $perpage);
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
$post = new stdClass;
$filepath = $v['dirname'] . '/' . $v['basename'];
// Extract the date
$arr = explode('_', $filepath);
// 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($filepath));
// Extract the title and body
$arr = explode('', $content);
$post->title = str_replace('
','',$arr[0]);
$post->body = $arr[1];
$tmp[] = $post;
}
return $tmp;
}
// Find post by year, month and name, previous, and next.
function find_post($year, $month, $name){
$posts = get_post_sorted();
foreach ($posts as $index => $v) {
$url = $v['basename'];
if( strpos($url, "$year-$month") !== false && strpos($url, $name.'.md') !== false){
// Use the get_posts method to return
// a properly parsed object
$ar = get_posts($posts, $index+1,1);
$nx = get_posts($posts, $index,1);
$pr = get_posts($posts, $index+2,1);
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]
);
}
}
}
}
// Return tag page.
function get_tag($tag, $page, $perpage){
$posts = get_post_sorted();
$tmp = array();
foreach ($posts as $index => $v) {
$url = $v['filename'];
if( strpos($url, "$tag") !== false){
$tmp[] = $v;
}
}
return $tmp = get_posts($tmp, $page, $perpage);
}
// Return archive page.
function get_archive($req, $page, $perpage){
$posts = get_post_sorted();
$tmp = array();
foreach ($posts as $index => $v) {
$url = $v['filename'];
if( strpos($url, "$req") !== false){
$tmp[] = $v;
}
}
return $tmp = get_posts($tmp, $page, $perpage);
}
// Return posts list on profile.
function get_profile($profile, $page, $perpage){
$posts = get_post_sorted();
$tmp = array();
foreach ($posts as $index => $v) {
$url = $v['dirname'];
$str = explode('/', $url);
$author = $str[count($str)-2];
if($profile === $author){
$tmp[] = $v;
}
}
if(empty($tmp)) {
not_found();
}
return $tmp = get_posts($tmp, $page, $perpage);
}
// Return author bio.
function get_bio($author){
$names = get_author_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($names as $index => $v){
$post = new stdClass;
// Replaced string
$replaced = substr($v, 0,strrpos($v, '/')) . '/';
// Author string
$str = explode('/', $replaced);
$profile = $str[count($str)-2];
if($author === $profile){
// Profile URL
$url = str_replace($replaced,'',$v);
$post->url = site_url() . 'author/' . $profile;
// Get the contents and convert it to HTML
$content = $md->transformMarkdown(file_get_contents($v));
// Extract the title and body
$arr = explode('
';
return $tmp[] = $profile;
}
// Return static page.
function get_static_post($static){
$posts = get_static_pages();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
if(strpos($v, $static.'.md') !== false){
$post = new stdClass;
// Replaced string
$replaced = substr($v, 0, strrpos($v, '/')) . '/';
// The static page URL
$url = str_replace($replaced,'',$v);
$post->url = site_url() . str_replace('.md','',$url);
// Get the contents and convert it to HTML
$content = $md->transformMarkdown(file_get_contents($v));
// Extract the title and body
$arr = explode('', $content);
$post->title = str_replace('
','',$arr[0]);
$post->body = $arr[1];
$tmp[] = $post;
}
}
return $tmp;
}
// Return search page.
function get_keyword($keyword){
$posts = get_post_unsorted();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
$words = explode(' ', $keyword);
foreach($posts as $index => $v){
$content = $md->transformMarkdown(file_get_contents($v));
foreach ($words as $word) {
if(strpos(strtolower(strip_tags($content)), strtolower($word)) !== false){
$post = new stdClass;
// Extract the date
$arr = explode('_', $v);
// 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 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];
// Extract the title and body
$arr = explode('