-
Notifications
You must be signed in to change notification settings - Fork 37
/
SmartLinks.pm
1242 lines (985 loc) · 35.1 KB
/
SmartLinks.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Text::SmartLinks;
use strict;
use warnings;
use 5.006;
our $VERSION = '0.01';
use File::ShareDir;
use FindBin;
use File::Spec;
use File::Path qw(mkpath);
use File::Basename qw(dirname basename);
use File::Slurp;
use CGI;
use Pod::Simple::HTML;
use Data::Dumper;
use base 'Class::Accessor';
__PACKAGE__->mk_accessors(qw(check count cssfile line_anchor
out_dir print_missing smoke_rev test_files version wiki));
# TODO: treat non-breaking spaces as breaking spces in the smart links
# in docs/Perl6/Spec/S03-operators.pod the section called
# "Changes to Perl 5 operators" has a non-breaking space between Perl and 5
# while the smartlink pointing to it does not have. This should be acceptable.
# probably by replacing every space by [\s$nbsp]+ in the regex.
# use charnames ":full";
# my $nbsp = "\N{NO-BREAK SPACE}";
=head1 NAME
Text::SmartLinks - connecting test files with pod documentation
=head1 SYNOPSIS
smartlinks.pl t/*/*.t t/*/*/*.t
smartlinks.pl --dir t
smartlinks.pl --css foo.css --out-dir=public_html t/syntax/*.t
smartlinks.pl --check t/*/*.t t/*/*/*.t
smartlinks.pl --check t/some/test.t
smartlinks.pl --missing t/*/*.t t/*/*/*.t
If in the root directory of a CPAN package type the following:
smartlinks.pl --pod-dir lib/ --dir t/ --out-dir html/ --index
In the root of Text::SmartLinks type in the following:
perl -Ilib script/smartlinks.pl --pod-dir lib/ --dir t/ --out-dir html/ --index
=head1 DESCRIPTION
The plan is to change the Text::SmartLinks module and write a new
smartlinks.pl script so it will be usable in any Perl 5 or Perl 6
project to generate the HTML pages combining the POD content from
the .pod and .pm files and test scripts.
In addition the script should be able to generate further reports
in HTML format that help the developers.
The usage should default to parsing the files in lib/ for documentation
and the .t files in the t/ subdirectory.
=head1 Requirements
Process both Perl 5 and Perl 6 test files in an arbitraty directory
to collect smartlinks.
Default should be either the local t/ directory or the t/spec directory
of Pugs (for historical reasons).
Process .pod and .pm files (but maybe other files as well) with either Perl 5
or Perl 6 pod in them and with possibly also code in them.
Smartlinks should be able to say the name of the document where they link to.
L<Smolder/Text of head1>
L<Smolder::Util/Text o head2>
Map to either Smolder.pm or Smolder.pod and Smolder/Util.pm or Smolder/Util.pod
Need special cases for the Perl 6 documentation so the smartlinks can
have the following links pointing to S06-routines.pod and
S32-setting-library/Abstraction.pod
L<S06/Blocks>
L<S32::Abstraction>
=head1 Old Design Decisions
=over
=item *
This script should have as few non-core module dependencies as possible.
=item *
One doesn't have to build pugs so as to run F<smartlinks.pl>. Of course,
optional advanced features may require the user to run pugs'
"make" or even "make smoke".
=back
=head1 Smartlink Syntax
Smartlinks are planted in the test file, and are pointed to the appropriate sections
of the Synopsis you are using to write the test.
They look like pod links:
L<S06/Blocks> # "S06" is synopsis 6, and "Blocks" is the section
L<S03/"Hyper operators"> # quotes can be used when spaces are in the title,
# but is NOT required.
L<S03/Hyper operators> # just fine
The section name should be copied verbatim from the POD
(usually after C<=head>), including any POD tags like C<...>
and punctuations. The sections, however, are not supposed to be nested.
That is, a C<=head1> won't really contain a C<=head2>; they're disjoint
according to the current implementation.
The smartlinks also have a weird (also important) extension:
you can specify some keyphrases, to skip forward from the linked
section, so the smartlink is put into
a more specific place:
L<S05/"Return values from matches"/"In numeric context" number 'matches:'>
The above smartlink is appropriate next to a test case checking rule application in
numeric context, and it will place the backlink appropriately.
All the keyphrases listed after the second slash in a smartlink should appear in
a single sentence from the synopsis text, and the order is significant. If
there're spaces in a keyphrase, quote it using either double-quotes or signle-quotes.
In contrast with the case of section name, you should never use POD tags like
C<...> in a keyphrase. util/smartlinks.pl will do the right thing. You can use,
however, pod directives in the keyphrases, just like this:
# L<S04/Str/"=item split">
Smartlinks in .t files can be preceded by nothing but spaces or "#", furthermore,
there should be no trailing text on the same line, otherwise
they can't be recognized by tools. Here're some *invalid* samples:
# the following smartlink is INVALID!!!
# Link is L<S04/Str>
# the following smartlink is INVALID TOO!!!
# L<S04/Str> # This is a comment
There's also a variant for the smartlink syntax:
# L<syn/sec/key phrases>>
A smartlink can span at most 2 lines:
# L<S04/section name/key1
# "key2" key3 key4>
Only the keyphrase list part can continue to the next line. So the following example
is invalid:
# L<S04/section
# name/blah blah blah> # WRONG!!!
Please don't put a smartlink in the middle of a group of tests. Put it right
*before* the group of tests it is related to.
Multiple adjacent smartlinks can share the same snippet of tests right below
them:
# L<S02/Context/boolean "?">
# L<S03/Changes to Perl 5 operators/"?" imposes boolean context>
{ ... }
By doing this, one can effectively link one group of tests to
multplie places in the Synopses, leading to m-to-n correspondance.
smartlinks.pl can take care of this kind of special cases.
You can put a URL to create a generic link:
L<"http://groups.google.de/group/perl.perl6.language/msg/07aefb88f5fc8429">
or without quotes:
L<http://www.nntp.perl.org/group/perl.perl6.language/26071>
To see some examples, or look at the *.t files in the t/ directory of this project.
There were also some legacy smartlinks using the following syntax:
L<S04/"section name" /regex/>
L<<S04/"section name" /regex/>>
L<<S04/"section name">>
They're no longer supported by util/smartlinks.pl. Use the current syntax.
=head1 Basic Algorithm
=over
=item 1.
We scan over all the specified .t files; collect smartlinks and positional
info about the test code snippets as we go. When all these work have been finished,
we obtain a tree structure, which is named C<$linktree> in the source code.
To make this tree minimal, we only store the .t file name and line numbers, rather
than the snippets' source code itself.
The structure of $linktree is like this:
{
'S12' => {
'Traits' => [
[
undef,
[
't/oo/traits/basic.t',
'13',
'38'
]
],
[
'/If you say/',
[
't/oo/delegation.t',
'56',
'69'
]
],
],
},
'S02' => {
'Whitespace and Comments' => [
[
'"Embedded comments" "#" plus any bracket',
[
't/syntax/comments.t',
10,
48
]
],
]
}
}
This step is mostly done by sub C<process_t_file>.
=item 2.
We process the synopsis .pod files one by one and generate
HTML files integrated with test code snippets using the
C<$linktree> structure discussed above.
This is mostly done by sub C<process_pod_file>.
Because it is an enormous step, we can further divide it into several
sub steps:
=over
=item *
We parse each .pod into a tree, which is known as C<$podtree> in the
source code. (See sub C<parse_pod>.)
The structure of C<$podtree> looks like this:
{
'Names and Variables' => [
'=over 4' . "\n",
'=item *' . "\n",
'The C<$Package\'var> syntax is gone. Use C<$Package::var> instead.' . "\n",
'=item *' . "\n",
'Perl 6 includes a system of B<sigils> to mark the fundamental' . "\n".
'structural type of a variable:' . "\n",
...
],
...
}
=item *
We look up every related smartlink from every C<$podtree>, generate .t code
snippets along the way, and insert placeholders (like "_SMART_LINK_3" into
the corresponding C<$podtree>. (See subs C<parse_pattern>, C<process_paragraph>,
and C<gen_code_snippet>.)
=item *
Now we emit Pod source back from the modified $C<podtree>'s. (See sub C<emit_pod>.)
=item *
After that, we generate HTML source from the Pod source with snippet placeholders
using L<Pod::Simple::HTML>. (See sub C<gen_html>.)
=item *
At last, we replace every snippet placeholders in the HTML source with the real
snippet code (also in HTML format).
=back
=back
=head1 SEE ALSO
=over
=item *
F<t/README> in the Pugs source tree.
=item *
The articles on the Pugs blogs:
L<http://pugs.blogs.com/pugs/2006/08/integrating_the.html>
L<http://pugs.blogs.com/pugs/2006/09/check_smoke_res.html>
L<http://pugs.blogs.com/pugs/2006/09/the_benefits_of.html>
=item *
The synopses in L<http://perlcabal.org/syn> are generated by this script.
=back
=head1 METHODS
=cut
=head2 new
Constructor, can get a HASH reference as it is a base class
of L<Class::Accessor>
=cut
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{link_count} = 0;
$self->{broken_link_count} = 0;
$self->{snippet_id} = 0;
$self->{test_files_missing_links} = [];
$self->{out_dir} ||= '.';
$self->{errors} = [];
$self->{X} = {};
$self->{C} = {};
$self->{invalid_link} = 0;
return $self;
}
=head2 process_test_files
Gets a list of .t test files, calls L<process_t_file> on each on of them.
=cut
sub process_test_files {
my ($self, @t_files) = @_;
$self->{test_files} = \@t_files;
for my $t_file (@t_files) {
my $links = $self->process_t_file($t_file);
if ($links) {
print "Found $links links in <$t_file>\n" if defined $self->count;
} else {
print "No smartlink found in <$t_file>\n" if defined $self->print_missing;
print "\"$t_file\"<http://github.com/perl6/roast/blob/master/$t_file>\n" if defined $self->wiki;
push @{ $self->{test_files_missing_links} }, $t_file;
}
}
}
=head2 process_t_file
Gets a path to a .t file, reads line by line and collects
the smartlinks in it to a hash structure using the
C<add_link> function.
=cut
sub process_t_file {
my ($self, $infile) = @_;
open my $in, $infile or
die "error: Can't open $infile for reading: $!\n";
my ($setter, $from, $to);
my $found_link = 0;
while (<$in>) {
chomp;
my $new_from;
my ($synopsis, $section, $pattern);
if (m{L<"?http://}) {
# TODO shall we also collect the http links for later reuse?
next;
}
elsif (m{^ \s* \# \s* (L<<+)}xoi) {
$self->error("Legacy smartlink. Use L< instead of $1 in line $. '$_' in file '$infile'");
$self->{invalid_link}++;
next;
}
elsif (m{^ \s* \# \s* L< ([^/]+) / ([^/]+) >\s*$}xo) {
($synopsis, $section) = ($1, $2);
$section =~ s/^\s+|\s+$//g;
$section =~ s/^"(.*)"$/$1/;
#warn "$synopsis $section" if $synopsis eq 'S06';
$new_from = $.;
$to = $. - 1;
$found_link++;
}
# extended and multiline smartlinks
elsif (m{^ \s* \# \s* L(<) ([^/]+) / ([^/]+) / (.*) }xo) {
#warn "$1, $2, $3\n";
my $brackets;
($brackets, $synopsis, $section, $pattern) = ($1, $2, $3, $4);
$brackets = length($brackets);
$section =~ s/^\s+|\s+$//g;
$section =~ s/^"(.*)"$/$1/;
if (!$section) {
$self->error("$infile: line $.: section name can't be empty.");
}
$pattern =~ s/^\s+|\s+$//g;
if (substr($pattern, -1, 1) ne '>') {
$_ = <$in>;
s/^\s*\#?\s*|\s+$//g;
if (!s/>{$brackets}$//) {
$self->error("$infile: line $.: smart links must terminate in the second line.");
next;
}
$pattern .= " $_";
$new_from = $. - 1;
$to = $. - 2;
} else {
$new_from = $.;
$to = $. - 1;
$pattern =~ s/\s*>{$brackets}$//;
}
#warn "*$synopsis* *$section* *$pattern*\n";
$found_link++;
}
# there are some # L<"http://... links that we should skip for now
# and not even report them as errors.
# any other L< thing should be reported.
elsif (m{^ \s* \# \s* L<}xoi) {
$self->error("Could not parse smartlink in line $. '$_' in file '$infile'");
$self->{invalid_link}++;
next;
}
else {
next;
}
#warn "*$synopsis* *$section*\n";
if ($from and $from == $to) {
my $old_setter = $setter;
my $old_from = $from;
$setter = sub {
$self->add_link($synopsis, $section, $pattern, $infile, $_[0], $_[1]);
$old_setter->($old_from, $_[1]);
#warn "$infile - $old_from ~ $_[1]";
};
#warn "$infile - $from ~ $to";
} else {
$setter->($from, $to) if $setter and $from;
$setter = sub {
$self->add_link($synopsis, $section, $pattern, $infile, $_[0], $_[1]);
};
}
$from = $new_from;
}
$setter->($from, $.) if $setter and $from;
close $in;
# print "No smartlink found in <$infile>\n" if (defined $print_missing && $found_link == 0);
return $found_link;
}
=begin private
=head2 add_link
add_link($synopsis, $section, $pattern, $infile, $from, $to);
=end private
=cut
# TODO add tests
sub add_link {
my ($self, $synopsis, $section, $pattern, $t_file, $from, $to) = @_;
if ($from == $to) {
warn "WARNING: empty snippet detected at $t_file (line $from ~ $to).\n";
}
$self->{linktree}->{$synopsis} ||= {};
$self->{linktree}->{$synopsis}->{$section} ||= [];
if ($pattern and substr($pattern, -1, 1) eq '/') { $pattern = "/$pattern"; }
push @{ $self->{linktree}->{$synopsis}->{$section} },
[$pattern => [$t_file, $from, $to]];
return $self->link_count_inc;
}
=head2 parse_pattern
Convert patterns used in 00-smartlinks.to perl 5 regexes
=cut
sub parse_pattern {
my ($self, $pat) = @_;
my @keys;
while (1) {
if ($pat =~ /\G\s*"([^"]+)"/gc ||
$pat =~ /\G\s*'([^']+)'/gc ||
$pat =~ /\G\s*(\S+)/gc) {
push @keys, $1;
} else { last }
}
my $str = join('.+?', map {
my $key = quotemeta $_;
$key =~ s/^\w/\\b$&/;
$key =~ s/\w$/$&\\b/;
$key;
} @keys);
$str;
}
=head2 process_paragraph
Process paragraphs of the pod file: unwrap lines, strip POD tags, and etc.
=cut
sub process_paragraph {
my ($self, $str) = @_;
# unwrap lines:
$str =~ s/\s*\n\s*/ /g;
# strip POD tags:
# FIXME: obviously we need a better way to do this:
$str =~ s/[LCFIB]<<<\s+(.*?)\s+>>>/$1/g;
$str =~ s/[LCFIB]<<\s+(.*?)\s+>>/$1/g;
$str =~ s/[LCFIB]<(.*?)>/$1/g;
$str;
}
=head2 gen_code_snippet
Gets a triplet of [file, from, to] and generates an HTML
snippet from that section of the given file.
Note that this function has been optimized for space rather
than time.
=cut
sub gen_code_snippet {
my ($self, $location) = @_;
my ($file, $from, $to) = @$location;
#warn "gen_code_snippet: @$location\n";
open my $in, $file or
die "Can't open $file for reading: $!\n";
# Strip leading realpath so the names start at t/
$file =~ s{.*?/t/}{t/};
my $i = 1;
my $src;
my $file_info;
$file_info = $self->{test_result}->{$file} if $self->{test_result};
my ($ok_count, $failed_count) = (0, 0);
while (<$in>) {
next if $i < $from;
last if $i > $to;
s/\&/\&/g;
s/"/\"/g;
s/</\</g;
s/>/\>/g;
s{^( *)}{" " x (length($1) / 2)}gem;
s/ / /g;
s{L\<(http://.*?)\>}{L\<<a href="$1">$1</a>\>}g;
s{L\<\"(http://.*?)\"\>}
{L\<<a href="$1">\"$1\"</a>\>}g;
my $mark = '';
if ($file_info) {
chomp;
if (!exists $file_info->{$i}) {
$mark = '';
} elsif ($file_info->{$i}) {
$mark = qq{<span class="ok"> √ </span>};
$ok_count++;
} else {
$mark = qq{<span class="nok"> × </span>};
$failed_count++;
}
}
$src .= qq{<tr><td><code>$mark</code></td><td><code>$_</code></td></tr>\n};
} continue { $i++ }
close $in;
$src =~ s/\n{3,}/\n\n/sg;
$src =~ s/\n+$//sg;
my $snippet_id = $self->snippet_id_inc;
#warn $snippet_id;
#warn "$file $to $from";
warn "NOT DEFINED!!! @$location $snippet_id" if !defined $src;
my $snippet;
if (!$self->{test_result}) {
#warn "No test results for $file $from to $to";
$snippet = qq{<pre class="snip">$src</pre>};
} else {
$snippet = qq{
<table class="snipres">
$src
</table>
};
}
my $stat;
if ($self->{test_result}) {
if ($ok_count == 0 && $failed_count == 0) {
$stat = " (no results)";
} else {
$stat = " (<code>$ok_count √, $failed_count ×</code>)";
}
} else {
$stat = '';
}
my $nlines = $to - $from + 1;
my $html_file = $file;
$html_file =~ s{.*(t/spec/|roast/)}{};
my $html = <<"_EOC_";
<div class="smartlink">
<p class="smartlink-file"><a href="http://github.com/perl6/roast/blob/master/$html_file#L$from-L$to"><code>$html_file</code> lines <code>$from–$to</code>$stat</a>
</p>
</div>
_EOC_
$self->set_snippet($snippet_id, $html);
return "\n\n_SMART_LINK_$snippet_id\n\n";
}
=head2 get_javascript
Returns the content of the smartlink.js file.
Probably we should just copy the .js file to the html directory
and not embed it.
=cut
sub get_javascript {
# for the test scripts in t/ and the smartlinks.pl in script/ directory
my $file = File::Spec->catfile($FindBin::Bin, '..', 'share', 'smartlinks.js');
if (not -e $file) {
# for smarlinks.pl in utils/ directory of Pugs if Text::SmartLinks is not installed
$file = File::Spec->catfile($FindBin::Bin, 'Text-SmartLinks', 'share', 'smartlinks.js');
}
# installed version of the file
if (not -e $file) {
$file = File::Spec->catfile(File::ShareDir::dist_dir('Text-SmartLinks'), 'smartlinks.js');
}
if (not $file) {
warn "Could not find 'smartlinks.js'\n";
return '';
}
#warn $file;
if (open my $fh, '<', $file) {
local $/ = undef;
return <$fh>;
}
warn "could not open '$file'";
return '';
}
sub test_files_missing_links {
return @{ $_[0]->{test_files_missing_links} };
}
sub emit_pod {
my ($self, $podtree) = @_;
my $str;
$str .= $podtree->{_header} if $podtree->{_header};
for my $elem (@{ $podtree->{_sections} }) {
my ($num, $sec) = @$elem;
$str .= "=head$num $sec\n\n";
for my $para (@{ $podtree->{$sec} }) {
if ($para eq '') {
$str .= "\n";
} elsif ($para =~ /^\s+/) {
$str .= $para;
} else {
$str .= "$para\n";
}
}
}
$str = "=pod\n\n_LINE_ANCHOR_1\n\n$str" if $self->line_anchor;
return $str;
}
# split a C<>, C<< >>, C<<< >>>, or C«» code into ($tag, $value)
sub split_code {
local ($_) = @_;
my $tag = substr $_, 0, 1;
s/^\w<<<\s+(.+?)\s+>>>$/$1/
or s/^\w<<\s+(.+?)\s+>>$/$1/
or s/^\w<(.+?)>$/$1/
or s/^\w«(.+?)»$/$1/;
return ($tag, $_);
}
sub parse_pod {
my ($self, $pod, $url) = @_;
my $podtree = {};
my $section;
my $line_no = 0;
foreach (@$pod) {
$line_no++;
# collect the X<> and C<> tags
while (/([CX](?:
(?:<<<\s+.+?\s+>>>)
|
(?:<<\s+.+?\s+>>)
|
(?:<.+?>)
|
(?:«.+?»)
))/gx) {
my ($tag, $value) = split_code($1);
if ($section) {
(my $s = $section) =~ s/ +/_/g;
$self->{$tag}{$value}{"$url#$s"}++;
} else {
$self->{$tag}{$value}{$url}++;
}
}
if (/^ =head(\d+) \s* (.*\S) \s* $/x) {
#warn "parse_pod: *$1*\n";
my $num = $1;
$section = $2;
$podtree->{_sections} ||= [];
push @{ $podtree->{_sections} }, [$num, $section];
} elsif (!$section) {
$podtree->{_header} .= $_;
} elsif (/^\s*$/) {
$podtree->{$section} ||= [];
#push @{ $podtree->{$section} }, "\n";
my @new = ('');;
if ($self->line_anchor and @{ $podtree->{$section} } and $podtree->{$section}->[-1] !~ /^=over\b|^=item\b/) {
unshift @new, "_LINE_ANCHOR_$line_no\n";
}
push @{ $podtree->{$section} }, @new;
} elsif (/^\s+(.+)/) {
$podtree->{$section} ||= [''];
$podtree->{$section}->[-1] .= $_;
push @{ $podtree->{$section} }, '';
} else {
$podtree->{$section} ||= [''];
$podtree->{$section}->[-1] .= $_;
}
}
$podtree;
}
sub process_yml_file {
my ($self, $yml_file) = @_;
if ($yml_file) {
eval {
require Test::TAP::Model;
require YAML::Syck;
};
if ($@) {
die "--smoke-res option requires both Test::TAP::Model and YAML::Syck. ".
"At least one of them is not installed.\n";
}
my $data = YAML::Syck::LoadFile($yml_file);
#warn $data;
my $structure;
if ($data->{meat}) {
$structure = delete $data->{meat};
}
my $tap = Test::TAP::Model->new_with_struct($structure);
for my $file ($tap->test_files) {
#warn " $file...\n";
(my $fname = $file->name) =~ s{.*?/t/}{t/};
my %file_info;
$self->{test_result}->{$fname} = \%file_info;
for my $case ($file->cases) {
next if $case->skipped or !$case->test_line;
$file_info{$case->test_line} = $case->actual_ok;
}
}
#YAML::Syck::DumpFile('test_result.yml', $self->{test_result});
my $smoke_rev = $data->{revision};
$self->smoke_rev($smoke_rev);
$smoke_rev = $smoke_rev ? "r$smoke_rev" : 'unknown';
warn "info: pugs smoke is at $smoke_rev.\n";
}
}
sub gen_html {
my ($self, $pod, $title) = @_;
$Pod::Simple::HTML::Perldoc_URL_Prefix = 'http://perlcabal.org/syn/';
$Pod::Simple::HTML::Perldoc_URL_Postfix = '.html';
$Pod::Simple::HTML::Content_decl =
q{<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >};
$Pod::Simple::HTML::Doctype_decl =
qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">\n};
my $pod2html = new Pod::Simple::HTML;
$pod2html->index(1);
$pod2html->html_css($self->cssfile);
my $javascript = $self->get_javascript();
$pod2html->html_javascript(qq{<script type="text/javascript">$javascript</script>});
$pod2html->force_title($title);
my $html;
open my $in, '<', \$pod;
open my $out, '>', \$html;
$pod2html->parse_from_file($in, $out);
# substitutes the placeholders introduced by `gen_code_snippet`
# with real code snippets:
$html =~ s,(?:<p>\s*)?\b_SMART_LINK_(\d+)\b(?:\s*</p>)?,$self->get_snippet($1),sge;
$self->fix_line_anchors(\$html) if $self->line_anchor;
$self->add_footer(\$html);
$self->add_user_css(\$html);
return $html
}
sub _gen_line_anchors {
my $list = shift;
my $curr = shift @$list;
my $html = '';
return $html if not defined $list->[0];
for ($curr .. $list->[0] - 1) {
$html .= qq{<a id="line_$_"></a>\n};
}
$html;
}
sub fix_line_anchors {
my ($self, $html) = @_;
my @lineno; # line numbers for each paragraph
while ($$html =~ /\b_LINE_ANCHOR_(\d+)\b/gsm) {
push @lineno, $1;
}
$$html =~ s{(?:<p>\s*)?\b_LINE_ANCHOR_(\d+)\b(?:\s*</p>)?}{ _gen_line_anchors(\@lineno) }sge;
}
sub add_footer {
my ($self, $html) = @_;
$$html =~ s{</body>}{
[ <a href="#__top">Top</a> ]
[ <a href="http://perlcabal.org/syn/">Index of Synopses</a> ]
</body>};
}
# isn't there a prettier way to do this?
sub add_user_css {
my ($self, $html) = @_;
my $user_css = << '.';
<style type="text/css">
.ok {
color: green;
font-weight: bold;
}
.nok {
color: red;
font-weight: bold;
}
.snip { margin-left: 6px; }
.snipres {
margin-left: 6px;
border-width: 0;
}
.smartlink_snippet {
border: 1px solid;
padding: 0.2em;
}
</style>
.
$$html =~ s{(</head>)}{$user_css\n$1};
}
sub gen_preamble {
my ($self) = @_;
my ($sec, $min, $hour, $mday, $mon, $year) = gmtime;
$year += 1900; $mon += 1;
my $time = sprintf "%04d-%02d-%02d %02d:%02d:%02d GMT",
$year, $mon, $mday, $hour, $min, $sec;
my $smoke_rev = $self->smoke_rev;
my $smoke_info = $smoke_rev ?
qq(, <a href="http://perlcabal.org/smoke.html">pugs-smoke</a> <strong>$smoke_rev</strong>)
:
'';
my $pugs_rev = $self->version || 'unknown';
return qq{
<I>This page was generated by Text::SmartLinks v$VERSION at $time.<br/>
(<a href"http://github.com/perl6/specs/">syn</a> <strong>$pugs_rev</strong>$smoke_info)</I>
[ <a href="http://perlcabal.org/syn/">Index of Synopses</a> ] <br/>
<a id='__top'></a>
};
}
sub create_stats_page {
my ($self) = @_;
my $html = "<html><head><title>Stats</title></head><body>";
$html .= $self->gen_preamble;
my $test_file_count = scalar @{ $self->test_files };
my $test_files_missing_links = scalar $self->test_files_missing_links;
$html .= sprintf("info: %s smartlinks found and %s broken in $test_file_count test files ($test_files_missing_links test files had no links).\n",
$self->link_count ,$self->broken_link_count);
#if (!$self->check and $self->broken_link_count > 0) {
# warn "hint: use the --check option for details on broken smartlinks.\n";
#}
if ($self->test_files_missing_links) {
$html .= "<h2>Test files without links</h2>\n<ul>";
foreach my $file ($self->test_files_missing_links) {
$html .= "<li>$file</li>\n";
}
$html .= "</ul>\n";
}
if (@{ $self->{errors} }) {
$html .= "<h2>Errors</h2>\n<ul>";
foreach my $e (@{ $self->{errors} }) {
$html .= "<li>" . CGI::escapeHTML(join "", @$e) . "</li>\n";
}
$html .= "</ul>\n";
}
$html .= "</body></html>";
write_file(File::Spec->catfile($self->out_dir, 'stats.html'), $html);
return;
}
sub outfile_name {
my ($self, $infile) = @_;
(my $syn_id = $infile) =~ s/\.(pod|pm)$//;
$syn_id =~ s{[/\\]}{::}g;
# special case for Perl 6 Synopsis
if ($ENV{PUGS_SMARTLINKS}) {
$syn_id =~ s{(S\d+)-[^:]+}{$1};
}
(my $outfile = $syn_id) =~ s{::}{/}g;
$outfile .= ".html";
return ($outfile, $syn_id);
}
=begin private