bee.php (6326B)
1 <?php 2 error_reporting( E_ALL ); 3 4 // Include external dependencies. 5 require('./parsedown/ParseDown.php'); // Markdown 6 require('./spyc/Spyc.php'); // YAML 7 8 require('./gen/GeneratorBase.class.php'); 9 require('./gen/DateGenerator.class.php'); 10 require('./gen/CategoryGenerator.class.php'); 11 require('./gen/HtmlGenerator.class.php'); 12 require('./gen/IndexGenerator.class.php'); 13 require('./gen/KeywordGenerator.class.php'); 14 require('./gen/MarkdownPageGenerator.class.php'); 15 require('./gen/StaticFileGenerator.class.php'); 16 require('./gen/ThemeResourceGenerator.class.php'); 17 require('./util/HttpUtil.class.php'); 18 require('./SiteReader.class.php'); 19 20 21 function init() 22 { 23 define('DEVELOPMENT',true); 24 define('HTML_EXTENSION','.html'); 25 26 $site = array( 27 'title'=>'My new Blog', 28 'keywords_dir'=>'tags', 29 'category_dir'=>'category', 30 'sites_dir'=>'site', 31 'theme_dir'=>'theme', 32 'html_extension'=>false, 33 'http_caching'=>true, 34 'theme'=>'default' 35 ); 36 37 define('SCRIPT',basename($_SERVER['SCRIPT_FILENAME'])); 38 39 define('SITES_DIR','../site'); 40 define('THEME_DIR','../theme'); 41 define('SLASH' ,'/'); 42 43 global $querypath; 44 $querypath = explode('/',$_SERVER['PATH_INFO']); 45 46 define('ROOT_UP',str_repeat('../',substr_count($_SERVER['PATH_INFO'],'/'))); 47 define('SITE_UP',str_repeat('../',substr_count($_SERVER['PATH_INFO'],'/')-3)); 48 49 array_shift($querypath); // Remove first slash 50 define('CMD' ,array_shift($querypath)); 51 define('SITE' ,array_shift($querypath)); 52 } 53 54 init(); 55 56 57 58 if (is_null(CMD)) 59 { 60 $options = array('Preview'=>ROOT_UP.SCRIPT.'/preview/'); 61 output( 'Select an option',$options); 62 63 } 64 elseif (CMD=='preview') 65 { 66 if ( is_null(SITE) || SITE=='') 67 { 68 $sitelinks = array(); 69 if ( $handle = opendir(SITES_DIR.SLASH.SITE.SLASH.implode('/',PATH)) ) 70 { 71 while (false !== ($entry = readdir($handle))) { 72 if ( $entry[0] != '.' ) 73 { 74 $sitelinks[$entry] = ROOT_UP.SCRIPT.'/preview/'.$entry.'/'; 75 } 76 } 77 closedir($handle); 78 } 79 output( 'Select a site for previewing',$sitelinks); 80 } 81 else 82 { 83 if (!is_dir(SITES_DIR.SLASH.SITE)) 84 { 85 HttpUtil::sendStatus(404,"Site ".SITE." not found"); // Site does not exist 86 } 87 88 if ( is_file(SITES_DIR.SLASH.SITE.SLASH.'site-config.ini')) 89 { 90 $siteconfig = parse_ini_file(SITES_DIR.SLASH.SITE.SLASH.'site-config.ini'); 91 if ( isset($siteconfig['site_dir'])) 92 define('SITE_DIR',$siteconfig['site_dir']); 93 else 94 define('SITE_DIR',SITES_DIR.SLASH.SITE); 95 96 if ( isset($siteconfig['theme'])) 97 define('THEME',$siteconfig['theme']); 98 else 99 define('THEME','default'); 100 101 if ( isset($siteconfig['locale'])) 102 setlocale(LC_ALL,$siteconfig['locale']); 103 } 104 else 105 { 106 define('THEME','default'); 107 define('SITE_DIR',SITES_DIR.SLASH.SITE); 108 } 109 110 if ( !is_file(THEME_DIR.SLASH.THEME.'.php') ) 111 HttpUtil::sendStatus(501,'Internal Server Error: Theme '.THEME.' not found'); // Theme does not exist 112 113 // global used arrays. 114 $PAGES_BY_URL = array(); 115 $PAGES_BY_DATE = array(); 116 $PAGES_BY_KEYWORD = array(); 117 $PAGES_BY_CATEGORY = array(); 118 $PAGES_RELATED = array(); 119 $CATEGORY_NAMES = array(); 120 $KEYWORD_NAMES = array(); 121 $PAGE = null; 122 123 // read all pages from file system. 124 125 readSite(); 126 $filename = implode(SLASH,$querypath); 127 128 define('PATH',$querypath); 129 $generator = null; 130 131 if (count($querypath)==2 && $querypath[0]=='tag' ) 132 { 133 if ( !isset($querypath[1])) 134 HttpUtil::sendStatus(410,'Missing keyword name'); // Pfad '/tag' ohne weiteres 135 136 137 $generator = new KeywordGenerator( $querypath[1] ); 138 } 139 140 elseif (count($querypath)==2 && $querypath[0]=='category' ) 141 { 142 if ( !isset($querypath[1])) 143 HttpUtil::sendStatus(410,'Missing category name'); // Pfad '/category' ohne weiteres 144 145 $generator = new CategoryGenerator( $querypath[1] ); 146 } 147 148 elseif (count($querypath)>=1 && strlen($querypath[0])==4 && is_numeric(($querypath[0])) ) 149 { 150 // By date 151 $year = intval($querypath[0]); 152 if ( isset($querypath[1]) ) 153 { 154 $month = intval($querypath[1]); 155 $toYear = $year; 156 157 if ( isset($querypath[2]) ) 158 { 159 // by day 160 $day = intval($querypath[2]); 161 $toMonth = $month; 162 $toDay = $day+1; 163 } 164 else 165 { 166 // by month 167 $day = 1; 168 $toDay = 1; 169 $toMonth = $month+1; 170 } 171 172 } 173 else 174 { 175 // By year 176 $day = 1; 177 $toDay = 1; 178 $month = 1; 179 $toMonth = 1; 180 $toYear = $year+1; 181 } 182 183 $from = mktime(0,0,0,$month,$day,$year); 184 $to = mktime(0,0,0,$toMonth,$toDay,$toYear); 185 $generator = new DateGenerator( $from,$to-1 ); 186 187 } 188 189 190 // Directory-Listing 191 elseif ( empty($filename)) 192 { 193 $generator = new IndexGenerator(); 194 } 195 196 // Static file from theme 197 elseif (is_file(THEME_DIR.SLASH.THEME.SLASH.$filename)) 198 { 199 $generator = new ThemeResourceGenerator($filename); 200 } 201 202 // Static file from site 203 elseif (is_file(SITE_DIR.SLASH.$filename)) 204 { 205 $generator = new StaticFileGenerator($filename); 206 } 207 elseif (isset($PAGES_BY_URL[$filename])) 208 { 209 $generator = new MarkdownPageGenerator( $PAGES_BY_URL[$filename] ); 210 } 211 elseif (is_file(SITES_DIR.SLASH.SITE.SLASH.$filename.'.html')) 212 { 213 $generator = new HtmlGenerator( $filename ); 214 } 215 else { 216 HttpUtil::sendStatus(404,'Resource not Found'); 217 } 218 219 $generator->generate(); 220 221 } 222 } 223 else { 224 HttpUtil::sendStatus(400,'Unknown command'); 225 } 226 227 228 229 230 231 232 233 234 235 236 function readSite() 237 { 238 global $PAGES_BY_URL; 239 global $PAGES_BY_DATE; 240 global $PAGES_BY_KEYWORD; 241 global $PAGES_BY_CATEGORY; 242 global $KEYWORD_NAMES; 243 global $CATEGORY_NAMES; 244 245 $reader = new SiteReader(); 246 $reader->readSite(); 247 248 $PAGES_BY_URL = $reader->pagesByUrl; 249 $PAGES_BY_DATE = $reader->pagesByDate; 250 $PAGES_BY_KEYWORD = $reader->pagesByKeyword; 251 $PAGES_BY_CATEGORY = $reader->pagesByCategory; 252 253 $KEYWORD_NAMES = $reader->keywordNames; 254 $CATEGORY_NAMES = $reader->categoryNames; 255 256 257 } 258 259 260 function output( $text, $options ) 261 { 262 echo '<html><head><meta charset="utf-8"><title>'.$text.' - Bee Static Site Generator</title>'; 263 echo '<style>'; 264 readfile('bee.css'); 265 echo '</style>'; 266 echo '</head>'; 267 echo '<body>'; 268 echo '<h1>'.$text.'</h1><ul>'; 269 foreach( $options as $option=>$url ) 270 { 271 echo '<li><a href="'.$url.'">'.$option.'</a></li>'; 272 } 273 echo '</ul></body>'; 274 echo '</html>'; 275 } 276 277 ?>