Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit 85b1d9a

Browse files
author
Woody Gilk
committed
Created initial kodoc module
0 parents  commit 85b1d9a

File tree

9 files changed

+3216
-0
lines changed

9 files changed

+3216
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Kohana user guide and live API documentation module

classes/controller/kodoc.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php defined('SYSPATH') or die('No direct script access.');
2+
3+
class Controller_Kodoc extends Controller_Template {
4+
5+
/**
6+
* @var object Kodoc instance
7+
*/
8+
public $kodoc;
9+
10+
public $template = 'kodoc/template';
11+
12+
public function before()
13+
{
14+
if ($this->request->action === 'media')
15+
{
16+
// Do not template media files
17+
$this->auto_render = FALSE;
18+
}
19+
20+
return parent::before();
21+
}
22+
23+
public function action_guide($lang = NULL, $page = NULL)
24+
{
25+
// Create a new guide instance
26+
$this->kodoc = Kodoc_Guide::factory($lang);
27+
28+
// Load the requested page content
29+
$this->template->content = $this->kodoc->page($page);
30+
31+
// Set the page title
32+
$this->template->title = $this->kodoc->page_title($page);
33+
}
34+
35+
public function action_api()
36+
{
37+
throw new Kohana_Exception('API is not implemented yet');
38+
}
39+
40+
public function action_media($file)
41+
{
42+
// Find the file extension
43+
$ext = pathinfo($file, PATHINFO_EXTENSION);
44+
45+
// Remove the extension from the filename
46+
$file = substr($file, 0, -(strlen($ext) + 1));
47+
48+
if ($file = Kohana::find_file('media', $file, $ext))
49+
{
50+
// Send the file content as the response
51+
$this->request->response = file_get_contents($file);
52+
}
53+
else
54+
{
55+
// Return a 404 status
56+
$this->request->status = 404;
57+
}
58+
59+
// Set the content type for this extension
60+
$this->request->headers['Content-Type'] = File::mime_by_ext($ext);
61+
}
62+
63+
public function after()
64+
{
65+
if ($this->auto_render)
66+
{
67+
// Attach the menu to the template
68+
$this->template->menu = $this->kodoc->page('menu');
69+
70+
// Get the media route
71+
$media = Route::get('kodoc_media');
72+
73+
echo $e;
74+
75+
// Add styles
76+
$this->template->styles = array(
77+
$media->uri(array('file' => 'css/print.css')) => 'print',
78+
$media->uri(array('file' => 'css/screen.css')) => 'screen',
79+
$media->uri(array('file' => 'css/kodoc.css')) => 'screen',
80+
);
81+
}
82+
83+
return parent::after();
84+
}
85+
86+
} // End Kodoc

classes/kodoc.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php defined('SYSPATH') or die('No direct script access.');
2+
3+
abstract class Kodoc {
4+
5+
public function __construct()
6+
{
7+
// Use customized Markdown parser
8+
define('MARKDOWN_PARSER_CLASS', 'Kodoc_Markdown');
9+
10+
// Load Markdown support
11+
require Kohana::find_file('vendor', 'markdown/markdown');
12+
}
13+
14+
} // End Kodoc

classes/kodoc/guide.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php defined('SYSPATH') or die('No direct script access.');
2+
3+
class Kodoc_Guide extends Kodoc {
4+
5+
/**
6+
* Create a new guide instance.
7+
*
8+
* @param string guide language
9+
* @return Kodoc_Guide
10+
*/
11+
public static function factory($lang = NULL)
12+
{
13+
return new Kodoc_Guide($lang);
14+
}
15+
16+
// Current language
17+
protected $_lang;
18+
19+
/**
20+
*
21+
*/
22+
public function __construct($lang = NULL)
23+
{
24+
if ( ! empty($lang))
25+
{
26+
// Set the language code
27+
$this->_lang = $lang;
28+
}
29+
30+
// Load Markdown
31+
parent::__construct();
32+
33+
// Set the base URL for links
34+
Kodoc_Markdown::$base_url = URL::site(Route::get('kodoc_guide')->uri(array('language' => $lang))).'/';
35+
}
36+
37+
public function find_file($name)
38+
{
39+
if ($this->_lang)
40+
{
41+
$name = $this->_lang.'/'.$name;
42+
}
43+
44+
return Kohana::find_file('guide', $name, 'md');
45+
}
46+
47+
public function page($name)
48+
{
49+
if ($file = $this->find_file($name))
50+
{
51+
return Markdown(file_get_contents($file));
52+
}
53+
else
54+
{
55+
return FALSE;
56+
}
57+
}
58+
59+
public function page_title($page)
60+
{
61+
if ($menu = $this->find_file('menu'))
62+
{
63+
if (preg_match('~\[([^\]]+)\]\('.preg_quote($page).'\)~mu', file_get_contents($menu), $matches))
64+
{
65+
// Found a title for this link
66+
return $matches[1];
67+
}
68+
}
69+
70+
return FALSE;
71+
}
72+
73+
} // End Kodoc_Guide

