diff --git a/.env.example b/.env.example index d4a78c8..008b8b6 100644 --- a/.env.example +++ b/.env.example @@ -3,4 +3,6 @@ API_URL=http://example.com/api SOURCE= DIST= THEME= -HIGHTLIGHT= \ No newline at end of file +HIGHTLIGHT= +NAME_OUTPUT= +VERSION=v1 \ No newline at end of file diff --git a/README.md b/README.md index 4817e64..9de5687 100644 --- a/README.md +++ b/README.md @@ -37,15 +37,19 @@ SOURCE= DIST= THEME= HIGHTLIGHT= -``` - -| Variable | Description | Default | -|------------|-------------------------|------------------| -| API_URL | Base URL of requests | | -| SOURCE | Path the markdown files | ./docs | -| DIST | Destination folder | ./public | -| THEME | Theme name | ./themes/default | -| HIGHTLIGHT | Highlight style | dark | +NAME_OUTPUT= +VERSION= +``` + +| Variable | Description | Default | +|-------------|-------------------------|------------------| +| API_URL | Base URL of requests | | +| SOURCE | Path the markdown files | ./docs | +| DIST | Destination folder | ./public | +| THEME | Theme name | ./themes/default | +| HIGHTLIGHT | Highlight style | dark | +| NAME_OUTPUT | Name output file | index.html | +| VERSION | Version doc output | | The `API_URL` variable is used to not repeat the complete URL of the request in all markdown files. You can use it `{{API_URL}}`, for example: diff --git a/src/Vaneves/Apirus/Filesystem.php b/src/Vaneves/Apirus/Filesystem.php index f3ce2ff..816e0a6 100644 --- a/src/Vaneves/Apirus/Filesystem.php +++ b/src/Vaneves/Apirus/Filesystem.php @@ -1,9 +1,9 @@ -path); $structure = []; - foreach($iterator as $folder) { + foreach ($iterator as $folder) { if ($folder->isDir() && !preg_match($this->ignoreRegex, $folder->getFilename())) { if (!$folder->isReadable()) { $this->console->error("Directory {$folder->getPathname()} not is readable"); @@ -38,7 +38,7 @@ public function getStructure() $subiterator = new FilesystemIterator($folder->getPathname()); $files = []; - foreach($subiterator as $file) { + foreach ($subiterator as $file) { if ($file->isFile()) { if (!$file->isReadable()) { $this->console->error("File {$file->getPathname()} not is readable"); @@ -57,4 +57,4 @@ public function getStructure() return $structure; } -} \ No newline at end of file +} diff --git a/src/Vaneves/Apirus/Markdown.php b/src/Vaneves/Apirus/Markdown.php index 61bf99d..c51c1bb 100644 --- a/src/Vaneves/Apirus/Markdown.php +++ b/src/Vaneves/Apirus/Markdown.php @@ -1,9 +1,9 @@ -responses($markdown); return new Section( - $meta - , $content - , $params - , $requests - , $responses + $meta, + $content, + $params, + $requests, + $responses ); } - + protected function variable($text) { $text = preg_replace_callback($this->regex['variable'], function ($matches) { if (isset($matches[2])) { return env($matches[2]); } + return $matches[1]; }, $text); + return $text; } - protected function meta($text) + protected function meta($text) { preg_match($this->regex['meta'], $text, $matches); if (isset($matches[2])) { return Yaml::parse(trim($matches[2])); } + return []; } - protected function params($text) + protected function params($text) { preg_match_all($this->regex['param'], $text, $matches); if (isset($matches[3]) && isset($matches[4])) { @@ -69,10 +72,11 @@ protected function params($text) return array_combine($matches[3], $matches[4]); } } + return []; } - protected function requests($text) + protected function requests($text) { preg_match_all($this->regex['request'], $text, $matches); if (isset($matches[3]) && isset($matches[4])) { @@ -80,28 +84,31 @@ protected function requests($text) return array_combine($matches[3], $matches[4]); } } + return []; } - protected function responses($text) + protected function responses($text) { preg_match_all($this->regex['response'], $text, $matches); if (isset($matches[3]) && isset($matches[6])) { if (count($matches[3]) == count($matches[6])) { $result = []; foreach ($matches[3] as $i => $code) { - $result[$code . $matches[5][$i]] = [ + $result[$code.$matches[5][$i]] = [ 'code' => $code, 'lang' => $matches[5][$i], 'body' => $matches[6][$i], ]; } + return $result; } } + return []; } - + protected function removeMeta($text) { return preg_replace($this->regex['meta'], '', $text); @@ -121,4 +128,4 @@ protected function removeResponses($text) { return preg_replace($this->regex['response'], '', $text); } -} \ No newline at end of file +} diff --git a/src/Vaneves/Apirus/Processor.php b/src/Vaneves/Apirus/Processor.php index 25a2c5c..f36fc15 100644 --- a/src/Vaneves/Apirus/Processor.php +++ b/src/Vaneves/Apirus/Processor.php @@ -1,19 +1,19 @@ -parser = new Markdown(); $this->console = new CLImate(); - $file_env = realpath(__DIR__ . '/../../../.env'); + $file_env = realpath(__DIR__.'/../../../.env'); if (!file_exists($file_env)) { - $this->console->error("File .env not found"); + $this->console->error('File .env not found'); exit; } @@ -53,37 +53,37 @@ protected function arguments() { $this->console->arguments->add([ 'help' => [ - 'longPrefix' => 'help', + 'longPrefix' => 'help', 'description' => 'Prints a usage statement', - 'noValue' => true, + 'noValue' => true, ], 'watch' => [ - 'longPrefix' => 'watch', + 'longPrefix' => 'watch', 'description' => 'Watching files changes', - 'noValue' => true, + 'noValue' => true, ], 'src' => [ - 'prefix' => 's', - 'longPrefix' => 'src', - 'description' => 'Path the markdown files', + 'prefix' => 's', + 'longPrefix' => 'src', + 'description' => 'Path the markdown files', 'defaultValue' => env('SOURCE', 'docs'), ], 'dist' => [ - 'prefix' => 'd', - 'longPrefix' => 'dist', - 'description' => 'Destination folder', + 'prefix' => 'd', + 'longPrefix' => 'dist', + 'description' => 'Destination folder', 'defaultValue' => env('DIST', 'public'), ], 'theme' => [ - 'prefix' => 't', - 'longPrefix' => 'theme', - 'description' => 'Theme name', + 'prefix' => 't', + 'longPrefix' => 'theme', + 'description' => 'Theme name', 'defaultValue' => env('THEME', 'themes/default'), ], 'highlight' => [ - 'prefix' => 'h', - 'longPrefix' => 'highlight', - 'description' => 'Highlight style', + 'prefix' => 'h', + 'longPrefix' => 'highlight', + 'description' => 'Highlight style', 'defaultValue' => env('HIGHTLIGHT', 'dark'), ], ]); @@ -109,22 +109,22 @@ protected function arguments() $real_dist = realpath($dist); $real_theme = realpath($theme); - if ($real_src === false) { + if (false === $real_src) { $this->console->error("Directory {$src} not found"); exit; } - if ($real_dist === false) { + if (false === $real_dist) { $this->console->error("Directory {$dist} not found"); exit; } - if ($real_theme === false) { + if (false === $real_theme) { $this->console->error("Directory {$theme} not found"); exit; } - $this->pathSrc = rtrim($real_src, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; - $this->pathDist = rtrim($real_dist, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; - $this->pathTheme = rtrim($real_theme, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'layout.php'; + $this->pathSrc = rtrim($real_src, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; + $this->pathDist = rtrim($real_dist, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; + $this->pathTheme = rtrim($real_theme, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'layout.php'; $this->highlightTheme = $highlight; } @@ -179,7 +179,7 @@ public function build() $this->buildHtml(); if ($this->watch) { - $this->console->whisper("Watching files for changes..."); + $this->console->whisper('Watching files for changes...'); } } @@ -238,6 +238,7 @@ protected function reprocessParams($params) 'params' => $yaml, ]); } + return $result; } @@ -251,27 +252,28 @@ protected function reprocessRequests($requests) foreach ($requests as $lang => $text) { $language = strtolower($lang); try { - $l = $language == 'curl' ? 'bash' : $language; + $l = 'curl' == $language ? 'bash' : $language; $highlighted = $highlighter->highlight($l, $text); $body = "
language}\">";
-                $body .=  $highlighted->value;
-                $body .=  "
"; + $body .= $highlighted->value; + $body .= ''; } catch (\Exception $e) { $markdown = "```{$language}\n{$text}\n```"; $body = $parsedown->text($markdown); $this->console->comment("Highlight to lang {$language} not found"); } - + array_push($result, [ 'first' => $first, - 'hash' => 'request-' . $language .'-'. md5(uniqid(rand(0, 99999), true)), + 'hash' => 'request-'.$language.'-'.md5(uniqid(rand(0, 99999), true)), 'lang' => $lang, 'body' => $body, ]); $first = false; } + return $result; } @@ -289,8 +291,8 @@ protected function reprocessResponses($responses) $highlighted = $highlighter->highlight($language, $response['body']); $body = "
language}\">";
-                $body .=  $highlighted->value;
-                $body .=  "
"; + $body .= $highlighted->value; + $body .= ''; } catch (\Exception $e) { $markdown = "```{$language}\n{$response['body']}\n```"; $body = $parsedown->text($markdown); @@ -302,17 +304,18 @@ protected function reprocessResponses($responses) array_push($result, [ 'first' => $first, - 'hash' => 'response-' . $code .'-'. md5(uniqid(rand(0, 99999), true)), + 'hash' => 'response-'.$code.'-'.md5(uniqid(rand(0, 99999), true)), 'code' => $code, 'lang' => $response['lang'], 'body' => $body, ]); $first = false; } + return $result; } - protected function extractName($text) + protected function extractName($text) { $regex = '/^([\d]{0,3}([\s\-]+)?)?([^.]+)(\.md)?$/i'; $matches = []; @@ -321,10 +324,11 @@ protected function extractName($text) return $matches[3]; } } + return $text; } - - protected function extractSlug($text) + + protected function extractSlug($text) { $text = preg_replace('~[^\pL\d]+~u', '-', $text); $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); @@ -335,6 +339,7 @@ protected function extractSlug($text) if (empty($text)) { return 'n-a'; } + return $text; } @@ -343,10 +348,11 @@ protected function runTosko() $tosko = './tosko.php'; if (!file_exists($tosko)) { - $this->console->whisper("Tosko disabled"); + $this->console->whisper('Tosko disabled'); + return; } - $this->console->whisper("Running tosko"); + $this->console->whisper('Running tosko'); include $tosko; } @@ -359,7 +365,7 @@ protected function buildHtml() } $this->console->whisper("Building theme {$this->pathTheme}"); - + $menu = $this->menu; $items = $this->items; @@ -377,16 +383,19 @@ protected function buildHtml() $this->console->error("Directory {$this->pathDist} not found"); exit; } + if (!is_dir($this->pathDist.env('VERSION', ''))) { + mkdir($this->pathDist.env('VERSION', ''), 0777, true); + } - $dist = $this->pathDist . 'index.html'; + $dist = $this->pathDist.env('VERSION', '').DIRECTORY_SEPARATOR.env('NAME_OUTPUT', 'index').'.html'; $this->console->whisper("Writing output {$dist}"); $ok = file_put_contents($dist, $html); if ($ok) { - $this->console->info("Build successful"); + $this->console->info('Build successful'); } else { $this->console->error("Error on write file {$dist}"); } } -} \ No newline at end of file +} diff --git a/src/Vaneves/Apirus/Section.php b/src/Vaneves/Apirus/Section.php index b0cf6d8..1341602 100644 --- a/src/Vaneves/Apirus/Section.php +++ b/src/Vaneves/Apirus/Section.php @@ -1,4 +1,4 @@ -meta = $meta; $this->content = $content; @@ -48,4 +48,4 @@ public function responses() { return $this->responses; } -} \ No newline at end of file +} diff --git a/src/Vaneves/Apirus/Watcher.php b/src/Vaneves/Apirus/Watcher.php index e13d499..bd1c22e 100644 --- a/src/Vaneves/Apirus/Watcher.php +++ b/src/Vaneves/Apirus/Watcher.php @@ -1,9 +1,8 @@ -console->error("Param is not callable"); + $this->console->error('Param is not callable'); exit; } $this->callback = $callback; $this->hashs = $this->getFiles(); - while(true) { + while (true) { $this->checkDoc(); sleep(2); } } -} \ No newline at end of file +} diff --git a/src/Vaneves/Apirus/functions.php b/src/Vaneves/Apirus/functions.php index 147c518..a309b37 100644 --- a/src/Vaneves/Apirus/functions.php +++ b/src/Vaneves/Apirus/functions.php @@ -1,18 +1,19 @@ - strtolower($b['name']); } -} \ No newline at end of file +}