]> git.quilime.com - plog.git/commitdiff
cleaned up entry logic
authorGabriel Dunne <gdunne@quilime.com>
Mon, 3 Oct 2011 16:54:31 +0000 (22:24 +0530)
committerGabriel Dunne <gdunne@quilime.com>
Mon, 3 Oct 2011 16:54:31 +0000 (22:24 +0530)
index.php
lib/data.php
lib/model.php
lib/output.php
templates/default.html.tpl
templates/entry.html.tpl
templates/single.html.tpl

index 754dcb84e6c1a6f0eb9310739b341c2784373fdb..74c0a58d47e127b8a11c6fac551ecc5bc16aea62 100644 (file)
--- 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"); 
 
index 648d5466b3580cdec7a24d5ab963372c253ebb83..9efbdd59b59849e4318d1265084ab952f5850a33 100644 (file)
@@ -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
index 4a69874ad15ebfb0a748252cc0789246a144b312..e5e7e165e4f2d4e344ee7da4707c2d239d7d7817 100644 (file)
@@ -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
+}
index 7ec4a734bb87416f365b61d0f65d2de05d695701..6afa47705bdc65522ef2352483cf43bd349bb534 100644 (file)
@@ -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 '<pre>';
-    // print_r($path_info);
-    // exit;
-
-    return $path_info; //substr($_SERVER['HTTP_HOST'], strlen(get_base_dir() . '/'));
+    return $path_info;
 }
 
 
index c7b1c13aa7599990bc76d4e71a6f7cbcd2b25d61..e2e4d77737aed3173870b5f166f48d82bf93b11f 100644 (file)
@@ -16,7 +16,7 @@
        <?  
     if (isset($entry['content_short'])) 
         $entry['content'] = $entry['content_short'] . '<br /><a class="more" href="'.$entry['url'].'">more &rarr;</a><br /><br />';
-    $this->include_template('entry.html.tpl', array('data' => $entry)); 
+    $this->include_template('entry.html.tpl', array('entry' => $entry)); 
     ?>
        <? endforeach; ?>       
        
index cd68910536f0576362329eede767826fe0613762..b1db1205641134c6e8c879cbfe34400531069f5c 100644 (file)
@@ -1,19 +1,19 @@
 <div class="entry">
 
        <h2>
-               <a title="posted on <?=$data['date']?>" href="<?=$data['url']?>"><?=$data['title']?></a>
+               <a title="posted on <?=$entry['date']?>" href="<?=$entry['url']?>"><?=$entry['title']?></a>
        </h2>
 
        <div class="content">
-               <?=$data['content']?>
+               <?=$entry['content']?>
        </div>
 
        <div class="metadata">
-               posted <abbr title="<?=get_relative_time($data['timestamp']);?>"><?=date("F d, Y", $data['timestamp'])?></abbr> 
-               in <a href="<?=$data['cat']['url'];?>"><?=$data['cat']['name'];?></a>
+               posted <abbr title="<?=get_relative_time($entry['timestamp']);?>"><?=date("F d, Y", $entry['timestamp'])?></abbr> 
+               in <a href="<?=$entry['cat']['url'];?>"><?=$entry['cat']['name'];?></a>
                <br/>
                <ul class="tags">
-               <? if ($data['tags']) foreach($data['tags'] as $tag) : ?>
+               <? if ($entry['tags']) foreach($entry['tags'] as $tag) : ?>
                <li>#<?=$tag?></li>
                <? endforeach; ?>
                </ul>
index 91a416ff850592211133e44eb87112af0eaa5573..2af39632a9c367130bd12c79ac96097b40f38821 100644 (file)
@@ -3,7 +3,7 @@
     
     <? $this->include_template('head-inc.html.tpl') ?>
     
-    <title><?=SITE_TITLE?><?=TITLE_DELIMITER?> <?=$data['title'];?></title>
+    <title><?=SITE_TITLE?><?=TITLE_DELIMITER?> <?=$entry['title'];?></title>
     
 </head>
 <body>
@@ -11,7 +11,7 @@
     <? $this->include_template('nav.html.tpl') ?>
     
        <div id="content">
-               <? $this->include_template('entry.html.tpl', array('data' => $data)); ?>        
+               <? $this->include_template('entry.html.tpl', array('entry' => $entries)); ?>    
        </div>
 
        <? $this->include_template('footer.html.tpl') ?>