From 4b013c2127075243420cd65c48c4cf6bcb9112b4 Mon Sep 17 00:00:00 2001 From: Antonio Date: Fri, 16 Aug 2019 14:08:17 +0200 Subject: [PATCH] improvement: deletes old code and update tests, close #93 --- docs/Type/Perl6/Documentable/Search.pod6 | 4 +- lib/Perl6/Documentable/CLI.pm6 | 5 ++- lib/Perl6/Documentable/Registry.pm6 | 55 +----------------------- t/305-search.t | 5 ++- 4 files changed, 12 insertions(+), 57 deletions(-) diff --git a/docs/Type/Perl6/Documentable/Search.pod6 b/docs/Type/Perl6/Documentable/Search.pod6 index 3d73635..2e746a1 100644 --- a/docs/Type/Perl6/Documentable/Search.pod6 +++ b/docs/Type/Perl6/Documentable/Search.pod6 @@ -1,4 +1,4 @@ -=begin pod :kind("type") :subkind("class") :category("basic") +=begin pod :kind("type") :subkind("class") :category("html-generation") =TITLE class Perl6::Documentable::Search @@ -71,6 +71,8 @@ url => url attribute =end item +=begin item + B this entries comes from L objects. One entry per found object. diff --git a/lib/Perl6/Documentable/CLI.pm6 b/lib/Perl6/Documentable/CLI.pm6 index 54f89fb..dac1e27 100644 --- a/lib/Perl6/Documentable/CLI.pm6 +++ b/lib/Perl6/Documentable/CLI.pm6 @@ -4,6 +4,7 @@ use Perl6::Documentable::Registry; use Perl6::Documentable::Config; use Perl6::Documentable::DocPage::Factory; +use Perl6::Documentable::Search; use Pod::Load; use Pod::To::Cached; @@ -184,7 +185,9 @@ package Perl6::Documentable::CLI { if ($search-index || $all ) { DEBUG("Writing search file...", $v); mkdir 'html/js'; - my @items = $registry.generate-search-index; + my $search-generator = Perl6::Documentable::Search.new(:$registry); + my @items = $search-generator.generate-entries; + say +@items; my $template = slurp("template/search_template.js"); $template = $template.subst("ITEMS", @items.join(",\n")) .subst("WARNING", "DO NOT EDIT generated by $?FILE:$?LINE"); diff --git a/lib/Perl6/Documentable/Registry.pm6 b/lib/Perl6/Documentable/Registry.pm6 index 850fceb..a18eb71 100644 --- a/lib/Perl6/Documentable/Registry.pm6 +++ b/lib/Perl6/Documentable/Registry.pm6 @@ -137,57 +137,4 @@ method docs-for(Str $name) { @!docs.grep({.name eq $name}) } -# ================================================================================= -# search index logic -# ================================================================================= - -method new-search-entry(Str :$category, Str :$value, Str :$url) { - qq[[\{ category: "{$category}", value: "{$value}", url: "{$url}" \}\n]] -} - -method generate-search-index() { - my @entries; - - for -> $kind { - @entries.append: self.lookup($kind, :by).map(-> $doc { - self.new-search-entry( - category => $doc.subkinds[0], - value => $doc.name, - url => $doc.url - ) - }).Slip; - } - - for -> $kind { - @entries.append: self.lookup($kind, :by) - .categorize({escape .name}) - .pairs.sort({.key}) - .map( -> (:key($name), :value(@docs)) { - self.new-search-entry( - category => @docs > 1 ?? $kind.gist !! @docs[0].subkinds[0] || '', - value => $name, - url => escape-json("/{$kind.lc}/{good-name($name)}") - ) - }); - } - - @entries.append: self.lookup('Reference', :by).map(-> $doc { - self.new-search-entry( - category => $doc.kind.gist, - value => escape($doc.name), - url => escape-json($doc.url) - ) - }).Slip; - - return @entries; -} - -#| We need to escape names like \. Otherwise, if we convert them to JSON, we -#| would have "\", and " would be escaped. -sub escape(Str $s) { - $s.trans([] => [<\\/ \\\\ \\">]); -} - -sub escape-json(Str $s) { - $s.subst(「\」, 「%5c」, :g).subst('"', '\"', :g).subst(「?」, 「%3F」, :g) -} \ No newline at end of file +# vim: expandtab shiftwidth=4 ft=perl6 \ No newline at end of file diff --git a/t/305-search.t b/t/305-search.t index 265942d..ec69eac 100644 --- a/t/305-search.t +++ b/t/305-search.t @@ -1,6 +1,7 @@ use Test; use Perl6::Documentable::Registry; +use Perl6::Documentable::Search; plan *; @@ -12,13 +13,15 @@ my $registry = Perl6::Documentable::Registry.new( $registry.compose; +my $search-generator = Perl6::Documentable::Search.new(:$registry); + my @expected = [ category => "Language", value => "Operators", url => "language/operators", category => "Language", value => "Perl 6 by example", url => "language/101-basics", category => "Language", value => "Terms", url => "language/terms" ]; -my @index = $registry.generate-search-index; +my @index = $search-generator.generate-entries; subtest "search index generation" => { for @index Z @expected -> $entry {