| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Cecil/Cecil package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Cecil\Step; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cecil\Collection\Page\Page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cecil\Converter\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Cecil\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Converts content of all pages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class PagesConvert extends AbstractStep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return 'Converting pages'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function init($options) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         /** @var \Cecil\Builder $builder */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         if (is_dir($this->builder->getConfig()->getContentPath())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->canProcess = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function process() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (count($this->builder->getPages()) <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($this->builder->getBuildOptions()['drafts']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $message = 'drafts included'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->builder->getLogger()->info($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $max = count($this->builder->getPages()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         /** @var Page $page */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         foreach ($this->builder->getPages() as $page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if (!$page->isVirtual()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $count++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     $convertedPage = $this->convertPage($page, (string) $this->config->get('frontmatter.format')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     $this->builder->getPages()->remove($page->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     $message = sprintf('Unable to convert front matter of page "%s"', $page->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $this->builder->getLogger()->error($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     $message = sprintf('Page "%s": %s', $page->getId(), $e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $this->builder->getLogger()->debug($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                  * Apply a specific path to pages of a section. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                  * ie: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                  * sections: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                  * - name: thesection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                  *   path: newpath/:slug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 if (is_array($this->config->get('sections'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     foreach ($this->config->get('sections') as $section) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                         if (array_key_exists('name', $section)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                             /** @var Page $page */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                             if ($page->getSection() == Page::slugify($section['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                                 if (array_key_exists('path', $section)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                                     var_dump($page->getVariables()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                                     $path = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                                         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                                             ':year', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                                             ':month', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                                             ':day', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                                             ':section', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                                             ':title', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                                             ':slug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                                         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                                             $page->getVariable('date')->format('Y'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                                             $page->getVariable('date')->format('m'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                                             $page->getVariable('date')->format('d'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                                             $page->getSection(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                                             $page->getSlug(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                                             $page->getSlug(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                                         $section['path'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                                     $page->setPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $message = $page->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 // forces drafts convert? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 if ($this->builder->getBuildOptions()['drafts']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $page->setVariable('published', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 if ($page->getVariable('published')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     $this->builder->getPages()->replace($page->getId(), $convertedPage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     $this->builder->getPages()->remove($page->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $message .= ' (not published)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Converts page content: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * - Yaml frontmatter to PHP array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * - Markdown body to HTML. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param Page   $page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param string $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return Page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     public function convertPage(Page $page, $format = 'yaml'): Page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         // converts frontmatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         if ($page->getFrontmatter()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 $variables = Converter::convertFrontmatter($page->getFrontmatter(), $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 throw new Exception($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             $page->setVariables($variables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         // converts body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $html = Converter::convertBody($page->getBody(), $this->builder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $page->setBodyHtml($html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |  |