diff --git a/bin/asd b/bin/asd index 979448d8..f4abbe99 100755 --- a/bin/asd +++ b/bin/asd @@ -71,7 +71,6 @@ if ($status !== 0) { echo 'Warning: Graphviz error. https://graphviz.org/download/' . PHP_EOL; } -$ext = 'html'; $index = new IndexPage($profile, $config->outputMode); file_put_contents($index->file, $index->content); echo "ASD generated. {$index->file}" . PHP_EOL; diff --git a/src/IndexPage.php b/src/IndexPage.php index 141a375a..c3f028c2 100644 --- a/src/IndexPage.php +++ b/src/IndexPage.php @@ -10,7 +10,6 @@ use function nl2br; use function pathinfo; use function sprintf; -use function str_replace; use function strtoupper; use function uasort; @@ -40,8 +39,9 @@ public function __construct(Profile $profile, string $mode = DumpDocs::MODE_HTML return $order[$a->type] <=> $order[$b->type]; }); $linkRelations = $this->linkRelations($profile->linkRelations); - $semantics = $this->semantics($descriptors); - $tags = $this->tags($profile->tags); + $ext = $mode === DumpDocs::MODE_MARKDOWN ? 'md' : DumpDocs::MODE_HTML; + $semantics = $this->semantics($descriptors, $ext); + $tags = $this->tags($profile->tags, $ext); $htmlTitle = htmlspecialchars($profile->title); $htmlDoc = nl2br(htmlspecialchars($profile->doc)); $profileImage = $mode === DumpDocs::MODE_HTML ? 'docs/asd.html' : 'profile.svg'; @@ -55,26 +55,18 @@ public function __construct(Profile $profile, string $mode = DumpDocs::MODE_HTML * Semantic Descriptors {$semantics}{$tags}{$linkRelations} EOT; - $fileBase = sprintf('%s/index.', dirname($profile->alpsFile)); - if ($mode === DumpDocs::MODE_MARKDOWN) { - $this->content = str_replace('.html', '.md', $md); - $this->file = $fileBase . 'md'; - - return; - } - - $this->content = (new MdToHtml())('ALPS', $md); - $this->file = $fileBase . 'html'; + $this->file = sprintf('%s/index.%s', dirname($profile->alpsFile), $ext); + $this->content = $mode === DumpDocs::MODE_MARKDOWN ? $md : (new MdToHtml())('ALPS', $md); } /** * @param array $semantics */ - private function semantics(array $semantics): string + private function semantics(array $semantics, string $ext): string { $lines = []; foreach ($semantics as $semantic) { - $href = sprintf('docs/%s.%s.html', $semantic->type, $semantic->id); + $href = sprintf('docs/%s.%s.%s', $semantic->type, $semantic->id, $ext); $title = $semantic->title ? sprintf(', %s', $semantic->title) : ''; $lines[] = sprintf(' * [%s](%s) (%s)%s', $semantic->id, $href, $semantic->type, $title); } @@ -85,7 +77,7 @@ private function semantics(array $semantics): string /** * @param array> $tags */ - private function tags(array $tags): string + private function tags(array $tags, string $ext): string { if ($tags === []) { return ''; @@ -93,7 +85,7 @@ private function tags(array $tags): string $lines = []; foreach ($tags as $tag => $item) { - $href = "docs/tag.{$tag}.html"; + $href = "docs/tag.{$tag}.{$ext}"; $lines[] = " * [{$tag}]({$href})"; } diff --git a/tests/Fake/alps_has_single_link.json b/tests/Fake/alps_has_single_link.json index 22f31cea..bf4da789 100644 --- a/tests/Fake/alps_has_single_link.json +++ b/tests/Fake/alps_has_single_link.json @@ -2,7 +2,7 @@ "$schema": "https://alps-io.github.io/schemas/alps.json", "alps": { "link": { - "href": "https://github.com/koriym/app-state-diagram/", + "href": "https://github.com/koriym/app-state-diagram/index.html", "rel": "about" }, "descriptor": [ diff --git a/tests/IndexPageTest.php b/tests/IndexPageTest.php index 6dead157..4fbe3563 100644 --- a/tests/IndexPageTest.php +++ b/tests/IndexPageTest.php @@ -60,7 +60,14 @@ public function testLinkRelationsString(): void { $alpsFile = __DIR__ . '/Fake/alps_has_single_link.json'; $html = (new IndexPage(new Profile($alpsFile, new LabelName())))->content; - $this->assertStringContainsString('
  • rel: about https://github.com/koriym/app-state-diagram/
  • ', $html); + $this->assertStringContainsString('
  • rel: about https://github.com/koriym/app-state-diagram/index.html
  • ', $html); + } + + public function testLinkRelationsStringMarkdownMode(): void + { + $alpsFile = __DIR__ . '/Fake/alps_has_single_link.json'; + $md = (new IndexPage(new Profile($alpsFile, new LabelName()), DumpDocs::MODE_MARKDOWN))->content; + $this->assertStringContainsString('* rel: about https://github.com/koriym/app-state-diagram/index.html', $md); } public function testMultipleLinkRelationsString(): void