Skip to content
Browse files

Created initial kodoc module

  • Loading branch information...
0 parents commit 85b1d9a484cf527e664ee0ef40cbd75f492fc361 @shadowhand shadowhand committed
Showing with 3,216 additions and 0 deletions.
  1. +1 −0 README.md
  2. +86 −0 classes/controller/kodoc.php
  3. +14 −0 classes/kodoc.php
  4. +73 −0 classes/kodoc/guide.php
  5. +42 −0 classes/kodoc/markdown.php
  6. +31 −0 init.php
  7. +36 −0 vendor/markdown/License.text
  8. +2,909 −0 vendor/markdown/markdown.php
  9. +24 −0 views/kodoc/template.php
1 README.md
@@ -0,0 +1 @@
+Kohana user guide and live API documentation module
86 classes/controller/kodoc.php
@@ -0,0 +1,86 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller_Kodoc extends Controller_Template {
+
+ /**
+ * @var object Kodoc instance
+ */
+ public $kodoc;
+
+ public $template = 'kodoc/template';
+
+ public function before()
+ {
+ if ($this->request->action === 'media')
+ {
+ // Do not template media files
+ $this->auto_render = FALSE;
+ }
+
+ return parent::before();
+ }
+
+ public function action_guide($lang = NULL, $page = NULL)
+ {
+ // Create a new guide instance
+ $this->kodoc = Kodoc_Guide::factory($lang);
+
+ // Load the requested page content
+ $this->template->content = $this->kodoc->page($page);
+
+ // Set the page title
+ $this->template->title = $this->kodoc->page_title($page);
+ }
+
+ public function action_api()
+ {
+ throw new Kohana_Exception('API is not implemented yet');
+ }
+
+ public function action_media($file)
+ {
+ // Find the file extension
+ $ext = pathinfo($file, PATHINFO_EXTENSION);
+
+ // Remove the extension from the filename
+ $file = substr($file, 0, -(strlen($ext) + 1));
+
+ if ($file = Kohana::find_file('media', $file, $ext))
+ {
+ // Send the file content as the response
+ $this->request->response = file_get_contents($file);
+ }
+ else
+ {
+ // Return a 404 status
+ $this->request->status = 404;
+ }
+
+ // Set the content type for this extension
+ $this->request->headers['Content-Type'] = File::mime_by_ext($ext);
+ }
+
+ public function after()
+ {
+ if ($this->auto_render)
+ {
+ // Attach the menu to the template
+ $this->template->menu = $this->kodoc->page('menu');
+
+ // Get the media route
+ $media = Route::get('kodoc_media');
+
+ echo $e;
+
+ // Add styles
+ $this->template->styles = array(
+ $media->uri(array('file' => 'css/print.css')) => 'print',
+ $media->uri(array('file' => 'css/screen.css')) => 'screen',
+ $media->uri(array('file' => 'css/kodoc.css')) => 'screen',
+ );
+ }
+
+ return parent::after();
+ }
+
+} // End Kodoc
14 classes/kodoc.php
@@ -0,0 +1,14 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+abstract class Kodoc {
+
+ public function __construct()
+ {
+ // Use customized Markdown parser
+ define('MARKDOWN_PARSER_CLASS', 'Kodoc_Markdown');
+
+ // Load Markdown support
+ require Kohana::find_file('vendor', 'markdown/markdown');
+ }
+
+} // End Kodoc
73 classes/kodoc/guide.php
@@ -0,0 +1,73 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Kodoc_Guide extends Kodoc {
+
+ /**
+ * Create a new guide instance.
+ *
+ * @param string guide language
+ * @return Kodoc_Guide
+ */
+ public static function factory($lang = NULL)
+ {
+ return new Kodoc_Guide($lang);
+ }
+
+ // Current language
+ protected $_lang;
+
+ /**
+ *
+ */
+ public function __construct($lang = NULL)
+ {
+ if ( ! empty($lang))
+ {
+ // Set the language code
+ $this->_lang = $lang;
+ }
+
+ // Load Markdown
+ parent::__construct();
+
+ // Set the base URL for links
+ Kodoc_Markdown::$base_url = URL::site(Route::get('kodoc_guide')->uri(array('language' => $lang))).'/';
+ }
+
+ public function find_file($name)
+ {
+ if ($this->_lang)
+ {
+ $name = $this->_lang.'/'.$name;
+ }
+
+ return Kohana::find_file('guide', $name, 'md');
+ }
+
+ public function page($name)
+ {
+ if ($file = $this->find_file($name))
+ {
+ return Markdown(file_get_contents($file));
+ }
+ else
+ {
+ return FALSE;
+ }
+ }
+
+ public function page_title($page)
+ {
+ if ($menu = $this->find_file('menu'))
+ {
+ if (preg_match('~\[([^\]]+)\]\('.preg_quote($page).'\)~mu', file_get_contents($menu), $matches))
+ {
+ // Found a title for this link
+ return $matches[1];
+ }
+ }
+
+ return FALSE;
+ }
+
+} // End Kodoc_Guide
42 classes/kodoc/markdown.php
@@ -0,0 +1,42 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Kodoc_Markdown extends MarkdownExtra_Parser {
+
+ /**
+ * @var string base url for links
+ */
+ public static $base_url = '';
+
+ public function __construct()
+ {
+ // doLinks is 20, execute just before
+ $this->span_gamut['do_add_base_url'] = 19;
+
+ // PHP4 makes me sad.
+ parent::MarkdownExtra_Parser();
+ }
+
+ /**
+ * Add the current base url to all links.
+ *
+ * @param string span text
+ * @return string
+ */
+ public function do_add_base_url($text)
+ {
+ return preg_replace_callback('-^\[([^\]]+)\]\((\S*)\)$-', array($this, '_do_add_base_url'), $text);
+ }
+
+ public function _do_add_base_url($matches)
+ {
+ if ($matches[2] AND strpos($matches[2], '://') === FALSE)
+ {
+ // Add the base url to the link URL
+ $matches[2] = Kodoc_Markdown::$base_url.$matches[2];
+ }
+
+ // Recreate the link
+ return "[{$matches[1]}]({$matches[2]})";
+ }
+
+} // End Kodoc_Markdown
31 init.php
@@ -0,0 +1,31 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+/**
+ * Add the Kodoc user guide, live api, and media assets routes.
+ */
+
+Route::set('kodoc_api', 'kodoc/api(/<class>)', array(
+ 'class' => '[a-zA-Z_]',
+ ))
+ ->defaults(array(
+ 'controller' => 'kodoc',
+ 'action' => 'api',
+ ));
+
+Route::set('kodoc_guide', 'kodoc/guide((/<language>)/<page>)', array(
+ 'action' => '(?:api|guide)',
+ 'language' => '[a-z-]{2,4}',
+ 'page' => '.+',
+ ))
+ ->defaults(array(
+ 'controller' => 'kodoc',
+ 'action' => 'guide',
+ ));
+
+Route::set('kodoc_media', 'kodoc/media/<file>', array(
+ 'file' => '.+',
+ ))
+ ->defaults(array(
+ 'controller' => 'kodoc',
+ 'action' => 'media',
+ ));
36 vendor/markdown/License.text
@@ -0,0 +1,36 @@
+PHP Markdown & Extra
+Copyright (c) 2004-2008 Michel Fortin
+<http://www.michelf.com/>
+All rights reserved.
+
+Based on Markdown
+Copyright (c) 2003-2006 John Gruber
+<http://daringfireball.net/>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+* Neither the name "Markdown" nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+This software is provided by the copyright holders and contributors "as
+is" and any express or implied warranties, including, but not limited
+to, the implied warranties of merchantability and fitness for a
+particular purpose are disclaimed. In no event shall the copyright owner
+or contributors be liable for any direct, indirect, incidental, special,
+exemplary, or consequential damages (including, but not limited to,
+procurement of substitute goods or services; loss of use, data, or
+profits; or business interruption) however caused and on any theory of
+liability, whether in contract, strict liability, or tort (including
+negligence or otherwise) arising in any way out of the use of this
+software, even if advised of the possibility of such damage.
2,909 vendor/markdown/markdown.php
2,909 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
24 views/kodoc/template.php
@@ -0,0 +1,24 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+
+<title><?php echo $title ?> | Kodoc</title>
+
+<?php foreach ($styles as $style => $media) echo html::style($style, array('media' => $media), TRUE), "\n" ?>
+
+</head>
+<body>
+
+<div id="docs">
+ <div id="content">
+ <?php echo $content ?>
+ </div>
+
+ <div id="menu">
+ <?php echo $menu ?>
+ </div>
+</div>
+
+</body>
+</html>

0 comments on commit 85b1d9a

Please sign in to comment.
Something went wrong with that request. Please try again.