From: Gabriel Dunne Date: Sat, 10 Jul 2010 01:46:45 +0000 (-0700) Subject: new lib files X-Git-Url: https://git.quilime.com/?a=commitdiff_plain;h=209b59fcff93953b3738ef9291e68aa8451b0e70;p=plog.git new lib files --- diff --git a/lib/data.php b/lib/data.php index 6d04a9e..e20e402 100644 --- a/lib/data.php +++ b/lib/data.php @@ -1,119 +1,124 @@ $info) { + if (!$info->isDir()) { + $entries[] = parse_entry($info); } } - - // order by date - foreach ($result as $key => $row) { - $time[$key] = $row['timestamp']; + + switch ($order_by) + { + default : + foreach ($entries as $key => $row) + $time[$key] = $row['timestamp']; + if ($time) + array_multisort($time, $order, $entries); } - if ($time) - array_multisort($time, SORT_DESC, $result); - - return array($result, $result_total); + + return array($entries, sizeof($entries)); } /** - * parse data file + * get all pages */ -function parse_file($f) +function get_pages() { - $pathparts = explode("/", dirname($f)); - - $file_contents = explode("\n", file_get_contents($f, FILE_USE_INCLUDE_PATH)); - $cc = ""; - $content = ""; - $conf = true; - foreach ( $file_contents as $fc ) { - if ($fc == CONFIG_DELIMITER) { - $conf = false; - continue; - } - if ($conf) $cc .= $fc . "\n"; - else $content .= $fc . "\n"; + $path = LOCAL_ROOT . PAGE_DIR; + $dir_iterator = new DirectoryIterator($path); + $pages = array(); + foreach($dir_iterator as $page) { + if ($page->isDir()) continue; + $arr = parse_entry($page, 1); + $arr['is_page'] = 1; + $pages[] = $arr; } - - $config = parse_ini_string($cc); - - $res = $config; - - $res['url'] = $res['is_page'] == 1 ? get_base_dir() . '/' . basename($f) . '/' : get_base_dir() . '/' . $pathparts[sizeof($pathparts)-1] . '/' . basename($f); - $res['timestamp'] = date('U', strtotime( $config['date'] ? $config['date'] : filemtime($f))); - $res['cat'] = $pathparts[sizeof($pathparts)-1]; - $res['content'] = Markdown($content); - $res['tags'] = explode(' ', $config['tags']); - - return $res; + return $pages; } /** - * get content folders + * returns directories of a folder + * @param path the path to search. defaults to the CONTENT_DIR + * @param args array */ -function get_content_folders() +function get_dirs( $path = "", $args = array()) { - $folders = glob(LOCAL_ROOT . CONTENT_DIR . DIRECTORY_SEPARATOR . '/*', GLOB_ONLYDIR); - $content_folders = array(); - foreach($folders as $folder) - $content_folders[] = array( - 'basename' => basename($folder), - 'title' => basename($folder), - 'url' => get_base_dir() . '/' . basename($folder) . '/' - ); - return $content_folders; + $recursive = isset($args['recursive']) ? $args['recursive'] : 1; + $path = LOCAL_ROOT . CONTENT_DIR . $path; + + if ($recursive) { + $iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME); + $dir_iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST); + } + else { + $dir_iterator = new DirectoryIterator($path); + } + $dirs = array(); + foreach ($dir_iterator as $dir => $info) { + if ($info->isDir() && $info->getFilename() != '.' && $info->getFilename() != '..') { + $dirs[] = str_replace($path, "", $info->getRealPath()); + } + } + return $dirs; } /** - * get pages + * @param splFileInfo SPLFileInfo Object + * @param page default is false */ -function get_pages() +function parse_entry($fileInfo, $page = 0) { - $page_files = glob(LOCAL_ROOT . PAGE_DIR . DIRECTORY_SEPARATOR . '/*'); - $pages = array(); - foreach($page_files as $page) { - $arr = parse_file($page); - $arr['is_page'] = 1; - $pages[] = $arr; + $config = ""; + $content = ""; + $passed_config = false; + $file_contents = file($fileInfo->getRealPath(), FILE_USE_INCLUDE_PATH); + foreach ( $file_contents as $line ) { + if (trim($line) == CONFIG_DELIMITER) { + $passed_config = true; + continue; + } + if (!$passed_config) { + $config .= $line; + continue; + } + $content .= $line; } - return $pages; + + $file = array(); + $file['config'] = parse_ini_string($config); + + $file['title'] = $file['config']['title']; + $file['timestamp'] = $file['config']['date'] ? date('U', strtotime( $file['config']['date'])) : $fileInfo->getCTime(); + $file['tags'] = $file['config']['tags'] ? explode(" ", $file['config']['tags']) : null; + $file['content'] = Markdown($content); + $file['cat'] = $page? null : substr(clean_slashes(str_replace(LOCAL_ROOT . CONTENT_DIR, "", $fileInfo->getPath())),1); + $file['path'] = $fileInfo->getRealPath(); + $file['url'] = WEB_ROOT . ($page ? '' : $file['cat'] . '/' ) . $fileInfo->getFilename(); + + return $file; } + ?> diff --git a/lib/output.php b/lib/output.php index e70c913..6866c6c 100644 --- a/lib/output.php +++ b/lib/output.php @@ -1,17 +1,22 @@ template_dir = join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), '..', TEMPLATE_DIR)); $t->template_cache_dir = join(DIRECTORY_SEPARATOR, array(dirname(__FILE__), '..', TEMPLATE_DIR, 'cache')); -// $t->caching = 0; return $t; } - +/** + * @param format + * @param default format + */ function parse_format($format, $default) { $types = array('html' => 'text/html', @@ -30,6 +35,9 @@ function parse_format($format, $default) } +/** + * parse ini file + */ if( !function_exists('parse_ini_string') ) { function parse_ini_string( $string ) { $array = Array(); @@ -51,6 +59,16 @@ if( !function_exists('parse_ini_string') ) { } +/** + * removes double slashes + * @param path + */ +function clean_slashes($path) +{ + return preg_replace('/\/+/', '/', $path); +} + + /** * @param int $seconds Number of seconds to convert into a human-readable timestamp * @return tring Human-readable approximate timestamp like "2 hours" diff --git a/lib/template.php b/lib/template.php index abbb3b7..a4558e9 100644 --- a/lib/template.php +++ b/lib/template.php @@ -50,15 +50,9 @@ class Template } - function ob_file_callback($buffer) + public function page_title($delim) { -// fwrite($this->cache_file, $buffer); - } - - - public function title() - { - return ""; + return SITE_TITLE . (isset($this->_tpl_vars['page_title']) ? $delim . $this->_tpl_vars['page_title'] : ""); } }