Skip to content

Commit 2f55811

Browse files
committed
Code clean-up
1 parent 9400c44 commit 2f55811

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

htmlify.p6

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ my @menu =
3838

3939
my $head = slurp 'template/head.html';
4040
my $footer = footer-html;
41-
sub header-html ($current-selection = 'nothing selected') {
41+
sub header-html ($current-selection = 'nothing selected') is cached {
4242
state $header = slurp 'template/header.html';
4343

4444
my $menu-items = [~]
@@ -139,40 +139,12 @@ sub MAIN(Bool :$debug, Bool :$typegraph = False) {
139139

140140
my $dr = Perl6::Documentable::Registry.new;
141141

142-
say 'Reading lib/Language ...';
143-
my @lang-doc-sources =
144-
recursive-dir('lib/Language/')\
145-
.map({; .path.subst('lib/Language/', '').subst(rx{\.pod$}, '') => $_ })\
146-
.sort;
147-
148-
say 'Processing Language Pod files ...';
149-
for @lang-doc-sources.kv -> $num, (:key($podname), :value($file)) {
150-
printf "% 4d/%d: % -40s => %s\n", $num, +@lang-doc-sources, $file.path, "language/$podname";
151-
my $pod = EVAL(slurp($file.path) ~ "\n\$=pod")[0];
152-
write-language-file(:$dr, :what<language>, :$pod, :$podname);
153-
}
154-
155-
# TODO: Abstract this duplication
156-
say 'Reading lib/Type ...';
157-
my @type-doc-sources =
158-
recursive-dir('lib/Type/').grep(*.f)\
159-
.map: {; .path.subst('lib/Type/', '').subst(rx{\.pod$}, '').subst(:g, '/', '::') => $_ };
160-
161142
say 'Reading type graph ...';
162143
$tg = Perl6::TypeGraph.new-from-file('type-graph.txt');
163-
{
164-
my %h = $tg.sorted.kv.flat.reverse;
165-
@type-doc-sources .= sort: { %h{.key} // -1 };
166-
}
144+
my %h = $tg.sorted.kv.flat.reverse;
167145

168-
# TODO: Abstract this duplication as well
169-
say 'Processing Type Pod files ...';
170-
for @type-doc-sources.kv -> $num, (:key($podname), :value($file)) {
171-
printf "% 4d/%d: % -40s => %s\n", $num, +@type-doc-sources, $file.path, "type/$podname";
172-
my $pod = EVAL(slurp($file.path) ~ "\n\$=pod")[0];
173-
say pod-gist($pod[0]) if $*DEBUG;
174-
write-type-file(:$dr, :what<type>, :$pod, :$podname);
175-
}
146+
process-pod-dir 'Language', :$dr;
147+
process-pod-dir 'Type', :$dr :sorted-by{ %h{.key} // -1 };
176148

177149
say 'Composing doc registry ...';
178150
$dr.compose;
@@ -194,7 +166,28 @@ sub MAIN(Bool :$debug, Bool :$typegraph = False) {
194166
say 'Processing complete.';
195167
}
196168

197-
sub write-language-file(:$dr, :$what, :$pod, :$podname) {
169+
sub process-pod-dir($dir, :$dr, :&sorted-by = &[cmp]) {
170+
say "Reading lib/$dir ...";
171+
my @pod-sources =
172+
recursive-dir("lib/$dir/")\
173+
.map({;
174+
.path.subst("lib/$dir/", '')\
175+
.subst(rx{\.pod$}, '')\
176+
.subst(:g, '/', '::')
177+
=> $_
178+
}).sort(&sorted-by);
179+
180+
say "Processing $dir Pod files ...";
181+
my $total = +@pod-sources;
182+
my $what = $dir.lc;
183+
for @pod-sources.kv -> $num, (:key($podname), :value($file)) {
184+
printf "% 4d/%d: % -40s => %s\n", $num, $total, $file.path, "$what/$podname";
185+
my $pod = EVAL(slurp($file.path) ~ "\n\$=pod")[0];
186+
process-pod-file($what, :$dr, :what($what), :$pod, :$podname);
187+
}
188+
}
189+
190+
multi process-pod-file("language", :$dr, :$what, :$pod, :$podname) {
198191
spurt "html/$what/$podname.html", p2h($pod, $what);
199192
my $name = $pod.content[0].name eq "TITLE"
200193
?? $pod.content[0].content[0].content[0]
@@ -229,7 +222,7 @@ sub write-language-file(:$dr, :$what, :$pod, :$podname) {
229222
}
230223
}
231224

232-
sub write-type-file(:$dr, :$what, :$pod, :$podname) {
225+
multi process-pod-file("type", :$dr, :$what, :$pod, :$podname) {
233226
my @chunks = chunks-grep($pod.content,
234227
:from({ $_ ~~ Pod::Heading and .level == 2}),
235228
:to({ $^b ~~ Pod::Heading and $^b.level <= $^a.level}),
@@ -522,8 +515,6 @@ sub write-search-file($dr) {
522515
spurt("html/js/search.js", $template.subst("ITEMS", $items));
523516
}
524517

525-
my %operator_disambiguation_file_written;
526-
527518
sub write-disambiguation-files($dr) {
528519
say 'Writing disambiguation files ...';
529520
for $dr.grouped-by('name').kv -> $name, $p is copy {
@@ -564,10 +555,6 @@ sub write-disambiguation-files($dr) {
564555
}
565556
my $html = p2h($pod, 'routine');
566557
spurt "html/$name.html", $html;
567-
if all($p>>.kind) eq 'operator' {
568-
spurt "html/op/$name.html", $html;
569-
%operator_disambiguation_file_written{$p[0].name} = 1;
570-
}
571558
}
572559
say '';
573560
}
@@ -634,8 +621,8 @@ sub write-routine-file($dr, $name) {
634621
pod-block("Documentation for $subkind $name, assembled from the
635622
following types:"),
636623
@docs.map({
637-
pod-heading("{.name} in {.origin.name}"), # TODO: better way to get link to origin
638-
pod-block("From ", pod-link(.origin.name, .origin.url ~ '#' ~ (.subkinds ~~ /fix/ ?? .subkinds~'_' !! '') ~ .name)),
624+
pod-heading("{.human-kind} {.name} defined in {.origin.human-kind} {.origin.name}"),
625+
pod-block("From ", pod-link(.origin.name, .origin.url ~ '#' ~ (.subkinds~'_' if .subkinds ~~ /fix/) ~ .name)),
639626
.pod.list,
640627
})
641628
);
@@ -648,7 +635,7 @@ sub write-qualified-method-call(:$name!, :$pod!, :$type!) {
648635
pod-block('From ', pod-link($type, "/type/{$type}#$name")),
649636
@$pod,
650637
);
651-
spurt "html/{$type}.{$name}.html", p2h($p, 'routine');
638+
spurt "html/routine/{$type}.{$name}.html", p2h($p, 'routine');
652639
}
653640

654641
sub footer-html() {

lib/Perl6/Documentable.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class Perl6::Documentable {
1919
!! ~@l[0]
2020
}
2121
method human-kind() { # SCNR
22-
$.kind eq 'operator'
23-
?? "@.subkinds[] operator"
24-
!! $.kind eq 'language'
22+
$.kind eq 'language'
2523
?? 'language documentation'
24+
!! @.categories eq 'operator'
25+
?? "@.subkinds[] operator"
2626
!! english-list @.subkinds // $.kind;
2727
}
2828

0 commit comments

Comments
 (0)