@@ -14,7 +14,7 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @param \Htsl\ReadingBuffer\Line $line |
| 16 | 16 | * |
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 17 | + * @return CodeEmbedment |
|
| 18 | 18 | */ |
| 19 | 19 | public function parseLine(Line $line):parent |
| 20 | 20 | { |
@@ -9,23 +9,23 @@ |
||
| 9 | 9 | |
| 10 | 10 | class CodeEmbedment extends AEmbedment |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Parsing line. |
|
| 14 | - * |
|
| 15 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | - * |
|
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 18 | - */ |
|
| 19 | - public function parseLine(Line $line):parent |
|
| 20 | - { |
|
| 21 | - $content = '<code>'.htmlentities($line->fullContent).'</code>'; |
|
| 12 | + /** |
|
| 13 | + * Parsing line. |
|
| 14 | + * |
|
| 15 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | + * |
|
| 17 | + * @return \Htsl\Embedment\Contracts |
|
| 18 | + */ |
|
| 19 | + public function parseLine(Line $line):parent |
|
| 20 | + { |
|
| 21 | + $content = '<code>'.htmlentities($line->fullContent).'</code>'; |
|
| 22 | 22 | |
| 23 | - $indentation = $this->document->indentation; |
|
| 23 | + $indentation = $this->document->indentation; |
|
| 24 | 24 | |
| 25 | - false !== $indentation and $content = str_repeat($indentation, $this->document->indentLevel).$content."\n"; |
|
| 25 | + false !== $indentation and $content = str_repeat($indentation, $this->document->indentLevel).$content."\n"; |
|
| 26 | 26 | |
| 27 | - $this->content .= $content; |
|
| 27 | + $this->content .= $content; |
|
| 28 | 28 | |
| 29 | - return $this; |
|
| 30 | - } |
|
| 29 | + return $this; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -16,15 +16,15 @@ |
||
| 16 | 16 | * |
| 17 | 17 | * @return \Htsl\Embedment\Contracts |
| 18 | 18 | */ |
| 19 | - public function parseLine(Line $line):parent |
|
| 19 | + public function parseLine( Line $line ):parent |
|
| 20 | 20 | { |
| 21 | - $content = '<code>'.htmlentities($line->fullContent).'</code>'; |
|
| 21 | + $content='<code>'.htmlentities($line->fullContent).'</code>'; |
|
| 22 | 22 | |
| 23 | - $indentation = $this->document->indentation; |
|
| 23 | + $indentation=$this->document->indentation; |
|
| 24 | 24 | |
| 25 | - false !== $indentation and $content = str_repeat($indentation, $this->document->indentLevel).$content."\n"; |
|
| 25 | + false!==$indentation and $content=str_repeat($indentation, $this->document->indentLevel).$content."\n"; |
|
| 26 | 26 | |
| 27 | - $this->content .= $content; |
|
| 27 | + $this->content.=$content; |
|
| 28 | 28 | |
| 29 | 29 | return $this; |
| 30 | 30 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @param \Htsl\ReadingBuffer\Line $line |
| 16 | 16 | * |
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 17 | + * @return CssEmbedment |
|
| 18 | 18 | */ |
| 19 | 19 | public function parseLine(Line $line):parent |
| 20 | 20 | { |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | |
| 10 | 10 | class CssEmbedment extends AEmbedment |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Parsing line. |
|
| 14 | - * |
|
| 15 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | - * |
|
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 18 | - */ |
|
| 19 | - public function parseLine(Line $line):parent |
|
| 20 | - { |
|
| 21 | - $this->content .= $line->content; |
|
| 12 | + /** |
|
| 13 | + * Parsing line. |
|
| 14 | + * |
|
| 15 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | + * |
|
| 17 | + * @return \Htsl\Embedment\Contracts |
|
| 18 | + */ |
|
| 19 | + public function parseLine(Line $line):parent |
|
| 20 | + { |
|
| 21 | + $this->content .= $line->content; |
|
| 22 | 22 | |
| 23 | - return $this; |
|
| 24 | - } |
|
| 23 | + return $this; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -16,9 +16,9 @@ |
||
| 16 | 16 | * |
| 17 | 17 | * @return \Htsl\Embedment\Contracts |
| 18 | 18 | */ |
| 19 | - public function parseLine(Line $line):parent |
|
| 19 | + public function parseLine( Line $line ):parent |
|
| 20 | 20 | { |
| 21 | - $this->content .= $line->content; |
|
| 21 | + $this->content.=$line->content; |
|
| 22 | 22 | |
| 23 | 23 | return $this; |
| 24 | 24 | } |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | * |
| 23 | 23 | * @param \Htsl\ReadingBuffer\Line $line |
| 24 | 24 | * |
| 25 | - * @return \Htsl\Embedment\Contracts |
|
| 25 | + * @return JsEmbedment |
|
| 26 | 26 | */ |
| 27 | 27 | public function parseLine(Line $line):parent |
| 28 | 28 | { |
@@ -9,25 +9,25 @@ |
||
| 9 | 9 | |
| 10 | 10 | class JsEmbedment extends AEmbedment |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Real constructor. |
|
| 14 | - */ |
|
| 15 | - protected function construct() |
|
| 16 | - { |
|
| 17 | - $this->content .= "\n"; |
|
| 18 | - } |
|
| 12 | + /** |
|
| 13 | + * Real constructor. |
|
| 14 | + */ |
|
| 15 | + protected function construct() |
|
| 16 | + { |
|
| 17 | + $this->content .= "\n"; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Parsing line. |
|
| 22 | - * |
|
| 23 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 24 | - * |
|
| 25 | - * @return \Htsl\Embedment\Contracts |
|
| 26 | - */ |
|
| 27 | - public function parseLine(Line $line):parent |
|
| 28 | - { |
|
| 29 | - $this->content .= $line->fullContent."\n"; |
|
| 20 | + /** |
|
| 21 | + * Parsing line. |
|
| 22 | + * |
|
| 23 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 24 | + * |
|
| 25 | + * @return \Htsl\Embedment\Contracts |
|
| 26 | + */ |
|
| 27 | + public function parseLine(Line $line):parent |
|
| 28 | + { |
|
| 29 | + $this->content .= $line->fullContent."\n"; |
|
| 30 | 30 | |
| 31 | - return $this; |
|
| 32 | - } |
|
| 31 | + return $this; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | protected function construct() |
| 16 | 16 | { |
| 17 | - $this->content .= "\n"; |
|
| 17 | + $this->content.="\n"; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
@@ -24,9 +24,9 @@ discard block |
||
| 24 | 24 | * |
| 25 | 25 | * @return \Htsl\Embedment\Contracts |
| 26 | 26 | */ |
| 27 | - public function parseLine(Line $line):parent |
|
| 27 | + public function parseLine( Line $line ):parent |
|
| 28 | 28 | { |
| 29 | - $this->content .= $line->fullContent."\n"; |
|
| 29 | + $this->content.=$line->fullContent."\n"; |
|
| 30 | 30 | |
| 31 | 31 | return $this; |
| 32 | 32 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @param \Htsl\ReadingBuffer\Line $line |
| 16 | 16 | * |
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 17 | + * @return PhpEmbedment |
|
| 18 | 18 | */ |
| 19 | 19 | public function parseLine(Line $line):parent |
| 20 | 20 | { |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | |
| 10 | 10 | class PhpEmbedment extends AEmbedment |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Parsing line. |
|
| 14 | - * |
|
| 15 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | - * |
|
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 18 | - */ |
|
| 19 | - public function parseLine(Line $line):parent |
|
| 20 | - { |
|
| 21 | - $this->content .= $line->content; |
|
| 12 | + /** |
|
| 13 | + * Parsing line. |
|
| 14 | + * |
|
| 15 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | + * |
|
| 17 | + * @return \Htsl\Embedment\Contracts |
|
| 18 | + */ |
|
| 19 | + public function parseLine(Line $line):parent |
|
| 20 | + { |
|
| 21 | + $this->content .= $line->content; |
|
| 22 | 22 | |
| 23 | - return $this; |
|
| 24 | - } |
|
| 23 | + return $this; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -16,9 +16,9 @@ |
||
| 16 | 16 | * |
| 17 | 17 | * @return \Htsl\Embedment\Contracts |
| 18 | 18 | */ |
| 19 | - public function parseLine(Line $line):parent |
|
| 19 | + public function parseLine( Line $line ):parent |
|
| 20 | 20 | { |
| 21 | - $this->content .= $line->content; |
|
| 21 | + $this->content.=$line->content; |
|
| 22 | 22 | |
| 23 | 23 | return $this; |
| 24 | 24 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | * |
| 15 | 15 | * @param \Htsl\ReadingBuffer\Line $line |
| 16 | 16 | * |
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 17 | + * @return TextEmbedment |
|
| 18 | 18 | */ |
| 19 | 19 | public function parseLine(Line $line):parent |
| 20 | 20 | { |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | |
| 10 | 10 | class TextEmbedment extends AEmbedment |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Parsing line. |
|
| 14 | - * |
|
| 15 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | - * |
|
| 17 | - * @return \Htsl\Embedment\Contracts |
|
| 18 | - */ |
|
| 19 | - public function parseLine(Line $line):parent |
|
| 20 | - { |
|
| 21 | - $this->content .= $line->fullContent."\n"; |
|
| 12 | + /** |
|
| 13 | + * Parsing line. |
|
| 14 | + * |
|
| 15 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 16 | + * |
|
| 17 | + * @return \Htsl\Embedment\Contracts |
|
| 18 | + */ |
|
| 19 | + public function parseLine(Line $line):parent |
|
| 20 | + { |
|
| 21 | + $this->content .= $line->fullContent."\n"; |
|
| 22 | 22 | |
| 23 | - return $this; |
|
| 24 | - } |
|
| 23 | + return $this; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -16,9 +16,9 @@ |
||
| 16 | 16 | * |
| 17 | 17 | * @return \Htsl\Embedment\Contracts |
| 18 | 18 | */ |
| 19 | - public function parseLine(Line $line):parent |
|
| 19 | + public function parseLine( Line $line ):parent |
|
| 20 | 20 | { |
| 21 | - $this->content .= $line->fullContent."\n"; |
|
| 21 | + $this->content.=$line->fullContent."\n"; |
|
| 22 | 22 | |
| 23 | 23 | return $this; |
| 24 | 24 | } |
@@ -123,7 +123,7 @@ |
||
| 123 | 123 | /** |
| 124 | 124 | * Getting the config of Htsl. |
| 125 | 125 | * |
| 126 | - * @param string $key |
|
| 126 | + * @param string $keys |
|
| 127 | 127 | * |
| 128 | 128 | * @return mixed |
| 129 | 129 | */ |
@@ -13,189 +13,189 @@ |
||
| 13 | 13 | |
| 14 | 14 | class Htsl implements IConfigProvider |
| 15 | 15 | { |
| 16 | - use Helper\TGetter; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Getter of file content. |
|
| 20 | - * |
|
| 21 | - * @var callable |
|
| 22 | - */ |
|
| 23 | - protected $fileGetter; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The base path. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $basePath; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Constructor of HTSL. |
|
| 34 | - * |
|
| 35 | - * @param array $config |
|
| 36 | - */ |
|
| 37 | - public function __construct(array $config = []) |
|
| 38 | - { |
|
| 39 | - $this->config = array_replace_recursive($this->getDefaultConfigs(), $config); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Parsing a HTSL code string into HTML or PHP code string. |
|
| 44 | - * |
|
| 45 | - * @param string $content |
|
| 46 | - * |
|
| 47 | - * @return string |
|
| 48 | - */ |
|
| 49 | - public function parse(string $content):string |
|
| 50 | - { |
|
| 51 | - return $this->execute(new StringBuffer($this, $content)); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Compiling a HTSL file into a HTML or PHP file. |
|
| 56 | - * |
|
| 57 | - * @param string $fromFile |
|
| 58 | - * @param string $toFile |
|
| 59 | - * |
|
| 60 | - * @return int|string |
|
| 61 | - */ |
|
| 62 | - public function compile(string $fromFile, string $toFile = null) |
|
| 63 | - { |
|
| 64 | - $fromFile = $this->getFilePath($fromFile); |
|
| 65 | - |
|
| 66 | - $result = $this->execute(new FileBuffer($this, $fromFile)); |
|
| 67 | - |
|
| 68 | - if ($toFile) { |
|
| 69 | - return file_put_contents($toFile, $result); |
|
| 70 | - } else { |
|
| 71 | - return $result; |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Setting the file getter. |
|
| 77 | - * |
|
| 78 | - * @param callable $fileGetter |
|
| 79 | - */ |
|
| 80 | - public function setFileGetter(callable $fileGetter):self |
|
| 81 | - { |
|
| 82 | - $this->fileGetter = $fileGetter; |
|
| 83 | - |
|
| 84 | - return $this; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Setting the base path of the HTSL project to parse. |
|
| 89 | - * |
|
| 90 | - * @param string $basePath |
|
| 91 | - * |
|
| 92 | - * @return self |
|
| 93 | - */ |
|
| 94 | - public function setBasePath(string $basePath):self |
|
| 95 | - { |
|
| 96 | - $this->basePath = '/' === substr($basePath, -1) ? substr($basePath, 0, -1) : $basePath; |
|
| 97 | - |
|
| 98 | - return $this; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Returning whether the debug model is on or off. |
|
| 103 | - * |
|
| 104 | - * @return bool |
|
| 105 | - */ |
|
| 106 | - public function isDebug():bool |
|
| 107 | - { |
|
| 108 | - return (bool) $this->getConfig('debug'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Executing the parsing. |
|
| 113 | - * |
|
| 114 | - * @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - protected function execute(ABuffer $buffer):string |
|
| 119 | - { |
|
| 120 | - return (new Document($this, $buffer))->content; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Getting the config of Htsl. |
|
| 125 | - * |
|
| 126 | - * @param string $key |
|
| 127 | - * |
|
| 128 | - * @return mixed |
|
| 129 | - */ |
|
| 130 | - public function getConfig(string ...$keys) |
|
| 131 | - { |
|
| 132 | - $result = $this->config; |
|
| 133 | - |
|
| 134 | - foreach ($keys as $key) { |
|
| 135 | - if (!isset($result[$key])) { |
|
| 136 | - return; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - $result = $result[$key]; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $result; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Getting the real file path of the HTSL file by relative path. |
|
| 147 | - * |
|
| 148 | - * @param string $filePath |
|
| 149 | - * @param string|null $path |
|
| 150 | - * |
|
| 151 | - * @return string |
|
| 152 | - */ |
|
| 153 | - public function getFilePath(string $filePath, string $path = null):string |
|
| 154 | - { |
|
| 155 | - if (!isset($this->basePath)) { |
|
| 156 | - throw new \Exception('BasePath musbe set.'); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (!strlen($filePath)) { |
|
| 160 | - throw new \Exception('FilePath cannot be empty.'); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - if ('/' === $filePath[0]) { |
|
| 164 | - if (is_null($path)) { |
|
| 165 | - return $filePath; |
|
| 166 | - } else { |
|
| 167 | - return $this->basePath.$filePath; |
|
| 168 | - } |
|
| 169 | - } else { |
|
| 170 | - if (!strlen($path)) { |
|
| 171 | - return $this->basePath.'/'.$filePath; |
|
| 172 | - } elseif ('/' === substr($path, -1)) { |
|
| 173 | - return $path.$filePath; |
|
| 174 | - } else { |
|
| 175 | - return $path.'/'.$filePath; |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Getting the content of file. |
|
| 182 | - * |
|
| 183 | - * @param string $filePath |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - public function getFileContent(string $filePath):string |
|
| 188 | - { |
|
| 189 | - return isset($this->fileGetter) ? $this->fileGetter($filePath) : file_get_contents($filePath); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Getting default config. |
|
| 194 | - * |
|
| 195 | - * @return array |
|
| 196 | - */ |
|
| 197 | - private function getDefaultConfigs():array |
|
| 198 | - { |
|
| 199 | - return DefaultConfigs::get(); |
|
| 200 | - } |
|
| 16 | + use Helper\TGetter; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Getter of file content. |
|
| 20 | + * |
|
| 21 | + * @var callable |
|
| 22 | + */ |
|
| 23 | + protected $fileGetter; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The base path. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $basePath; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Constructor of HTSL. |
|
| 34 | + * |
|
| 35 | + * @param array $config |
|
| 36 | + */ |
|
| 37 | + public function __construct(array $config = []) |
|
| 38 | + { |
|
| 39 | + $this->config = array_replace_recursive($this->getDefaultConfigs(), $config); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Parsing a HTSL code string into HTML or PHP code string. |
|
| 44 | + * |
|
| 45 | + * @param string $content |
|
| 46 | + * |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 49 | + public function parse(string $content):string |
|
| 50 | + { |
|
| 51 | + return $this->execute(new StringBuffer($this, $content)); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Compiling a HTSL file into a HTML or PHP file. |
|
| 56 | + * |
|
| 57 | + * @param string $fromFile |
|
| 58 | + * @param string $toFile |
|
| 59 | + * |
|
| 60 | + * @return int|string |
|
| 61 | + */ |
|
| 62 | + public function compile(string $fromFile, string $toFile = null) |
|
| 63 | + { |
|
| 64 | + $fromFile = $this->getFilePath($fromFile); |
|
| 65 | + |
|
| 66 | + $result = $this->execute(new FileBuffer($this, $fromFile)); |
|
| 67 | + |
|
| 68 | + if ($toFile) { |
|
| 69 | + return file_put_contents($toFile, $result); |
|
| 70 | + } else { |
|
| 71 | + return $result; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Setting the file getter. |
|
| 77 | + * |
|
| 78 | + * @param callable $fileGetter |
|
| 79 | + */ |
|
| 80 | + public function setFileGetter(callable $fileGetter):self |
|
| 81 | + { |
|
| 82 | + $this->fileGetter = $fileGetter; |
|
| 83 | + |
|
| 84 | + return $this; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Setting the base path of the HTSL project to parse. |
|
| 89 | + * |
|
| 90 | + * @param string $basePath |
|
| 91 | + * |
|
| 92 | + * @return self |
|
| 93 | + */ |
|
| 94 | + public function setBasePath(string $basePath):self |
|
| 95 | + { |
|
| 96 | + $this->basePath = '/' === substr($basePath, -1) ? substr($basePath, 0, -1) : $basePath; |
|
| 97 | + |
|
| 98 | + return $this; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Returning whether the debug model is on or off. |
|
| 103 | + * |
|
| 104 | + * @return bool |
|
| 105 | + */ |
|
| 106 | + public function isDebug():bool |
|
| 107 | + { |
|
| 108 | + return (bool) $this->getConfig('debug'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Executing the parsing. |
|
| 113 | + * |
|
| 114 | + * @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + protected function execute(ABuffer $buffer):string |
|
| 119 | + { |
|
| 120 | + return (new Document($this, $buffer))->content; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Getting the config of Htsl. |
|
| 125 | + * |
|
| 126 | + * @param string $key |
|
| 127 | + * |
|
| 128 | + * @return mixed |
|
| 129 | + */ |
|
| 130 | + public function getConfig(string ...$keys) |
|
| 131 | + { |
|
| 132 | + $result = $this->config; |
|
| 133 | + |
|
| 134 | + foreach ($keys as $key) { |
|
| 135 | + if (!isset($result[$key])) { |
|
| 136 | + return; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + $result = $result[$key]; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $result; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Getting the real file path of the HTSL file by relative path. |
|
| 147 | + * |
|
| 148 | + * @param string $filePath |
|
| 149 | + * @param string|null $path |
|
| 150 | + * |
|
| 151 | + * @return string |
|
| 152 | + */ |
|
| 153 | + public function getFilePath(string $filePath, string $path = null):string |
|
| 154 | + { |
|
| 155 | + if (!isset($this->basePath)) { |
|
| 156 | + throw new \Exception('BasePath musbe set.'); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (!strlen($filePath)) { |
|
| 160 | + throw new \Exception('FilePath cannot be empty.'); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + if ('/' === $filePath[0]) { |
|
| 164 | + if (is_null($path)) { |
|
| 165 | + return $filePath; |
|
| 166 | + } else { |
|
| 167 | + return $this->basePath.$filePath; |
|
| 168 | + } |
|
| 169 | + } else { |
|
| 170 | + if (!strlen($path)) { |
|
| 171 | + return $this->basePath.'/'.$filePath; |
|
| 172 | + } elseif ('/' === substr($path, -1)) { |
|
| 173 | + return $path.$filePath; |
|
| 174 | + } else { |
|
| 175 | + return $path.'/'.$filePath; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Getting the content of file. |
|
| 182 | + * |
|
| 183 | + * @param string $filePath |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + public function getFileContent(string $filePath):string |
|
| 188 | + { |
|
| 189 | + return isset($this->fileGetter) ? $this->fileGetter($filePath) : file_get_contents($filePath); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Getting default config. |
|
| 194 | + * |
|
| 195 | + * @return array |
|
| 196 | + */ |
|
| 197 | + private function getDefaultConfigs():array |
|
| 198 | + { |
|
| 199 | + return DefaultConfigs::get(); |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -34,9 +34,9 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param array $config |
| 36 | 36 | */ |
| 37 | - public function __construct(array $config = []) |
|
| 37 | + public function __construct( array $config=[ ] ) |
|
| 38 | 38 | { |
| 39 | - $this->config = array_replace_recursive($this->getDefaultConfigs(), $config); |
|
| 39 | + $this->config=array_replace_recursive($this->getDefaultConfigs(), $config); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @return string |
| 48 | 48 | */ |
| 49 | - public function parse(string $content):string |
|
| 49 | + public function parse( string $content ):string |
|
| 50 | 50 | { |
| 51 | 51 | return $this->execute(new StringBuffer($this, $content)); |
| 52 | 52 | } |
@@ -59,15 +59,15 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @return int|string |
| 61 | 61 | */ |
| 62 | - public function compile(string $fromFile, string $toFile = null) |
|
| 62 | + public function compile( string $fromFile, string $toFile=null ) |
|
| 63 | 63 | { |
| 64 | - $fromFile = $this->getFilePath($fromFile); |
|
| 64 | + $fromFile=$this->getFilePath($fromFile); |
|
| 65 | 65 | |
| 66 | - $result = $this->execute(new FileBuffer($this, $fromFile)); |
|
| 66 | + $result=$this->execute(new FileBuffer($this, $fromFile)); |
|
| 67 | 67 | |
| 68 | - if ($toFile) { |
|
| 68 | + if( $toFile ){ |
|
| 69 | 69 | return file_put_contents($toFile, $result); |
| 70 | - } else { |
|
| 70 | + }else{ |
|
| 71 | 71 | return $result; |
| 72 | 72 | } |
| 73 | 73 | } |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * |
| 78 | 78 | * @param callable $fileGetter |
| 79 | 79 | */ |
| 80 | - public function setFileGetter(callable $fileGetter):self |
|
| 80 | + public function setFileGetter( callable $fileGetter ):self |
|
| 81 | 81 | { |
| 82 | - $this->fileGetter = $fileGetter; |
|
| 82 | + $this->fileGetter=$fileGetter; |
|
| 83 | 83 | |
| 84 | 84 | return $this; |
| 85 | 85 | } |
@@ -91,9 +91,9 @@ discard block |
||
| 91 | 91 | * |
| 92 | 92 | * @return self |
| 93 | 93 | */ |
| 94 | - public function setBasePath(string $basePath):self |
|
| 94 | + public function setBasePath( string $basePath ):self |
|
| 95 | 95 | { |
| 96 | - $this->basePath = '/' === substr($basePath, -1) ? substr($basePath, 0, -1) : $basePath; |
|
| 96 | + $this->basePath='/'===substr($basePath, -1)? substr($basePath, 0, -1) : $basePath; |
|
| 97 | 97 | |
| 98 | 98 | return $this; |
| 99 | 99 | } |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | */ |
| 106 | 106 | public function isDebug():bool |
| 107 | 107 | { |
| 108 | - return (bool) $this->getConfig('debug'); |
|
| 108 | + return (bool)$this->getConfig('debug'); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | * |
| 116 | 116 | * @return string |
| 117 | 117 | */ |
| 118 | - protected function execute(ABuffer $buffer):string |
|
| 118 | + protected function execute( ABuffer $buffer ):string |
|
| 119 | 119 | { |
| 120 | 120 | return (new Document($this, $buffer))->content; |
| 121 | 121 | } |
@@ -127,16 +127,16 @@ discard block |
||
| 127 | 127 | * |
| 128 | 128 | * @return mixed |
| 129 | 129 | */ |
| 130 | - public function getConfig(string ...$keys) |
|
| 130 | + public function getConfig( string ...$keys ) |
|
| 131 | 131 | { |
| 132 | - $result = $this->config; |
|
| 132 | + $result=$this->config; |
|
| 133 | 133 | |
| 134 | - foreach ($keys as $key) { |
|
| 135 | - if (!isset($result[$key])) { |
|
| 134 | + foreach( $keys as $key ){ |
|
| 135 | + if( !isset($result[ $key ]) ){ |
|
| 136 | 136 | return; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - $result = $result[$key]; |
|
| 139 | + $result=$result[ $key ]; |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | return $result; |
@@ -150,28 +150,28 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return string |
| 152 | 152 | */ |
| 153 | - public function getFilePath(string $filePath, string $path = null):string |
|
| 153 | + public function getFilePath( string $filePath, string $path=null ):string |
|
| 154 | 154 | { |
| 155 | - if (!isset($this->basePath)) { |
|
| 155 | + if( !isset($this->basePath) ){ |
|
| 156 | 156 | throw new \Exception('BasePath musbe set.'); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if (!strlen($filePath)) { |
|
| 159 | + if( !strlen($filePath) ){ |
|
| 160 | 160 | throw new \Exception('FilePath cannot be empty.'); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - if ('/' === $filePath[0]) { |
|
| 164 | - if (is_null($path)) { |
|
| 163 | + if( '/'===$filePath[ 0 ] ){ |
|
| 164 | + if( is_null($path) ){ |
|
| 165 | 165 | return $filePath; |
| 166 | - } else { |
|
| 166 | + }else{ |
|
| 167 | 167 | return $this->basePath.$filePath; |
| 168 | 168 | } |
| 169 | - } else { |
|
| 170 | - if (!strlen($path)) { |
|
| 169 | + }else{ |
|
| 170 | + if( !strlen($path) ){ |
|
| 171 | 171 | return $this->basePath.'/'.$filePath; |
| 172 | - } elseif ('/' === substr($path, -1)) { |
|
| 172 | + } elseif( '/'===substr($path, -1) ){ |
|
| 173 | 173 | return $path.$filePath; |
| 174 | - } else { |
|
| 174 | + }else{ |
|
| 175 | 175 | return $path.'/'.$filePath; |
| 176 | 176 | } |
| 177 | 177 | } |
@@ -184,9 +184,9 @@ discard block |
||
| 184 | 184 | * |
| 185 | 185 | * @return string |
| 186 | 186 | */ |
| 187 | - public function getFileContent(string $filePath):string |
|
| 187 | + public function getFileContent( string $filePath ):string |
|
| 188 | 188 | { |
| 189 | - return isset($this->fileGetter) ? $this->fileGetter($filePath) : file_get_contents($filePath); |
|
| 189 | + return isset($this->fileGetter)? $this->fileGetter($filePath) : file_get_contents($filePath); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** |
@@ -65,9 +65,9 @@ discard block |
||
| 65 | 65 | |
| 66 | 66 | $result = $this->execute(new FileBuffer($this, $fromFile)); |
| 67 | 67 | |
| 68 | - if ($toFile) { |
|
| 68 | + if ($toFile){ |
|
| 69 | 69 | return file_put_contents($toFile, $result); |
| 70 | - } else { |
|
| 70 | + } else{ |
|
| 71 | 71 | return $result; |
| 72 | 72 | } |
| 73 | 73 | } |
@@ -131,8 +131,8 @@ discard block |
||
| 131 | 131 | { |
| 132 | 132 | $result = $this->config; |
| 133 | 133 | |
| 134 | - foreach ($keys as $key) { |
|
| 135 | - if (!isset($result[$key])) { |
|
| 134 | + foreach ($keys as $key){ |
|
| 135 | + if (!isset($result[$key])){ |
|
| 136 | 136 | return; |
| 137 | 137 | } |
| 138 | 138 | |
@@ -152,26 +152,26 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public function getFilePath(string $filePath, string $path = null):string |
| 154 | 154 | { |
| 155 | - if (!isset($this->basePath)) { |
|
| 155 | + if (!isset($this->basePath)){ |
|
| 156 | 156 | throw new \Exception('BasePath musbe set.'); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if (!strlen($filePath)) { |
|
| 159 | + if (!strlen($filePath)){ |
|
| 160 | 160 | throw new \Exception('FilePath cannot be empty.'); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - if ('/' === $filePath[0]) { |
|
| 164 | - if (is_null($path)) { |
|
| 163 | + if ('/' === $filePath[0]){ |
|
| 164 | + if (is_null($path)){ |
|
| 165 | 165 | return $filePath; |
| 166 | - } else { |
|
| 166 | + } else{ |
|
| 167 | 167 | return $this->basePath.$filePath; |
| 168 | 168 | } |
| 169 | - } else { |
|
| 170 | - if (!strlen($path)) { |
|
| 169 | + } else{ |
|
| 170 | + if (!strlen($path)){ |
|
| 171 | 171 | return $this->basePath.'/'.$filePath; |
| 172 | - } elseif ('/' === substr($path, -1)) { |
|
| 172 | + } elseif ('/' === substr($path, -1)){ |
|
| 173 | 173 | return $path.$filePath; |
| 174 | - } else { |
|
| 174 | + } else{ |
|
| 175 | 175 | return $path.'/'.$filePath; |
| 176 | 176 | } |
| 177 | 177 | } |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | /** |
| 173 | 173 | * Alias of getContent. |
| 174 | 174 | * |
| 175 | - * @return sting |
|
| 175 | + * @return string |
|
| 176 | 176 | */ |
| 177 | 177 | public function __toString():string |
| 178 | 178 | { |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @param [ string, ] ...$keys |
| 214 | 214 | * |
| 215 | - * @return mixed |
|
| 215 | + * @return string |
|
| 216 | 216 | */ |
| 217 | 217 | public function getConfig(string ...$keys) |
| 218 | 218 | { |
@@ -19,871 +19,871 @@ |
||
| 19 | 19 | |
| 20 | 20 | class Document implements IConfigProvider |
| 21 | 21 | { |
| 22 | - use TGetter; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Htsl main object owns this document. |
|
| 26 | - * |
|
| 27 | - * @var \Htsl\Htsl |
|
| 28 | - */ |
|
| 29 | - private $htsl; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Parent document. |
|
| 33 | - * |
|
| 34 | - * @var \Htsl\Parser\Document |
|
| 35 | - */ |
|
| 36 | - private $parent; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Reading buffer of this document. |
|
| 40 | - * |
|
| 41 | - * @var \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 42 | - */ |
|
| 43 | - private $buffer; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The indentation and whether output with linefeed and indent. |
|
| 47 | - * |
|
| 48 | - * @var string | bool |
|
| 49 | - */ |
|
| 50 | - private $indentation; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Type of this document. |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $docType; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Current embead script. |
|
| 61 | - * |
|
| 62 | - * @var \Htsl\Embedment\Contract\Embedment |
|
| 63 | - */ |
|
| 64 | - private $embedment; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Current indent level. |
|
| 68 | - * |
|
| 69 | - * @var int |
|
| 70 | - */ |
|
| 71 | - private $level = 0; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Section indent level. |
|
| 75 | - * |
|
| 76 | - * @var int |
|
| 77 | - */ |
|
| 78 | - private $sectionLevel = 0; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Opened nodes. |
|
| 82 | - * |
|
| 83 | - * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
|
| 84 | - */ |
|
| 85 | - private $openedNodes = []; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Current scopes. |
|
| 89 | - * |
|
| 90 | - * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
|
| 91 | - */ |
|
| 92 | - private $scopes = []; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Current line number. |
|
| 96 | - * |
|
| 97 | - * @var int |
|
| 98 | - */ |
|
| 99 | - private $lineNumber = 0; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Current line. |
|
| 103 | - * |
|
| 104 | - * @var \Htsl\ReadingBuffer\Line |
|
| 105 | - */ |
|
| 106 | - private $currentLine; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Sections that can be show. |
|
| 110 | - * |
|
| 111 | - * @var [ Htsl\Parser\Section, ] |
|
| 112 | - */ |
|
| 113 | - private $sections = []; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Whether the document is executed. |
|
| 117 | - * |
|
| 118 | - * @var bool |
|
| 119 | - */ |
|
| 120 | - private $isExecuted; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Current Section. |
|
| 124 | - * |
|
| 125 | - * @var \Htsl\Parser\Section |
|
| 126 | - */ |
|
| 127 | - private $currentSection; |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * The content of this document. |
|
| 131 | - * |
|
| 132 | - * @var string |
|
| 133 | - */ |
|
| 134 | - private $content; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Constructor of the Document. |
|
| 138 | - * |
|
| 139 | - * @param \Htsl\Htsl $htsl |
|
| 140 | - * @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
|
| 141 | - * @param \Htsl\Parser\Document | null $parent |
|
| 142 | - */ |
|
| 143 | - public function __construct(Htsl $htsl, Buffer $buffer, self $parent = null) |
|
| 144 | - { |
|
| 145 | - $this->htsl = $htsl; |
|
| 146 | - $this->buffer = $buffer; |
|
| 147 | - |
|
| 148 | - if ($parent) { |
|
| 149 | - $this->parent = $parent; |
|
| 150 | - $this->docType = $parent->docType; |
|
| 151 | - $this->indentation = $parent->indentation; |
|
| 152 | - } else { |
|
| 153 | - $this->parseFirstLine(); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Executing the document. |
|
| 159 | - * |
|
| 160 | - * @return \Htsl\Parser\Document |
|
| 161 | - */ |
|
| 162 | - public function execute():self |
|
| 163 | - { |
|
| 164 | - if ($this->isExecuted) { |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return $this->lineByLine() |
|
| 169 | - ->bubbleSections(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Alias of getContent. |
|
| 174 | - * |
|
| 175 | - * @return sting |
|
| 176 | - */ |
|
| 177 | - public function __toString():string |
|
| 178 | - { |
|
| 179 | - return $this->getContent(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Getting the result of document executing. |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - protected function getContent():string |
|
| 188 | - { |
|
| 189 | - if ($this->parent) { |
|
| 190 | - return $this->execute()->parent->getContent(); |
|
| 191 | - } else { |
|
| 192 | - return $this->execute()->content; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Getting the next line. |
|
| 198 | - * |
|
| 199 | - * @return \Htsl\ReadingBuffer\Line |
|
| 200 | - */ |
|
| 201 | - protected function getLine():Line |
|
| 202 | - { |
|
| 203 | - do { |
|
| 204 | - $line = $this->buffer->getLine(); |
|
| 205 | - } while ($line->isEmpty() && $line->hasMore()); |
|
| 206 | - |
|
| 207 | - return $line; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Getting the config of type of this document. |
|
| 212 | - * |
|
| 213 | - * @param [ string, ] ...$keys |
|
| 214 | - * |
|
| 215 | - * @return mixed |
|
| 216 | - */ |
|
| 217 | - public function getConfig(string ...$keys) |
|
| 218 | - { |
|
| 219 | - return $this->htsl->getConfig(array_shift($keys), $this->docType, ...$keys); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Getting the type of this document. |
|
| 224 | - * |
|
| 225 | - * @return string |
|
| 226 | - */ |
|
| 227 | - public function getDoctype():string |
|
| 228 | - { |
|
| 229 | - return $this->docType; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Getting the indentation. |
|
| 234 | - * |
|
| 235 | - * @return string | bool |
|
| 236 | - */ |
|
| 237 | - public function getIndentation() |
|
| 238 | - { |
|
| 239 | - return $this->indentation; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Getting the indent level. |
|
| 244 | - * |
|
| 245 | - * @return int |
|
| 246 | - */ |
|
| 247 | - public function getIndentLevel():int |
|
| 248 | - { |
|
| 249 | - return $this->level; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Parsing the first line. |
|
| 254 | - * |
|
| 255 | - * @return \Htsl\Parser\Document |
|
| 256 | - */ |
|
| 257 | - protected function parseFirstLine():self |
|
| 258 | - { |
|
| 259 | - $line = $this->getLine(); |
|
| 260 | - |
|
| 261 | - if ('@' === $line->getChar(0)) { |
|
| 262 | - return $this->setExtending($line); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - $this->docType = $line->content; |
|
| 266 | - $docTypeContent = $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
|
| 267 | - |
|
| 268 | - $this->indentation = $this->htsl->getConfig('indentation', $this->docType) ?? (function ($scalarOrFalse) { |
|
| 269 | - return is_scalar($scalarOrFalse) ? $scalarOrFalse : false; |
|
| 270 | - })($this->htsl->getConfig('indentation')); |
|
| 271 | - |
|
| 272 | - $this->appendLine($docTypeContent); |
|
| 273 | - |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Setting that this document extends another document. |
|
| 279 | - * |
|
| 280 | - * @param \Htsl\ReadingBuffer\Line $firstLine |
|
| 281 | - */ |
|
| 282 | - protected function setExtending(Line $firstLine):self |
|
| 283 | - { |
|
| 284 | - switch ($name = $firstLine->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 285 | - default:{ |
|
| 286 | - $this->throw("The @$name is not supported."); |
|
| 287 | - }break; |
|
| 288 | - case 'extend':{ |
|
| 289 | - $this->extend($firstLine->pregGet('/(?<=\( ).*(?= \))/')); |
|
| 290 | - }break; |
|
| 291 | - case 'show': |
|
| 292 | - case 'include': |
|
| 293 | - case 'section':{ |
|
| 294 | - $this->throw("The @$name can not be used on first line."); |
|
| 295 | - }break; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $this; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Parsing this document line by line. |
|
| 303 | - * |
|
| 304 | - * @return \Htsl\Parser\Document |
|
| 305 | - */ |
|
| 306 | - protected function lineByLine():self |
|
| 307 | - { |
|
| 308 | - while (($line = $this->getLine())->hasMore()) { |
|
| 309 | - $this->lineNumber += 1; |
|
| 310 | - |
|
| 311 | - if ($this->embedment) { |
|
| 312 | - $this->embeddingParse($line); |
|
| 313 | - } else { |
|
| 314 | - $this->parseLine($line); |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $this->embedment and $this->breakEmbedding(); |
|
| 319 | - |
|
| 320 | - $this->closeNodes($this->level); |
|
| 321 | - |
|
| 322 | - $this->isExecuted = true; |
|
| 323 | - |
|
| 324 | - return $this; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Parsing embedded line. |
|
| 329 | - * |
|
| 330 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 331 | - * |
|
| 332 | - * @return \Htsl\Parser\Document |
|
| 333 | - */ |
|
| 334 | - protected function embeddingParse(Line $line):self |
|
| 335 | - { |
|
| 336 | - if ($line->content === '<}') { |
|
| 337 | - $this->breakEmbedding(); |
|
| 338 | - } else { |
|
| 339 | - $this->embedment->parseLine($line->getSubIndentLine()); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $this; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Starting the embedding. |
|
| 347 | - * |
|
| 348 | - * @param string $embedType |
|
| 349 | - * |
|
| 350 | - * @return \Htsl\Parser\Document |
|
| 351 | - */ |
|
| 352 | - protected function startEmbedding(string $embedType):self |
|
| 353 | - { |
|
| 354 | - $embedmentClass = '\\Htsl\\Embedment\\'.ucfirst($embedType).'Embedment'; |
|
| 355 | - class_exists($embedmentClass) or $this->throw("Embed type $embedType not exists."); |
|
| 356 | - |
|
| 357 | - $this->embedment = new $embedmentClass($this); |
|
| 358 | - |
|
| 359 | - return $this; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Ending the embedding. |
|
| 364 | - * |
|
| 365 | - * @return \Htsl\Parser\Document |
|
| 366 | - */ |
|
| 367 | - public function breakEmbedding():self |
|
| 368 | - { |
|
| 369 | - $this->append($this->embedment->getContent()); |
|
| 370 | - $this->embedment = null; |
|
| 371 | - |
|
| 372 | - return $this; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Parsing line. |
|
| 377 | - * |
|
| 378 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 379 | - * |
|
| 380 | - * @return \Htsl\Parser\Document |
|
| 381 | - */ |
|
| 382 | - protected function parseLine(Line $line):self |
|
| 383 | - { |
|
| 384 | - $this->currentLine = $line; |
|
| 385 | - $this->setLevel($line->getIndentLevel()); |
|
| 386 | - |
|
| 387 | - switch ($line->getChar(0)) { |
|
| 388 | - default:{ |
|
| 389 | - $this->parseStringLine($line); |
|
| 390 | - }break; |
|
| 391 | - case '`':{ |
|
| 392 | - if ('=' === $line->getChar(1)) { |
|
| 393 | - $this->parseExpressionHtmlLine($line); |
|
| 394 | - } else { |
|
| 395 | - $this->parseHtmlLine($line); |
|
| 396 | - } |
|
| 397 | - }break; |
|
| 398 | - case '=':{ |
|
| 399 | - $this->parseExpressionLine($line); |
|
| 400 | - }break; |
|
| 401 | - case '!':{ |
|
| 402 | - $this->parseCommentLine($line); |
|
| 403 | - }break; |
|
| 404 | - case '-':{ |
|
| 405 | - $this->parseTagLine($line); |
|
| 406 | - }break; |
|
| 407 | - case '~':{ |
|
| 408 | - $this->parseControlLine($line); |
|
| 409 | - }break; |
|
| 410 | - case '@':{ |
|
| 411 | - $this->parseDocControlLine($line); |
|
| 412 | - }break; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - return $this; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Parsing line as HTML content. |
|
| 420 | - * |
|
| 421 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 422 | - * |
|
| 423 | - * @return \Htsl\Parser\Document |
|
| 424 | - */ |
|
| 425 | - protected function parseHtmlLine(Line $line):self |
|
| 426 | - { |
|
| 427 | - $node = new StringNode($this, $line); |
|
| 428 | - |
|
| 429 | - $this->openNode($node); |
|
| 430 | - |
|
| 431 | - $this->appendLine($line->slice(1)); |
|
| 432 | - |
|
| 433 | - return $this; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Parsing line as string content. |
|
| 438 | - * |
|
| 439 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 440 | - * |
|
| 441 | - * @return \Htsl\Parser\Document |
|
| 442 | - */ |
|
| 443 | - protected function parseStringLine(Line $line):self |
|
| 444 | - { |
|
| 445 | - $node = new StringNode($this, $line); |
|
| 446 | - |
|
| 447 | - $this->openNode($node); |
|
| 448 | - |
|
| 449 | - $this->appendLine($this->htmlEntities(trim($line->getContent()))); |
|
| 450 | - |
|
| 451 | - return $this; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Parsing line as PHP expression. |
|
| 456 | - * |
|
| 457 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 458 | - * |
|
| 459 | - * @return \Htsl\Parser\Document |
|
| 460 | - */ |
|
| 461 | - protected function parseExpressionLine(Line $line):self |
|
| 462 | - { |
|
| 463 | - $node = new StringNode($this, $line); |
|
| 464 | - |
|
| 465 | - $this->openNode($node); |
|
| 466 | - |
|
| 467 | - $content = $line->slice(1); |
|
| 468 | - $ent_flag = $this->htsl->getConfig('ENT_flags', $this->docType); |
|
| 469 | - |
|
| 470 | - $this->appendLine("<?=htmlentities($content,'$ent_flag','UTF-8',false);?>"); |
|
| 471 | - |
|
| 472 | - return $this; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Parsing line as PHP expression with HTML result. |
|
| 477 | - * |
|
| 478 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 479 | - * |
|
| 480 | - * @return \Htsl\Parser\Document |
|
| 481 | - */ |
|
| 482 | - protected function parseExpressionHtmlLine(Line $line):self |
|
| 483 | - { |
|
| 484 | - $node = new StringNode($this, $line); |
|
| 485 | - |
|
| 486 | - $this->openNode($node); |
|
| 487 | - |
|
| 488 | - $content = $line->slice(1); |
|
| 489 | - |
|
| 490 | - $this->appendLine("<?$content?>"); |
|
| 491 | - |
|
| 492 | - return $this; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * Parsing line as comment. |
|
| 497 | - * |
|
| 498 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 499 | - * |
|
| 500 | - * @return \Htsl\Parser\Document |
|
| 501 | - */ |
|
| 502 | - protected function parseCommentLine(Line $line):self |
|
| 503 | - { |
|
| 504 | - $node = new CommentNode($this, $line); |
|
| 505 | - |
|
| 506 | - $this->openNode($node); |
|
| 507 | - |
|
| 508 | - $this->appendLine($node->open()); |
|
| 509 | - |
|
| 510 | - return $this; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * Parsing line as HTSL tag. |
|
| 515 | - * |
|
| 516 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 517 | - * |
|
| 518 | - * @return \Htsl\Parser\Document |
|
| 519 | - */ |
|
| 520 | - protected function parseTagLine(Line $line):self |
|
| 521 | - { |
|
| 522 | - $tag = new TagNode($this, $line); |
|
| 523 | - |
|
| 524 | - $this->appendLine($tag->open()); |
|
| 525 | - |
|
| 526 | - $tag->embed and $this->startEmbedding($tag->embed); |
|
| 527 | - |
|
| 528 | - $this->openNode($tag); |
|
| 529 | - |
|
| 530 | - return $this; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Parsing line as control node of Htsl.php. |
|
| 535 | - * |
|
| 536 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 537 | - * |
|
| 538 | - * @return \Htsl\Parser\Document |
|
| 539 | - */ |
|
| 540 | - protected function parseControlLine(Line $line):self |
|
| 541 | - { |
|
| 542 | - $controlStructure = new ControlNode($this, $line); |
|
| 543 | - |
|
| 544 | - $this->appendLine($controlStructure->open()); |
|
| 545 | - |
|
| 546 | - $this->openNode($controlStructure); |
|
| 547 | - |
|
| 548 | - return $this; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Parsing line as document control node of Htsl.php. |
|
| 553 | - * |
|
| 554 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 555 | - * |
|
| 556 | - * @return \Htsl\Parser\Document |
|
| 557 | - */ |
|
| 558 | - protected function parseDocControlLine(Line $line):self |
|
| 559 | - { |
|
| 560 | - switch ($name = $line->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 561 | - default:{ |
|
| 562 | - $this->throw("The @$name is not supported."); |
|
| 563 | - }break; |
|
| 564 | - case 'extend':{ |
|
| 565 | - $this->throw('The @extend can only be used on first line.'); |
|
| 566 | - }break; |
|
| 567 | - case 'include':{ |
|
| 568 | - $this->include($line); |
|
| 569 | - }break; |
|
| 570 | - case 'section':{ |
|
| 571 | - $this->defineSection($line); |
|
| 572 | - }break; |
|
| 573 | - case 'show':{ |
|
| 574 | - $this->showSection($line); |
|
| 575 | - }break; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - return $this; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Parsing extending defination. |
|
| 583 | - * |
|
| 584 | - * @param string $fileName |
|
| 585 | - * |
|
| 586 | - * @return \Htsl\Parser\Document |
|
| 587 | - */ |
|
| 588 | - protected function extend(string $fileName):self |
|
| 589 | - { |
|
| 590 | - $this->parent = new static($this->htsl, $this->buffer->goSide($fileName), null, $this->indentation); |
|
| 591 | - |
|
| 592 | - $this->docType = $this->parent->docType; |
|
| 593 | - $this->indentation = $this->parent->indentation; |
|
| 594 | - |
|
| 595 | - return $this; |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Include another document. |
|
| 600 | - * |
|
| 601 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 602 | - * |
|
| 603 | - * @return \Htsl\Parser\Document |
|
| 604 | - */ |
|
| 605 | - protected function include(Line $line):self |
|
| 606 | - { |
|
| 607 | - $inclued = (new static($this->htsl, $this->buffer->goSide($line->pregGet('/(?<=\( ).*(?= \))/')), $this, $this->indentation))->execute()->content; |
|
| 608 | - |
|
| 609 | - if (false !== $this->indentation) { |
|
| 610 | - $inclued = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level - $this->sectionLevel), $inclued); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - $node = new StringNode($this, $line); |
|
| 614 | - |
|
| 615 | - $this->openNode($node); |
|
| 616 | - |
|
| 617 | - $this->append($inclued); |
|
| 618 | - |
|
| 619 | - return $this; |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * Starting to define a section. |
|
| 624 | - * |
|
| 625 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 626 | - * |
|
| 627 | - * @return \Htsl\Parser\Document |
|
| 628 | - */ |
|
| 629 | - protected function defineSection(Line $line):self |
|
| 630 | - { |
|
| 631 | - $node = new SectionNode($this, $line); |
|
| 632 | - |
|
| 633 | - $node->open(); |
|
| 634 | - |
|
| 635 | - $this->openNode($node); |
|
| 636 | - |
|
| 637 | - return $this; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * Showing a section. |
|
| 642 | - * |
|
| 643 | - * @param \Htsl\ReadingBuffer\Line $line |
|
| 644 | - * |
|
| 645 | - * @return \Htsl\Parser\Document |
|
| 646 | - */ |
|
| 647 | - protected function showSection(Line $line):self |
|
| 648 | - { |
|
| 649 | - $sectionName = $line->pregGet('/(?<=\( ).*(?= \))/'); |
|
| 650 | - |
|
| 651 | - if (!isset($this->sections[$sectionName])) { |
|
| 652 | - $this->openNode(new StringNode($this, $line)); |
|
| 653 | - |
|
| 654 | - return $this; |
|
| 655 | - } |
|
| 656 | - $content = $this->sections[$sectionName]->content; |
|
| 657 | - |
|
| 658 | - if (false !== $this->indentation) { |
|
| 659 | - $content = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level), $content); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - $this->append($content); |
|
| 663 | - |
|
| 664 | - $node = new NamelessSectionNode($this, $line); |
|
| 665 | - |
|
| 666 | - $node->open(); |
|
| 667 | - |
|
| 668 | - $this->openNode($node); |
|
| 669 | - |
|
| 670 | - return $this; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Setting document as section definer. |
|
| 675 | - * |
|
| 676 | - * @param Section | null $section |
|
| 677 | - */ |
|
| 678 | - public function setSection(Section $section = null):self |
|
| 679 | - { |
|
| 680 | - if (!$section) { |
|
| 681 | - $this->sectionLevel = 0; |
|
| 682 | - $this->currentSection = null; |
|
| 683 | - |
|
| 684 | - return $this; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - if ($this->currentSection) { |
|
| 688 | - $this->throw('Nesting definition of section is forbidden.'); |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - if (isset($this->parent->sections[$section->name])) { |
|
| 692 | - $this->throw("Section $sectionName already defined."); |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - $this->currentSection = $section; |
|
| 696 | - |
|
| 697 | - if ($section->name) { |
|
| 698 | - $this->parent->sections[$section->name] = $section; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - $this->sectionLevel = $this->level + 1; |
|
| 702 | - |
|
| 703 | - return $this; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Bubble the sections to parent document. |
|
| 708 | - * |
|
| 709 | - * @return \Htsl\Parser\Document |
|
| 710 | - */ |
|
| 711 | - protected function bubbleSections():self |
|
| 712 | - { |
|
| 713 | - if ($this->parent) { |
|
| 714 | - foreach ($this->sections as $name => $section) { |
|
| 715 | - if (!isset($this->parent->sections[$name])) { |
|
| 716 | - $this->parent->sections[$name] = $section; |
|
| 717 | - } |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - return $this; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Escaping characters to HTML entities. |
|
| 726 | - * |
|
| 727 | - * @param string $input |
|
| 728 | - * |
|
| 729 | - * @return string |
|
| 730 | - */ |
|
| 731 | - public function htmlEntities(string $input):string |
|
| 732 | - { |
|
| 733 | - return htmlentities($input, $this->htsl->getConfig('ENT_flags', $this->docType), 'UTF-8', false); |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Setting indent level of this document. |
|
| 738 | - * |
|
| 739 | - * @param int $level |
|
| 740 | - */ |
|
| 741 | - protected function setLevel(int $level):self |
|
| 742 | - { |
|
| 743 | - $level -= $this->level; |
|
| 744 | - |
|
| 745 | - if ($level <= 0) { |
|
| 746 | - $this->closeNodes(-$level); |
|
| 747 | - } elseif ($level == 1) { |
|
| 748 | - $this->level += 1; |
|
| 749 | - } else { |
|
| 750 | - $this->throw('Indent error.'); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - return $this; |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Opening a node. |
|
| 758 | - * |
|
| 759 | - * @param \Htsl\Parser\Node\Contracts\ANode $node |
|
| 760 | - * |
|
| 761 | - * @return \Htsl\Parser\Document |
|
| 762 | - */ |
|
| 763 | - protected function openNode(Node $node):self |
|
| 764 | - { |
|
| 765 | - array_push($this->openedNodes, $node); |
|
| 766 | - |
|
| 767 | - $node->scope and $this->setScope($node); |
|
| 768 | - |
|
| 769 | - return $this; |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - /** |
|
| 773 | - * Closing open node or nodes. |
|
| 774 | - * |
|
| 775 | - * @param int $level |
|
| 776 | - * |
|
| 777 | - * @return \Htsl\Parser\Document |
|
| 778 | - */ |
|
| 779 | - protected function closeNodes(int $level = 0):self |
|
| 780 | - { |
|
| 781 | - if (empty($this->openedNodes)) { |
|
| 782 | - return $this; |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - while ($level-- >= 0) { |
|
| 786 | - $node = array_pop($this->openedNodes); |
|
| 787 | - |
|
| 788 | - $node->scope and $this->removeScope($node); |
|
| 789 | - |
|
| 790 | - $closer = $node->close($this->currentLine) and $this->appendLine($closer); |
|
| 791 | - |
|
| 792 | - $this->level -= $level >= 0 ? 1 : 0; |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - return $this; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * Pushing a scope to stack. |
|
| 800 | - * |
|
| 801 | - * @param \Htsl\Parser\Node\Contracts\ANode $scope |
|
| 802 | - */ |
|
| 803 | - protected function setScope(Node $scope):int |
|
| 804 | - { |
|
| 805 | - return array_unshift($this->scopes, $scope); |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - /** |
|
| 809 | - * Getting current scope on top of stack. |
|
| 810 | - * |
|
| 811 | - * @return \Htsl\Parser\Node\Contracts\ANode | null |
|
| 812 | - */ |
|
| 813 | - public function getScope() |
|
| 814 | - { |
|
| 815 | - return $this->scopes[0] ?? null; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * Pop a scope from stack. |
|
| 820 | - * |
|
| 821 | - * @param \Htsl\Parser\Node\Contracts\ANode $scope |
|
| 822 | - * |
|
| 823 | - * @return \Htsl\Parser\Document |
|
| 824 | - */ |
|
| 825 | - protected function removeScope(Node $scope):self |
|
| 826 | - { |
|
| 827 | - if ($scope !== array_shift($this->scopes)) { |
|
| 828 | - $this->throw('Scope nesting error'); |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - return $this; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - /** |
|
| 835 | - * Appending a line of content to parsing result. |
|
| 836 | - * |
|
| 837 | - * @param string $content |
|
| 838 | - * |
|
| 839 | - * @return \Htsl\Parser\Document |
|
| 840 | - */ |
|
| 841 | - protected function appendLine(string $content):self |
|
| 842 | - { |
|
| 843 | - if (false !== $this->indentation) { |
|
| 844 | - $content = str_repeat($this->indentation, $this->level - $this->sectionLevel).$content."\n"; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - return $this->append($content); |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - /** |
|
| 851 | - * Appending some content to parsing result. |
|
| 852 | - * |
|
| 853 | - * @param string $content |
|
| 854 | - * |
|
| 855 | - * @return \Htsl\Parser\Document |
|
| 856 | - */ |
|
| 857 | - protected function append(string $content):self |
|
| 858 | - { |
|
| 859 | - if ($this->currentSection) { |
|
| 860 | - $this->currentSection->append($content); |
|
| 861 | - } else { |
|
| 862 | - $this->content .= $content; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - return $this; |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - /** |
|
| 869 | - * Getting the Htsl main object. |
|
| 870 | - * |
|
| 871 | - * @return \Htsl\Htsl |
|
| 872 | - */ |
|
| 873 | - public function getHtsl() |
|
| 874 | - { |
|
| 875 | - return $this->htsl; |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - /** |
|
| 879 | - * Throw exception with document name and line number. |
|
| 880 | - * |
|
| 881 | - * @param string $message |
|
| 882 | - * |
|
| 883 | - * @throw \Htsl\Parser\HtslParsingException |
|
| 884 | - */ |
|
| 885 | - public function throw(string $message) |
|
| 886 | - { |
|
| 887 | - throw new HtslParsingException("$message at file {$this->buffer->fileName} line $this->lineNumber"); |
|
| 888 | - } |
|
| 22 | + use TGetter; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Htsl main object owns this document. |
|
| 26 | + * |
|
| 27 | + * @var \Htsl\Htsl |
|
| 28 | + */ |
|
| 29 | + private $htsl; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Parent document. |
|
| 33 | + * |
|
| 34 | + * @var \Htsl\Parser\Document |
|
| 35 | + */ |
|
| 36 | + private $parent; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Reading buffer of this document. |
|
| 40 | + * |
|
| 41 | + * @var \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 42 | + */ |
|
| 43 | + private $buffer; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The indentation and whether output with linefeed and indent. |
|
| 47 | + * |
|
| 48 | + * @var string | bool |
|
| 49 | + */ |
|
| 50 | + private $indentation; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Type of this document. |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $docType; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Current embead script. |
|
| 61 | + * |
|
| 62 | + * @var \Htsl\Embedment\Contract\Embedment |
|
| 63 | + */ |
|
| 64 | + private $embedment; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Current indent level. |
|
| 68 | + * |
|
| 69 | + * @var int |
|
| 70 | + */ |
|
| 71 | + private $level = 0; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Section indent level. |
|
| 75 | + * |
|
| 76 | + * @var int |
|
| 77 | + */ |
|
| 78 | + private $sectionLevel = 0; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Opened nodes. |
|
| 82 | + * |
|
| 83 | + * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
|
| 84 | + */ |
|
| 85 | + private $openedNodes = []; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Current scopes. |
|
| 89 | + * |
|
| 90 | + * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
|
| 91 | + */ |
|
| 92 | + private $scopes = []; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Current line number. |
|
| 96 | + * |
|
| 97 | + * @var int |
|
| 98 | + */ |
|
| 99 | + private $lineNumber = 0; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Current line. |
|
| 103 | + * |
|
| 104 | + * @var \Htsl\ReadingBuffer\Line |
|
| 105 | + */ |
|
| 106 | + private $currentLine; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Sections that can be show. |
|
| 110 | + * |
|
| 111 | + * @var [ Htsl\Parser\Section, ] |
|
| 112 | + */ |
|
| 113 | + private $sections = []; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Whether the document is executed. |
|
| 117 | + * |
|
| 118 | + * @var bool |
|
| 119 | + */ |
|
| 120 | + private $isExecuted; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Current Section. |
|
| 124 | + * |
|
| 125 | + * @var \Htsl\Parser\Section |
|
| 126 | + */ |
|
| 127 | + private $currentSection; |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * The content of this document. |
|
| 131 | + * |
|
| 132 | + * @var string |
|
| 133 | + */ |
|
| 134 | + private $content; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Constructor of the Document. |
|
| 138 | + * |
|
| 139 | + * @param \Htsl\Htsl $htsl |
|
| 140 | + * @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
|
| 141 | + * @param \Htsl\Parser\Document | null $parent |
|
| 142 | + */ |
|
| 143 | + public function __construct(Htsl $htsl, Buffer $buffer, self $parent = null) |
|
| 144 | + { |
|
| 145 | + $this->htsl = $htsl; |
|
| 146 | + $this->buffer = $buffer; |
|
| 147 | + |
|
| 148 | + if ($parent) { |
|
| 149 | + $this->parent = $parent; |
|
| 150 | + $this->docType = $parent->docType; |
|
| 151 | + $this->indentation = $parent->indentation; |
|
| 152 | + } else { |
|
| 153 | + $this->parseFirstLine(); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Executing the document. |
|
| 159 | + * |
|
| 160 | + * @return \Htsl\Parser\Document |
|
| 161 | + */ |
|
| 162 | + public function execute():self |
|
| 163 | + { |
|
| 164 | + if ($this->isExecuted) { |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return $this->lineByLine() |
|
| 169 | + ->bubbleSections(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Alias of getContent. |
|
| 174 | + * |
|
| 175 | + * @return sting |
|
| 176 | + */ |
|
| 177 | + public function __toString():string |
|
| 178 | + { |
|
| 179 | + return $this->getContent(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Getting the result of document executing. |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + protected function getContent():string |
|
| 188 | + { |
|
| 189 | + if ($this->parent) { |
|
| 190 | + return $this->execute()->parent->getContent(); |
|
| 191 | + } else { |
|
| 192 | + return $this->execute()->content; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Getting the next line. |
|
| 198 | + * |
|
| 199 | + * @return \Htsl\ReadingBuffer\Line |
|
| 200 | + */ |
|
| 201 | + protected function getLine():Line |
|
| 202 | + { |
|
| 203 | + do { |
|
| 204 | + $line = $this->buffer->getLine(); |
|
| 205 | + } while ($line->isEmpty() && $line->hasMore()); |
|
| 206 | + |
|
| 207 | + return $line; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Getting the config of type of this document. |
|
| 212 | + * |
|
| 213 | + * @param [ string, ] ...$keys |
|
| 214 | + * |
|
| 215 | + * @return mixed |
|
| 216 | + */ |
|
| 217 | + public function getConfig(string ...$keys) |
|
| 218 | + { |
|
| 219 | + return $this->htsl->getConfig(array_shift($keys), $this->docType, ...$keys); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Getting the type of this document. |
|
| 224 | + * |
|
| 225 | + * @return string |
|
| 226 | + */ |
|
| 227 | + public function getDoctype():string |
|
| 228 | + { |
|
| 229 | + return $this->docType; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Getting the indentation. |
|
| 234 | + * |
|
| 235 | + * @return string | bool |
|
| 236 | + */ |
|
| 237 | + public function getIndentation() |
|
| 238 | + { |
|
| 239 | + return $this->indentation; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Getting the indent level. |
|
| 244 | + * |
|
| 245 | + * @return int |
|
| 246 | + */ |
|
| 247 | + public function getIndentLevel():int |
|
| 248 | + { |
|
| 249 | + return $this->level; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Parsing the first line. |
|
| 254 | + * |
|
| 255 | + * @return \Htsl\Parser\Document |
|
| 256 | + */ |
|
| 257 | + protected function parseFirstLine():self |
|
| 258 | + { |
|
| 259 | + $line = $this->getLine(); |
|
| 260 | + |
|
| 261 | + if ('@' === $line->getChar(0)) { |
|
| 262 | + return $this->setExtending($line); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + $this->docType = $line->content; |
|
| 266 | + $docTypeContent = $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
|
| 267 | + |
|
| 268 | + $this->indentation = $this->htsl->getConfig('indentation', $this->docType) ?? (function ($scalarOrFalse) { |
|
| 269 | + return is_scalar($scalarOrFalse) ? $scalarOrFalse : false; |
|
| 270 | + })($this->htsl->getConfig('indentation')); |
|
| 271 | + |
|
| 272 | + $this->appendLine($docTypeContent); |
|
| 273 | + |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Setting that this document extends another document. |
|
| 279 | + * |
|
| 280 | + * @param \Htsl\ReadingBuffer\Line $firstLine |
|
| 281 | + */ |
|
| 282 | + protected function setExtending(Line $firstLine):self |
|
| 283 | + { |
|
| 284 | + switch ($name = $firstLine->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 285 | + default:{ |
|
| 286 | + $this->throw("The @$name is not supported."); |
|
| 287 | + }break; |
|
| 288 | + case 'extend':{ |
|
| 289 | + $this->extend($firstLine->pregGet('/(?<=\( ).*(?= \))/')); |
|
| 290 | + }break; |
|
| 291 | + case 'show': |
|
| 292 | + case 'include': |
|
| 293 | + case 'section':{ |
|
| 294 | + $this->throw("The @$name can not be used on first line."); |
|
| 295 | + }break; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $this; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Parsing this document line by line. |
|
| 303 | + * |
|
| 304 | + * @return \Htsl\Parser\Document |
|
| 305 | + */ |
|
| 306 | + protected function lineByLine():self |
|
| 307 | + { |
|
| 308 | + while (($line = $this->getLine())->hasMore()) { |
|
| 309 | + $this->lineNumber += 1; |
|
| 310 | + |
|
| 311 | + if ($this->embedment) { |
|
| 312 | + $this->embeddingParse($line); |
|
| 313 | + } else { |
|
| 314 | + $this->parseLine($line); |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $this->embedment and $this->breakEmbedding(); |
|
| 319 | + |
|
| 320 | + $this->closeNodes($this->level); |
|
| 321 | + |
|
| 322 | + $this->isExecuted = true; |
|
| 323 | + |
|
| 324 | + return $this; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Parsing embedded line. |
|
| 329 | + * |
|
| 330 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 331 | + * |
|
| 332 | + * @return \Htsl\Parser\Document |
|
| 333 | + */ |
|
| 334 | + protected function embeddingParse(Line $line):self |
|
| 335 | + { |
|
| 336 | + if ($line->content === '<}') { |
|
| 337 | + $this->breakEmbedding(); |
|
| 338 | + } else { |
|
| 339 | + $this->embedment->parseLine($line->getSubIndentLine()); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $this; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Starting the embedding. |
|
| 347 | + * |
|
| 348 | + * @param string $embedType |
|
| 349 | + * |
|
| 350 | + * @return \Htsl\Parser\Document |
|
| 351 | + */ |
|
| 352 | + protected function startEmbedding(string $embedType):self |
|
| 353 | + { |
|
| 354 | + $embedmentClass = '\\Htsl\\Embedment\\'.ucfirst($embedType).'Embedment'; |
|
| 355 | + class_exists($embedmentClass) or $this->throw("Embed type $embedType not exists."); |
|
| 356 | + |
|
| 357 | + $this->embedment = new $embedmentClass($this); |
|
| 358 | + |
|
| 359 | + return $this; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Ending the embedding. |
|
| 364 | + * |
|
| 365 | + * @return \Htsl\Parser\Document |
|
| 366 | + */ |
|
| 367 | + public function breakEmbedding():self |
|
| 368 | + { |
|
| 369 | + $this->append($this->embedment->getContent()); |
|
| 370 | + $this->embedment = null; |
|
| 371 | + |
|
| 372 | + return $this; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Parsing line. |
|
| 377 | + * |
|
| 378 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 379 | + * |
|
| 380 | + * @return \Htsl\Parser\Document |
|
| 381 | + */ |
|
| 382 | + protected function parseLine(Line $line):self |
|
| 383 | + { |
|
| 384 | + $this->currentLine = $line; |
|
| 385 | + $this->setLevel($line->getIndentLevel()); |
|
| 386 | + |
|
| 387 | + switch ($line->getChar(0)) { |
|
| 388 | + default:{ |
|
| 389 | + $this->parseStringLine($line); |
|
| 390 | + }break; |
|
| 391 | + case '`':{ |
|
| 392 | + if ('=' === $line->getChar(1)) { |
|
| 393 | + $this->parseExpressionHtmlLine($line); |
|
| 394 | + } else { |
|
| 395 | + $this->parseHtmlLine($line); |
|
| 396 | + } |
|
| 397 | + }break; |
|
| 398 | + case '=':{ |
|
| 399 | + $this->parseExpressionLine($line); |
|
| 400 | + }break; |
|
| 401 | + case '!':{ |
|
| 402 | + $this->parseCommentLine($line); |
|
| 403 | + }break; |
|
| 404 | + case '-':{ |
|
| 405 | + $this->parseTagLine($line); |
|
| 406 | + }break; |
|
| 407 | + case '~':{ |
|
| 408 | + $this->parseControlLine($line); |
|
| 409 | + }break; |
|
| 410 | + case '@':{ |
|
| 411 | + $this->parseDocControlLine($line); |
|
| 412 | + }break; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + return $this; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Parsing line as HTML content. |
|
| 420 | + * |
|
| 421 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 422 | + * |
|
| 423 | + * @return \Htsl\Parser\Document |
|
| 424 | + */ |
|
| 425 | + protected function parseHtmlLine(Line $line):self |
|
| 426 | + { |
|
| 427 | + $node = new StringNode($this, $line); |
|
| 428 | + |
|
| 429 | + $this->openNode($node); |
|
| 430 | + |
|
| 431 | + $this->appendLine($line->slice(1)); |
|
| 432 | + |
|
| 433 | + return $this; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Parsing line as string content. |
|
| 438 | + * |
|
| 439 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 440 | + * |
|
| 441 | + * @return \Htsl\Parser\Document |
|
| 442 | + */ |
|
| 443 | + protected function parseStringLine(Line $line):self |
|
| 444 | + { |
|
| 445 | + $node = new StringNode($this, $line); |
|
| 446 | + |
|
| 447 | + $this->openNode($node); |
|
| 448 | + |
|
| 449 | + $this->appendLine($this->htmlEntities(trim($line->getContent()))); |
|
| 450 | + |
|
| 451 | + return $this; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Parsing line as PHP expression. |
|
| 456 | + * |
|
| 457 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 458 | + * |
|
| 459 | + * @return \Htsl\Parser\Document |
|
| 460 | + */ |
|
| 461 | + protected function parseExpressionLine(Line $line):self |
|
| 462 | + { |
|
| 463 | + $node = new StringNode($this, $line); |
|
| 464 | + |
|
| 465 | + $this->openNode($node); |
|
| 466 | + |
|
| 467 | + $content = $line->slice(1); |
|
| 468 | + $ent_flag = $this->htsl->getConfig('ENT_flags', $this->docType); |
|
| 469 | + |
|
| 470 | + $this->appendLine("<?=htmlentities($content,'$ent_flag','UTF-8',false);?>"); |
|
| 471 | + |
|
| 472 | + return $this; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Parsing line as PHP expression with HTML result. |
|
| 477 | + * |
|
| 478 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 479 | + * |
|
| 480 | + * @return \Htsl\Parser\Document |
|
| 481 | + */ |
|
| 482 | + protected function parseExpressionHtmlLine(Line $line):self |
|
| 483 | + { |
|
| 484 | + $node = new StringNode($this, $line); |
|
| 485 | + |
|
| 486 | + $this->openNode($node); |
|
| 487 | + |
|
| 488 | + $content = $line->slice(1); |
|
| 489 | + |
|
| 490 | + $this->appendLine("<?$content?>"); |
|
| 491 | + |
|
| 492 | + return $this; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * Parsing line as comment. |
|
| 497 | + * |
|
| 498 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 499 | + * |
|
| 500 | + * @return \Htsl\Parser\Document |
|
| 501 | + */ |
|
| 502 | + protected function parseCommentLine(Line $line):self |
|
| 503 | + { |
|
| 504 | + $node = new CommentNode($this, $line); |
|
| 505 | + |
|
| 506 | + $this->openNode($node); |
|
| 507 | + |
|
| 508 | + $this->appendLine($node->open()); |
|
| 509 | + |
|
| 510 | + return $this; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * Parsing line as HTSL tag. |
|
| 515 | + * |
|
| 516 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 517 | + * |
|
| 518 | + * @return \Htsl\Parser\Document |
|
| 519 | + */ |
|
| 520 | + protected function parseTagLine(Line $line):self |
|
| 521 | + { |
|
| 522 | + $tag = new TagNode($this, $line); |
|
| 523 | + |
|
| 524 | + $this->appendLine($tag->open()); |
|
| 525 | + |
|
| 526 | + $tag->embed and $this->startEmbedding($tag->embed); |
|
| 527 | + |
|
| 528 | + $this->openNode($tag); |
|
| 529 | + |
|
| 530 | + return $this; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Parsing line as control node of Htsl.php. |
|
| 535 | + * |
|
| 536 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 537 | + * |
|
| 538 | + * @return \Htsl\Parser\Document |
|
| 539 | + */ |
|
| 540 | + protected function parseControlLine(Line $line):self |
|
| 541 | + { |
|
| 542 | + $controlStructure = new ControlNode($this, $line); |
|
| 543 | + |
|
| 544 | + $this->appendLine($controlStructure->open()); |
|
| 545 | + |
|
| 546 | + $this->openNode($controlStructure); |
|
| 547 | + |
|
| 548 | + return $this; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Parsing line as document control node of Htsl.php. |
|
| 553 | + * |
|
| 554 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 555 | + * |
|
| 556 | + * @return \Htsl\Parser\Document |
|
| 557 | + */ |
|
| 558 | + protected function parseDocControlLine(Line $line):self |
|
| 559 | + { |
|
| 560 | + switch ($name = $line->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 561 | + default:{ |
|
| 562 | + $this->throw("The @$name is not supported."); |
|
| 563 | + }break; |
|
| 564 | + case 'extend':{ |
|
| 565 | + $this->throw('The @extend can only be used on first line.'); |
|
| 566 | + }break; |
|
| 567 | + case 'include':{ |
|
| 568 | + $this->include($line); |
|
| 569 | + }break; |
|
| 570 | + case 'section':{ |
|
| 571 | + $this->defineSection($line); |
|
| 572 | + }break; |
|
| 573 | + case 'show':{ |
|
| 574 | + $this->showSection($line); |
|
| 575 | + }break; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + return $this; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Parsing extending defination. |
|
| 583 | + * |
|
| 584 | + * @param string $fileName |
|
| 585 | + * |
|
| 586 | + * @return \Htsl\Parser\Document |
|
| 587 | + */ |
|
| 588 | + protected function extend(string $fileName):self |
|
| 589 | + { |
|
| 590 | + $this->parent = new static($this->htsl, $this->buffer->goSide($fileName), null, $this->indentation); |
|
| 591 | + |
|
| 592 | + $this->docType = $this->parent->docType; |
|
| 593 | + $this->indentation = $this->parent->indentation; |
|
| 594 | + |
|
| 595 | + return $this; |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Include another document. |
|
| 600 | + * |
|
| 601 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 602 | + * |
|
| 603 | + * @return \Htsl\Parser\Document |
|
| 604 | + */ |
|
| 605 | + protected function include(Line $line):self |
|
| 606 | + { |
|
| 607 | + $inclued = (new static($this->htsl, $this->buffer->goSide($line->pregGet('/(?<=\( ).*(?= \))/')), $this, $this->indentation))->execute()->content; |
|
| 608 | + |
|
| 609 | + if (false !== $this->indentation) { |
|
| 610 | + $inclued = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level - $this->sectionLevel), $inclued); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + $node = new StringNode($this, $line); |
|
| 614 | + |
|
| 615 | + $this->openNode($node); |
|
| 616 | + |
|
| 617 | + $this->append($inclued); |
|
| 618 | + |
|
| 619 | + return $this; |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * Starting to define a section. |
|
| 624 | + * |
|
| 625 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 626 | + * |
|
| 627 | + * @return \Htsl\Parser\Document |
|
| 628 | + */ |
|
| 629 | + protected function defineSection(Line $line):self |
|
| 630 | + { |
|
| 631 | + $node = new SectionNode($this, $line); |
|
| 632 | + |
|
| 633 | + $node->open(); |
|
| 634 | + |
|
| 635 | + $this->openNode($node); |
|
| 636 | + |
|
| 637 | + return $this; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * Showing a section. |
|
| 642 | + * |
|
| 643 | + * @param \Htsl\ReadingBuffer\Line $line |
|
| 644 | + * |
|
| 645 | + * @return \Htsl\Parser\Document |
|
| 646 | + */ |
|
| 647 | + protected function showSection(Line $line):self |
|
| 648 | + { |
|
| 649 | + $sectionName = $line->pregGet('/(?<=\( ).*(?= \))/'); |
|
| 650 | + |
|
| 651 | + if (!isset($this->sections[$sectionName])) { |
|
| 652 | + $this->openNode(new StringNode($this, $line)); |
|
| 653 | + |
|
| 654 | + return $this; |
|
| 655 | + } |
|
| 656 | + $content = $this->sections[$sectionName]->content; |
|
| 657 | + |
|
| 658 | + if (false !== $this->indentation) { |
|
| 659 | + $content = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level), $content); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + $this->append($content); |
|
| 663 | + |
|
| 664 | + $node = new NamelessSectionNode($this, $line); |
|
| 665 | + |
|
| 666 | + $node->open(); |
|
| 667 | + |
|
| 668 | + $this->openNode($node); |
|
| 669 | + |
|
| 670 | + return $this; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Setting document as section definer. |
|
| 675 | + * |
|
| 676 | + * @param Section | null $section |
|
| 677 | + */ |
|
| 678 | + public function setSection(Section $section = null):self |
|
| 679 | + { |
|
| 680 | + if (!$section) { |
|
| 681 | + $this->sectionLevel = 0; |
|
| 682 | + $this->currentSection = null; |
|
| 683 | + |
|
| 684 | + return $this; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + if ($this->currentSection) { |
|
| 688 | + $this->throw('Nesting definition of section is forbidden.'); |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + if (isset($this->parent->sections[$section->name])) { |
|
| 692 | + $this->throw("Section $sectionName already defined."); |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + $this->currentSection = $section; |
|
| 696 | + |
|
| 697 | + if ($section->name) { |
|
| 698 | + $this->parent->sections[$section->name] = $section; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + $this->sectionLevel = $this->level + 1; |
|
| 702 | + |
|
| 703 | + return $this; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Bubble the sections to parent document. |
|
| 708 | + * |
|
| 709 | + * @return \Htsl\Parser\Document |
|
| 710 | + */ |
|
| 711 | + protected function bubbleSections():self |
|
| 712 | + { |
|
| 713 | + if ($this->parent) { |
|
| 714 | + foreach ($this->sections as $name => $section) { |
|
| 715 | + if (!isset($this->parent->sections[$name])) { |
|
| 716 | + $this->parent->sections[$name] = $section; |
|
| 717 | + } |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + return $this; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Escaping characters to HTML entities. |
|
| 726 | + * |
|
| 727 | + * @param string $input |
|
| 728 | + * |
|
| 729 | + * @return string |
|
| 730 | + */ |
|
| 731 | + public function htmlEntities(string $input):string |
|
| 732 | + { |
|
| 733 | + return htmlentities($input, $this->htsl->getConfig('ENT_flags', $this->docType), 'UTF-8', false); |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Setting indent level of this document. |
|
| 738 | + * |
|
| 739 | + * @param int $level |
|
| 740 | + */ |
|
| 741 | + protected function setLevel(int $level):self |
|
| 742 | + { |
|
| 743 | + $level -= $this->level; |
|
| 744 | + |
|
| 745 | + if ($level <= 0) { |
|
| 746 | + $this->closeNodes(-$level); |
|
| 747 | + } elseif ($level == 1) { |
|
| 748 | + $this->level += 1; |
|
| 749 | + } else { |
|
| 750 | + $this->throw('Indent error.'); |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + return $this; |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Opening a node. |
|
| 758 | + * |
|
| 759 | + * @param \Htsl\Parser\Node\Contracts\ANode $node |
|
| 760 | + * |
|
| 761 | + * @return \Htsl\Parser\Document |
|
| 762 | + */ |
|
| 763 | + protected function openNode(Node $node):self |
|
| 764 | + { |
|
| 765 | + array_push($this->openedNodes, $node); |
|
| 766 | + |
|
| 767 | + $node->scope and $this->setScope($node); |
|
| 768 | + |
|
| 769 | + return $this; |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + /** |
|
| 773 | + * Closing open node or nodes. |
|
| 774 | + * |
|
| 775 | + * @param int $level |
|
| 776 | + * |
|
| 777 | + * @return \Htsl\Parser\Document |
|
| 778 | + */ |
|
| 779 | + protected function closeNodes(int $level = 0):self |
|
| 780 | + { |
|
| 781 | + if (empty($this->openedNodes)) { |
|
| 782 | + return $this; |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + while ($level-- >= 0) { |
|
| 786 | + $node = array_pop($this->openedNodes); |
|
| 787 | + |
|
| 788 | + $node->scope and $this->removeScope($node); |
|
| 789 | + |
|
| 790 | + $closer = $node->close($this->currentLine) and $this->appendLine($closer); |
|
| 791 | + |
|
| 792 | + $this->level -= $level >= 0 ? 1 : 0; |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + return $this; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * Pushing a scope to stack. |
|
| 800 | + * |
|
| 801 | + * @param \Htsl\Parser\Node\Contracts\ANode $scope |
|
| 802 | + */ |
|
| 803 | + protected function setScope(Node $scope):int |
|
| 804 | + { |
|
| 805 | + return array_unshift($this->scopes, $scope); |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + /** |
|
| 809 | + * Getting current scope on top of stack. |
|
| 810 | + * |
|
| 811 | + * @return \Htsl\Parser\Node\Contracts\ANode | null |
|
| 812 | + */ |
|
| 813 | + public function getScope() |
|
| 814 | + { |
|
| 815 | + return $this->scopes[0] ?? null; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * Pop a scope from stack. |
|
| 820 | + * |
|
| 821 | + * @param \Htsl\Parser\Node\Contracts\ANode $scope |
|
| 822 | + * |
|
| 823 | + * @return \Htsl\Parser\Document |
|
| 824 | + */ |
|
| 825 | + protected function removeScope(Node $scope):self |
|
| 826 | + { |
|
| 827 | + if ($scope !== array_shift($this->scopes)) { |
|
| 828 | + $this->throw('Scope nesting error'); |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + return $this; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + /** |
|
| 835 | + * Appending a line of content to parsing result. |
|
| 836 | + * |
|
| 837 | + * @param string $content |
|
| 838 | + * |
|
| 839 | + * @return \Htsl\Parser\Document |
|
| 840 | + */ |
|
| 841 | + protected function appendLine(string $content):self |
|
| 842 | + { |
|
| 843 | + if (false !== $this->indentation) { |
|
| 844 | + $content = str_repeat($this->indentation, $this->level - $this->sectionLevel).$content."\n"; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + return $this->append($content); |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + /** |
|
| 851 | + * Appending some content to parsing result. |
|
| 852 | + * |
|
| 853 | + * @param string $content |
|
| 854 | + * |
|
| 855 | + * @return \Htsl\Parser\Document |
|
| 856 | + */ |
|
| 857 | + protected function append(string $content):self |
|
| 858 | + { |
|
| 859 | + if ($this->currentSection) { |
|
| 860 | + $this->currentSection->append($content); |
|
| 861 | + } else { |
|
| 862 | + $this->content .= $content; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + return $this; |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + /** |
|
| 869 | + * Getting the Htsl main object. |
|
| 870 | + * |
|
| 871 | + * @return \Htsl\Htsl |
|
| 872 | + */ |
|
| 873 | + public function getHtsl() |
|
| 874 | + { |
|
| 875 | + return $this->htsl; |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + /** |
|
| 879 | + * Throw exception with document name and line number. |
|
| 880 | + * |
|
| 881 | + * @param string $message |
|
| 882 | + * |
|
| 883 | + * @throw \Htsl\Parser\HtslParsingException |
|
| 884 | + */ |
|
| 885 | + public function throw(string $message) |
|
| 886 | + { |
|
| 887 | + throw new HtslParsingException("$message at file {$this->buffer->fileName} line $this->lineNumber"); |
|
| 888 | + } |
|
| 889 | 889 | } |
@@ -68,35 +68,35 @@ discard block |
||
| 68 | 68 | * |
| 69 | 69 | * @var int |
| 70 | 70 | */ |
| 71 | - private $level = 0; |
|
| 71 | + private $level=0; |
|
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | 74 | * Section indent level. |
| 75 | 75 | * |
| 76 | 76 | * @var int |
| 77 | 77 | */ |
| 78 | - private $sectionLevel = 0; |
|
| 78 | + private $sectionLevel=0; |
|
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | 81 | * Opened nodes. |
| 82 | 82 | * |
| 83 | 83 | * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
| 84 | 84 | */ |
| 85 | - private $openedNodes = []; |
|
| 85 | + private $openedNodes=[ ]; |
|
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | 88 | * Current scopes. |
| 89 | 89 | * |
| 90 | 90 | * @var [ Htsl\Parser\Node\Contracts\ANode, ] |
| 91 | 91 | */ |
| 92 | - private $scopes = []; |
|
| 92 | + private $scopes=[ ]; |
|
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * Current line number. |
| 96 | 96 | * |
| 97 | 97 | * @var int |
| 98 | 98 | */ |
| 99 | - private $lineNumber = 0; |
|
| 99 | + private $lineNumber=0; |
|
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * Current line. |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @var [ Htsl\Parser\Section, ] |
| 112 | 112 | */ |
| 113 | - private $sections = []; |
|
| 113 | + private $sections=[ ]; |
|
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | 116 | * Whether the document is executed. |
@@ -140,16 +140,16 @@ discard block |
||
| 140 | 140 | * @param \Htsl\ReadingBuffer\Contracts\ABuffer $buffer |
| 141 | 141 | * @param \Htsl\Parser\Document | null $parent |
| 142 | 142 | */ |
| 143 | - public function __construct(Htsl $htsl, Buffer $buffer, self $parent = null) |
|
| 143 | + public function __construct( Htsl $htsl, Buffer $buffer, self $parent=null ) |
|
| 144 | 144 | { |
| 145 | - $this->htsl = $htsl; |
|
| 146 | - $this->buffer = $buffer; |
|
| 145 | + $this->htsl=$htsl; |
|
| 146 | + $this->buffer=$buffer; |
|
| 147 | 147 | |
| 148 | - if ($parent) { |
|
| 149 | - $this->parent = $parent; |
|
| 150 | - $this->docType = $parent->docType; |
|
| 151 | - $this->indentation = $parent->indentation; |
|
| 152 | - } else { |
|
| 148 | + if( $parent ){ |
|
| 149 | + $this->parent=$parent; |
|
| 150 | + $this->docType=$parent->docType; |
|
| 151 | + $this->indentation=$parent->indentation; |
|
| 152 | + }else{ |
|
| 153 | 153 | $this->parseFirstLine(); |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | public function execute():self |
| 163 | 163 | { |
| 164 | - if ($this->isExecuted) { |
|
| 164 | + if( $this->isExecuted ){ |
|
| 165 | 165 | return $this; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | */ |
| 187 | 187 | protected function getContent():string |
| 188 | 188 | { |
| 189 | - if ($this->parent) { |
|
| 189 | + if( $this->parent ){ |
|
| 190 | 190 | return $this->execute()->parent->getContent(); |
| 191 | - } else { |
|
| 191 | + }else{ |
|
| 192 | 192 | return $this->execute()->content; |
| 193 | 193 | } |
| 194 | 194 | } |
@@ -200,9 +200,9 @@ discard block |
||
| 200 | 200 | */ |
| 201 | 201 | protected function getLine():Line |
| 202 | 202 | { |
| 203 | - do { |
|
| 204 | - $line = $this->buffer->getLine(); |
|
| 205 | - } while ($line->isEmpty() && $line->hasMore()); |
|
| 203 | + do{ |
|
| 204 | + $line=$this->buffer->getLine(); |
|
| 205 | + }while( $line->isEmpty() && $line->hasMore() ); |
|
| 206 | 206 | |
| 207 | 207 | return $line; |
| 208 | 208 | } |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | * |
| 215 | 215 | * @return mixed |
| 216 | 216 | */ |
| 217 | - public function getConfig(string ...$keys) |
|
| 217 | + public function getConfig( string ...$keys ) |
|
| 218 | 218 | { |
| 219 | 219 | return $this->htsl->getConfig(array_shift($keys), $this->docType, ...$keys); |
| 220 | 220 | } |
@@ -256,17 +256,17 @@ discard block |
||
| 256 | 256 | */ |
| 257 | 257 | protected function parseFirstLine():self |
| 258 | 258 | { |
| 259 | - $line = $this->getLine(); |
|
| 259 | + $line=$this->getLine(); |
|
| 260 | 260 | |
| 261 | - if ('@' === $line->getChar(0)) { |
|
| 261 | + if( '@'===$line->getChar(0) ){ |
|
| 262 | 262 | return $this->setExtending($line); |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - $this->docType = $line->content; |
|
| 266 | - $docTypeContent = $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
|
| 265 | + $this->docType=$line->content; |
|
| 266 | + $docTypeContent=$this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
|
| 267 | 267 | |
| 268 | - $this->indentation = $this->htsl->getConfig('indentation', $this->docType) ?? (function ($scalarOrFalse) { |
|
| 269 | - return is_scalar($scalarOrFalse) ? $scalarOrFalse : false; |
|
| 268 | + $this->indentation=$this->htsl->getConfig('indentation', $this->docType) ?? (function( $scalarOrFalse ){ |
|
| 269 | + return is_scalar($scalarOrFalse)? $scalarOrFalse : false; |
|
| 270 | 270 | })($this->htsl->getConfig('indentation')); |
| 271 | 271 | |
| 272 | 272 | $this->appendLine($docTypeContent); |
@@ -279,9 +279,9 @@ discard block |
||
| 279 | 279 | * |
| 280 | 280 | * @param \Htsl\ReadingBuffer\Line $firstLine |
| 281 | 281 | */ |
| 282 | - protected function setExtending(Line $firstLine):self |
|
| 282 | + protected function setExtending( Line $firstLine ):self |
|
| 283 | 283 | { |
| 284 | - switch ($name = $firstLine->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 284 | + switch( $name=$firstLine->pregGet('/(?<=^@)[\w-:]+/') ){ |
|
| 285 | 285 | default:{ |
| 286 | 286 | $this->throw("The @$name is not supported."); |
| 287 | 287 | }break; |
@@ -305,12 +305,12 @@ discard block |
||
| 305 | 305 | */ |
| 306 | 306 | protected function lineByLine():self |
| 307 | 307 | { |
| 308 | - while (($line = $this->getLine())->hasMore()) { |
|
| 309 | - $this->lineNumber += 1; |
|
| 308 | + while( ($line=$this->getLine())->hasMore() ){ |
|
| 309 | + $this->lineNumber+=1; |
|
| 310 | 310 | |
| 311 | - if ($this->embedment) { |
|
| 311 | + if( $this->embedment ){ |
|
| 312 | 312 | $this->embeddingParse($line); |
| 313 | - } else { |
|
| 313 | + }else{ |
|
| 314 | 314 | $this->parseLine($line); |
| 315 | 315 | } |
| 316 | 316 | } |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | |
| 320 | 320 | $this->closeNodes($this->level); |
| 321 | 321 | |
| 322 | - $this->isExecuted = true; |
|
| 322 | + $this->isExecuted=true; |
|
| 323 | 323 | |
| 324 | 324 | return $this; |
| 325 | 325 | } |
@@ -331,11 +331,11 @@ discard block |
||
| 331 | 331 | * |
| 332 | 332 | * @return \Htsl\Parser\Document |
| 333 | 333 | */ |
| 334 | - protected function embeddingParse(Line $line):self |
|
| 334 | + protected function embeddingParse( Line $line ):self |
|
| 335 | 335 | { |
| 336 | - if ($line->content === '<}') { |
|
| 336 | + if( $line->content==='<}' ){ |
|
| 337 | 337 | $this->breakEmbedding(); |
| 338 | - } else { |
|
| 338 | + }else{ |
|
| 339 | 339 | $this->embedment->parseLine($line->getSubIndentLine()); |
| 340 | 340 | } |
| 341 | 341 | |
@@ -349,12 +349,12 @@ discard block |
||
| 349 | 349 | * |
| 350 | 350 | * @return \Htsl\Parser\Document |
| 351 | 351 | */ |
| 352 | - protected function startEmbedding(string $embedType):self |
|
| 352 | + protected function startEmbedding( string $embedType ):self |
|
| 353 | 353 | { |
| 354 | - $embedmentClass = '\\Htsl\\Embedment\\'.ucfirst($embedType).'Embedment'; |
|
| 354 | + $embedmentClass='\\Htsl\\Embedment\\'.ucfirst($embedType).'Embedment'; |
|
| 355 | 355 | class_exists($embedmentClass) or $this->throw("Embed type $embedType not exists."); |
| 356 | 356 | |
| 357 | - $this->embedment = new $embedmentClass($this); |
|
| 357 | + $this->embedment=new $embedmentClass($this); |
|
| 358 | 358 | |
| 359 | 359 | return $this; |
| 360 | 360 | } |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | public function breakEmbedding():self |
| 368 | 368 | { |
| 369 | 369 | $this->append($this->embedment->getContent()); |
| 370 | - $this->embedment = null; |
|
| 370 | + $this->embedment=null; |
|
| 371 | 371 | |
| 372 | 372 | return $this; |
| 373 | 373 | } |
@@ -379,19 +379,19 @@ discard block |
||
| 379 | 379 | * |
| 380 | 380 | * @return \Htsl\Parser\Document |
| 381 | 381 | */ |
| 382 | - protected function parseLine(Line $line):self |
|
| 382 | + protected function parseLine( Line $line ):self |
|
| 383 | 383 | { |
| 384 | - $this->currentLine = $line; |
|
| 384 | + $this->currentLine=$line; |
|
| 385 | 385 | $this->setLevel($line->getIndentLevel()); |
| 386 | 386 | |
| 387 | - switch ($line->getChar(0)) { |
|
| 387 | + switch( $line->getChar(0) ){ |
|
| 388 | 388 | default:{ |
| 389 | 389 | $this->parseStringLine($line); |
| 390 | 390 | }break; |
| 391 | 391 | case '`':{ |
| 392 | - if ('=' === $line->getChar(1)) { |
|
| 392 | + if( '='===$line->getChar(1) ){ |
|
| 393 | 393 | $this->parseExpressionHtmlLine($line); |
| 394 | - } else { |
|
| 394 | + }else{ |
|
| 395 | 395 | $this->parseHtmlLine($line); |
| 396 | 396 | } |
| 397 | 397 | }break; |
@@ -422,9 +422,9 @@ discard block |
||
| 422 | 422 | * |
| 423 | 423 | * @return \Htsl\Parser\Document |
| 424 | 424 | */ |
| 425 | - protected function parseHtmlLine(Line $line):self |
|
| 425 | + protected function parseHtmlLine( Line $line ):self |
|
| 426 | 426 | { |
| 427 | - $node = new StringNode($this, $line); |
|
| 427 | + $node=new StringNode($this, $line); |
|
| 428 | 428 | |
| 429 | 429 | $this->openNode($node); |
| 430 | 430 | |
@@ -440,9 +440,9 @@ discard block |
||
| 440 | 440 | * |
| 441 | 441 | * @return \Htsl\Parser\Document |
| 442 | 442 | */ |
| 443 | - protected function parseStringLine(Line $line):self |
|
| 443 | + protected function parseStringLine( Line $line ):self |
|
| 444 | 444 | { |
| 445 | - $node = new StringNode($this, $line); |
|
| 445 | + $node=new StringNode($this, $line); |
|
| 446 | 446 | |
| 447 | 447 | $this->openNode($node); |
| 448 | 448 | |
@@ -458,14 +458,14 @@ discard block |
||
| 458 | 458 | * |
| 459 | 459 | * @return \Htsl\Parser\Document |
| 460 | 460 | */ |
| 461 | - protected function parseExpressionLine(Line $line):self |
|
| 461 | + protected function parseExpressionLine( Line $line ):self |
|
| 462 | 462 | { |
| 463 | - $node = new StringNode($this, $line); |
|
| 463 | + $node=new StringNode($this, $line); |
|
| 464 | 464 | |
| 465 | 465 | $this->openNode($node); |
| 466 | 466 | |
| 467 | - $content = $line->slice(1); |
|
| 468 | - $ent_flag = $this->htsl->getConfig('ENT_flags', $this->docType); |
|
| 467 | + $content=$line->slice(1); |
|
| 468 | + $ent_flag=$this->htsl->getConfig('ENT_flags', $this->docType); |
|
| 469 | 469 | |
| 470 | 470 | $this->appendLine("<?=htmlentities($content,'$ent_flag','UTF-8',false);?>"); |
| 471 | 471 | |
@@ -479,13 +479,13 @@ discard block |
||
| 479 | 479 | * |
| 480 | 480 | * @return \Htsl\Parser\Document |
| 481 | 481 | */ |
| 482 | - protected function parseExpressionHtmlLine(Line $line):self |
|
| 482 | + protected function parseExpressionHtmlLine( Line $line ):self |
|
| 483 | 483 | { |
| 484 | - $node = new StringNode($this, $line); |
|
| 484 | + $node=new StringNode($this, $line); |
|
| 485 | 485 | |
| 486 | 486 | $this->openNode($node); |
| 487 | 487 | |
| 488 | - $content = $line->slice(1); |
|
| 488 | + $content=$line->slice(1); |
|
| 489 | 489 | |
| 490 | 490 | $this->appendLine("<?$content?>"); |
| 491 | 491 | |
@@ -499,9 +499,9 @@ discard block |
||
| 499 | 499 | * |
| 500 | 500 | * @return \Htsl\Parser\Document |
| 501 | 501 | */ |
| 502 | - protected function parseCommentLine(Line $line):self |
|
| 502 | + protected function parseCommentLine( Line $line ):self |
|
| 503 | 503 | { |
| 504 | - $node = new CommentNode($this, $line); |
|
| 504 | + $node=new CommentNode($this, $line); |
|
| 505 | 505 | |
| 506 | 506 | $this->openNode($node); |
| 507 | 507 | |
@@ -517,9 +517,9 @@ discard block |
||
| 517 | 517 | * |
| 518 | 518 | * @return \Htsl\Parser\Document |
| 519 | 519 | */ |
| 520 | - protected function parseTagLine(Line $line):self |
|
| 520 | + protected function parseTagLine( Line $line ):self |
|
| 521 | 521 | { |
| 522 | - $tag = new TagNode($this, $line); |
|
| 522 | + $tag=new TagNode($this, $line); |
|
| 523 | 523 | |
| 524 | 524 | $this->appendLine($tag->open()); |
| 525 | 525 | |
@@ -537,9 +537,9 @@ discard block |
||
| 537 | 537 | * |
| 538 | 538 | * @return \Htsl\Parser\Document |
| 539 | 539 | */ |
| 540 | - protected function parseControlLine(Line $line):self |
|
| 540 | + protected function parseControlLine( Line $line ):self |
|
| 541 | 541 | { |
| 542 | - $controlStructure = new ControlNode($this, $line); |
|
| 542 | + $controlStructure=new ControlNode($this, $line); |
|
| 543 | 543 | |
| 544 | 544 | $this->appendLine($controlStructure->open()); |
| 545 | 545 | |
@@ -555,9 +555,9 @@ discard block |
||
| 555 | 555 | * |
| 556 | 556 | * @return \Htsl\Parser\Document |
| 557 | 557 | */ |
| 558 | - protected function parseDocControlLine(Line $line):self |
|
| 558 | + protected function parseDocControlLine( Line $line ):self |
|
| 559 | 559 | { |
| 560 | - switch ($name = $line->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 560 | + switch( $name=$line->pregGet('/(?<=^@)[\w-:]+/') ){ |
|
| 561 | 561 | default:{ |
| 562 | 562 | $this->throw("The @$name is not supported."); |
| 563 | 563 | }break; |
@@ -585,12 +585,12 @@ discard block |
||
| 585 | 585 | * |
| 586 | 586 | * @return \Htsl\Parser\Document |
| 587 | 587 | */ |
| 588 | - protected function extend(string $fileName):self |
|
| 588 | + protected function extend( string $fileName ):self |
|
| 589 | 589 | { |
| 590 | - $this->parent = new static($this->htsl, $this->buffer->goSide($fileName), null, $this->indentation); |
|
| 590 | + $this->parent=new static($this->htsl, $this->buffer->goSide($fileName), null, $this->indentation); |
|
| 591 | 591 | |
| 592 | - $this->docType = $this->parent->docType; |
|
| 593 | - $this->indentation = $this->parent->indentation; |
|
| 592 | + $this->docType=$this->parent->docType; |
|
| 593 | + $this->indentation=$this->parent->indentation; |
|
| 594 | 594 | |
| 595 | 595 | return $this; |
| 596 | 596 | } |
@@ -604,13 +604,13 @@ discard block |
||
| 604 | 604 | */ |
| 605 | 605 | protected function include(Line $line):self |
| 606 | 606 | { |
| 607 | - $inclued = (new static($this->htsl, $this->buffer->goSide($line->pregGet('/(?<=\( ).*(?= \))/')), $this, $this->indentation))->execute()->content; |
|
| 607 | + $inclued=(new static($this->htsl, $this->buffer->goSide($line->pregGet('/(?<=\( ).*(?= \))/')), $this, $this->indentation))->execute()->content; |
|
| 608 | 608 | |
| 609 | - if (false !== $this->indentation) { |
|
| 610 | - $inclued = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level - $this->sectionLevel), $inclued); |
|
| 609 | + if( false!==$this->indentation ){ |
|
| 610 | + $inclued=preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level-$this->sectionLevel), $inclued); |
|
| 611 | 611 | } |
| 612 | 612 | |
| 613 | - $node = new StringNode($this, $line); |
|
| 613 | + $node=new StringNode($this, $line); |
|
| 614 | 614 | |
| 615 | 615 | $this->openNode($node); |
| 616 | 616 | |
@@ -626,9 +626,9 @@ discard block |
||
| 626 | 626 | * |
| 627 | 627 | * @return \Htsl\Parser\Document |
| 628 | 628 | */ |
| 629 | - protected function defineSection(Line $line):self |
|
| 629 | + protected function defineSection( Line $line ):self |
|
| 630 | 630 | { |
| 631 | - $node = new SectionNode($this, $line); |
|
| 631 | + $node=new SectionNode($this, $line); |
|
| 632 | 632 | |
| 633 | 633 | $node->open(); |
| 634 | 634 | |
@@ -644,24 +644,24 @@ discard block |
||
| 644 | 644 | * |
| 645 | 645 | * @return \Htsl\Parser\Document |
| 646 | 646 | */ |
| 647 | - protected function showSection(Line $line):self |
|
| 647 | + protected function showSection( Line $line ):self |
|
| 648 | 648 | { |
| 649 | - $sectionName = $line->pregGet('/(?<=\( ).*(?= \))/'); |
|
| 649 | + $sectionName=$line->pregGet('/(?<=\( ).*(?= \))/'); |
|
| 650 | 650 | |
| 651 | - if (!isset($this->sections[$sectionName])) { |
|
| 651 | + if( !isset($this->sections[ $sectionName ]) ){ |
|
| 652 | 652 | $this->openNode(new StringNode($this, $line)); |
| 653 | 653 | |
| 654 | 654 | return $this; |
| 655 | 655 | } |
| 656 | - $content = $this->sections[$sectionName]->content; |
|
| 656 | + $content=$this->sections[ $sectionName ]->content; |
|
| 657 | 657 | |
| 658 | - if (false !== $this->indentation) { |
|
| 659 | - $content = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level), $content); |
|
| 658 | + if( false!==$this->indentation ){ |
|
| 659 | + $content=preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level), $content); |
|
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | $this->append($content); |
| 663 | 663 | |
| 664 | - $node = new NamelessSectionNode($this, $line); |
|
| 664 | + $node=new NamelessSectionNode($this, $line); |
|
| 665 | 665 | |
| 666 | 666 | $node->open(); |
| 667 | 667 | |
@@ -675,30 +675,30 @@ discard block |
||
| 675 | 675 | * |
| 676 | 676 | * @param Section | null $section |
| 677 | 677 | */ |
| 678 | - public function setSection(Section $section = null):self |
|
| 678 | + public function setSection( Section $section=null ):self |
|
| 679 | 679 | { |
| 680 | - if (!$section) { |
|
| 681 | - $this->sectionLevel = 0; |
|
| 682 | - $this->currentSection = null; |
|
| 680 | + if( !$section ){ |
|
| 681 | + $this->sectionLevel=0; |
|
| 682 | + $this->currentSection=null; |
|
| 683 | 683 | |
| 684 | 684 | return $this; |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - if ($this->currentSection) { |
|
| 687 | + if( $this->currentSection ){ |
|
| 688 | 688 | $this->throw('Nesting definition of section is forbidden.'); |
| 689 | 689 | } |
| 690 | 690 | |
| 691 | - if (isset($this->parent->sections[$section->name])) { |
|
| 691 | + if( isset($this->parent->sections[ $section->name ]) ){ |
|
| 692 | 692 | $this->throw("Section $sectionName already defined."); |
| 693 | 693 | } |
| 694 | 694 | |
| 695 | - $this->currentSection = $section; |
|
| 695 | + $this->currentSection=$section; |
|
| 696 | 696 | |
| 697 | - if ($section->name) { |
|
| 698 | - $this->parent->sections[$section->name] = $section; |
|
| 697 | + if( $section->name ){ |
|
| 698 | + $this->parent->sections[ $section->name ]=$section; |
|
| 699 | 699 | } |
| 700 | 700 | |
| 701 | - $this->sectionLevel = $this->level + 1; |
|
| 701 | + $this->sectionLevel=$this->level+1; |
|
| 702 | 702 | |
| 703 | 703 | return $this; |
| 704 | 704 | } |
@@ -710,10 +710,10 @@ discard block |
||
| 710 | 710 | */ |
| 711 | 711 | protected function bubbleSections():self |
| 712 | 712 | { |
| 713 | - if ($this->parent) { |
|
| 714 | - foreach ($this->sections as $name => $section) { |
|
| 715 | - if (!isset($this->parent->sections[$name])) { |
|
| 716 | - $this->parent->sections[$name] = $section; |
|
| 713 | + if( $this->parent ){ |
|
| 714 | + foreach( $this->sections as $name => $section ){ |
|
| 715 | + if( !isset($this->parent->sections[ $name ]) ){ |
|
| 716 | + $this->parent->sections[ $name ]=$section; |
|
| 717 | 717 | } |
| 718 | 718 | } |
| 719 | 719 | } |
@@ -728,7 +728,7 @@ discard block |
||
| 728 | 728 | * |
| 729 | 729 | * @return string |
| 730 | 730 | */ |
| 731 | - public function htmlEntities(string $input):string |
|
| 731 | + public function htmlEntities( string $input ):string |
|
| 732 | 732 | { |
| 733 | 733 | return htmlentities($input, $this->htsl->getConfig('ENT_flags', $this->docType), 'UTF-8', false); |
| 734 | 734 | } |
@@ -738,15 +738,15 @@ discard block |
||
| 738 | 738 | * |
| 739 | 739 | * @param int $level |
| 740 | 740 | */ |
| 741 | - protected function setLevel(int $level):self |
|
| 741 | + protected function setLevel( int $level ):self |
|
| 742 | 742 | { |
| 743 | - $level -= $this->level; |
|
| 743 | + $level-=$this->level; |
|
| 744 | 744 | |
| 745 | - if ($level <= 0) { |
|
| 745 | + if( $level<=0 ){ |
|
| 746 | 746 | $this->closeNodes(-$level); |
| 747 | - } elseif ($level == 1) { |
|
| 748 | - $this->level += 1; |
|
| 749 | - } else { |
|
| 747 | + } elseif( $level==1 ){ |
|
| 748 | + $this->level+=1; |
|
| 749 | + }else{ |
|
| 750 | 750 | $this->throw('Indent error.'); |
| 751 | 751 | } |
| 752 | 752 | |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | * |
| 761 | 761 | * @return \Htsl\Parser\Document |
| 762 | 762 | */ |
| 763 | - protected function openNode(Node $node):self |
|
| 763 | + protected function openNode( Node $node ):self |
|
| 764 | 764 | { |
| 765 | 765 | array_push($this->openedNodes, $node); |
| 766 | 766 | |
@@ -776,20 +776,20 @@ discard block |
||
| 776 | 776 | * |
| 777 | 777 | * @return \Htsl\Parser\Document |
| 778 | 778 | */ |
| 779 | - protected function closeNodes(int $level = 0):self |
|
| 779 | + protected function closeNodes( int $level=0 ):self |
|
| 780 | 780 | { |
| 781 | - if (empty($this->openedNodes)) { |
|
| 781 | + if( empty($this->openedNodes) ){ |
|
| 782 | 782 | return $this; |
| 783 | 783 | } |
| 784 | 784 | |
| 785 | - while ($level-- >= 0) { |
|
| 786 | - $node = array_pop($this->openedNodes); |
|
| 785 | + while( $level-->=0 ){ |
|
| 786 | + $node=array_pop($this->openedNodes); |
|
| 787 | 787 | |
| 788 | 788 | $node->scope and $this->removeScope($node); |
| 789 | 789 | |
| 790 | - $closer = $node->close($this->currentLine) and $this->appendLine($closer); |
|
| 790 | + $closer=$node->close($this->currentLine) and $this->appendLine($closer); |
|
| 791 | 791 | |
| 792 | - $this->level -= $level >= 0 ? 1 : 0; |
|
| 792 | + $this->level-=$level>=0? 1 : 0; |
|
| 793 | 793 | } |
| 794 | 794 | |
| 795 | 795 | return $this; |
@@ -800,7 +800,7 @@ discard block |
||
| 800 | 800 | * |
| 801 | 801 | * @param \Htsl\Parser\Node\Contracts\ANode $scope |
| 802 | 802 | */ |
| 803 | - protected function setScope(Node $scope):int |
|
| 803 | + protected function setScope( Node $scope ):int |
|
| 804 | 804 | { |
| 805 | 805 | return array_unshift($this->scopes, $scope); |
| 806 | 806 | } |
@@ -812,7 +812,7 @@ discard block |
||
| 812 | 812 | */ |
| 813 | 813 | public function getScope() |
| 814 | 814 | { |
| 815 | - return $this->scopes[0] ?? null; |
|
| 815 | + return $this->scopes[ 0 ] ?? null; |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | /** |
@@ -822,9 +822,9 @@ discard block |
||
| 822 | 822 | * |
| 823 | 823 | * @return \Htsl\Parser\Document |
| 824 | 824 | */ |
| 825 | - protected function removeScope(Node $scope):self |
|
| 825 | + protected function removeScope( Node $scope ):self |
|
| 826 | 826 | { |
| 827 | - if ($scope !== array_shift($this->scopes)) { |
|
| 827 | + if( $scope!==array_shift($this->scopes) ){ |
|
| 828 | 828 | $this->throw('Scope nesting error'); |
| 829 | 829 | } |
| 830 | 830 | |
@@ -838,10 +838,10 @@ discard block |
||
| 838 | 838 | * |
| 839 | 839 | * @return \Htsl\Parser\Document |
| 840 | 840 | */ |
| 841 | - protected function appendLine(string $content):self |
|
| 841 | + protected function appendLine( string $content ):self |
|
| 842 | 842 | { |
| 843 | - if (false !== $this->indentation) { |
|
| 844 | - $content = str_repeat($this->indentation, $this->level - $this->sectionLevel).$content."\n"; |
|
| 843 | + if( false!==$this->indentation ){ |
|
| 844 | + $content=str_repeat($this->indentation, $this->level-$this->sectionLevel).$content."\n"; |
|
| 845 | 845 | } |
| 846 | 846 | |
| 847 | 847 | return $this->append($content); |
@@ -854,12 +854,12 @@ discard block |
||
| 854 | 854 | * |
| 855 | 855 | * @return \Htsl\Parser\Document |
| 856 | 856 | */ |
| 857 | - protected function append(string $content):self |
|
| 857 | + protected function append( string $content ):self |
|
| 858 | 858 | { |
| 859 | - if ($this->currentSection) { |
|
| 859 | + if( $this->currentSection ){ |
|
| 860 | 860 | $this->currentSection->append($content); |
| 861 | - } else { |
|
| 862 | - $this->content .= $content; |
|
| 861 | + }else{ |
|
| 862 | + $this->content.=$content; |
|
| 863 | 863 | } |
| 864 | 864 | |
| 865 | 865 | return $this; |
@@ -145,11 +145,11 @@ discard block |
||
| 145 | 145 | $this->htsl = $htsl; |
| 146 | 146 | $this->buffer = $buffer; |
| 147 | 147 | |
| 148 | - if ($parent) { |
|
| 148 | + if ($parent){ |
|
| 149 | 149 | $this->parent = $parent; |
| 150 | 150 | $this->docType = $parent->docType; |
| 151 | 151 | $this->indentation = $parent->indentation; |
| 152 | - } else { |
|
| 152 | + } else{ |
|
| 153 | 153 | $this->parseFirstLine(); |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | public function execute():self |
| 163 | 163 | { |
| 164 | - if ($this->isExecuted) { |
|
| 164 | + if ($this->isExecuted){ |
|
| 165 | 165 | return $this; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | */ |
| 187 | 187 | protected function getContent():string |
| 188 | 188 | { |
| 189 | - if ($this->parent) { |
|
| 189 | + if ($this->parent){ |
|
| 190 | 190 | return $this->execute()->parent->getContent(); |
| 191 | - } else { |
|
| 191 | + } else{ |
|
| 192 | 192 | return $this->execute()->content; |
| 193 | 193 | } |
| 194 | 194 | } |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | */ |
| 201 | 201 | protected function getLine():Line |
| 202 | 202 | { |
| 203 | - do { |
|
| 203 | + do{ |
|
| 204 | 204 | $line = $this->buffer->getLine(); |
| 205 | 205 | } while ($line->isEmpty() && $line->hasMore()); |
| 206 | 206 | |
@@ -258,14 +258,14 @@ discard block |
||
| 258 | 258 | { |
| 259 | 259 | $line = $this->getLine(); |
| 260 | 260 | |
| 261 | - if ('@' === $line->getChar(0)) { |
|
| 261 | + if ('@' === $line->getChar(0)){ |
|
| 262 | 262 | return $this->setExtending($line); |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | $this->docType = $line->content; |
| 266 | 266 | $docTypeContent = $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
| 267 | 267 | |
| 268 | - $this->indentation = $this->htsl->getConfig('indentation', $this->docType) ?? (function ($scalarOrFalse) { |
|
| 268 | + $this->indentation = $this->htsl->getConfig('indentation', $this->docType) ?? (function ($scalarOrFalse){ |
|
| 269 | 269 | return is_scalar($scalarOrFalse) ? $scalarOrFalse : false; |
| 270 | 270 | })($this->htsl->getConfig('indentation')); |
| 271 | 271 | |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | */ |
| 282 | 282 | protected function setExtending(Line $firstLine):self |
| 283 | 283 | { |
| 284 | - switch ($name = $firstLine->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 284 | + switch ($name = $firstLine->pregGet('/(?<=^@)[\w-:]+/')){ |
|
| 285 | 285 | default:{ |
| 286 | 286 | $this->throw("The @$name is not supported."); |
| 287 | 287 | }break; |
@@ -305,12 +305,12 @@ discard block |
||
| 305 | 305 | */ |
| 306 | 306 | protected function lineByLine():self |
| 307 | 307 | { |
| 308 | - while (($line = $this->getLine())->hasMore()) { |
|
| 308 | + while (($line = $this->getLine())->hasMore()){ |
|
| 309 | 309 | $this->lineNumber += 1; |
| 310 | 310 | |
| 311 | - if ($this->embedment) { |
|
| 311 | + if ($this->embedment){ |
|
| 312 | 312 | $this->embeddingParse($line); |
| 313 | - } else { |
|
| 313 | + } else{ |
|
| 314 | 314 | $this->parseLine($line); |
| 315 | 315 | } |
| 316 | 316 | } |
@@ -333,9 +333,9 @@ discard block |
||
| 333 | 333 | */ |
| 334 | 334 | protected function embeddingParse(Line $line):self |
| 335 | 335 | { |
| 336 | - if ($line->content === '<}') { |
|
| 336 | + if ($line->content === '<}'){ |
|
| 337 | 337 | $this->breakEmbedding(); |
| 338 | - } else { |
|
| 338 | + } else{ |
|
| 339 | 339 | $this->embedment->parseLine($line->getSubIndentLine()); |
| 340 | 340 | } |
| 341 | 341 | |
@@ -384,14 +384,14 @@ discard block |
||
| 384 | 384 | $this->currentLine = $line; |
| 385 | 385 | $this->setLevel($line->getIndentLevel()); |
| 386 | 386 | |
| 387 | - switch ($line->getChar(0)) { |
|
| 387 | + switch ($line->getChar(0)){ |
|
| 388 | 388 | default:{ |
| 389 | 389 | $this->parseStringLine($line); |
| 390 | 390 | }break; |
| 391 | 391 | case '`':{ |
| 392 | - if ('=' === $line->getChar(1)) { |
|
| 392 | + if ('=' === $line->getChar(1)){ |
|
| 393 | 393 | $this->parseExpressionHtmlLine($line); |
| 394 | - } else { |
|
| 394 | + } else{ |
|
| 395 | 395 | $this->parseHtmlLine($line); |
| 396 | 396 | } |
| 397 | 397 | }break; |
@@ -557,7 +557,7 @@ discard block |
||
| 557 | 557 | */ |
| 558 | 558 | protected function parseDocControlLine(Line $line):self |
| 559 | 559 | { |
| 560 | - switch ($name = $line->pregGet('/(?<=^@)[\w-:]+/')) { |
|
| 560 | + switch ($name = $line->pregGet('/(?<=^@)[\w-:]+/')){ |
|
| 561 | 561 | default:{ |
| 562 | 562 | $this->throw("The @$name is not supported."); |
| 563 | 563 | }break; |
@@ -606,7 +606,7 @@ discard block |
||
| 606 | 606 | { |
| 607 | 607 | $inclued = (new static($this->htsl, $this->buffer->goSide($line->pregGet('/(?<=\( ).*(?= \))/')), $this, $this->indentation))->execute()->content; |
| 608 | 608 | |
| 609 | - if (false !== $this->indentation) { |
|
| 609 | + if (false !== $this->indentation){ |
|
| 610 | 610 | $inclued = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level - $this->sectionLevel), $inclued); |
| 611 | 611 | } |
| 612 | 612 | |
@@ -648,14 +648,14 @@ discard block |
||
| 648 | 648 | { |
| 649 | 649 | $sectionName = $line->pregGet('/(?<=\( ).*(?= \))/'); |
| 650 | 650 | |
| 651 | - if (!isset($this->sections[$sectionName])) { |
|
| 651 | + if (!isset($this->sections[$sectionName])){ |
|
| 652 | 652 | $this->openNode(new StringNode($this, $line)); |
| 653 | 653 | |
| 654 | 654 | return $this; |
| 655 | 655 | } |
| 656 | 656 | $content = $this->sections[$sectionName]->content; |
| 657 | 657 | |
| 658 | - if (false !== $this->indentation) { |
|
| 658 | + if (false !== $this->indentation){ |
|
| 659 | 659 | $content = preg_replace('/(?<=^|\\n)(?!$)/', str_repeat($this->indentation, $this->level), $content); |
| 660 | 660 | } |
| 661 | 661 | |
@@ -677,24 +677,24 @@ discard block |
||
| 677 | 677 | */ |
| 678 | 678 | public function setSection(Section $section = null):self |
| 679 | 679 | { |
| 680 | - if (!$section) { |
|
| 680 | + if (!$section){ |
|
| 681 | 681 | $this->sectionLevel = 0; |
| 682 | 682 | $this->currentSection = null; |
| 683 | 683 | |
| 684 | 684 | return $this; |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - if ($this->currentSection) { |
|
| 687 | + if ($this->currentSection){ |
|
| 688 | 688 | $this->throw('Nesting definition of section is forbidden.'); |
| 689 | 689 | } |
| 690 | 690 | |
| 691 | - if (isset($this->parent->sections[$section->name])) { |
|
| 691 | + if (isset($this->parent->sections[$section->name])){ |
|
| 692 | 692 | $this->throw("Section $sectionName already defined."); |
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | $this->currentSection = $section; |
| 696 | 696 | |
| 697 | - if ($section->name) { |
|
| 697 | + if ($section->name){ |
|
| 698 | 698 | $this->parent->sections[$section->name] = $section; |
| 699 | 699 | } |
| 700 | 700 | |
@@ -710,9 +710,9 @@ discard block |
||
| 710 | 710 | */ |
| 711 | 711 | protected function bubbleSections():self |
| 712 | 712 | { |
| 713 | - if ($this->parent) { |
|
| 714 | - foreach ($this->sections as $name => $section) { |
|
| 715 | - if (!isset($this->parent->sections[$name])) { |
|
| 713 | + if ($this->parent){ |
|
| 714 | + foreach ($this->sections as $name => $section){ |
|
| 715 | + if (!isset($this->parent->sections[$name])){ |
|
| 716 | 716 | $this->parent->sections[$name] = $section; |
| 717 | 717 | } |
| 718 | 718 | } |
@@ -742,11 +742,11 @@ discard block |
||
| 742 | 742 | { |
| 743 | 743 | $level -= $this->level; |
| 744 | 744 | |
| 745 | - if ($level <= 0) { |
|
| 745 | + if ($level <= 0){ |
|
| 746 | 746 | $this->closeNodes(-$level); |
| 747 | - } elseif ($level == 1) { |
|
| 747 | + } elseif ($level == 1){ |
|
| 748 | 748 | $this->level += 1; |
| 749 | - } else { |
|
| 749 | + } else{ |
|
| 750 | 750 | $this->throw('Indent error.'); |
| 751 | 751 | } |
| 752 | 752 | |
@@ -778,11 +778,11 @@ discard block |
||
| 778 | 778 | */ |
| 779 | 779 | protected function closeNodes(int $level = 0):self |
| 780 | 780 | { |
| 781 | - if (empty($this->openedNodes)) { |
|
| 781 | + if (empty($this->openedNodes)){ |
|
| 782 | 782 | return $this; |
| 783 | 783 | } |
| 784 | 784 | |
| 785 | - while ($level-- >= 0) { |
|
| 785 | + while ($level-- >= 0){ |
|
| 786 | 786 | $node = array_pop($this->openedNodes); |
| 787 | 787 | |
| 788 | 788 | $node->scope and $this->removeScope($node); |
@@ -824,7 +824,7 @@ discard block |
||
| 824 | 824 | */ |
| 825 | 825 | protected function removeScope(Node $scope):self |
| 826 | 826 | { |
| 827 | - if ($scope !== array_shift($this->scopes)) { |
|
| 827 | + if ($scope !== array_shift($this->scopes)){ |
|
| 828 | 828 | $this->throw('Scope nesting error'); |
| 829 | 829 | } |
| 830 | 830 | |
@@ -840,7 +840,7 @@ discard block |
||
| 840 | 840 | */ |
| 841 | 841 | protected function appendLine(string $content):self |
| 842 | 842 | { |
| 843 | - if (false !== $this->indentation) { |
|
| 843 | + if (false !== $this->indentation){ |
|
| 844 | 844 | $content = str_repeat($this->indentation, $this->level - $this->sectionLevel).$content."\n"; |
| 845 | 845 | } |
| 846 | 846 | |
@@ -856,9 +856,9 @@ discard block |
||
| 856 | 856 | */ |
| 857 | 857 | protected function append(string $content):self |
| 858 | 858 | { |
| 859 | - if ($this->currentSection) { |
|
| 859 | + if ($this->currentSection){ |
|
| 860 | 860 | $this->currentSection->append($content); |
| 861 | - } else { |
|
| 861 | + } else{ |
|
| 862 | 862 | $this->content .= $content; |
| 863 | 863 | } |
| 864 | 864 | |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | $this->docType = $line->content; |
| 266 | - $docTypeContent = $this->getConfig('doc_types') or $this->throw("DocType $this->docType is not supported"); |
|
| 266 | + $docTypeContent = $this->getConfig('doc_types') or $this->throw("doctype $this->docType is not supported"); |
|
| 267 | 267 | |
| 268 | 268 | $this->indentation = $this->htsl->getConfig('indentation', $this->docType) ?? (function ($scalarOrFalse) { |
| 269 | 269 | return is_scalar($scalarOrFalse) ? $scalarOrFalse : false; |
@@ -689,7 +689,7 @@ discard block |
||
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | if (isset($this->parent->sections[$section->name])) { |
| 692 | - $this->throw("Section $sectionName already defined."); |
|
| 692 | + $this->throw("section $sectionName already defined."); |
|
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | $this->currentSection = $section; |
@@ -97,7 +97,6 @@ discard block |
||
| 97 | 97 | /** |
| 98 | 98 | * Close this tag node, and returning node closer. |
| 99 | 99 | * |
| 100 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 101 | 100 | * |
| 102 | 101 | * @return string |
| 103 | 102 | */ |
@@ -221,7 +220,7 @@ discard block |
||
| 221 | 220 | /** |
| 222 | 221 | * Parsing #ids .classes ^titles [styles] %event{>listeners<} and {other attributes}. |
| 223 | 222 | * |
| 224 | - * @return \Htsl\Parser\Node\TagNode |
|
| 223 | + * @return string |
|
| 225 | 224 | */ |
| 226 | 225 | protected function parseCommonAttributes():string |
| 227 | 226 | { |
@@ -10,352 +10,352 @@ |
||
| 10 | 10 | |
| 11 | 11 | class TagNode extends ANode implements ArrayAccess |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The html tag name of this node. |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - private $tagName; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Whether the html tag is empty. |
|
| 22 | - * |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - private $isEmpty; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The attributes of this node. |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - private $attributes = []; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Real constructor. |
|
| 36 | - * |
|
| 37 | - * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 38 | - */ |
|
| 39 | - protected function construct():parent |
|
| 40 | - { |
|
| 41 | - $name = $this->line->pregGet('/(?<=^-)[\w-:]+/'); |
|
| 42 | - $this->name = $name; |
|
| 43 | - |
|
| 44 | - $this->loadConfig($name, $this->document); |
|
| 45 | - |
|
| 46 | - $this->tagName = $this->config['name'] ?? $name; |
|
| 47 | - $this->isEmpty = $this->line->getChar(-1) === '/' || $this->document->getConfig('empty_tags', $this->tagName); |
|
| 48 | - isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'], function ($value, $key) { |
|
| 49 | - $this->setAttribute($key, $value); |
|
| 50 | - }); |
|
| 51 | - |
|
| 52 | - return $this; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Opening this tag node, and returning node opener. |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function open():string |
|
| 61 | - { |
|
| 62 | - if (isset($this->config['opener'])) { |
|
| 63 | - return $this->config['opener']; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (isset($this->config['params'])) { |
|
| 67 | - $this->parseParams(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if (isset($this->config['name_value'])) { |
|
| 71 | - $this->parseNameValue(); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if (isset($this->config['link'])) { |
|
| 75 | - $this->parseLink(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if (isset($this->config['target'])) { |
|
| 79 | - $this->parseTarget(); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if (isset($this->config['alt'])) { |
|
| 83 | - $this->parseAlt(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $this->parseCommonAttributes(); |
|
| 87 | - |
|
| 88 | - if (isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function'])) { |
|
| 89 | - $this->config['scope_function']->call($this, $this->document->scope); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $finisher = $this->isEmpty ? ' />' : '>'; |
|
| 93 | - |
|
| 94 | - return "<{$this->tagName}{$this->attributesString}{$finisher}"; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Close this tag node, and returning node closer. |
|
| 99 | - * |
|
| 100 | - * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 101 | - * |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - public function close(Line $Line):string |
|
| 105 | - { |
|
| 106 | - return $this->isEmpty ? '' : $this->config['closer'] ?? "</{$this->tagName}>"; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Getting whether this is embedding node and embeding type. |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function getEmbed():string |
|
| 115 | - { |
|
| 116 | - return $this->config['embedding'] ?? ''; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Getting whether this node contains a scope and scope name. |
|
| 121 | - * |
|
| 122 | - * @return string | null |
|
| 123 | - */ |
|
| 124 | - public function getScope() |
|
| 125 | - { |
|
| 126 | - return $this->config['scope'] ?? null; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Parsing node parameters if needed. |
|
| 131 | - * |
|
| 132 | - * @return \Htsl\Parser\Node\TagNode |
|
| 133 | - */ |
|
| 134 | - protected function parseParams():self |
|
| 135 | - { |
|
| 136 | - $params = preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
|
| 137 | - |
|
| 138 | - if (($m = count($params)) != ($n = count($this->config['params']))) { |
|
| 139 | - $this->document->throw("Tag $this->name has $n parameters $m given."); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - array_map(function ($key, $value) { |
|
| 143 | - return $this->setAttribute($key, str_replace('\\|', '|', $value)); |
|
| 144 | - }, $this->config['params'], $params); |
|
| 145 | - |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Parsing <name|value> attributes. |
|
| 151 | - * |
|
| 152 | - * @return \Htsl\Parser\Node\TagNode |
|
| 153 | - */ |
|
| 154 | - protected function parseNameValue():self |
|
| 155 | - { |
|
| 156 | - $params = $this->line->pregGet('/ <(.*?)>(?= |$)/', 1) |
|
| 157 | - and $params = preg_split('/(?<!\\\\)\\|/', $params) |
|
| 158 | - and array_map(function ($key, $value) { |
|
| 159 | - return isset($key) && isset($value) ? $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))) : ''; |
|
| 160 | - }, $this->config['name_value'], $params); |
|
| 161 | - |
|
| 162 | - return $this; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Parsing @links. |
|
| 167 | - * |
|
| 168 | - * @return \Htsl\Parser\Node\TagNode |
|
| 169 | - */ |
|
| 170 | - protected function parseLink():self |
|
| 171 | - { |
|
| 172 | - $link = $this->line->pregGet('/ @((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 173 | - |
|
| 174 | - if (strlen($link)) { |
|
| 175 | - if (isset($this->config['target']) && ':' === $link[0]) { |
|
| 176 | - $this->setAttribute($this->config['link'], 'javascript'.$link); |
|
| 177 | - } elseif ('//' === ($firstTwoLetters = substr($link, 0, 2))) { |
|
| 178 | - $this->setAttribute($this->config['link'], 'http:'.$link); |
|
| 179 | - } elseif ('\\\\' === $firstTwoLetters) { |
|
| 180 | - $this->setAttribute($this->config['link'], 'https://'.substr($link, 2)); |
|
| 181 | - } else { |
|
| 182 | - $this->setAttribute($this->config['link'], $this->checkExpression($link)); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return $this; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Parsing >target. |
|
| 191 | - * |
|
| 192 | - * @return \Htsl\Parser\Node\TagNode |
|
| 193 | - */ |
|
| 194 | - protected function parseTarget():self |
|
| 195 | - { |
|
| 196 | - $target = $this->line->pregGet('/ >((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 197 | - |
|
| 198 | - if (strlen($target)) { |
|
| 199 | - $this->setAttribute($this->config['target'], $this->checkExpression($target)); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $this; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Parsing _placeholders. |
|
| 207 | - * |
|
| 208 | - * @return \Htsl\Parser\Node\TagNode |
|
| 209 | - */ |
|
| 210 | - protected function parseAlt():self |
|
| 211 | - { |
|
| 212 | - $alt = $this->line->pregGet('/ _((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 213 | - |
|
| 214 | - if (strlen($alt)) { |
|
| 215 | - $this->setAttribute($this->config['alt'], $this->checkExpression($alt)); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return $this; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Parsing #ids .classes ^titles [styles] %event{>listeners<} and {other attributes}. |
|
| 223 | - * |
|
| 224 | - * @return \Htsl\Parser\Node\TagNode |
|
| 225 | - */ |
|
| 226 | - protected function parseCommonAttributes():string |
|
| 227 | - { |
|
| 228 | - $attributes = ''; |
|
| 229 | - |
|
| 230 | - $id = $this->line->pregGet('/ #([^ ]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 231 | - and $this->setAttribute('id', $id); |
|
| 232 | - |
|
| 233 | - $classes = $this->line->pregGet('/ \.[^ ]+(?= |$)/') |
|
| 234 | - and preg_match_all('/\.((?(?!\()[^.]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\))))/', $classes, $matches) |
|
| 235 | - and $classes = implode(' ', array_filter(array_map(function ($className) { |
|
| 236 | - return $this->checkExpression($className); |
|
| 237 | - }, $matches[1]))) |
|
| 238 | - and $this->setAttribute('class', $classes); |
|
| 239 | - |
|
| 240 | - $title = $this->line->pregGet('/ \^((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 241 | - and $this->setAttribute('title', $title); |
|
| 242 | - |
|
| 243 | - $style = $this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/', 1) |
|
| 244 | - and $this->setAttribute('style', $style); |
|
| 245 | - |
|
| 246 | - $eventListeners = $this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function ($string, $name, $code) { |
|
| 247 | - $this->setAttribute('on'.$name, str_replace('"', '"', $code)); |
|
| 248 | - }) |
|
| 249 | - and implode('', $eventListeners); |
|
| 250 | - |
|
| 251 | - $other = $this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
|
| 252 | - and array_map(function ($keyValue) { |
|
| 253 | - preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function ($matches) { |
|
| 254 | - $this->setAttribute($matches[1], ($matches[3] ?? $matches[1]) ?: $matches[1], $matches[2] ?? null); |
|
| 255 | - }, $keyValue); |
|
| 256 | - }, explode(';', $other)); |
|
| 257 | - |
|
| 258 | - return $attributes; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Checking and parse PHP expressions. |
|
| 263 | - * |
|
| 264 | - * @param string $value |
|
| 265 | - * |
|
| 266 | - * @return string |
|
| 267 | - */ |
|
| 268 | - protected function checkExpression(string $value):string |
|
| 269 | - { |
|
| 270 | - return preg_match('/^\(.*\)$/', $value) ? '<?='.substr($value, 1, -1).';?>' : str_replace('"', '"', $value); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Getting attribute string with HTML syntax. |
|
| 275 | - * |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - protected function getAttributesString():string |
|
| 279 | - { |
|
| 280 | - ksort($this->attributes); |
|
| 281 | - |
|
| 282 | - return implode('', array_map(static function (string $key, array $data) { |
|
| 283 | - return isset($data['condition']) && strlen($data['condition']) ? |
|
| 284 | - "<?php if( {$data['condition']} ){?> $key=\"{$data['value']}\"<?php }?>" |
|
| 285 | - : |
|
| 286 | - " $key=\"{$data['value']}\""; |
|
| 287 | - }, array_keys($this->attributes), $this->attributes)); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Setting attribute. |
|
| 292 | - * |
|
| 293 | - * @param string $key Attribute name. |
|
| 294 | - * @param string $value Attribute value |
|
| 295 | - * @param string|null $condition Optional condition, If given, attribute will seted only when condition is true. |
|
| 296 | - */ |
|
| 297 | - protected function setAttribute(string $key, string $value, string $condition = null):self |
|
| 298 | - { |
|
| 299 | - if (isset($this->attributes[$key])) { |
|
| 300 | - $this->document->throw("Attribute $key of $this->name cannot redeclare."); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - $this->attributes[$key] = [ |
|
| 304 | - 'value' => $value, |
|
| 305 | - 'condition' => $condition, |
|
| 306 | - ]; |
|
| 307 | - |
|
| 308 | - return $this; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /* *\ |
|
| 13 | + /** |
|
| 14 | + * The html tag name of this node. |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + private $tagName; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Whether the html tag is empty. |
|
| 22 | + * |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + private $isEmpty; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The attributes of this node. |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + private $attributes = []; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Real constructor. |
|
| 36 | + * |
|
| 37 | + * @return \Htsl\Parser\Node\Contracts\ANode |
|
| 38 | + */ |
|
| 39 | + protected function construct():parent |
|
| 40 | + { |
|
| 41 | + $name = $this->line->pregGet('/(?<=^-)[\w-:]+/'); |
|
| 42 | + $this->name = $name; |
|
| 43 | + |
|
| 44 | + $this->loadConfig($name, $this->document); |
|
| 45 | + |
|
| 46 | + $this->tagName = $this->config['name'] ?? $name; |
|
| 47 | + $this->isEmpty = $this->line->getChar(-1) === '/' || $this->document->getConfig('empty_tags', $this->tagName); |
|
| 48 | + isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'], function ($value, $key) { |
|
| 49 | + $this->setAttribute($key, $value); |
|
| 50 | + }); |
|
| 51 | + |
|
| 52 | + return $this; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Opening this tag node, and returning node opener. |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function open():string |
|
| 61 | + { |
|
| 62 | + if (isset($this->config['opener'])) { |
|
| 63 | + return $this->config['opener']; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (isset($this->config['params'])) { |
|
| 67 | + $this->parseParams(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if (isset($this->config['name_value'])) { |
|
| 71 | + $this->parseNameValue(); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if (isset($this->config['link'])) { |
|
| 75 | + $this->parseLink(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if (isset($this->config['target'])) { |
|
| 79 | + $this->parseTarget(); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if (isset($this->config['alt'])) { |
|
| 83 | + $this->parseAlt(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $this->parseCommonAttributes(); |
|
| 87 | + |
|
| 88 | + if (isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function'])) { |
|
| 89 | + $this->config['scope_function']->call($this, $this->document->scope); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $finisher = $this->isEmpty ? ' />' : '>'; |
|
| 93 | + |
|
| 94 | + return "<{$this->tagName}{$this->attributesString}{$finisher}"; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Close this tag node, and returning node closer. |
|
| 99 | + * |
|
| 100 | + * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
|
| 101 | + * |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function close(Line $Line):string |
|
| 105 | + { |
|
| 106 | + return $this->isEmpty ? '' : $this->config['closer'] ?? "</{$this->tagName}>"; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Getting whether this is embedding node and embeding type. |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function getEmbed():string |
|
| 115 | + { |
|
| 116 | + return $this->config['embedding'] ?? ''; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Getting whether this node contains a scope and scope name. |
|
| 121 | + * |
|
| 122 | + * @return string | null |
|
| 123 | + */ |
|
| 124 | + public function getScope() |
|
| 125 | + { |
|
| 126 | + return $this->config['scope'] ?? null; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Parsing node parameters if needed. |
|
| 131 | + * |
|
| 132 | + * @return \Htsl\Parser\Node\TagNode |
|
| 133 | + */ |
|
| 134 | + protected function parseParams():self |
|
| 135 | + { |
|
| 136 | + $params = preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
|
| 137 | + |
|
| 138 | + if (($m = count($params)) != ($n = count($this->config['params']))) { |
|
| 139 | + $this->document->throw("Tag $this->name has $n parameters $m given."); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + array_map(function ($key, $value) { |
|
| 143 | + return $this->setAttribute($key, str_replace('\\|', '|', $value)); |
|
| 144 | + }, $this->config['params'], $params); |
|
| 145 | + |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Parsing <name|value> attributes. |
|
| 151 | + * |
|
| 152 | + * @return \Htsl\Parser\Node\TagNode |
|
| 153 | + */ |
|
| 154 | + protected function parseNameValue():self |
|
| 155 | + { |
|
| 156 | + $params = $this->line->pregGet('/ <(.*?)>(?= |$)/', 1) |
|
| 157 | + and $params = preg_split('/(?<!\\\\)\\|/', $params) |
|
| 158 | + and array_map(function ($key, $value) { |
|
| 159 | + return isset($key) && isset($value) ? $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))) : ''; |
|
| 160 | + }, $this->config['name_value'], $params); |
|
| 161 | + |
|
| 162 | + return $this; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Parsing @links. |
|
| 167 | + * |
|
| 168 | + * @return \Htsl\Parser\Node\TagNode |
|
| 169 | + */ |
|
| 170 | + protected function parseLink():self |
|
| 171 | + { |
|
| 172 | + $link = $this->line->pregGet('/ @((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 173 | + |
|
| 174 | + if (strlen($link)) { |
|
| 175 | + if (isset($this->config['target']) && ':' === $link[0]) { |
|
| 176 | + $this->setAttribute($this->config['link'], 'javascript'.$link); |
|
| 177 | + } elseif ('//' === ($firstTwoLetters = substr($link, 0, 2))) { |
|
| 178 | + $this->setAttribute($this->config['link'], 'http:'.$link); |
|
| 179 | + } elseif ('\\\\' === $firstTwoLetters) { |
|
| 180 | + $this->setAttribute($this->config['link'], 'https://'.substr($link, 2)); |
|
| 181 | + } else { |
|
| 182 | + $this->setAttribute($this->config['link'], $this->checkExpression($link)); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return $this; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Parsing >target. |
|
| 191 | + * |
|
| 192 | + * @return \Htsl\Parser\Node\TagNode |
|
| 193 | + */ |
|
| 194 | + protected function parseTarget():self |
|
| 195 | + { |
|
| 196 | + $target = $this->line->pregGet('/ >((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 197 | + |
|
| 198 | + if (strlen($target)) { |
|
| 199 | + $this->setAttribute($this->config['target'], $this->checkExpression($target)); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $this; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Parsing _placeholders. |
|
| 207 | + * |
|
| 208 | + * @return \Htsl\Parser\Node\TagNode |
|
| 209 | + */ |
|
| 210 | + protected function parseAlt():self |
|
| 211 | + { |
|
| 212 | + $alt = $this->line->pregGet('/ _((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 213 | + |
|
| 214 | + if (strlen($alt)) { |
|
| 215 | + $this->setAttribute($this->config['alt'], $this->checkExpression($alt)); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return $this; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Parsing #ids .classes ^titles [styles] %event{>listeners<} and {other attributes}. |
|
| 223 | + * |
|
| 224 | + * @return \Htsl\Parser\Node\TagNode |
|
| 225 | + */ |
|
| 226 | + protected function parseCommonAttributes():string |
|
| 227 | + { |
|
| 228 | + $attributes = ''; |
|
| 229 | + |
|
| 230 | + $id = $this->line->pregGet('/ #([^ ]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 231 | + and $this->setAttribute('id', $id); |
|
| 232 | + |
|
| 233 | + $classes = $this->line->pregGet('/ \.[^ ]+(?= |$)/') |
|
| 234 | + and preg_match_all('/\.((?(?!\()[^.]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\))))/', $classes, $matches) |
|
| 235 | + and $classes = implode(' ', array_filter(array_map(function ($className) { |
|
| 236 | + return $this->checkExpression($className); |
|
| 237 | + }, $matches[1]))) |
|
| 238 | + and $this->setAttribute('class', $classes); |
|
| 239 | + |
|
| 240 | + $title = $this->line->pregGet('/ \^((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 241 | + and $this->setAttribute('title', $title); |
|
| 242 | + |
|
| 243 | + $style = $this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/', 1) |
|
| 244 | + and $this->setAttribute('style', $style); |
|
| 245 | + |
|
| 246 | + $eventListeners = $this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function ($string, $name, $code) { |
|
| 247 | + $this->setAttribute('on'.$name, str_replace('"', '"', $code)); |
|
| 248 | + }) |
|
| 249 | + and implode('', $eventListeners); |
|
| 250 | + |
|
| 251 | + $other = $this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
|
| 252 | + and array_map(function ($keyValue) { |
|
| 253 | + preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function ($matches) { |
|
| 254 | + $this->setAttribute($matches[1], ($matches[3] ?? $matches[1]) ?: $matches[1], $matches[2] ?? null); |
|
| 255 | + }, $keyValue); |
|
| 256 | + }, explode(';', $other)); |
|
| 257 | + |
|
| 258 | + return $attributes; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Checking and parse PHP expressions. |
|
| 263 | + * |
|
| 264 | + * @param string $value |
|
| 265 | + * |
|
| 266 | + * @return string |
|
| 267 | + */ |
|
| 268 | + protected function checkExpression(string $value):string |
|
| 269 | + { |
|
| 270 | + return preg_match('/^\(.*\)$/', $value) ? '<?='.substr($value, 1, -1).';?>' : str_replace('"', '"', $value); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Getting attribute string with HTML syntax. |
|
| 275 | + * |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + protected function getAttributesString():string |
|
| 279 | + { |
|
| 280 | + ksort($this->attributes); |
|
| 281 | + |
|
| 282 | + return implode('', array_map(static function (string $key, array $data) { |
|
| 283 | + return isset($data['condition']) && strlen($data['condition']) ? |
|
| 284 | + "<?php if( {$data['condition']} ){?> $key=\"{$data['value']}\"<?php }?>" |
|
| 285 | + : |
|
| 286 | + " $key=\"{$data['value']}\""; |
|
| 287 | + }, array_keys($this->attributes), $this->attributes)); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Setting attribute. |
|
| 292 | + * |
|
| 293 | + * @param string $key Attribute name. |
|
| 294 | + * @param string $value Attribute value |
|
| 295 | + * @param string|null $condition Optional condition, If given, attribute will seted only when condition is true. |
|
| 296 | + */ |
|
| 297 | + protected function setAttribute(string $key, string $value, string $condition = null):self |
|
| 298 | + { |
|
| 299 | + if (isset($this->attributes[$key])) { |
|
| 300 | + $this->document->throw("Attribute $key of $this->name cannot redeclare."); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + $this->attributes[$key] = [ |
|
| 304 | + 'value' => $value, |
|
| 305 | + 'condition' => $condition, |
|
| 306 | + ]; |
|
| 307 | + |
|
| 308 | + return $this; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /* *\ |
|
| 312 | 312 | ArrayAccess |
| 313 | 313 | \* */ |
| 314 | 314 | |
| 315 | - /** |
|
| 316 | - * Whether the attribute isset. |
|
| 317 | - * |
|
| 318 | - * @param mixed $offset |
|
| 319 | - * |
|
| 320 | - * @return bool |
|
| 321 | - */ |
|
| 322 | - public function offsetExists($offset):bool |
|
| 323 | - { |
|
| 324 | - return isset($this->attributes[$offset]); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Getting attribute with array access. |
|
| 329 | - * |
|
| 330 | - * @param mixed $offset |
|
| 331 | - * |
|
| 332 | - * @return mixed |
|
| 333 | - */ |
|
| 334 | - public function offsetGet($offset) |
|
| 335 | - { |
|
| 336 | - return $this->attributes[$offset] ?? null; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Setting Attribute with array access. |
|
| 341 | - * |
|
| 342 | - * @param mixed $offset |
|
| 343 | - * @param mixed $value |
|
| 344 | - */ |
|
| 345 | - public function offsetSet($offset, $value) |
|
| 346 | - { |
|
| 347 | - $this->setAttribute($offset, $value); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Unset an attribute with array access. |
|
| 352 | - * |
|
| 353 | - * @param mixed $offset |
|
| 354 | - */ |
|
| 355 | - public function offsetUnset($offset) |
|
| 356 | - { |
|
| 357 | - if (isset($this->attributes[$offset])) { |
|
| 358 | - unset($this->attributes[$offset]); |
|
| 359 | - } |
|
| 360 | - } |
|
| 315 | + /** |
|
| 316 | + * Whether the attribute isset. |
|
| 317 | + * |
|
| 318 | + * @param mixed $offset |
|
| 319 | + * |
|
| 320 | + * @return bool |
|
| 321 | + */ |
|
| 322 | + public function offsetExists($offset):bool |
|
| 323 | + { |
|
| 324 | + return isset($this->attributes[$offset]); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Getting attribute with array access. |
|
| 329 | + * |
|
| 330 | + * @param mixed $offset |
|
| 331 | + * |
|
| 332 | + * @return mixed |
|
| 333 | + */ |
|
| 334 | + public function offsetGet($offset) |
|
| 335 | + { |
|
| 336 | + return $this->attributes[$offset] ?? null; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Setting Attribute with array access. |
|
| 341 | + * |
|
| 342 | + * @param mixed $offset |
|
| 343 | + * @param mixed $value |
|
| 344 | + */ |
|
| 345 | + public function offsetSet($offset, $value) |
|
| 346 | + { |
|
| 347 | + $this->setAttribute($offset, $value); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Unset an attribute with array access. |
|
| 352 | + * |
|
| 353 | + * @param mixed $offset |
|
| 354 | + */ |
|
| 355 | + public function offsetUnset($offset) |
|
| 356 | + { |
|
| 357 | + if (isset($this->attributes[$offset])) { |
|
| 358 | + unset($this->attributes[$offset]); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | 361 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @var array |
| 31 | 31 | */ |
| 32 | - private $attributes = []; |
|
| 32 | + private $attributes=[ ]; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * Real constructor. |
@@ -38,14 +38,14 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | protected function construct():parent |
| 40 | 40 | { |
| 41 | - $name = $this->line->pregGet('/(?<=^-)[\w-:]+/'); |
|
| 42 | - $this->name = $name; |
|
| 41 | + $name=$this->line->pregGet('/(?<=^-)[\w-:]+/'); |
|
| 42 | + $this->name=$name; |
|
| 43 | 43 | |
| 44 | 44 | $this->loadConfig($name, $this->document); |
| 45 | 45 | |
| 46 | - $this->tagName = $this->config['name'] ?? $name; |
|
| 47 | - $this->isEmpty = $this->line->getChar(-1) === '/' || $this->document->getConfig('empty_tags', $this->tagName); |
|
| 48 | - isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'], function ($value, $key) { |
|
| 46 | + $this->tagName=$this->config[ 'name' ] ?? $name; |
|
| 47 | + $this->isEmpty=$this->line->getChar(-1)==='/' || $this->document->getConfig('empty_tags', $this->tagName); |
|
| 48 | + isset($this->config[ 'default_attributes' ]) and array_walk($this->config[ 'default_attributes' ], function( $value, $key ){ |
|
| 49 | 49 | $this->setAttribute($key, $value); |
| 50 | 50 | }); |
| 51 | 51 | |
@@ -59,37 +59,37 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function open():string |
| 61 | 61 | { |
| 62 | - if (isset($this->config['opener'])) { |
|
| 63 | - return $this->config['opener']; |
|
| 62 | + if( isset($this->config[ 'opener' ]) ){ |
|
| 63 | + return $this->config[ 'opener' ]; |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if (isset($this->config['params'])) { |
|
| 66 | + if( isset($this->config[ 'params' ]) ){ |
|
| 67 | 67 | $this->parseParams(); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - if (isset($this->config['name_value'])) { |
|
| 70 | + if( isset($this->config[ 'name_value' ]) ){ |
|
| 71 | 71 | $this->parseNameValue(); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if (isset($this->config['link'])) { |
|
| 74 | + if( isset($this->config[ 'link' ]) ){ |
|
| 75 | 75 | $this->parseLink(); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if (isset($this->config['target'])) { |
|
| 78 | + if( isset($this->config[ 'target' ]) ){ |
|
| 79 | 79 | $this->parseTarget(); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - if (isset($this->config['alt'])) { |
|
| 82 | + if( isset($this->config[ 'alt' ]) ){ |
|
| 83 | 83 | $this->parseAlt(); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | $this->parseCommonAttributes(); |
| 87 | 87 | |
| 88 | - if (isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function'])) { |
|
| 89 | - $this->config['scope_function']->call($this, $this->document->scope); |
|
| 88 | + if( isset($this->config[ 'in_scope' ]) && isset($this->config[ 'scope_function' ]) && is_callable($this->config[ 'scope_function' ]) ){ |
|
| 89 | + $this->config[ 'scope_function' ]->call($this, $this->document->scope); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - $finisher = $this->isEmpty ? ' />' : '>'; |
|
| 92 | + $finisher=$this->isEmpty? ' />' : '>'; |
|
| 93 | 93 | |
| 94 | 94 | return "<{$this->tagName}{$this->attributesString}{$finisher}"; |
| 95 | 95 | } |
@@ -101,9 +101,9 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @return string |
| 103 | 103 | */ |
| 104 | - public function close(Line $Line):string |
|
| 104 | + public function close( Line $Line ):string |
|
| 105 | 105 | { |
| 106 | - return $this->isEmpty ? '' : $this->config['closer'] ?? "</{$this->tagName}>"; |
|
| 106 | + return $this->isEmpty? '' : $this->config[ 'closer' ] ?? "</{$this->tagName}>"; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function getEmbed():string |
| 115 | 115 | { |
| 116 | - return $this->config['embedding'] ?? ''; |
|
| 116 | + return $this->config[ 'embedding' ] ?? ''; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | */ |
| 124 | 124 | public function getScope() |
| 125 | 125 | { |
| 126 | - return $this->config['scope'] ?? null; |
|
| 126 | + return $this->config[ 'scope' ] ?? null; |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -133,15 +133,15 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | protected function parseParams():self |
| 135 | 135 | { |
| 136 | - $params = preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
|
| 136 | + $params=preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
|
| 137 | 137 | |
| 138 | - if (($m = count($params)) != ($n = count($this->config['params']))) { |
|
| 138 | + if( ($m=count($params))!=($n=count($this->config[ 'params' ])) ){ |
|
| 139 | 139 | $this->document->throw("Tag $this->name has $n parameters $m given."); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - array_map(function ($key, $value) { |
|
| 142 | + array_map(function( $key, $value ){ |
|
| 143 | 143 | return $this->setAttribute($key, str_replace('\\|', '|', $value)); |
| 144 | - }, $this->config['params'], $params); |
|
| 144 | + }, $this->config[ 'params' ], $params); |
|
| 145 | 145 | |
| 146 | 146 | return $this; |
| 147 | 147 | } |
@@ -153,11 +153,11 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | protected function parseNameValue():self |
| 155 | 155 | { |
| 156 | - $params = $this->line->pregGet('/ <(.*?)>(?= |$)/', 1) |
|
| 157 | - and $params = preg_split('/(?<!\\\\)\\|/', $params) |
|
| 158 | - and array_map(function ($key, $value) { |
|
| 159 | - return isset($key) && isset($value) ? $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))) : ''; |
|
| 160 | - }, $this->config['name_value'], $params); |
|
| 156 | + $params=$this->line->pregGet('/ <(.*?)>(?= |$)/', 1) |
|
| 157 | + and $params=preg_split('/(?<!\\\\)\\|/', $params) |
|
| 158 | + and array_map(function( $key, $value ){ |
|
| 159 | + return isset($key) && isset($value)? $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))) : ''; |
|
| 160 | + }, $this->config[ 'name_value' ], $params); |
|
| 161 | 161 | |
| 162 | 162 | return $this; |
| 163 | 163 | } |
@@ -169,17 +169,17 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | protected function parseLink():self |
| 171 | 171 | { |
| 172 | - $link = $this->line->pregGet('/ @((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 173 | - |
|
| 174 | - if (strlen($link)) { |
|
| 175 | - if (isset($this->config['target']) && ':' === $link[0]) { |
|
| 176 | - $this->setAttribute($this->config['link'], 'javascript'.$link); |
|
| 177 | - } elseif ('//' === ($firstTwoLetters = substr($link, 0, 2))) { |
|
| 178 | - $this->setAttribute($this->config['link'], 'http:'.$link); |
|
| 179 | - } elseif ('\\\\' === $firstTwoLetters) { |
|
| 180 | - $this->setAttribute($this->config['link'], 'https://'.substr($link, 2)); |
|
| 181 | - } else { |
|
| 182 | - $this->setAttribute($this->config['link'], $this->checkExpression($link)); |
|
| 172 | + $link=$this->line->pregGet('/ @((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 173 | + |
|
| 174 | + if( strlen($link) ){ |
|
| 175 | + if( isset($this->config[ 'target' ]) && ':'===$link[ 0 ] ){ |
|
| 176 | + $this->setAttribute($this->config[ 'link' ], 'javascript'.$link); |
|
| 177 | + } elseif( '//'===($firstTwoLetters=substr($link, 0, 2)) ){ |
|
| 178 | + $this->setAttribute($this->config[ 'link' ], 'http:'.$link); |
|
| 179 | + } elseif( '\\\\'===$firstTwoLetters ){ |
|
| 180 | + $this->setAttribute($this->config[ 'link' ], 'https://'.substr($link, 2)); |
|
| 181 | + }else{ |
|
| 182 | + $this->setAttribute($this->config[ 'link' ], $this->checkExpression($link)); |
|
| 183 | 183 | } |
| 184 | 184 | } |
| 185 | 185 | |
@@ -193,10 +193,10 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | protected function parseTarget():self |
| 195 | 195 | { |
| 196 | - $target = $this->line->pregGet('/ >((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 196 | + $target=$this->line->pregGet('/ >((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 197 | 197 | |
| 198 | - if (strlen($target)) { |
|
| 199 | - $this->setAttribute($this->config['target'], $this->checkExpression($target)); |
|
| 198 | + if( strlen($target) ){ |
|
| 199 | + $this->setAttribute($this->config[ 'target' ], $this->checkExpression($target)); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | return $this; |
@@ -209,10 +209,10 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | protected function parseAlt():self |
| 211 | 211 | { |
| 212 | - $alt = $this->line->pregGet('/ _((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 212 | + $alt=$this->line->pregGet('/ _((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
|
| 213 | 213 | |
| 214 | - if (strlen($alt)) { |
|
| 215 | - $this->setAttribute($this->config['alt'], $this->checkExpression($alt)); |
|
| 214 | + if( strlen($alt) ){ |
|
| 215 | + $this->setAttribute($this->config[ 'alt' ], $this->checkExpression($alt)); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | return $this; |
@@ -225,33 +225,33 @@ discard block |
||
| 225 | 225 | */ |
| 226 | 226 | protected function parseCommonAttributes():string |
| 227 | 227 | { |
| 228 | - $attributes = ''; |
|
| 228 | + $attributes=''; |
|
| 229 | 229 | |
| 230 | - $id = $this->line->pregGet('/ #([^ ]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 230 | + $id=$this->line->pregGet('/ #([^ ]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 231 | 231 | and $this->setAttribute('id', $id); |
| 232 | 232 | |
| 233 | - $classes = $this->line->pregGet('/ \.[^ ]+(?= |$)/') |
|
| 233 | + $classes=$this->line->pregGet('/ \.[^ ]+(?= |$)/') |
|
| 234 | 234 | and preg_match_all('/\.((?(?!\()[^.]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\))))/', $classes, $matches) |
| 235 | - and $classes = implode(' ', array_filter(array_map(function ($className) { |
|
| 235 | + and $classes=implode(' ', array_filter(array_map(function( $className ){ |
|
| 236 | 236 | return $this->checkExpression($className); |
| 237 | - }, $matches[1]))) |
|
| 237 | + }, $matches[ 1 ]))) |
|
| 238 | 238 | and $this->setAttribute('class', $classes); |
| 239 | 239 | |
| 240 | - $title = $this->line->pregGet('/ \^((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 240 | + $title=$this->line->pregGet('/ \^((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1) |
|
| 241 | 241 | and $this->setAttribute('title', $title); |
| 242 | 242 | |
| 243 | - $style = $this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/', 1) |
|
| 243 | + $style=$this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/', 1) |
|
| 244 | 244 | and $this->setAttribute('style', $style); |
| 245 | 245 | |
| 246 | - $eventListeners = $this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function ($string, $name, $code) { |
|
| 246 | + $eventListeners=$this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function( $string, $name, $code ){ |
|
| 247 | 247 | $this->setAttribute('on'.$name, str_replace('"', '"', $code)); |
| 248 | 248 | }) |
| 249 | 249 | and implode('', $eventListeners); |
| 250 | 250 | |
| 251 | - $other = $this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
|
| 252 | - and array_map(function ($keyValue) { |
|
| 253 | - preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function ($matches) { |
|
| 254 | - $this->setAttribute($matches[1], ($matches[3] ?? $matches[1]) ?: $matches[1], $matches[2] ?? null); |
|
| 251 | + $other=$this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
|
| 252 | + and array_map(function( $keyValue ){ |
|
| 253 | + preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function( $matches ){ |
|
| 254 | + $this->setAttribute($matches[ 1 ], ($matches[ 3 ] ?? $matches[ 1 ])?: $matches[ 1 ], $matches[ 2 ] ?? null); |
|
| 255 | 255 | }, $keyValue); |
| 256 | 256 | }, explode(';', $other)); |
| 257 | 257 | |
@@ -265,9 +265,9 @@ discard block |
||
| 265 | 265 | * |
| 266 | 266 | * @return string |
| 267 | 267 | */ |
| 268 | - protected function checkExpression(string $value):string |
|
| 268 | + protected function checkExpression( string $value ):string |
|
| 269 | 269 | { |
| 270 | - return preg_match('/^\(.*\)$/', $value) ? '<?='.substr($value, 1, -1).';?>' : str_replace('"', '"', $value); |
|
| 270 | + return preg_match('/^\(.*\)$/', $value)? '<?='.substr($value, 1, -1).';?>' : str_replace('"', '"', $value); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | /** |
@@ -279,11 +279,11 @@ discard block |
||
| 279 | 279 | { |
| 280 | 280 | ksort($this->attributes); |
| 281 | 281 | |
| 282 | - return implode('', array_map(static function (string $key, array $data) { |
|
| 283 | - return isset($data['condition']) && strlen($data['condition']) ? |
|
| 284 | - "<?php if( {$data['condition']} ){?> $key=\"{$data['value']}\"<?php }?>" |
|
| 282 | + return implode('', array_map(static function( string $key, array $data ){ |
|
| 283 | + return isset($data[ 'condition' ]) && strlen($data[ 'condition' ])? |
|
| 284 | + "<?php if( {$data[ 'condition' ]} ){?> $key=\"{$data[ 'value' ]}\"<?php }?>" |
|
| 285 | 285 | : |
| 286 | - " $key=\"{$data['value']}\""; |
|
| 286 | + " $key=\"{$data[ 'value' ]}\""; |
|
| 287 | 287 | }, array_keys($this->attributes), $this->attributes)); |
| 288 | 288 | } |
| 289 | 289 | |
@@ -294,13 +294,13 @@ discard block |
||
| 294 | 294 | * @param string $value Attribute value |
| 295 | 295 | * @param string|null $condition Optional condition, If given, attribute will seted only when condition is true. |
| 296 | 296 | */ |
| 297 | - protected function setAttribute(string $key, string $value, string $condition = null):self |
|
| 297 | + protected function setAttribute( string $key, string $value, string $condition=null ):self |
|
| 298 | 298 | { |
| 299 | - if (isset($this->attributes[$key])) { |
|
| 299 | + if( isset($this->attributes[ $key ]) ){ |
|
| 300 | 300 | $this->document->throw("Attribute $key of $this->name cannot redeclare."); |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - $this->attributes[$key] = [ |
|
| 303 | + $this->attributes[ $key ]=[ |
|
| 304 | 304 | 'value' => $value, |
| 305 | 305 | 'condition' => $condition, |
| 306 | 306 | ]; |
@@ -319,9 +319,9 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @return bool |
| 321 | 321 | */ |
| 322 | - public function offsetExists($offset):bool |
|
| 322 | + public function offsetExists( $offset ):bool |
|
| 323 | 323 | { |
| 324 | - return isset($this->attributes[$offset]); |
|
| 324 | + return isset($this->attributes[ $offset ]); |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | /** |
@@ -331,9 +331,9 @@ discard block |
||
| 331 | 331 | * |
| 332 | 332 | * @return mixed |
| 333 | 333 | */ |
| 334 | - public function offsetGet($offset) |
|
| 334 | + public function offsetGet( $offset ) |
|
| 335 | 335 | { |
| 336 | - return $this->attributes[$offset] ?? null; |
|
| 336 | + return $this->attributes[ $offset ] ?? null; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | * @param mixed $offset |
| 343 | 343 | * @param mixed $value |
| 344 | 344 | */ |
| 345 | - public function offsetSet($offset, $value) |
|
| 345 | + public function offsetSet( $offset, $value ) |
|
| 346 | 346 | { |
| 347 | 347 | $this->setAttribute($offset, $value); |
| 348 | 348 | } |
@@ -352,10 +352,10 @@ discard block |
||
| 352 | 352 | * |
| 353 | 353 | * @param mixed $offset |
| 354 | 354 | */ |
| 355 | - public function offsetUnset($offset) |
|
| 355 | + public function offsetUnset( $offset ) |
|
| 356 | 356 | { |
| 357 | - if (isset($this->attributes[$offset])) { |
|
| 358 | - unset($this->attributes[$offset]); |
|
| 357 | + if( isset($this->attributes[ $offset ]) ){ |
|
| 358 | + unset($this->attributes[ $offset ]); |
|
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | 361 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | $this->tagName = $this->config['name'] ?? $name; |
| 47 | 47 | $this->isEmpty = $this->line->getChar(-1) === '/' || $this->document->getConfig('empty_tags', $this->tagName); |
| 48 | - isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'], function ($value, $key) { |
|
| 48 | + isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'], function ($value, $key){ |
|
| 49 | 49 | $this->setAttribute($key, $value); |
| 50 | 50 | }); |
| 51 | 51 | |
@@ -59,33 +59,33 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function open():string |
| 61 | 61 | { |
| 62 | - if (isset($this->config['opener'])) { |
|
| 62 | + if (isset($this->config['opener'])){ |
|
| 63 | 63 | return $this->config['opener']; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - if (isset($this->config['params'])) { |
|
| 66 | + if (isset($this->config['params'])){ |
|
| 67 | 67 | $this->parseParams(); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - if (isset($this->config['name_value'])) { |
|
| 70 | + if (isset($this->config['name_value'])){ |
|
| 71 | 71 | $this->parseNameValue(); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if (isset($this->config['link'])) { |
|
| 74 | + if (isset($this->config['link'])){ |
|
| 75 | 75 | $this->parseLink(); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if (isset($this->config['target'])) { |
|
| 78 | + if (isset($this->config['target'])){ |
|
| 79 | 79 | $this->parseTarget(); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - if (isset($this->config['alt'])) { |
|
| 82 | + if (isset($this->config['alt'])){ |
|
| 83 | 83 | $this->parseAlt(); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | $this->parseCommonAttributes(); |
| 87 | 87 | |
| 88 | - if (isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function'])) { |
|
| 88 | + if (isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function'])){ |
|
| 89 | 89 | $this->config['scope_function']->call($this, $this->document->scope); |
| 90 | 90 | } |
| 91 | 91 | |
@@ -135,11 +135,11 @@ discard block |
||
| 135 | 135 | { |
| 136 | 136 | $params = preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
| 137 | 137 | |
| 138 | - if (($m = count($params)) != ($n = count($this->config['params']))) { |
|
| 138 | + if (($m = count($params)) != ($n = count($this->config['params']))){ |
|
| 139 | 139 | $this->document->throw("Tag $this->name has $n parameters $m given."); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - array_map(function ($key, $value) { |
|
| 142 | + array_map(function ($key, $value){ |
|
| 143 | 143 | return $this->setAttribute($key, str_replace('\\|', '|', $value)); |
| 144 | 144 | }, $this->config['params'], $params); |
| 145 | 145 | |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | { |
| 156 | 156 | $params = $this->line->pregGet('/ <(.*?)>(?= |$)/', 1) |
| 157 | 157 | and $params = preg_split('/(?<!\\\\)\\|/', $params) |
| 158 | - and array_map(function ($key, $value) { |
|
| 158 | + and array_map(function ($key, $value){ |
|
| 159 | 159 | return isset($key) && isset($value) ? $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))) : ''; |
| 160 | 160 | }, $this->config['name_value'], $params); |
| 161 | 161 | |
@@ -171,14 +171,14 @@ discard block |
||
| 171 | 171 | { |
| 172 | 172 | $link = $this->line->pregGet('/ @((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
| 173 | 173 | |
| 174 | - if (strlen($link)) { |
|
| 175 | - if (isset($this->config['target']) && ':' === $link[0]) { |
|
| 174 | + if (strlen($link)){ |
|
| 175 | + if (isset($this->config['target']) && ':' === $link[0]){ |
|
| 176 | 176 | $this->setAttribute($this->config['link'], 'javascript'.$link); |
| 177 | - } elseif ('//' === ($firstTwoLetters = substr($link, 0, 2))) { |
|
| 177 | + } elseif ('//' === ($firstTwoLetters = substr($link, 0, 2))){ |
|
| 178 | 178 | $this->setAttribute($this->config['link'], 'http:'.$link); |
| 179 | - } elseif ('\\\\' === $firstTwoLetters) { |
|
| 179 | + } elseif ('\\\\' === $firstTwoLetters){ |
|
| 180 | 180 | $this->setAttribute($this->config['link'], 'https://'.substr($link, 2)); |
| 181 | - } else { |
|
| 181 | + } else{ |
|
| 182 | 182 | $this->setAttribute($this->config['link'], $this->checkExpression($link)); |
| 183 | 183 | } |
| 184 | 184 | } |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | { |
| 196 | 196 | $target = $this->line->pregGet('/ >((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
| 197 | 197 | |
| 198 | - if (strlen($target)) { |
|
| 198 | + if (strlen($target)){ |
|
| 199 | 199 | $this->setAttribute($this->config['target'], $this->checkExpression($target)); |
| 200 | 200 | } |
| 201 | 201 | |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | { |
| 212 | 212 | $alt = $this->line->pregGet('/ _((?!\()(?:[^ ]| (?=[a-zA-Z0-9]))+|(?<exp>\((?:[^()]+|(?&exp)?)+?\)))(?= |$)/', 1); |
| 213 | 213 | |
| 214 | - if (strlen($alt)) { |
|
| 214 | + if (strlen($alt)){ |
|
| 215 | 215 | $this->setAttribute($this->config['alt'], $this->checkExpression($alt)); |
| 216 | 216 | } |
| 217 | 217 | |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | |
| 233 | 233 | $classes = $this->line->pregGet('/ \.[^ ]+(?= |$)/') |
| 234 | 234 | and preg_match_all('/\.((?(?!\()[^.]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\))))/', $classes, $matches) |
| 235 | - and $classes = implode(' ', array_filter(array_map(function ($className) { |
|
| 235 | + and $classes = implode(' ', array_filter(array_map(function ($className){ |
|
| 236 | 236 | return $this->checkExpression($className); |
| 237 | 237 | }, $matches[1]))) |
| 238 | 238 | and $this->setAttribute('class', $classes); |
@@ -243,14 +243,14 @@ discard block |
||
| 243 | 243 | $style = $this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/', 1) |
| 244 | 244 | and $this->setAttribute('style', $style); |
| 245 | 245 | |
| 246 | - $eventListeners = $this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function ($string, $name, $code) { |
|
| 246 | + $eventListeners = $this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function ($string, $name, $code){ |
|
| 247 | 247 | $this->setAttribute('on'.$name, str_replace('"', '"', $code)); |
| 248 | 248 | }) |
| 249 | 249 | and implode('', $eventListeners); |
| 250 | 250 | |
| 251 | 251 | $other = $this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
| 252 | - and array_map(function ($keyValue) { |
|
| 253 | - preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function ($matches) { |
|
| 252 | + and array_map(function ($keyValue){ |
|
| 253 | + preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function ($matches){ |
|
| 254 | 254 | $this->setAttribute($matches[1], ($matches[3] ?? $matches[1]) ?: $matches[1], $matches[2] ?? null); |
| 255 | 255 | }, $keyValue); |
| 256 | 256 | }, explode(';', $other)); |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | { |
| 280 | 280 | ksort($this->attributes); |
| 281 | 281 | |
| 282 | - return implode('', array_map(static function (string $key, array $data) { |
|
| 282 | + return implode('', array_map(static function (string $key, array $data){ |
|
| 283 | 283 | return isset($data['condition']) && strlen($data['condition']) ? |
| 284 | 284 | "<?php if( {$data['condition']} ){?> $key=\"{$data['value']}\"<?php }?>" |
| 285 | 285 | : |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | */ |
| 297 | 297 | protected function setAttribute(string $key, string $value, string $condition = null):self |
| 298 | 298 | { |
| 299 | - if (isset($this->attributes[$key])) { |
|
| 299 | + if (isset($this->attributes[$key])){ |
|
| 300 | 300 | $this->document->throw("Attribute $key of $this->name cannot redeclare."); |
| 301 | 301 | } |
| 302 | 302 | |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | */ |
| 355 | 355 | public function offsetUnset($offset) |
| 356 | 356 | { |
| 357 | - if (isset($this->attributes[$offset])) { |
|
| 357 | + if (isset($this->attributes[$offset])){ |
|
| 358 | 358 | unset($this->attributes[$offset]); |
| 359 | 359 | } |
| 360 | 360 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | $params = preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
| 137 | 137 | |
| 138 | 138 | if (($m = count($params)) != ($n = count($this->config['params']))) { |
| 139 | - $this->document->throw("Tag $this->name has $n parameters $m given."); |
|
| 139 | + $this->document->throw("tag $this->name has $n parameters $m given."); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | array_map(function ($key, $value) { |
@@ -297,7 +297,7 @@ discard block |
||
| 297 | 297 | protected function setAttribute(string $key, string $value, string $condition = null):self |
| 298 | 298 | { |
| 299 | 299 | if (isset($this->attributes[$key])) { |
| 300 | - $this->document->throw("Attribute $key of $this->name cannot redeclare."); |
|
| 300 | + $this->document->throw("attribute $key of $this->name cannot redeclare."); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | $this->attributes[$key] = [ |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | /** |
| 41 | 41 | * Get a line of the document. |
| 42 | 42 | * |
| 43 | - * @return \Htsl\ReadingBuffer\Line |
|
| 43 | + * @return string |
|
| 44 | 44 | */ |
| 45 | 45 | public function getFileName():string |
| 46 | 46 | { |
@@ -50,8 +50,8 @@ discard block |
||
| 50 | 50 | /** |
| 51 | 51 | * Getting another file reference file of this buffer. |
| 52 | 52 | * |
| 53 | - * @param string $filePath |
|
| 54 | 53 | * |
| 54 | + * @param string $fileName |
|
| 55 | 55 | * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
| 56 | 56 | */ |
| 57 | 57 | abstract public function goSide($fileName):self; |
@@ -9,50 +9,50 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class ABuffer |
| 11 | 11 | { |
| 12 | - use TGetter; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * The htsl server. |
|
| 16 | - * |
|
| 17 | - * @var \Htsl\Htsl |
|
| 18 | - */ |
|
| 19 | - protected $htsl; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The file name. |
|
| 23 | - * |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - protected $fileName = ''; |
|
| 27 | - |
|
| 28 | - public function __construct(Htsl $htsl) |
|
| 29 | - { |
|
| 30 | - $this->htsl = $htsl; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Get a line of the document. |
|
| 35 | - * |
|
| 36 | - * @return \Htsl\ReadingBuffer\Line |
|
| 37 | - */ |
|
| 38 | - abstract public function getLine(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Get a line of the document. |
|
| 42 | - * |
|
| 43 | - * @return \Htsl\ReadingBuffer\Line |
|
| 44 | - */ |
|
| 45 | - public function getFileName():string |
|
| 46 | - { |
|
| 47 | - return $this->fileName; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Getting another file reference file of this buffer. |
|
| 52 | - * |
|
| 53 | - * @param string $filePath |
|
| 54 | - * |
|
| 55 | - * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 56 | - */ |
|
| 57 | - abstract public function goSide($fileName):self; |
|
| 12 | + use TGetter; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * The htsl server. |
|
| 16 | + * |
|
| 17 | + * @var \Htsl\Htsl |
|
| 18 | + */ |
|
| 19 | + protected $htsl; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The file name. |
|
| 23 | + * |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + protected $fileName = ''; |
|
| 27 | + |
|
| 28 | + public function __construct(Htsl $htsl) |
|
| 29 | + { |
|
| 30 | + $this->htsl = $htsl; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Get a line of the document. |
|
| 35 | + * |
|
| 36 | + * @return \Htsl\ReadingBuffer\Line |
|
| 37 | + */ |
|
| 38 | + abstract public function getLine(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Get a line of the document. |
|
| 42 | + * |
|
| 43 | + * @return \Htsl\ReadingBuffer\Line |
|
| 44 | + */ |
|
| 45 | + public function getFileName():string |
|
| 46 | + { |
|
| 47 | + return $this->fileName; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Getting another file reference file of this buffer. |
|
| 52 | + * |
|
| 53 | + * @param string $filePath |
|
| 54 | + * |
|
| 55 | + * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
|
| 56 | + */ |
|
| 57 | + abstract public function goSide($fileName):self; |
|
| 58 | 58 | } |
@@ -23,11 +23,11 @@ discard block |
||
| 23 | 23 | * |
| 24 | 24 | * @var string |
| 25 | 25 | */ |
| 26 | - protected $fileName = ''; |
|
| 26 | + protected $fileName=''; |
|
| 27 | 27 | |
| 28 | - public function __construct(Htsl $htsl) |
|
| 28 | + public function __construct( Htsl $htsl ) |
|
| 29 | 29 | { |
| 30 | - $this->htsl = $htsl; |
|
| 30 | + $this->htsl=$htsl; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
@@ -54,5 +54,5 @@ discard block |
||
| 54 | 54 | * |
| 55 | 55 | * @return \Htsl\ReadingBuffer\Contracts\ABuffer |
| 56 | 56 | */ |
| 57 | - abstract public function goSide($fileName):self; |
|
| 57 | + abstract public function goSide( $fileName ):self; |
|
| 58 | 58 | } |