Skip to content

Commit

Permalink
Sort out situation with escaping URLs
Browse files Browse the repository at this point in the history
And streamline forming of the search index entry
fragment URL.

Since the beginning, results of the url method for search index
entries and secondary pages were not percent encoded,
but instead did sort of escaping to work well with
the "static files on a UNIX file system" assumption.

The Index one also relied on various components of the X<>
formatting code, which is not desireable, instead it has to
refer only to exact text of the index entry itself, so that
other components, category and text, could be changed without
creating a 404 case.

This commit adds percent encoding to ensure:
- URLs we form for both anchors and secondary are understood
  properly by browsers, text editors and various software where
  a clickable URL is good
- We don't rely on various hacks to serve such URLs
  • Loading branch information
Altai-man committed Jul 30, 2022
1 parent 6280206 commit 00c4432
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
13 changes: 8 additions & 5 deletions lib/Documentable/Index.pm6
Expand Up @@ -2,6 +2,7 @@ use Documentable;
use Documentable::Utils::Text;
use Pod::Utilities;
use Pod::Utilities::Build;
use URI::Escape;

unit class Documentable::Index is Documentable;

Expand Down Expand Up @@ -33,12 +34,14 @@ method new(
}

method url() {
my $index-text = recurse-until-str($.pod).join;
my @indices = $.pod.meta;
my $fragment = qq[index-entry{@indices ?? "-@indices.map(*[1]).join('-')" !! ''}{$index-text ?? '-' !! ''}$index-text]
.subst('_', '__', :g).subst(' ', '_', :g);

return $.origin.url ~ "#" ~ good-name($fragment);
# meta is by default a list of lists, so take the first element
# and then take `foo` part of `X<Text?|Category,foo>`.
my $index-entry-text = @indices[0][1];
my $fragment = "index-entry-$index-entry-text";
# don't forget to properly HTML escape the fragment, as it might
# contain various special characters
return $.origin.url ~ "#" ~ uri-escape($fragment);
}

# vim: expandtab shiftwidth=4 ft=perl6
8 changes: 4 additions & 4 deletions lib/Documentable/Search.pm6
@@ -1,4 +1,5 @@
use Documentable;
use URI::Escape;

class Documentable::Search {

Expand All @@ -20,11 +21,11 @@ class Documentable::Search {
.categorize({ .name })
.pairs.sort({ .key })
.map(-> (:key($name), :value(@docs)) {
if @docs.elems > 1 {
if @docs.elems >= 1 {
my $category = self.calculate-category(@docs, $kind);
self.search-entry(
:$category,
value => escape($name), url => escape-json("/{ $kind.lc }/{ good-name($name) }"))
value => escape($name), url => escape-json("/{ $kind.lc }/{ uri-escape($name) }"))
} else {
Slip.new;
}
Expand Down Expand Up @@ -68,9 +69,8 @@ class Documentable::Search {
method search-entry(Str :$category, Str :$value, Str :$url is copy) {
$url = $.prefix ?? "/" ~ $.prefix ~ $url !! $url;
if ($url ~~ /\.$/) { $url = "{$url}.html" }
qq[[\{ category: "{ $category }", value: "{ $value }", url: "{ $url }" \}\n]]
qq[[\{ "category": "{ $category }", "value": "{ $value }", "url": "{ $url }" \}\n]]
}

}

#| We need to escape names like \. Otherwise, if we convert them to JSON, we
Expand Down
3 changes: 2 additions & 1 deletion lib/Documentable/Secondary.pm6
Expand Up @@ -2,6 +2,7 @@ use Documentable;
use Documentable::Utils::Text;
use Pod::Utilities;
use Pod::Utilities::Build;
use URI::Escape;

class Documentable::Secondary is Documentable {

Expand All @@ -18,7 +19,7 @@ class Documentable::Secondary is Documentable {
:$origin
) {

my $url = "/{$kind.Str.lc}/{good-name($name)}";
my $url = "/{$kind.Str.lc}/{uri-escape($name)}";
my $url-in-origin = $origin.url ~ "#" ~textify-pod($pod[0]).trim.subst(/\s+/, '_', :g);

# normalize the pod
Expand Down

0 comments on commit 00c4432

Please sign in to comment.