示例#1
0
 public function generate(Page $page)
 {
     $template = $this->blazon->getTwig()->loadTemplate($page->getSrc());
     $site = $this->blazon->getSite();
     $data = ['site' => $site, 'page' => $page];
     $output = $template->render($data);
     file_put_contents($page->getBaseDir() . '/' . $page->getName() . '.html', $output);
 }
示例#2
0
 public function init(Page $page)
 {
     $data = file_get_contents($this->blazon->getSrc() . '/' . $page->getSrc());
     $doc = FrontMatterParser::parse($data);
     $config = $doc->getConfig();
     foreach ($config as $key => $value) {
         $page->setProperty($key, $value);
     }
     /*
     if (isset($config['layout'])) {
         $page->setLayout($config['layout']);
     }
     */
     $this->content = $doc->getContent();
 }
示例#3
0
文件: Blazon.php 项目: linkorb/blazon
 public function load()
 {
     $this->site = new Site();
     $parser = new YamlParser();
     $config = $parser->parse(file_get_contents($this->src . '/blazon.yml'));
     if (!$this->dest) {
         if (isset($config['dest'])) {
             $dest = rtrim($config['dest'], '/');
             if ($dest[0] != '/') {
                 $this->dest = $this->src . '/' . $dest;
             } else {
                 $this->dest = $dest;
             }
         }
         if (!$this->dest) {
             $this->dest = dirname($this->filename) . '/build';
         }
     }
     if (isset($config['src'])) {
         $src = rtrim($config['src'], '/');
         if ($src[0] != '/') {
             $this->src .= '/' . $src;
         } else {
             $this->src = $src;
         }
     }
     if (isset($config['site']['properties'])) {
         foreach ($config['site']['properties'] as $key => $value) {
             $this->site->setProperty($key, $value);
         }
     }
     $pageDirs = array();
     if (isset($config['pages'])) {
         $pagePathPattern = sprintf('@^(.+)%s([^%s]*)$@', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
         foreach ($config['pages'] as $name => $pageNode) {
             $page = new Page($name, $pageNode);
             // handle directory structure in the page 'name'
             $pathMatches = array();
             if (preg_match($pagePathPattern, $name, $pathMatches)) {
                 $pageDirs[] = $pathMatches[1];
                 $page->setBaseDir($this->dest . DIRECTORY_SEPARATOR . $pathMatches[1]);
                 $page->setName($pathMatches[2]);
             } else {
                 $page->setBaseDir($this->dest);
             }
             $handler = null;
             if (isset($pageNode['src'])) {
                 $pageSrc = $this->src . '/' . $pageNode['src'];
                 if (!file_exists($pageSrc)) {
                     throw new RuntimeException("Page src does not exist: " . $pageSrc);
                 }
                 $page->setSrc($pageNode['src']);
             }
             if (isset($pageNode['layout'])) {
                 $page->setLayout($pageNode['layout']);
             }
             if (isset($pageNode['handler'])) {
                 $handlerClassName = $pageNode['handler'];
                 $handler = new $handlerClassName($this);
             }
             if (!$handler) {
                 if ($page->getSrc() == '') {
                     throw new RuntimeException("No handler and no src specified for page: " . $page->getName());
                 }
                 // guess handler based on file-extension
                 if (substr($pageSrc, -3) == '.md') {
                     $handler = new \Blazon\Handler\MarkdownHandler($this);
                 }
                 if (substr($pageSrc, -5) == '.twig') {
                     $handler = new \Blazon\Handler\TwigHandler($this);
                 }
                 if (substr($pageSrc, -5) == '.html') {
                     $handler = new \Blazon\Handler\HtmlHandler($this);
                 }
             }
             if (!$handler) {
                 throw new RuntimeException("No handler for src file of page " . $page->getName());
             }
             $page->setHandler($handler);
             $handler->init($page, $config);
             if (isset($pageNode['properties'])) {
                 foreach ($pageNode['properties'] as $key => $value) {
                     $page->setProperty($key, $value);
                 }
             }
             if (isset($pageNode['title'])) {
                 $page->setTitle($pageNode['title']);
             }
             $this->addPage($page);
         }
     }
     if (!file_exists($this->src)) {
         throw new RuntimeException("Source directory does not exist: " . $this->src);
     }
     if (!file_exists($this->dest)) {
         mkdir($this->dest, 0755);
     }
     $this->makeOutputDirectories($pageDirs, $this->dest, 0755);
     $loader = new \Twig_Loader_Filesystem($this->src);
     $loader->addPath($this->src . '/templates', 'Templates');
     $this->twig = new \Twig_Environment($loader, []);
     $filter = new \Twig_SimpleFilter('urlsafe_command_name', function (\Twig_Environment $env, $string) {
         // get the current charset for instance
         $string = str_replace(':', '__', $string);
         return $string;
     }, array('needs_environment' => true));
     $this->twig->addFilter($filter);
     return $this;
 }