Skip to content

Commit

Permalink
improvement: deletes old code and update tests, close #93
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniogamiz committed Aug 16, 2019
1 parent ba8bd2b commit 4b013c2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 57 deletions.
4 changes: 3 additions & 1 deletion docs/Type/Perl6/Documentable/Search.pod6
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -71,6 +71,8 @@ url => url attribute
=end item
=begin item
B<Reference entries:> this entries comes from L<Perl6::Documentable::Index|/type/Perl6::Documentable::Index> objects.
One entry per found object.
Expand Down
5 changes: 4 additions & 1 deletion lib/Perl6/Documentable/CLI.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
55 changes: 1 addition & 54 deletions lib/Perl6/Documentable/Registry.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Type Language Programs> -> $kind {
@entries.append: self.lookup($kind, :by<kind>).map(-> $doc {
self.new-search-entry(
category => $doc.subkinds[0],
value => $doc.name,
url => $doc.url
)
}).Slip;
}

for <Routine Syntax> -> $kind {
@entries.append: self.lookup($kind, :by<kind>)
.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<kind>).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)
}
# vim: expandtab shiftwidth=4 ft=perl6
5 changes: 4 additions & 1 deletion t/305-search.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use Test;

use Perl6::Documentable::Registry;
use Perl6::Documentable::Search;

plan *;

Expand All @@ -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 {
Expand Down

0 comments on commit 4b013c2

Please sign in to comment.