From 2f8383b82567b09e4e3971359850cb2ca6613827 Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sat, 27 Nov 2021 22:59:59 -0300 Subject: [PATCH 1/7] Add NAME_OUTPUT --- .env.example | 3 +- src/Vaneves/Apirus/Markdown.php | 39 ++++++----- src/Vaneves/Apirus/Processor.php | 108 +++++++++++++++++-------------- 3 files changed, 85 insertions(+), 65 deletions(-) diff --git a/.env.example b/.env.example index d4a78c8..db1232a 100644 --- a/.env.example +++ b/.env.example @@ -3,4 +3,5 @@ API_URL=http://example.com/api SOURCE= DIST= THEME= -HIGHTLIGHT= \ No newline at end of file +HIGHTLIGHT= +NAME_OUTPUT \ 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..84a828c 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,39 +53,45 @@ 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'), ], + 'name_otput' => [ + 'prefix' => 'n', + 'longPrefix' => 'name_otput', + 'description' => 'Name of output file', + 'defaultValue' => env('NAME_OUTPUT', 'index'), + ], ]); $this->console->arguments->parse(); @@ -109,22 +115,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 +185,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 +244,7 @@ protected function reprocessParams($params) 'params' => $yaml, ]); } + return $result; } @@ -251,27 +258,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 +297,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 +310,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 +330,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 +345,7 @@ protected function extractSlug($text) if (empty($text)) { return 'n-a'; } + return $text; } @@ -343,10 +354,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 +371,7 @@ protected function buildHtml() } $this->console->whisper("Building theme {$this->pathTheme}"); - + $menu = $this->menu; $items = $this->items; @@ -378,15 +390,15 @@ protected function buildHtml() exit; } - $dist = $this->pathDist . 'index.html'; + $dist = $this->pathDist.env('NAME_OUTPUT').'.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 +} From f5e0ce11cc90ab428d96308c26dd23e094576527 Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sat, 27 Nov 2021 23:01:43 -0300 Subject: [PATCH 2/7] fix name_output --- .env.example | 2 +- src/Vaneves/Apirus/Processor.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index db1232a..f226a1d 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,4 @@ SOURCE= DIST= THEME= HIGHTLIGHT= -NAME_OUTPUT \ No newline at end of file +NAME_OUTPUT= \ No newline at end of file diff --git a/src/Vaneves/Apirus/Processor.php b/src/Vaneves/Apirus/Processor.php index 84a828c..69af91c 100644 --- a/src/Vaneves/Apirus/Processor.php +++ b/src/Vaneves/Apirus/Processor.php @@ -390,7 +390,7 @@ protected function buildHtml() exit; } - $dist = $this->pathDist.env('NAME_OUTPUT').'.html'; + $dist = $this->pathDist.env('NAME_OUTPUT', 'index').'.html'; $this->console->whisper("Writing output {$dist}"); From a1388e89e63d7750df75b51eb7ab6577975406b1 Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sat, 27 Nov 2021 23:13:34 -0300 Subject: [PATCH 3/7] add version output --- .env.example | 3 ++- src/Vaneves/Apirus/Processor.php | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index f226a1d..008b8b6 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,5 @@ SOURCE= DIST= THEME= HIGHTLIGHT= -NAME_OUTPUT= \ No newline at end of file +NAME_OUTPUT= +VERSION=v1 \ No newline at end of file diff --git a/src/Vaneves/Apirus/Processor.php b/src/Vaneves/Apirus/Processor.php index 69af91c..1e8d7d7 100644 --- a/src/Vaneves/Apirus/Processor.php +++ b/src/Vaneves/Apirus/Processor.php @@ -92,6 +92,12 @@ protected function arguments() 'description' => 'Name of output file', 'defaultValue' => env('NAME_OUTPUT', 'index'), ], + 'version' => [ + 'prefix' => 'v', + 'longPrefix' => 'version', + 'description' => 'Version of output file', + 'defaultValue' => env('VERSION', ''), + ], ]); $this->console->arguments->parse(); @@ -389,8 +395,11 @@ 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.env('NAME_OUTPUT', 'index').'.html'; + $dist = $this->pathDist.env('VERSION', '').DIRECTORY_SEPARATOR.env('NAME_OUTPUT', 'index').'.html'; $this->console->whisper("Writing output {$dist}"); From 6f39f3d9fe6b04788041b73f7b92b9b5f22d2700 Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sat, 27 Nov 2021 23:21:58 -0300 Subject: [PATCH 4/7] remove unnecessary items --- src/Vaneves/Apirus/Processor.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Vaneves/Apirus/Processor.php b/src/Vaneves/Apirus/Processor.php index 1e8d7d7..65c7d98 100644 --- a/src/Vaneves/Apirus/Processor.php +++ b/src/Vaneves/Apirus/Processor.php @@ -86,18 +86,6 @@ protected function arguments() 'description' => 'Highlight style', 'defaultValue' => env('HIGHTLIGHT', 'dark'), ], - 'name_otput' => [ - 'prefix' => 'n', - 'longPrefix' => 'name_otput', - 'description' => 'Name of output file', - 'defaultValue' => env('NAME_OUTPUT', 'index'), - ], - 'version' => [ - 'prefix' => 'v', - 'longPrefix' => 'version', - 'description' => 'Version of output file', - 'defaultValue' => env('VERSION', ''), - ], ]); $this->console->arguments->parse(); @@ -117,10 +105,12 @@ protected function arguments() $theme = $this->console->arguments->get('theme'); $highlight = $this->console->arguments->get('highlight'); + $real_src = realpath($src); $real_dist = realpath($dist); $real_theme = realpath($theme); + if (false === $real_src) { $this->console->error("Directory {$src} not found"); exit; From 967966e7cb34302fe000656ddb92e7b103e59370 Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sat, 27 Nov 2021 23:48:07 -0300 Subject: [PATCH 5/7] fix psr-12 --- src/Vaneves/Apirus/Filesystem.php | 12 ++++++------ src/Vaneves/Apirus/Processor.php | 2 -- src/Vaneves/Apirus/Section.php | 14 +++++++------- src/Vaneves/Apirus/Watcher.php | 12 ++++++------ src/Vaneves/Apirus/functions.php | 9 +++++---- 5 files changed, 24 insertions(+), 25 deletions(-) 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/Processor.php b/src/Vaneves/Apirus/Processor.php index 65c7d98..f36fc15 100644 --- a/src/Vaneves/Apirus/Processor.php +++ b/src/Vaneves/Apirus/Processor.php @@ -105,12 +105,10 @@ protected function arguments() $theme = $this->console->arguments->get('theme'); $highlight = $this->console->arguments->get('highlight'); - $real_src = realpath($src); $real_dist = realpath($dist); $real_theme = realpath($theme); - if (false === $real_src) { $this->console->error("Directory {$src} not found"); exit; 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 +} From ba5a7ed2ceca68f40898708a4d69d4f2175358e7 Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sat, 27 Nov 2021 23:57:19 -0300 Subject: [PATCH 6/7] update doc NAME_OUTPUT and VERSION --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4817e64..498d665 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ SOURCE= DIST= THEME= HIGHTLIGHT= +NAME_OUTPUT= +VERSION= ``` | Variable | Description | Default | From 7cbd92b4edbaf8c52ff02d02b5d8a16b84b5c78b Mon Sep 17 00:00:00 2001 From: Renan de Souza Oliveira Date: Sun, 28 Nov 2021 00:06:37 -0300 Subject: [PATCH 7/7] update readme.md --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 498d665..9de5687 100644 --- a/README.md +++ b/README.md @@ -41,13 +41,15 @@ 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 | +| 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: