From: Gabriel Dunne Date: Mon, 3 Oct 2011 16:54:31 +0000 (+0530) Subject: cleaned up entry logic X-Git-Url: https://git.quilime.com/?a=commitdiff_plain;h=b9862058c6c480d2fd5c3635d3ffa978b411abac;p=plog.git cleaned up entry logic --- diff --git a/index.php b/index.php index 754dcb8..74c0a58 100644 --- a/index.php +++ b/index.php @@ -4,12 +4,12 @@ require 'lib/init.php'; $request = get_request(); -$v = new View ($request); +$v = new View ($request); $m = new Model ($request); $v->assign('is_single', $m->is_single()); -$v->assign('is_page', $m->is_page()); -$v->assign('entries', $m->entries); +$v->assign('is_page', $m->is_page()); +$v->assign('entries', $m->entries); header("Content-Type: {$m->response_mime_type}; charset=UTF-8"); diff --git a/lib/data.php b/lib/data.php index 648d546..9efbdd5 100644 --- a/lib/data.php +++ b/lib/data.php @@ -24,8 +24,8 @@ function get_entries( $path = "", $args = array()) } $entries = array(); foreach ($dir_iterator as $file => $info) { - if (!$info->isDir() && !in_array($info->getFilename(), $_FILE_IGNORES)) { - $entries[] = parse_entry($info); + if ( !$info->isDir() && !in_array( $info->getFilename(), $_FILE_IGNORES )) { + $entries[] = parse_entry( $info ); } } @@ -35,10 +35,10 @@ function get_entries( $path = "", $args = array()) foreach ($entries as $key => $row) $time[$key] = $row['timestamp']; if ($time) - array_multisort($time, $order, $entries); + array_multisort( $time, $order, $entries ); } - return array($entries, sizeof($entries)); + return $entries; } @@ -100,7 +100,7 @@ function get_pages() * @param splFileInfo SPLFileInfo Object * @param page default is false */ -function parse_entry($fileInfo, $page = 0) +function parse_entry($fileInfo, $page = false) { $config = ""; $content = ""; @@ -117,12 +117,10 @@ function parse_entry($fileInfo, $page = 0) $config .= $line; continue; } - if (trim($line) == MORE_DELIM) { + if (trim($line) == MORE_DELIM) $passed_more = true; - } - if (!$passed_more) { + if (!$passed_more) $content_short .= $line; - } $content .= $line; } @@ -133,22 +131,38 @@ function parse_entry($fileInfo, $page = 0) $f['timestamp'] = $f['config']['date'] ? date('U', strtotime( $f['config']['date'])) : $fileInfo->getCTime(); $f['tags'] = isset($f['config']['tags']) ? explode(" ", $f['config']['tags']) : null; $f['content'] = Markdown($content); + if ($passed_more) $f['content_short'] = Markdown($content_short); + $cat = clean_slashes(str_replace(LOCAL_ROOT . CONTENT_DIR, "", $fileInfo->getPath())); - $f['cat'] = $page ? null : array('name' => substr($cat,1), 'url' => $cat ); + $clean_path = str_replace(LOCAL_ROOT . CONTENT_DIR, "", clean_slashes($fileInfo->getPath())); + + + $f['cat'] = $page ? null : array('name' => $clean_path, 'url' => $clean_path ); $f['path'] = $fileInfo->getRealPath(); - $f['url'] = ($page ? '' : substr($f['cat']['url'],1)) . $fileInfo->getFilename(); + $f['url'] = ($page ? '' : $f['cat']['url']) . '/' . $fileInfo->getFilename(); if (!CLEAN_URLS) { + $f['cat']['url'] = WEB_ROOT . '?p=' . $f['cat']['url']; $f['url'] = WEB_ROOT . '?p=' . $f['url']; } - + return $f; } -function parse_config ($location) +function get_entry ( $relative_entry_path ) +{ + return parse_entry(new SplFileInfo(join(array(LOCAL_ROOT, CONTENT_DIR, $relative_entry_path), DIRECTORY_SEPARATOR))); +} + +function get_page ( $relative_page_path ) +{ + return parse_entry(new SplFileInfo(join(array(LOCAL_ROOT, PAGE_DIR, $relative_page_path), DIRECTORY_SEPARATOR))); +} + +function parse_config ( $relative_path ) { - return parse_entry(new SplFileInfo(join(array(LOCAL_ROOT, CONTENT_DIR, $location, CONFIG_FILE, DIRECTORY_SEPARATOR)))); + return parse_entry(new SplFileInfo(join(array(LOCAL_ROOT, CONTENT_DIR, $relative_path, CONFIG_FILE), DIRECTORY_SEPARATOR))); } \ No newline at end of file diff --git a/lib/model.php b/lib/model.php index 4a69874..e5e7e16 100644 --- a/lib/model.php +++ b/lib/model.php @@ -2,6 +2,7 @@ class Model { + var $content_request = null; var $page_request = null; @@ -12,7 +13,7 @@ class Model var $entries = null; var $config = null; - var $total = 0; + function __construct( $request ) { @@ -21,42 +22,46 @@ class Model $this->parse_request( $this->request ); } + function parse_request( $request ) { + $this->content_request = join(array($this->request['dirname'], $this->request['filename']), DIRECTORY_SEPARATOR ); $this->page_request = $this->request['filename']; - # check if multiple entries (dir) + + # if multiple entries (dir in CONTENT dir) if ($this->is_multiple()) { # check if config file exists if ($this->has_config()) { - $this->config = parse_config($this->content_request); + $this->config = parse_config( $this->content_request ); if (isset($this->config['config']['template'])) { $this->template = $this->config['config']['template'] . '.' . $this->response_format . '.tpl' ; } } - list($this->entries, $this->total) = get_entries($this->content_request); + $this->entries = get_entries( $this->content_request ); $this->page_title = preg_replace('{^/|/$}', '', $this->request['path']); } - # check if single + + + # if single entry (file in CONTENT dir) else if ($this->is_single()) { - $this->entries = parse_entry($this->content_request); - $this->template = 'single.'.$this->response_format.'.tpl'; + $this->entries = get_entry( $this->content_request ); + $this->template = 'single.' . $this->response_format . '.tpl'; } - # check if page + + + # if page (file in PAGES dir) else if ($this->is_page()) { - $this->entries = parse_entry($this->page_request, 1); + $this->entries = get_page( $this->page_request ); $this->template = isset($page['config']['template']) ? $this->entries['config']['template'] . '.' . $response_format . '.tpl' : 'page.' . $response_format . '.tpl'; } - // # check if index - // else if ($this->request['filename'] == 'index') - // { - // $this->template = 'index.html.tpl'; - // } - # poop 404 + + + # not found else { $this->template = '404.html.tpl'; } @@ -64,26 +69,21 @@ class Model function has_config() { - return is_file(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request, CONFIG_FILE ), DIRECTORY_SEPARATOR )); - } - - function get_config() - { - + return is_file(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request, CONFIG_FILE ), DIRECTORY_SEPARATOR )) ? 1 : 0; } function is_multiple() { - return is_dir(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request ), DIRECTORY_SEPARATOR )); + return is_dir(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request ), DIRECTORY_SEPARATOR )) ? 1 : 0; } function is_single() { - return is_file(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request ), DIRECTORY_SEPARATOR )); + return is_file(join(array( LOCAL_ROOT, CONTENT_DIR, $this->content_request ), DIRECTORY_SEPARATOR )) ? 1 : 0; } function is_page() { - return is_file(join(array( LOCAL_ROOT, PAGE_DIR, $this->page_request ), DIRECTORY_SEPARATOR )); + return is_file(join(array( LOCAL_ROOT, PAGE_DIR, $this->page_request ), DIRECTORY_SEPARATOR )) ? 1 : 0; } -} \ No newline at end of file +} diff --git a/lib/output.php b/lib/output.php index 7ec4a73..6afa477 100644 --- a/lib/output.php +++ b/lib/output.php @@ -53,18 +53,10 @@ if( !function_exists('parse_ini_string') ) { */ function clean_slashes($path) { - return preg_replace('/\/+/', '/', $path); + return preg_replace('/\/+/', '/', $path); } -/** - * makes sure path is valid - */ -function validate_path() -{ - -} - /** * @param int $seconds Number of seconds to convert into a human-readable timestamp @@ -152,7 +144,7 @@ function get_base_href() function get_url_parts() { $parts = explode('/', substr($_SERVER['HTTP_HOST'], strlen(get_base_dir() . '/'))); - return $parts[0] ? $parts : 0; + return $parts[0] ? $parts : 0; } @@ -171,12 +163,10 @@ function get_request() $path_info = pathinfo( $path ); $path_info['path'] = preg_match("/\.\.\//", $path) ? '/' : $path; $path_info['extension'] = null; + if ($path_info['dirname'] == '.') + $path_info['dirname'] = ''; - // echo '
';
-    // print_r($path_info);
-    // exit;
-
-    return $path_info; //substr($_SERVER['HTTP_HOST'], strlen(get_base_dir() . '/'));
+    return $path_info;
 }
 
 
diff --git a/templates/default.html.tpl b/templates/default.html.tpl
index c7b1c13..e2e4d77 100644
--- a/templates/default.html.tpl
+++ b/templates/default.html.tpl
@@ -16,7 +16,7 @@
 	more →

'; - $this->include_template('entry.html.tpl', array('data' => $entry)); + $this->include_template('entry.html.tpl', array('entry' => $entry)); ?> diff --git a/templates/entry.html.tpl b/templates/entry.html.tpl index cd68910..b1db120 100644 --- a/templates/entry.html.tpl +++ b/templates/entry.html.tpl @@ -1,19 +1,19 @@

- +

- +