'index',
'$Module/$Class' => 'parsePage'
);
/**
* An array of files to ignore from the listing
*
* @var array
*/
static $ignored_files = array('.', '..', '.DS_Store', '.svn', '.git', 'assets');
/**
* Documentation Home
*
* Displays a welcome message as well as links to the sapphire sections and the
* installed modules
*/
function index() {
$this->writeHeader();
$base = Director::baseURL();
$readme = ($this->readmeExists('sapphire')) ? "Read Me" : false;
// write the main content (sapphire) on the left
echo "
";
echo "
sapphire $readme
";
$this->generateNestedTree('sapphire');
echo "
";
echo "
";
$modules = scandir(BASE_PATH);
// modules which are not documented
$undocumented = array();
// generate a list of module documentation (not core)
if($modules) {
foreach($modules as $module) {
// skip sapphire since this is on the left
$ignored_modules = array('sapphire', 'assets', 'themes');
if(!in_array($module, $ignored_modules) && !in_array($module, self::$ignored_files) && is_dir(BASE_PATH .'/'. $module)) {
// see if docs folder is present
$subfolders = scandir(BASE_PATH .'/'. $module);
if($subfolders && in_array('docs', $subfolders)) {
$readme = ($filename = $this->readmeExists($module)) ? "Read Me" : false;
echo "
". $module .' '. $readme."
";
$this->generateNestedTree($module);
echo "
";
}
else {
$undocumented[] = $module;
}
}
}
}
// for each of the modules. Display them here
echo "
";
}
/**
* Work out if a module contains a readme
*
* @param String - Module to check
* @return bool|String - of path
*/
private function readmeExists($module) {
$children = scandir(BASE_PATH.'/'.$module);
$readmeOptions = array('readme', 'readme.md', 'readme.txt');
if($children) {
foreach($children as $i => $file) {
if(in_array(strtolower($file), $readmeOptions)) return $file;
}
}
return false;
}
/**
* Find a documentation page within a given module.
*
* @todo Currently this only works on pages - eg if you go /dev/docs/Forms/ it won't show the
* overall forms page
*
* @param String - Name of Module
* @param String - Name of doc page
*
* @return String|false - File path
*/
private function findPage($path, $name) {
// open docs folder
$handle = opendir($path);
if($handle) {
while (false !== ($file = readdir($handle))) {
$newpath = $path .'/'. $file;
if(!in_array($file, self::$ignored_files)) {
if(is_dir($newpath)) {
// keep looking down the tree
return $this->findPage($newpath, $name);
}
elseif(strtolower($this->formatStringForTitle($file)) == strtolower($name)) {
return $newpath;
}
}
}
}
return false;
}
/**
* Generate a nested tree for a given folder via recursion
*
* @param String - module to generate
*/
private function generateNestedTree($module) {
$path = BASE_PATH . '/'. $module .'/docs/';
return (is_dir($path)) ? $this->recursivelyGenerateTree($path, $module) : false;
}
/**
* Recursive method to generate the tree
*
* @param String - folder to work through
* @param String - module we're working through
*/
private function recursivelyGenerateTree($path, $module) {
echo "
";
$handle = opendir($path);
if($handle) {
while (false !== ($file = readdir($handle))) {
if(!in_array($file, self::$ignored_files)) {
$newPath = $path.'/'.$file;
// if the file is a dir nest the pages
if(is_dir($newPath)) {
// if this has a number
echo "
";
}
/**
* Take a file name and generate a 'nice' title for it
*
* @todo find a nicer way of removing the numbers.
*
* @param String
* @return String
*/
private function formatStringForTitle($title) {
// remove numbers if used.
if(substr($title, 2, 1) == '-') $title = substr($title, 3);
// change - to spaces
$title = str_ireplace('-', ' ', $title);
// remove extension
$title = str_ireplace('.md', '', $title);
return $title;
}
}