date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
}
// Return blog post
function get_posts($posts, $page = 1, $perpage = 0){
if(empty($posts)) {
$posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
$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 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('', $content);
$post->title = str_replace('
','',$arr[0]);
$post->body = $arr[1];
$tmp[] = $post;
}
usort($tmp,'cmp');
// Extract a specific page with results
$tmp = array_slice($tmp, ($page-1) * $perpage, $perpage);
return $tmp;
}
else {
// Extract a specific page with results
$tmp = array_slice($posts, ($page-1) * $perpage, $perpage);
return $tmp;
}
}
// Find post by year, month and name, previous, and next.
function find_post($year, $month, $name){
$posts = get_posts(null, null, null);
$tmp = $posts;
foreach ($tmp as $index => $v) {
$url = $v->url;
if (strpos($url, $year . '/' . $month . '/' . $name) !== 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){
$posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
if( strpos($v, "$tag") !== false){
$post = new stdClass;
// Extract the date
$arr = explode('_', $v);
// Make sure the tag request available
if ($tag === $arr[1]) {
// 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];
// 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;
}
else {
not_found();
}
}
}
usort($tmp,'cmp');
return $tmp;
}
// Return an archive page
function get_archive($req){
$posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
if( strpos($v, "$req") !== 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];
// 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;
}
}
usort($tmp,'cmp');
return $tmp;
}
// Return an archive list, categorized by year and month
function archive_list() {
$posts = get_post_names();
$by_year = array();
$col = array();
foreach($posts as $index => $v){
$arr = explode('_', $v);
// Replaced string
$str = $arr[0];
$replaced = substr($str, 0,strrpos($str, '/')) . '/';
$date = str_replace($replaced,'',$arr[0]);
$data = explode('-', $date);
$col[] = $data;
}
foreach ($col as $row){
$y = $row['0'];
$m = $row['1'];
$by_year[$y][] = $m;
}
# Most recent year first
krsort($by_year);
# Iterate for display
echo '
';
}
// Return static page
function get_spage($posts, $spage){
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
if( strpos($v, "$spage") !== false && strpos($v, $spage.'.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;
}
// Find static page
function find_spage($spage){
$posts = get_spage_names();
foreach($posts as $index => $v){
if( strpos($v, "$spage") !== false && strpos($v, $spage.'.md') !== false){
// Use the get_spage method to return
// a properly parsed object
$arr = get_spage($posts, $spage);
return $arr[0];
}
}
return false;
}
// Return profile page
function get_profile($profile){
$posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
if( strpos($v, "$profile") !== 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];
// Make sure the tag request available
if ($profile === $author) {
// 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];
// 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;
}
}
}
usort($tmp,'cmp');
return $tmp;
}
// Return author bio
function get_bio($names, $author){
$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('
', $content);
$post->title = str_replace('
','',$arr[0]);
$post->body = $arr[1];
$tmp[] = $post;
}
}
return $tmp;
}
// Find author bio
function find_bio($author){
$names = get_author_names();
foreach($names as $index => $v){
if( strpos($v, $author) !== false && strpos($v, 'author.md') !== false){
// Use the get_spage method to return
// a properly parsed object
$arr = get_bio($names, $author);
if (isset($arr[0])) {
return $arr[0];
}
}
}
return false;
}
// Return search page
function get_keyword($keyword){
$posts = get_post_names();
$tmp = array();
// Create a new instance of the markdown parser
$md = new MarkdownParser();
foreach($posts as $index => $v){
$content = $md->transformMarkdown(file_get_contents($v));
if(strpos(strtolower(strip_tags($content)), strtolower($keyword)) !== 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('
';
}
// Search form
function search() {
echo <<
EOF;
if(isset($_GET['search'])) {
$url = site_url() . 'search/' . $_GET['search'];
header ("Location: $url");
}
}
// The not found error
function not_found(){
error(404, render('404', null, false));
}
// Turn an array of posts into an RSS feed
function generate_rss($posts){
$feed = new Feed();
$channel = new Channel();
$channel
->title(config('blog.title'))
->description(config('blog.description'))
->url(site_url())
->appendTo($feed);
foreach($posts as $p){
$item = new Item();
$item
->title($p->title)
->description($p->body)
->url($p->url)
->appendTo($channel);
}
echo $feed;
}
// Turn an array of posts into a JSON
function generate_json($posts){
return json_encode($posts);
}