|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace SymfonyDocs; |
| 4 | + |
| 5 | +use Doctrine\RST\HTML\Document; |
| 6 | +use Doctrine\RST\HTML\Environment; |
| 7 | +use Doctrine\RST\MetaEntry; |
| 8 | +use Symfony\Component\DomCrawler\Crawler; |
| 9 | +use Symfony\Component\Filesystem\Filesystem; |
| 10 | +use Symfony\Component\Finder\Finder; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class JsonGenerator |
| 14 | + */ |
| 15 | +class JsonGenerator |
| 16 | +{ |
| 17 | + /** @var Environment[] */ |
| 18 | + private $environments; |
| 19 | + |
| 20 | + public function __construct(array $documents) |
| 21 | + { |
| 22 | + $this->environments = array_map( |
| 23 | + function (Document $document) { |
| 24 | + return $document->getEnvironment(); |
| 25 | + }, |
| 26 | + $documents |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + public function generateJson(string $inputDir, string $outputDir) |
| 31 | + { |
| 32 | + $finder = new Finder(); |
| 33 | + $finder->in($inputDir) |
| 34 | + ->name('*.html') |
| 35 | + ->files(); |
| 36 | + |
| 37 | + $fs = new Filesystem(); |
| 38 | + $fs->remove($outputDir); |
| 39 | + |
| 40 | + foreach ($finder as $file) { |
| 41 | + $crawler = new Crawler($file->getContents()); |
| 42 | + |
| 43 | + $parserFilename = $this->getParserFilename($file->getRealPath(), $inputDir); |
| 44 | + $meta = $this->getMeta($parserFilename); |
| 45 | + |
| 46 | + $data = [ |
| 47 | + 'body' => $crawler->filter('body')->html(), |
| 48 | + 'title' => $meta->getTitle(), |
| 49 | + 'current_page_name' => $parserFilename, |
| 50 | + 'toc' => $this->generateToc($meta, current($meta->getTitles())[1]), |
| 51 | + 'next' => $this->guessNext($parserFilename), |
| 52 | + 'prev' => $this->guessPrev($parserFilename), |
| 53 | + 'rellinks' => [ |
| 54 | + $this->guessNext($parserFilename), |
| 55 | + $this->guessPrev($parserFilename), |
| 56 | + ], |
| 57 | + ]; |
| 58 | + |
| 59 | + $fs->dumpFile( |
| 60 | + str_replace([$inputDir, '.html'], [$outputDir, '.json'], $file->getRealPath()), |
| 61 | + json_encode($data, JSON_PRETTY_PRINT) |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private function getParserFilename(string $filePath, string $inputDir): string |
| 67 | + { |
| 68 | + return $parserFilename = str_replace([$inputDir.'/', '.html'], ['', ''], $filePath); |
| 69 | + } |
| 70 | + |
| 71 | + private function getEnvironment(string $parserFilename): Environment |
| 72 | + { |
| 73 | + if (!isset($this->environments[$parserFilename])) { |
| 74 | + throw new \LogicException(sprintf('Cannot guess file name in parser "%s"', $parserFilename)); |
| 75 | + } |
| 76 | + |
| 77 | + return $this->environments[$parserFilename]; |
| 78 | + } |
| 79 | + |
| 80 | + private function getMeta(string $parserFilename): MetaEntry |
| 81 | + { |
| 82 | + $environment = $this->getEnvironment($parserFilename); |
| 83 | + |
| 84 | + $allMetas = $environment->getMetas()->getAll(); |
| 85 | + |
| 86 | + if (!isset($allMetas[$parserFilename])) { |
| 87 | + throw new \LogicException(sprintf('Cannot find metas for file "%s"', $parserFilename)); |
| 88 | + } |
| 89 | + |
| 90 | + return $allMetas[$parserFilename]; |
| 91 | + } |
| 92 | + |
| 93 | + private function generateToc(MetaEntry $metaEntry, array $titles): array |
| 94 | + { |
| 95 | + $tocTree = []; |
| 96 | + |
| 97 | + foreach ($titles as $title) { |
| 98 | + $tocTree[] = [ |
| 99 | + 'url' => sprintf('%s#%s', $metaEntry->getUrl(), Environment::slugify($title[0])), |
| 100 | + 'title' => $title[0], |
| 101 | + 'children' => $this->generateToc($metaEntry, $title[1]), |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + return $tocTree; |
| 106 | + } |
| 107 | + |
| 108 | + private function guessNext(string $parserFilename): ?array |
| 109 | + { |
| 110 | + list($toc, $indexCurrentFile) = $this->getNextPrevInformation($parserFilename); |
| 111 | + |
| 112 | + if (!isset($toc[$indexCurrentFile + 1])) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + return [ |
| 117 | + 'title' => $this->getMeta($toc[$indexCurrentFile + 1])->getTitle(), |
| 118 | + 'link' => $this->getMeta($toc[$indexCurrentFile + 1])->getUrl(), |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + private function guessPrev(string $parserFilename): ?array |
| 123 | + { |
| 124 | + list($toc, $indexCurrentFile) = $this->getNextPrevInformation($parserFilename); |
| 125 | + |
| 126 | + if (!isset($toc[$indexCurrentFile - 1])) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + return [ |
| 131 | + 'title' => $this->getMeta($toc[$indexCurrentFile - 1])->getTitle(), |
| 132 | + 'link' => $this->getMeta($toc[$indexCurrentFile - 1])->getUrl(), |
| 133 | + ]; |
| 134 | + } |
| 135 | + |
| 136 | + private function getNextPrevInformation(string $parserFilename): ?array |
| 137 | + { |
| 138 | + $meta = $this->getMeta($parserFilename); |
| 139 | + $parentFile = $meta->getParent(); |
| 140 | + |
| 141 | + if (!$parentFile) { |
| 142 | + return [null, null]; |
| 143 | + } |
| 144 | + |
| 145 | + $metaParent = $this->getMeta($parentFile); |
| 146 | + |
| 147 | + if (!$metaParent->getTocs() || \count($metaParent->getTocs()) !== 1) { |
| 148 | + return [null, null]; |
| 149 | + } |
| 150 | + |
| 151 | + $toc = current($metaParent->getTocs()); |
| 152 | + |
| 153 | + if (\count($toc) < 2 || !isset(array_flip($toc)[$parserFilename])) { |
| 154 | + return [null, null]; |
| 155 | + } |
| 156 | + |
| 157 | + $indexCurrentFile = array_flip($toc)[$parserFilename]; |
| 158 | + |
| 159 | + return [$toc, $indexCurrentFile]; |
| 160 | + } |
| 161 | +} |
0 commit comments