forked from rossriley/phrocco
/
phrocco.php
executable file
·107 lines (90 loc) · 3.33 KB
/
phrocco.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
require_once("lib/markdown.php");
require_once("lib/pygment.php");
class Phrocco {
public $sources;
public $sections = array();
public $title;
public $file;
public $adapter;
public $output_file;
public $path;
public function __construct($language, $file) {
$this->adapter = ucfirst($language)."Adapter";
require_once(__DIR__."/lib/adapters/".$this->adapter.".php");
$this->adapter = new $this->adapter;
$this->file = $file;
$this->title = basename($this->file);
}
public function parse() {
$this->sections = $this->adapter->parse($this->file);
}
public function render() {
ob_start();
$this->parse();
$view_file = __DIR__."/lib/template/layout.html";
$this->style = file_get_contents(__DIR__."/lib/template/layout.css");
extract((array)$this);
if(!is_readable($view_file)) throw new Exception("Unable to find Template File");
if(!include($view_file)) throw new Exception("PHP Error in $view_file");
$content = ob_get_clean();
file_put_contents($this->output_file, $content);
}
}
class PhroccoIterator extends RecursiveDirectoryIterator {
public function getExtension() {
$Filename = $this->getFilename();
$FileExtension = strrpos($Filename, ".", 1) + 1;
if ($FileExtension != false)
return strtolower(substr($Filename, $FileExtension, strlen($Filename) - $FileExtension));
else
return "";
}
}
class PhroccoGroup {
public $extensions = array(
"php" => array("php","phps","phpt"),
"xml" => array("xml")
);
public $language = "php";
public $defaults = array(
"i" => __DIR__,
"l" => "php",
"o" => false
);
public $options = array();
public $group = array();
public function __construct($options) {
$sources = array();
$this->options = $options + $this->defaults;
$dir_iterator = new PhroccoIterator($this->options["i"]);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if(!$iterator->isDot() && in_array($iterator->getExtension(), $this->extensions[$this->options["l"]])) {
$base_path = $this->options["i"];
$rpath = str_replace($base_path, "",$file->getPath());
$phrocco = new Phrocco($this->options["l"], $file);
if(!$this->options["o"]) $output_dir = $file->getPath();
else $output_dir = $this->options["o"];
if($rpath !=$file->getPath()) $output_dir.="/".$rpath;
if(!is_writable($output_dir)) @mkdir($output_dir, 0777, true);
if(!is_writable($output_dir)) throw new Exception("Invalid Output Directory - Couldn't Create Because of Permissions");
$file_out = $output_dir."/".$file->getBasename($iterator->getExtension())."html";
$phrocco->output_file = $file_out;
$subpath = $iterator->getSubPath();
$phrocco->path = (!empty($subpath) ? "./" : '') . $subpath;
$this->group[$file->getBasename()] = $phrocco;
$subpath .= (!empty($subpath) ? '/' : '');
$this->sources[] = array(
"url"=>$subpath.$file->getBasename($iterator->getExtension())."html",
"name"=>$file->getBasename()
);
}
}
foreach($this->group as $name=>$file) {
$file->sources = $this->sources;
echo "*** Processing: ".$name."\n";
$file->render();
}
}
}