Skip to content

Commit c7e0805

Browse files
committed
- move escape-filename to support module
- add new URL escape sub
1 parent 45b9d06 commit c7e0805

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

htmlify.p6

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,13 @@ use Pod::Htmlify;
3838
});
3939

4040
my @__URLS;
41-
&url-munge.wrap(sub (|c){
41+
&rewrite-url.wrap(sub (|c){
4242
@__URLS.push: uri-unescape(c[0]);
4343
callsame
4444
});
4545

4646
use experimental :cached;
4747

48-
sub escape-filename($s is copy) {
49-
return $s if $s ~~ m{^ <[a..z]>+ '://'}; # bail on external links
50-
constant badchars = qw[$ / \ . % ? & = # + " ' : ~ < >];
51-
constant goodnames = badchars.map: '$' ~ *.uniname.subst(' ', '_', :g);
52-
constant length = badchars.elems;
53-
54-
loop (my int $i = 0;$i < length;$i++) {
55-
$s = $s.subst(badchars[$i], goodnames[$i], :g)
56-
}
57-
58-
$s
59-
}
60-
6148
my $type-graph;
6249
my %routines-by-type;
6350
my %*POD2HTML-CALLBACKS;
@@ -107,7 +94,7 @@ sub header-html($current-selection = 'nothing selected') is cached {
10794

10895
sub p2h($pod, $selection = 'nothing selected', :$pod-path = 'unknown') {
10996
pod2html $pod,
110-
:url(&url-munge),
97+
:url(&rewrite-url),
11198
:$head,
11299
:header(header-html $selection),
113100
:footer(footer-html($pod-path)),

lib/Pod/Htmlify.pm6

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,56 @@ sub url-munge($_) is export {
1414
return $_;
1515
}
1616

17+
constant badchars = qw[$ / \ . % ? & = # + " ' : ~ < >];
18+
my \goodnames = badchars.map: '$' ~ *.uniname.subst(' ', '_', :g);
19+
constant length = badchars.elems;
20+
21+
sub escape-filename($s is copy) is export {
22+
# return $s if $s ~~ m{^ <[a..z]>+ '://'}; # bail on external links
23+
24+
loop (my int $i = 0;$i < length;$i++) {
25+
$s = $s.subst(badchars[$i], goodnames[$i], :g)
26+
}
27+
28+
$s
29+
}
30+
31+
sub rewrite-url($s) is export {
32+
my Str $r;
33+
given $s {
34+
when / ^ [ 'http' | 'https' | 'irc' ] '://' / {
35+
# external link, we bail
36+
return $s;
37+
}
38+
39+
when / ^ '#' / {
40+
# on-page link, we bail
41+
return $s;
42+
}
43+
44+
# special case the really nasty ones
45+
when / ^ '/routine//' $ / { return '/routine/' ~ escape-filename('/'); succeed; }
46+
when / ^ '/routine///' $ / { return '/routine/' ~ escape-filename('//'); succeed; }
47+
48+
when / ^ ([ '/routine/' | '/syntax/' | '/language/' | '/programs/' | '/type/' ]) (<-[#/]>+) [ ('#') (<-[/#]>+) ]* $ / {
49+
$r = $0 ~ escape-filename($1) ~ $2 ~ uri_escape($3);
50+
succeed;
51+
}
52+
53+
default {
54+
my @parts = $s.split('#');
55+
$r = escape-filename(@parts[0]) ~ '#' ~ uri_escape(@parts[1]) if @parts[1];
56+
$r = escape-filename(@parts[0]) unless @parts[1];
57+
}
58+
}
59+
60+
my $file-part = $r.split('#')[0] ~ '.html';
61+
62+
die "$file-part not found" unless $file-part.IO:e:f:s;
63+
64+
return $r;
65+
}
66+
1767
#| Return the footer HTML for each page
1868
sub footer-html($pod-path) is export {
1969
my $footer = slurp 'template/footer.html';

0 commit comments

Comments
 (0)