classes/kodoc/markdown.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php defined('SYSPATH') or die('No direct script access.');
2+
3+
class Kodoc_Markdown extends MarkdownExtra_Parser {
4+
5+
/**
6+
* @var string base url for links
7+
*/
8+
public static $base_url = '';
9+
10+
public function __construct()
11+
{
12+
// doLinks is 20, execute just before
13+
$this->span_gamut['do_add_base_url'] = 19;
14+
15+
// PHP4 makes me sad.
16+
parent::MarkdownExtra_Parser();
17+
}
18+
19+
/**
20+
* Add the current base url to all links.
21+
*
22+
* @param string span text
23+
* @return string
24+
*/
25+
public function do_add_base_url($text)
26+
{
27+
return preg_replace_callback('-^\[([^\]]+)\]\((\S*)\)$-', array($this, '_do_add_base_url'), $text);
28+
}
29+
30+
public function _do_add_base_url($matches)
31+
{
32+
if ($matches[2] AND strpos($matches[2], '://') === FALSE)
33+
{
34+
// Add the base url to the link URL
35+
$matches[2] = Kodoc_Markdown::$base_url.$matches[2];
36+
}
37+
38+
// Recreate the link
39+
return "[{$matches[1]}]({$matches[2]})";
40+
}
41+
42+
} // End Kodoc_Markdown

init.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php defined('SYSPATH') or die('No direct script access.');
2+
3+
/**
4+
* Add the Kodoc user guide, live api, and media assets routes.
5+
*/
6+
7+
Route::set('kodoc_api', 'kodoc/api(/<class>)', array(
8+
'class' => '[a-zA-Z_]',
9+
))
10+
->defaults(array(
11+
'controller' => 'kodoc',
12+
'action' => 'api',
13+
));
14+
15+
Route::set('kodoc_guide', 'kodoc/guide((/<language>)/<page>)', array(
16+
'action' => '(?:api|guide)',
17+
'language' => '[a-z-]{2,4}',
18+
'page' => '.+',
19+
))
20+
->defaults(array(
21+
'controller' => 'kodoc',
22+
'action' => 'guide',
23+
));
24+
25+
Route::set('kodoc_media', 'kodoc/media/<file>', array(
26+
'file' => '.+',
27+
))
28+
->defaults(array(
29+
'controller' => 'kodoc',
30+
'action' => 'media',
31+
));

vendor/markdown/License.text

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
PHP Markdown & Extra
2+
Copyright (c) 2004-2008 Michel Fortin
3+
<http://www.michelf.com/>
4+
All rights reserved.
5+
6+
Based on Markdown
7+
Copyright (c) 2003-2006 John Gruber
8+
<http://daringfireball.net/>
9+
All rights reserved.
10+
11+
Redistribution and use in source and binary forms, with or without
12+
modification, are permitted provided that the following conditions are
13+
met:
14+
15+
* Redistributions of source code must retain the above copyright notice,
16+
this list of conditions and the following disclaimer.
17+
18+
* Redistributions in binary form must reproduce the above copyright
19+
notice, this list of conditions and the following disclaimer in the
20+
documentation and/or other materials provided with the distribution.
21+
22+
* Neither the name "Markdown" nor the names of its contributors may
23+
be used to endorse or promote products derived from this software
24+
without specific prior written permission.
25+
26+
This software is provided by the copyright holders and contributors "as
27+
is" and any express or implied warranties, including, but not limited
28+
to, the implied warranties of merchantability and fitness for a
29+
particular purpose are disclaimed. In no event shall the copyright owner
30+
or contributors be liable for any direct, indirect, incidental, special,
31+
exemplary, or consequential damages (including, but not limited to,
32+
procurement of substitute goods or services; loss of use, data, or
33+
profits; or business interruption) however caused and on any theory of
34+
liability, whether in contract, strict liability, or tort (including
35+
negligence or otherwise) arising in any way out of the use of this
36+
software, even if advised of the possibility of such damage.

0 commit comments

Comments
 (0)