2010-08-21 04:43:03 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
|
|
/**
|
|
|
|
* Kohana user guide and api browser.
|
|
|
|
*
|
|
|
|
* @package Kohana/Userguide
|
|
|
|
* @category Controllers
|
|
|
|
* @author Kohana Team
|
|
|
|
*/
|
|
|
|
class Controller_Userguide extends Controller_Template {
|
|
|
|
|
|
|
|
public $template = 'userguide/template';
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
protected $media;
|
|
|
|
protected $api;
|
|
|
|
protected $guide;
|
|
|
|
|
|
|
|
public function before()
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
if ($this->request->action() === 'media')
|
2010-08-21 04:43:03 +00:00
|
|
|
{
|
|
|
|
// Do not template media files
|
|
|
|
$this->auto_render = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Grab the necessary routes
|
|
|
|
$this->media = Route::get('docs/media');
|
|
|
|
$this->guide = Route::get('docs/guide');
|
|
|
|
|
|
|
|
if (defined('MARKDOWN_PARSER_CLASS'))
|
|
|
|
{
|
|
|
|
throw new Kohana_Exception('Markdown parser already registered. Live documentation will not work in your environment.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use customized Markdown parser
|
|
|
|
define('MARKDOWN_PARSER_CLASS', 'Kodoc_Markdown');
|
|
|
|
|
|
|
|
if ( ! class_exists('Markdown', FALSE))
|
|
|
|
{
|
|
|
|
// Load Markdown support
|
|
|
|
require Kohana::find_file('vendor', 'markdown/markdown');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the base URL for links and images
|
|
|
|
Kodoc_Markdown::$base_url = URL::site($this->guide->uri()).'/';
|
|
|
|
Kodoc_Markdown::$image_url = URL::site($this->media->uri()).'/';
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::before();
|
|
|
|
}
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// List all modules that have userguides
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->template->title = "Userguide";
|
|
|
|
$this->template->breadcrumb = array('User Guide');
|
|
|
|
$this->template->content = View::factory('userguide/index', array('modules' => $this->_modules()));
|
|
|
|
$this->template->menu = View::factory('userguide/menu', array('modules' => $this->_modules()));
|
|
|
|
|
|
|
|
// Don't show disqus on the index page
|
|
|
|
$this->template->hide_disqus = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display an error if a page isn't found
|
|
|
|
public function error($message)
|
|
|
|
{
|
|
|
|
$this->response->status(404);
|
|
|
|
$this->template->title = "Userguide - Error";
|
|
|
|
$this->template->content = View::factory('userguide/error',array('message' => $message));
|
|
|
|
|
|
|
|
// Don't show disqus on error pages
|
|
|
|
$this->template->hide_disqus = TRUE;
|
|
|
|
|
|
|
|
// If we are in a module and that module has a menu, show that
|
|
|
|
if ($module = $this->request->param('module') AND $menu = $this->file($module.'/menu') AND Kohana::config('userguide.modules.'.$module.'.enabled'))
|
|
|
|
{
|
|
|
|
// Namespace the markdown parser
|
|
|
|
Kodoc_Markdown::$base_url = URL::site($this->guide->uri()).'/'.$module.'/';
|
|
|
|
Kodoc_Markdown::$image_url = URL::site($this->media->uri()).'/'.$module.'/';
|
|
|
|
|
|
|
|
$this->template->menu = Markdown($this->_get_all_menu_markdown());
|
|
|
|
$this->template->breadcrumb = array(
|
|
|
|
$this->guide->uri() => 'User Guide',
|
|
|
|
$this->guide->uri(array('module' => $module)) => Kohana::config('userguide.modules.'.$module.'.name'),
|
|
|
|
'Error'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// If we are in the api browser, show the menu and show the api browser in the breadcrumbs
|
|
|
|
else if (Route::name($this->request->route()) == 'docs/api')
|
|
|
|
{
|
|
|
|
$this->template->menu = Kodoc::menu();
|
|
|
|
|
|
|
|
// Bind the breadcrumb
|
|
|
|
$this->template->breadcrumb = array(
|
|
|
|
$this->guide->uri(array('page' => NULL)) => 'User Guide',
|
|
|
|
$this->request->route()->uri() => 'API Browser',
|
|
|
|
'Error'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Otherwise, show the userguide module menu on the side
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->template->menu = View::factory('userguide/menu',array('modules' => $this->_modules()));
|
|
|
|
$this->template->breadcrumb = array($this->request->route()->uri() => 'User Guide','Error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
public function action_docs()
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
$module = $this->request->param('module');
|
2010-08-21 04:43:03 +00:00
|
|
|
$page = $this->request->param('page');
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// Trim trailing slash
|
|
|
|
$page = rtrim($page, '/');
|
|
|
|
|
|
|
|
// If no module provided in the url, show the user guide index page, which lists the modules.
|
|
|
|
if ( ! $module)
|
2010-08-21 04:43:03 +00:00
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
return $this->index();
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// If this module's userguide pages are disabled, show the error page
|
|
|
|
if ( ! Kohana::config('userguide.modules.'.$module.'.enabled'))
|
|
|
|
{
|
|
|
|
return $this->error(__('That module doesn\'t exist, or has userguide pages disabled.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent "guide/module" and "guide/module/index" from having duplicate content
|
|
|
|
if ( $page == 'index')
|
|
|
|
{
|
|
|
|
return $this->error(__('Userguide page not found'));
|
|
|
|
}
|
2010-08-21 04:43:03 +00:00
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// If a module is set, but no page was provided in the url, show the index page
|
|
|
|
if ( ! $page )
|
|
|
|
{
|
|
|
|
$page = 'index';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the markdown file for this page
|
|
|
|
$file = $this->file($module.'/'.$page);
|
|
|
|
|
|
|
|
// If it's not found, show the error page
|
2010-08-21 04:43:03 +00:00
|
|
|
if ( ! $file)
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
return $this->error(__('Userguide page not found'));
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// Namespace the markdown parser
|
|
|
|
Kodoc_Markdown::$base_url = URL::site($this->guide->uri()).'/'.$module.'/';
|
|
|
|
Kodoc_Markdown::$image_url = URL::site($this->media->uri()).'/'.$module.'/';
|
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
// Set the page title
|
2011-05-13 06:00:25 +00:00
|
|
|
$this->template->title = $page == 'index' ? Kohana::config('userguide.modules.'.$module.'.name') : $this->title($page);
|
2010-08-21 04:43:03 +00:00
|
|
|
|
|
|
|
// Parse the page contents into the template
|
2011-05-13 06:00:25 +00:00
|
|
|
Kodoc_Markdown::$show_toc = true;
|
2010-08-21 04:43:03 +00:00
|
|
|
$this->template->content = Markdown(file_get_contents($file));
|
2011-05-13 06:00:25 +00:00
|
|
|
Kodoc_Markdown::$show_toc = false;
|
2010-08-21 04:43:03 +00:00
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// Attach this module's menu to the template
|
|
|
|
$this->template->menu = Markdown($this->_get_all_menu_markdown());
|
2010-08-21 04:43:03 +00:00
|
|
|
|
|
|
|
// Bind the breadcrumb
|
|
|
|
$this->template->bind('breadcrumb', $breadcrumb);
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// Bind the copyright
|
|
|
|
$this->template->copyright = Kohana::config('userguide.modules.'.$module.'.copyright');
|
|
|
|
|
|
|
|
// Add the breadcrumb trail
|
2010-08-21 04:43:03 +00:00
|
|
|
$breadcrumb = array();
|
|
|
|
$breadcrumb[$this->guide->uri()] = __('User Guide');
|
2011-05-13 06:00:25 +00:00
|
|
|
$breadcrumb[$this->guide->uri(array('module' => $module))] = Kohana::config('userguide.modules.'.$module.'.name');
|
|
|
|
|
|
|
|
// TODO try and get parent category names (from menu). Regex magic or javascript dom stuff perhaps?
|
|
|
|
|
|
|
|
// Only add the current page title to breadcrumbs if it isn't the index, otherwise we get repeats.
|
|
|
|
if ($page != 'index')
|
|
|
|
{
|
|
|
|
$breadcrumb[] = $this->template->title;
|
|
|
|
}
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function action_api()
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
// Enable the missing class autoloader. If a class cannot be found a
|
|
|
|
// fake class will be created that extends Kodoc_Missing
|
2010-08-21 04:43:03 +00:00
|
|
|
spl_autoload_register(array('Kodoc_Missing', 'create_class'));
|
|
|
|
|
|
|
|
// Get the class from the request
|
|
|
|
$class = $this->request->param('class');
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
// If no class was passed to the url, display the API index page
|
|
|
|
if ( ! $class)
|
2010-08-21 04:43:03 +00:00
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
$this->template->title = __('Table of Contents');
|
|
|
|
|
|
|
|
$this->template->content = View::factory('userguide/api/toc')
|
|
|
|
->set('classes', Kodoc::class_methods())
|
|
|
|
->set('route', $this->request->route());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create the Kodoc_Class version of this class.
|
|
|
|
$_class = Kodoc_Class::factory($class);
|
|
|
|
|
|
|
|
// If the class requested and the actual class name are different
|
|
|
|
// (different case, orm vs ORM, auth vs Auth) redirect
|
|
|
|
if ($_class->class->name != $class)
|
2010-08-21 04:43:03 +00:00
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
$this->request->redirect($this->request->route()->uri(array('class'=>$_class->class->name)));
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
2011-05-13 06:00:25 +00:00
|
|
|
|
|
|
|
// If this classes immediate parent is Kodoc_Missing, then it should 404
|
|
|
|
if ($_class->class->getParentClass() AND $_class->class->getParentClass()->name == 'Kodoc_Missing')
|
|
|
|
return $this->error('That class was not found. Check your url and make sure that the module with that class is enabled.');
|
|
|
|
|
|
|
|
// If this classes package has been disabled via the config, 404
|
|
|
|
if ( ! Kodoc::show_class($_class))
|
|
|
|
return $this->error('That class is in package that is hidden. Check the <code>api_packages</code> config setting.');
|
|
|
|
|
|
|
|
// Everything is fine, display the class.
|
2010-08-21 04:43:03 +00:00
|
|
|
$this->template->title = $class;
|
|
|
|
|
|
|
|
$this->template->content = View::factory('userguide/api/class')
|
|
|
|
->set('doc', Kodoc::factory($class))
|
2011-05-13 06:00:25 +00:00
|
|
|
->set('route', $this->request->route());
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the menu to the template
|
|
|
|
$this->template->menu = Kodoc::menu();
|
|
|
|
|
|
|
|
// Bind the breadcrumb
|
|
|
|
$this->template->bind('breadcrumb', $breadcrumb);
|
|
|
|
|
|
|
|
// Get the docs URI
|
|
|
|
$guide = Route::get('docs/guide');
|
|
|
|
|
|
|
|
// Add the breadcrumb
|
|
|
|
$breadcrumb = array();
|
|
|
|
$breadcrumb[$this->guide->uri(array('page' => NULL))] = __('User Guide');
|
2011-05-13 06:00:25 +00:00
|
|
|
$breadcrumb[$this->request->route()->uri()] = 'API Browser';
|
2010-08-21 04:43:03 +00:00
|
|
|
$breadcrumb[] = $this->template->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function action_media()
|
|
|
|
{
|
|
|
|
// Get the file path from the request
|
|
|
|
$file = $this->request->param('file');
|
|
|
|
|
|
|
|
// Find the file extension
|
|
|
|
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
// Remove the extension from the filename
|
|
|
|
$file = substr($file, 0, -(strlen($ext) + 1));
|
|
|
|
|
2011-05-13 06:00:25 +00:00
|
|
|
if ($file = Kohana::find_file('media/guide', $file, $ext))
|
2010-08-21 04:43:03 +00:00
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
|
|
|
|
$this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);
|
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
// Send the file content as the response
|
2011-05-13 06:00:25 +00:00
|
|
|
$this->response->body(file_get_contents($file));
|
|
|
|
|
|
|
|
// Set the proper headers to allow caching
|
|
|
|
$this->response->headers('content-type', File::mime_by_ext($ext));
|
|
|
|
$this->response->headers('last-modified', date('r', filemtime($file)));
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Return a 404 status
|
2011-05-13 06:00:25 +00:00
|
|
|
$this->response->status(404);
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function after()
|
|
|
|
{
|
|
|
|
if ($this->auto_render)
|
|
|
|
{
|
|
|
|
// Get the media route
|
|
|
|
$media = Route::get('docs/media');
|
|
|
|
|
|
|
|
// 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',
|
|
|
|
$media->uri(array('file' => 'css/shCore.css')) => 'screen',
|
|
|
|
$media->uri(array('file' => 'css/shThemeKodoc.css')) => 'screen',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add scripts
|
|
|
|
$this->template->scripts = array(
|
|
|
|
$media->uri(array('file' => 'js/jquery.min.js')),
|
2011-05-13 06:00:25 +00:00
|
|
|
$media->uri(array('file' => 'js/jquery.cookie.js')),
|
2010-08-21 04:43:03 +00:00
|
|
|
$media->uri(array('file' => 'js/kodoc.js')),
|
2011-05-13 06:00:25 +00:00
|
|
|
// Syntax Highlighter
|
2010-08-21 04:43:03 +00:00
|
|
|
$media->uri(array('file' => 'js/shCore.js')),
|
|
|
|
$media->uri(array('file' => 'js/shBrushPhp.js')),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add languages
|
|
|
|
$this->template->translations = Kohana::message('userguide', 'translations');
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::after();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function file($page)
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
return Kohana::find_file('guide', $page, 'md');
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function section($page)
|
|
|
|
{
|
|
|
|
$markdown = $this->_get_all_menu_markdown();
|
2011-05-13 06:00:25 +00:00
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
if (preg_match('~\*{2}(.+?)\*{2}[^*]+\[[^\]]+\]\('.preg_quote($page).'\)~mu', $markdown, $matches))
|
|
|
|
{
|
|
|
|
return $matches[1];
|
|
|
|
}
|
2011-05-13 06:00:25 +00:00
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function title($page)
|
|
|
|
{
|
|
|
|
$markdown = $this->_get_all_menu_markdown();
|
2011-05-13 06:00:25 +00:00
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
if (preg_match('~\[([^\]]+)\]\('.preg_quote($page).'\)~mu', $markdown, $matches))
|
|
|
|
{
|
|
|
|
// Found a title for this link
|
|
|
|
return $matches[1];
|
|
|
|
}
|
2011-05-13 06:00:25 +00:00
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
return $page;
|
|
|
|
}
|
2011-05-13 06:00:25 +00:00
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
protected function _get_all_menu_markdown()
|
|
|
|
{
|
|
|
|
// Only do this once per request...
|
|
|
|
static $markdown = '';
|
2011-05-13 06:00:25 +00:00
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
if (empty($markdown))
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
// Get menu items
|
|
|
|
$file = $this->file($this->request->param('module').'/menu');
|
|
|
|
|
2010-08-21 04:43:03 +00:00
|
|
|
if ($file AND $text = file_get_contents($file))
|
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
// Add spans around non-link categories. This is a terrible hack.
|
|
|
|
//echo Kohana::debug($text);
|
|
|
|
|
|
|
|
//$text = preg_replace('/(\s*[\-\*\+]\s*)(.*)/','$1<span>$2</span>',$text);
|
|
|
|
$text = preg_replace('/^(\s*[\-\*\+]\s*)([^\[\]]+)$/m','$1<span>$2</span>',$text);
|
|
|
|
//echo Kohana::debug($text);
|
2010-08-21 04:43:03 +00:00
|
|
|
$markdown .= $text;
|
|
|
|
}
|
2011-05-13 06:00:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $markdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the list of modules from the config, and reverses it so it displays in the order the modules are added, but move Kohana to the top.
|
|
|
|
protected function _modules()
|
|
|
|
{
|
|
|
|
$modules = array_reverse(Kohana::config('userguide.modules'));
|
|
|
|
|
|
|
|
if (isset($modules['kohana']))
|
|
|
|
{
|
|
|
|
$kohana = $modules['kohana'];
|
|
|
|
unset($modules['kohana']);
|
|
|
|
$modules = array_merge(array('kohana' => $kohana), $modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove modules that have been disabled via config
|
|
|
|
foreach ($modules as $key => $value)
|
|
|
|
{
|
|
|
|
if ( ! Kohana::config('userguide.modules.'.$key.'.enabled'))
|
2010-08-21 04:43:03 +00:00
|
|
|
{
|
2011-05-13 06:00:25 +00:00
|
|
|
unset($modules[$key]);
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-13 06:00:25 +00:00
|
|
|
|
|
|
|
return $modules;
|
2010-08-21 04:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End Userguide
|