Passed
Push — master ( 308939...613a97 )
by Jakub
02:01
created

Generator::clearOutputFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\SiteGenerator;
5
6
use cebe\markdown\GithubMarkdown,
7
    Nette\Utils\Finder,
8
    Nette\Neon\Neon,
9
    Nette\Utils\FileSystem,
10
    Symfony\Component\OptionsResolver\OptionsResolver,
11
    Nette\Utils\Validators;
12
13
/**
14
 * Generator
15
 *
16
 * @author Jakub Konečný
17
 * @property string $source
18
 * @property string $output
19
 * @method void onBeforeGenerate()
20
 * @method void onAfterGenerate()
21
 */
22 1
class Generator {
23 1
  use \Nette\SmartObject;
24
  
25
  /** @var string */
26
  protected $source;
27
  /** @var string */
28
  protected $output;
29
  /** @var string[] */
30
  protected $assets = [];
31
  /** @var callable[] */
32
  protected $metaNormalizers = [];
33
  /** @var callable[] */
34
  public $onBeforeGenerate = [];
35
  /** @var callable[] */
36
  public $onAfterGenerate = [];
37
  
38
  public function __construct(string $source, string $output) {
39 1
    $this->setSource($source);
40 1
    FileSystem::createDir($output);
41 1
    $this->setOutput($output);
42 1
    $this->onBeforeGenerate[] = [$this, "clearOutputFolder"];
43 1
    $this->onAfterGenerate[] = [$this, "copyAssets"];
44 1
    $this->addMetaNormalizer([$this, "normalizeTitle"]);
45 1
    $this->addMetaNormalizer([$this, "normalizeStyles"]);
46 1
    $this->addMetaNormalizer([$this, "normalizeScripts"]);
47 1
  }
48
  
49
  public function addMetaNormalizer(callable $callback): void {
50 1
    $this->metaNormalizers[] = $callback;
51 1
  }
52
  
53
  public function getSource(): string {
54 1
    return $this->source;
55
  }
56
  
57
  public function setSource(string $source) {
58 1
    if(is_dir($source)) {
59 1
      $this->source = realpath($source);
60
    }
61 1
  }
62
  
63
  public function getOutput(): string {
64 1
    return $this->output;
65
  }
66
  
67
  public function setOutput(string $output) {
68 1
    $this->output = realpath($output);
69 1
  }
70
  
71
  protected function getMeta(string $filename, string &$html): array {
72 1
    $resolver = new OptionsResolver();
73 1
    $resolver->setDefaults([
74 1
      "title" => "",
75
      "styles" => [],
76
      "scripts" => [],
77
    ]);
78 1
    $isArrayOfStrings = function(array $value) {
79 1
      return Validators::everyIs($value, "string");
80
    };
81 1
    $resolver->setAllowedTypes("title", "string");
82 1
    $resolver->setAllowedTypes("styles", "array");
83 1
    $resolver->setAllowedValues("styles", $isArrayOfStrings);
84 1
    $resolver->setAllowedTypes("scripts", "array");
85 1
    $resolver->setAllowedValues("scripts", $isArrayOfStrings);
86 1
    $metaFilename = str_replace(".md", ".neon", $filename);
87 1
    $meta = [];
88 1
    if(file_exists($metaFilename)) {
89 1
      $meta = Neon::decode(file_get_contents($metaFilename));
90
    }
91 1
    $result = $resolver->resolve($meta);
92 1
    foreach($this->metaNormalizers as $normalizer) {
93 1
      $normalizer($result, $html, $filename);
94
    }
95 1
    return $result;
96
  }
97
  
98
  protected function addAsset(string $asset): void {
99 1
    if(!in_array($asset, $this->assets)) {
100 1
      $this->assets[] = realpath($asset);
101
    }
102 1
  }
103
  
104
  protected function normalizeTitle(array &$meta, string &$html, string $filename): void {
2 ignored issues
show
introduced by
The method parameter $filename is never used
Loading history...
introduced by
Line exceeds 80 characters; contains 90 characters
Loading history...
Unused Code introduced by
The parameter $filename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105 1
    if(strlen($meta["title"]) === 0) {
106 1
      unset($meta["title"]);
107 1
      $html = str_replace("
108 1
  <title>%%title%%</title>", "", $html);
109
    }
110 1
  }
111
  
112 View Code Duplication
  protected function normalizeStyles(array &$meta, string &$html, string $filename): void {
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 91 characters
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113 1
    $basePath = dirname($filename);
114 1
    $meta["styles"] = array_filter($meta["styles"], function($value) use($basePath) {
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 85 characters
Loading history...
115 1
      return file_exists("$basePath/$value");
116 1
    });
117 1
    if(!count($meta["styles"])) {
118 1
      unset($meta["styles"]);
119 1
      $html = str_replace("
120 1
  %%styles%%", "", $html);
121 1
      return;
122
    }
123 1
    array_walk($meta["styles"], function(&$value) use($basePath) {
124 1
      $this->addAsset("$basePath/$value");
125 1
      $value = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$value\">";
126 1
    });
127 1
    $meta["styles"] = implode("\n  ", $meta["styles"]);
128 1
  }
129
  
130 View Code Duplication
  protected function normalizeScripts(array &$meta, string &$html, string $filename): void {
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 92 characters
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131 1
    $basePath = dirname($filename);
132 1
    $meta["scripts"] = array_filter($meta["scripts"], function($value) use($basePath) {
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 87 characters
Loading history...
133 1
      return file_exists("$basePath/$value");
134 1
    });
135 1
    if(!count($meta["scripts"])) {
136 1
      unset($meta["scripts"]);
137 1
      $html = str_replace("
138 1
  %%scripts%%", "", $html);
139 1
      return;
140
    }
141 1
    array_walk($meta["scripts"], function(&$value) use($basePath) {
142 1
      $this->addAsset("$basePath/$value");
143 1
      $value = "<script type=\"text/javascript\" src=\"$value\"></script>";
144 1
    });
145 1
    $meta["scripts"] = implode("\n  ", $meta["scripts"]);
146 1
  }
147
  
148
  protected function createHtml(string $filename): string {
149 1
    $parser = new GithubMarkdown();
150 1
    $parser->html5 = $parser->keepListStartNumber = $parser->enableNewlines = true;
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 83 characters
Loading history...
151 1
    $source = $parser->parse(file_get_contents($filename));
152 1
    $html = file_get_contents(__DIR__ . "/template.html");
153 1
    if(substr($source, -1) === PHP_EOL) {
154 1
      $source = substr($source, 0, -1);
155
    }
156 1
    $html = str_replace("%%source%%", $source, $html);
157 1
    return $html;
158
  }
159
  
160
  /**
161
   * @internal
162
   */
163
  public function clearOutputFolder(): void {
164 1
    FileSystem::delete($this->output);
165 1
  }
166
  
167
  /**
168
   * @internal
169
   */
170
  public function copyAssets(): void {
171 1
    foreach($this->assets as $asset) {
172 1
      $path = str_replace($this->source, "", $asset);
173 1
      $target = "$this->output$path";
174 1
      FileSystem::copy($asset, $target);
175 1
      echo "Copied $path";
176
    }
177 1
  }
178
  
179
  /**
180
   * Generate the site
181
   */
182
  public function generate(): void {
183 1
    $this->onBeforeGenerate();
184 1
    $files = Finder::findFiles("*.md")
185 1
      ->exclude("README.md")
186 1
      ->from($this->source)
187 1
      ->exclude("vendor", ".git", "tests");
188
    /** @var \SplFileInfo $file */
189 1
    foreach($files as $file) {
190 1
      $path = str_replace($this->source, "", dirname($file->getRealPath()));
191 1
      $html = $this->createHtml($file->getRealPath());
192 1
      $meta = $this->getMeta($file->getRealPath(), $html);
193 1
      foreach($meta as $key => $value) {
194 1
        $html = str_replace("%%$key%%", $value, $html);
195
      }
196 1
      $basename = $file->getBasename(".md") . ".html";
197 1
      $filename = "$this->output$path/$basename";
198 1
      FileSystem::write($filename, $html);
199 1
      echo "Created $path/$basename\n";
200
    }
201 1
    $this->onAfterGenerate();
202 1
  }
203
}
204
?>