diff --git a/system/htmly.php b/system/htmly.php
index 738a365..6c6f65f 100644
--- a/system/htmly.php
+++ b/system/htmly.php
@@ -52,12 +52,9 @@ get('/tag/:tag',function($tag){
$page = $page ? (int)$page : 1;
$perpage = config('tag.perpage');
- $posts = get_tag($tag);
+ $posts = get_tag($tag, $page, $perpage);
- $total = count($posts);
-
- // Extract a specific page with results
- $posts = array_slice($posts, ($page-1) * $perpage, $perpage);
+ $total = get_count($tag, 'filename');
if(empty($posts) || $page < 1){
// a non-existing page
@@ -83,12 +80,9 @@ get('/archive/:req',function($req){
$page = $page ? (int)$page : 1;
$perpage = config('archive.perpage');
- $posts = get_archive($req);
-
- $total = count($posts);
+ $posts = get_archive($req, $page, $perpage);
- // Extract a specific page with results
- $posts = array_slice($posts, ($page-1) * $perpage, $perpage);
+ $total = get_count($req, 'filename');
if(empty($posts) || $page < 1){
// a non-existing page
@@ -136,7 +130,14 @@ get('/:year/:month/:name', function($year, $month, $name){
not_found();
}
- $bio = find_bio($current->author);
+ $bio = get_bio($current->author);
+
+ if(isset($bio[0])) {
+ $bio = $bio[0];
+ }
+ else {
+ $bio = default_profile($current->author);
+ }
if (array_key_exists('prev', $post)) {
$prev = $post['prev'];
@@ -200,13 +201,15 @@ get('/search/:keyword', function($keyword){
});
// The static page
-get('/:spage', function($spage){
+get('/:static', function($static){
- $post = find_spage($spage);
+ $post = get_static_post($static);
if(!$post){
not_found();
}
+
+ $post = $post[0];
render('post',array(
'title' => $post->title .' - ' . config('blog.title'),
@@ -220,20 +223,25 @@ get('/:spage', function($spage){
});
// The author page
-get('/author/:profile',function($profile){
+get('/author/:profile', function($profile){
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('profile.perpage');
- $posts = get_profile($profile);
- $bio = find_bio($profile);
+ $posts = get_profile($profile, $page, $perpage);
- $total = count($posts);
+ $total = get_count($profile, 'dirname');
+
+ $bio = get_bio($profile);
+
+ if(isset($bio[0])) {
+ $bio = $bio[0];
+ }
+ else {
+ $bio = default_profile($profile);
+ }
- // Extract a specific page with results
- $posts = array_slice($posts, ($page-1) * $perpage, $perpage);
-
if(empty($posts) || $page < 1){
// a non-existing page
not_found();
diff --git a/system/includes/functions.php b/system/includes/functions.php
index b4a38b1..a63c43e 100644
--- a/system/includes/functions.php
+++ b/system/includes/functions.php
@@ -8,8 +8,8 @@ use \Suin\RSSWriter\Feed;
use \Suin\RSSWriter\Channel;
use \Suin\RSSWriter\Item;
-// Get blog post
-function get_post_names(){
+// Get blog post path. Unsorted. Mostly used on widget.
+function get_post_unsorted(){
static $_cache = array();
@@ -23,8 +23,32 @@ function get_post_names(){
return $_cache;
}
-// Get static page
-function get_spage_names(){
+// Get blog post with more info about the path. Sorted by filename.
+function get_post_sorted(){
+
+ static $tmp= array();
+
+ static $_cache = array();
+
+ if(empty($_cache)){
+
+ // Get the names of all the posts
+
+ $tmp = glob('content/*/blog/*.md', GLOB_NOSORT);
+
+ foreach($tmp as $file) {
+ $_cache[] = pathinfo($file);
+ }
+
+ }
+
+ usort($_cache, "sortfile");
+
+ return $_cache;
+}
+
+// Get static page path. Unsorted.
+function get_static_pages(){
static $_cache = array();
@@ -39,7 +63,7 @@ function get_spage_names(){
return $_cache;
}
-// Get author bio
+// Get author bio path. Unsorted.
function get_author_names(){
static $_cache = array();
@@ -55,95 +79,88 @@ function get_author_names(){
return $_cache;
}
+// usort function. Sort by filename.
+function sortfile($a, $b) {
+ return $a['filename'] == $b['filename'] ? 0 : ( $a['filename'] < $b['filename'] ) ? 1 : -1;
+}
+
// usort function. Sort by date.
-function cmp($a, $b) {
+function sortdate($a, $b) {
return $a->date == $b->date ? 0 : ( $a->date < $b->date ) ? 1 : -1;
}
-// Return blog post
+// 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);
- $posts = get_post_names();
-
- $tmp = array();
+ // Create a new instance of the markdown parser
+ $md = new MarkdownParser();
+
+ foreach($posts as $index => $v){
- // Create a new instance of the markdown parser
- $md = new MarkdownParser();
+ $post = new stdClass;
- 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));
+ $filepath = $v['dirname'] . '/' . $v['basename'];
- // Extract the title and body
- $arr = explode('', $content);
- $post->title = str_replace('
','',$arr[0]);
- $post->body = $arr[1];
-
- $tmp[] = $post;
- }
+ // 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;
- usort($tmp,'cmp');
+ // 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]);
- // Extract a specific page with results
- $tmp = array_slice($tmp, ($page-1) * $perpage, $perpage);
+ // The post tag
+ $post->tag = str_replace($replaced,'',$arr[1]);
- return $tmp;
-
- }
- else {
-
- // Extract a specific page with results
- $tmp = array_slice($posts, ($page-1) * $perpage, $perpage);
-
- return $tmp;
-
+ // 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_posts(null, null, null);
- $tmp = $posts;
-
- foreach ($tmp as $index => $v) {
- $url = $v->url;
- if (strpos($url, $year . '/' . $month . '/' . $name) !== false) {
+ $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
@@ -184,225 +201,90 @@ function find_post($year, $month, $name){
}
}
-// 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]));
+// Return tag page.
+function get_tag($tag, $page, $perpage){
- // 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();
+ $posts = get_post_sorted();
+
$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;
+
+ foreach ($posts as $index => $v) {
+ $url = $v['filename'];
+ if( strpos($url, "$tag") !== false){
+ $tmp[] = $v;
}
}
-
- usort($tmp,'cmp');
- return $tmp;
+
+ return $tmp = get_posts($tmp, $page, $perpage);
+
}
-// Return an archive list, categorized by year and month
-function archive_list() {
+// Return archive page.
+function get_archive($req, $page, $perpage){
- $posts = get_post_names();
- $by_year = array();
- $col = array();
+ $posts = get_post_sorted();
- foreach($posts as $index => $v){
+ $tmp = array();
- $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 ($posts as $index => $v) {
+ $url = $v['filename'];
+ if( strpos($url, "$req") !== false){
+ $tmp[] = $v;
+ }
}
- foreach ($col as $row){
+ return $tmp = get_posts($tmp, $page, $perpage);
- $y = $row['0'];
- $m = $row['1'];
- $by_year[$y][] = $m;
-
- }
-
- # Most recent year first
- krsort($by_year);
- # Iterate for display
- echo 'Archive
';
- foreach ($by_year as $year => $months){
-
- echo '' . $year . ' ';
- echo '(' . count($months) . ')';
- echo '';
-
- # Sort the months
- ksort($months);
- $by_month = array_count_values($months);
- foreach ($by_month as $month => $count){
- $name = date('F', mktime(0,0,0,$month,1,2010));
- echo '- ' . $name . '';
- echo ' (' . $count . ')
';
- }
-
- echo '
';
-
- }
-
}
-// Return tag cloud
-function tag_cloud() {
+// Return posts list on profile.
+function get_profile($profile, $page, $perpage){
- $posts = get_post_names();
- $tags = array();
+ $posts = get_post_sorted();
- foreach($posts as $index => $v){
+ $tmp = array();
- $arr = explode('_', $v);
-
- $data = $arr[1];
- $tags[] = $data;
-
+ foreach ($posts as $index => $v) {
+ $url = $v['dirname'];
+ $str = explode('/', $url);
+ $author = $str[count($str)-2];
+ if($profile === $author){
+ $tmp[] = $v;
+ }
}
- $tag_collection = array_count_values($tags);
- ksort($tag_collection);
-
- echo 'Tags
';
- echo '';
- foreach ($tag_collection as $tag => $count){
- echo '- ' . $tag . ' (' . $count . ')
';
+ if(empty($tmp)) {
+ not_found();
}
- echo '
';
+
+ return $tmp = get_posts($tmp, $page, $perpage);
}
-// Return static page
-function get_spage($posts, $spage){
+// 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($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
+ 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() . str_replace('.md','',$url);
+ $post->url = site_url() . 'author/' . $profile;
// Get the contents and convert it to HTML
$content = $md->transformMarkdown(file_get_contents($v));
@@ -419,109 +301,39 @@ function get_spage($posts, $spage){
return $tmp;
}
-// Find static page
-function find_spage($spage){
-
- $posts = get_spage_names();
+function default_profile($author) {
- foreach($posts as $index => $v){
- if( strpos($v, "$spage") !== false && strpos($v, $spage.'.md') !== false){
+ $tmp = array();
+ $profile = new stdClass;
+
+ $profile->title = $author;
+ $profile->body = '
Just another HTMLy user.
';
+
+ return $tmp[] = $profile;
- // 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){
+// Return static page.
+function get_static_post($static){
+
+ $posts = get_static_pages();
- $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){
-
+ if(strpos($v, $static.'.md') !== 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];
+ $replaced = substr($v, 0, strrpos($v, '/')) . '/';
- // 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
+ // The static page URL
$url = str_replace($replaced,'',$v);
- $post->url = site_url() . 'author/' . $profile;
+ $post->url = site_url() . str_replace('.md','',$url);
// Get the contents and convert it to HTML
$content = $md->transformMarkdown(file_get_contents($v));
@@ -532,35 +344,17 @@ function get_bio($names, $author){
$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
+// Return search page.
function get_keyword($keyword){
- $posts = get_post_names();
+ $posts = get_post_unsorted();
$tmp = array();
// Create a new instance of the markdown parser
@@ -615,16 +409,16 @@ function get_keyword($keyword){
$tmp = array_unique($tmp, SORT_REGULAR);
- usort($tmp,'cmp');
+ usort($tmp,'sortdate');
return $tmp;
}
-// Get related posts
+// Get related posts base on post tag.
function get_related($tag) {
$perpage = config('related.count');
- $posts = get_tag($tag);
+ $posts = get_tag($tag, 1, $perpage+1);
$tmp = array();
$req = $_SERVER['REQUEST_URI'];
@@ -652,6 +446,104 @@ function get_related($tag) {
}
+// Return post count. Matching $var and $str provided.
+function get_count($var, $str) {
+
+ $posts = get_post_sorted();
+
+ $tmp = array();
+
+ foreach ($posts as $index => $v) {
+ $url = $v[$str];
+ if( strpos($url, "$var") !== false){
+ $tmp[] = $v;
+ }
+ }
+
+ return count($tmp);
+
+}
+
+// Return an archive list, categorized by year and month.
+function archive_list() {
+
+ $posts = get_post_unsorted();
+ $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 'Archive
';
+ foreach ($by_year as $year => $months){
+
+ echo '' . $year . ' ';
+ echo '(' . count($months) . ')';
+ echo '';
+
+ # Sort the months
+ ksort($months);
+ $by_month = array_count_values($months);
+ foreach ($by_month as $month => $count){
+ $name = date('F', mktime(0,0,0,$month,1,2010));
+ echo '- ' . $name . '';
+ echo ' (' . $count . ')
';
+ }
+
+ echo '
';
+
+ }
+
+}
+
+// Return tag cloud.
+function tag_cloud() {
+
+ $posts = get_post_unsorted();
+ $tags = array();
+
+ foreach($posts as $index => $v){
+
+ $arr = explode('_', $v);
+
+ $data = $arr[1];
+ $tags[] = $data;
+
+ }
+
+ $tag_collection = array_count_values($tags);
+ ksort($tag_collection);
+
+ echo 'Tags
';
+ echo '';
+ foreach ($tag_collection as $tag => $count){
+ echo '- ' . $tag . ' (' . $count . ')
';
+ }
+ echo '
';
+
+}
// Helper function to determine whether
// to show the previous buttons
@@ -679,7 +571,7 @@ function has_next($next){
// to show the pagination buttons
function has_pagination($total, $perpage, $page = 1){
if(!$total) {
- $total = count(get_post_names());
+ $total = count(get_post_unsorted());
}
return array(
'prev'=> $page > 1,
@@ -809,7 +701,7 @@ function copyright(){
}
-// Disqus
+// Disqus on post.
function disqus($title, $url){
$disqus = config('disqus.shortname');
$script = <<';