tools/gen_help.pl

filter: Statementstmt 98.3% 177 / 180 Branchbran 88.7% 55 / 62 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 95.3% 244 / 256 CC 26 SCAR 22.6
CC
26
Cov
96%
get_text
13.1
print_description
12.0
get_name
2.0
CRAP 9.6
« blib/lib/XML/XSH2/Iterators.pm ↑ Summary tools/gen_commands.pl »
1 #!/usr/bin/perl
2  
3 # $Id: gen_help.pl,v 2.2 2004-12-09 08:41:13 pajas Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::Wrap qw(wrap);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶if ($ARGV[0]=~'^(-h|--help)?\$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] =~ /^(-h|--help)?\$/) 0 1 1
10 0 print <<EOF;
11 Generates help module from RecDescentXML source.
12  
13 Usage: $0 <source.xml>
14  
15 EOF
16 0 exit;
17 }
18  
19 1 my $parser=XML::LibXML->new();
20 1 $parser->load_ext_dtd(1);
21 1 $parser->validation(1);
22 1 $parser->keep_blanks(1);
23 1 my $doc=$parser->parse_file($ARGV[0]);
24  
25 1 my $dom=$doc->getDocumentElement();
26 1 my ($rules)=$dom->findnodes('./rules');
27  
28 1 my $ruledoc;
29 my $title;
30 1 my @aliases;
31 1 my @seealso;
32 1 my @usage;
33 1 my $desc;
34  
35  
36 1 print "# This file was automatically generated from $ARGV[0] on \n# ",scalar(localtime),"\n";
37 1 print <<'PREAMB';
38  
39 package XML::XSH2::Help;
40 use strict;
41 use vars qw($HELP %HELP $Apropos);
42  
43  
44 PREAMB
45  
46 1 print "\$HELP=<<'END';\n";
47 1 print "\n Welcome to XSH help\n";
48 1 print " -------------------\n\n";
49 1 print " In this help:\n [topic] is a cross reference that can be followed using 'help topic'\n\n";
50 1 ($desc)=$dom->findnodes('./doc/description');
51 1 ▶print_description($desc," "," ") if ($desc);
Branch: 1 branch
BranchTrueFalseTotal
if $desc 1 0 1
52 1 print "\n\n Within the interactive shell, press <TAB> for auto-completion.\n";
53 1 print "END\n\n";
54  
55 1 print "\$HELP{'toc'}=[<<'END'];\n";
56 1 print "\nHelp items:\n";
57 1 print "-----------\n\n";
58 1 print " toc - this page\n\n";
59 1 print " XSH Language Topics:\n\n";
60 1 foreach (sort { $a->getAttribute('id') cmp
61 $b->getAttribute('id') }
62 $dom->findnodes("/recdescent-xml/doc/section")) {
63 13 print " ",$_->getAttribute('id')," - ";
64 13 print wrap(""," ",
65 get_text($_->findnodes("title"))),
66 "\n";
67 }
68 1 print "\n XSH Commands:\n\n";
69 print wrap(" "," ",
70 117 join ", ", sort map { get_name($_) }
71 1 grep {defined($_)}
72 $dom->findnodes("//rules/rule[\@type='command']")),
73 "\n\n";
74  
75 1 print " XSH Argument Types:\n\n";
76 print wrap(" "," ",
77 13 join ", ", sort map { get_name($_) }
78 1 grep {defined($_)}
79 $dom->findnodes("//rules/rule[\@type='argtype']")),
80 "\n\n";
81  
82 1 print " XPath Extension Functions:\n\n";
83 print wrap(" "," ",
84 48 join ", ", sort map { get_name($_) }
85 1 grep {defined($_)}
86 $dom->findnodes("//rules/rule[\@type='function']")),
87 "\n\n";
88 1 print "END\n\n";
89  
90  
91 1 my %apropos;
92 1 foreach my $r ($rules->findnodes('./rule')) {
93 223 ▶ next unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 223 223
94 223 my ($ruledoc)=$r->findnodes('./documentation');
95 223 ▶ next unless $ruledoc;
Branch: 1 branch
BranchTrueFalseTotal
unless $ruledoc 44 179 223
96 179 my $name=get_name($r);
97  
98 179 my ($shortdesc)=$ruledoc->findnodes('./shortdesc');
99 179 ▶ $apropos{$name} = get_text($shortdesc) if ($shortdesc);
Branch: 1 branch
BranchTrueFalseTotal
if $shortdesc 129 50 179
100 179 $apropos{$name} =~ s/\s+/ /g;
101  
102 179 print "\$HELP{'$name'}=[<<'END'];\n";
103 179 ($title)=$ruledoc->findnodes('./title');
104 179 ▶ print get_text($title),"\n\n" if ($title);
Branch: 1 branch
BranchTrueFalseTotal
if $title 14 165 179
105  
106 179 @usage=$ruledoc->findnodes('./usage');
107 179 ▶ if (@usage) {
Branch: 1 branch
BranchTrueFalseTotal
if (@usage) 165 14 179
108 165 print "usage: ";
109 165 foreach (@usage) {
110 180 my $usage=get_text($_);
111 180 $usage=~s/\s+/ /;
112 180 print $usage,"\n ";
113 }
114 165 print "\n";
115 }
116 179 @aliases=grep {defined($_)} $r->findnodes('./aliases/alias');
117 179 ▶ if (@aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (@aliases) 49 130 179
118 49 print "aliases: ",join " ",map { get_name($_) } @aliases;
119 49 print "\n\n";
120 }
121 179 ($desc)=$ruledoc->findnodes('./description');
122 179 ▶ if ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 179 0 179
123 179 print "description:\n";
124 179 print_description($desc," "x(13)," "x(13));
125 }
126 179 @seealso=grep {defined($_)} $ruledoc->findnodes('./see-also/ruleref');
127 179 ▶ if (@seealso) {
Branch: 1 branch
BranchTrueFalseTotal
if (@seealso) 79 100 179
128 173 print "see also: ",join " ", map { get_name($_) }# grep {defined($_)}
129 79 map { $_->findnodes('id(@ref)') } @seealso;
130 79 print "\n\n";
131 }
132  
133 179 print "END\n\n";
134  
135 179 foreach (@aliases) {
136 69 print "\$HELP{'",get_name($_),"'}=\$HELP{'$name'};\n";
137 }
138 179 print "\n";
139  
140 }
141  
142 1 foreach my $sec ($dom->findnodes('/recdescent-xml/doc/section')) {
143 13 my $name=$sec->getAttribute('id');
144  
145 13 print "\$HELP{'$name'}=[<<'END'];\n";
146 13 ($title)=$sec->findnodes('./title');
147 13 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 13 0 13
148 13 my $t=get_text($title);
149 13 print $t,"\n";
150 13 print '-' x length($t),"\n\n";
151 }
152  
153 13 print_description($sec," "x(2)," "x(2));
154  
155 13 my @commands=$dom->findnodes("//rules/rule[\@type='command' and ".
156 "documentation[contains(\@sections,'$name')]]");
157 13 ▶ if (@commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commands) 9 4 13
158 9 print "\nRelated help items:\n";
159 print wrap(" "," ",
160 9 join ", ", sort map { get_name($_) }
161 @commands),
162 "\n\n";
163 }
164 13 print "END\n\n";
165 }
166  
167 1 print "\$HELP{'commands'}=\$HELP{'command'};\n";
168  
169 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 print Data::Dumper->Dump([\%apropos],[qw(Apropos)]);
171  
172 1 print "\n1;\n__END__\n\n";
173  
174 1 exit;
175  
176 ## ================================================
177  
178 sub strip_space {
179 1214 ▶ my ($text)=@_;
Subroutine strip_space: called CC=1 SCAR=0.0
180 1214 $text=~s/^\s*//;
181 1214 $text=~s/\s*$//;
182 1214 return $text;
183 }
184  
185 sub get_name {
186 1489 ▶ my ($r)=@_;
Subroutine get_name: called CC=2 SCAR=6.9
187 1489 ▶ return $r->getAttribute('name') ne ""
Branch: 1 branch
BranchTrueFalseTotal
$r->getAttribute('name') ne '' ? : 1400 89 1489
188 ? $r->getAttribute('name')
189 : $r->getAttribute('id');
190 }
191  
192 sub get_text {
193 1919 ▶ my ($node,$no_strip)=@_;
Subroutine get_text: called CC=13 SCAR=25.7
194 1919 my $text="";
195 1919 foreach my $n ($node->childNodes()) {
196 5169 ▶ if ($n->nodeType() == XML::LibXML::XML_TEXT_NODE ||
Branch: 2 branches
BranchTrueFalseTotal
if ($n->nodeType == 3 or $n->nodeType == 4) { } 3439 1730 5169
elsif ($n->nodeType == 1) { } 1594 136 1730
Condition66%2/3
$n->nodeType == 3 or $n->nodeType == 4
l!l&&r!l&&!r
343901730
MC/DC50%1/2
$n->nodeType == 3 or $n->nodeType == 4
$n->nodeType == 3 $n->nodeType == 4
Truth table66%2/3
A or B
A= $n->nodeType == 3B= $n->nodeType == 4
ABresult
1X1
011
000
197 $n->nodeType() == XML::LibXML::XML_CDATA_SECTION_NODE) {
198 3439 my $data=$n->getData();
199 3439 $data=~s/\t/ /g;
200 3439 $text.=$data;
201 } elsif ($n->nodeType() == XML::LibXML::XML_ELEMENT_NODE) {
202 1594 ▶ if ($n->nodeName() eq 'link') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n->nodeName eq 'link') { } 18 1576 1594
elsif ($n->nodeName eq 'xref') { } 499 1077 1576
elsif ($n->nodeName eq 'typeref') { } 3 1074 1077
elsif ($n->nodeName eq 'tab') { } 256 818 1074
elsif ($n->nodeName eq 'literal') { } 687 131 818
203 18 $text.="[".get_text($n,1)."]";
204 } elsif ($n->nodeName() eq 'xref') {
205 499 $text.="[";
206 499 my ($ref)=$node->findnodes("id('".$n->getAttribute('linkend')."')");
207 499 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 499 0 499
208 499 $text.=get_name($ref);
209 } else {
210 0 print STDERR "Reference to undefined identifier: ",$n->getAttribute('linkend'),"\n";
211 }
212 499 $text.="]";
213 } elsif ($n->nodeName() eq 'typeref') {
214 3 foreach (split /\s/,$n->getAttribute('types')) {
215 3 $text.=join ", ", sort map { get_name($_) } grep {defined($_)} $node->findnodes("//rules/rule[\@type='$_']");
216 }
217 } elsif ($n->nodeName() eq 'tab') {
218 256 $text.="\t" x $n->getAttribute('count');
219 } elsif ($n->nodeName() eq 'literal') {
220 687 $text.="'".get_text($n,1)."'";
221 } else {
222 131 $text.=get_text($n);
223 }
224 }
225 }
226 1919 ▶ return $no_strip ? $text : strip_space($text);
Branch: 1 branch
BranchTrueFalseTotal
$no_strip ? : 705 1214 1919
227 }
228  
229 66 ▶sub max { ($_[0] > $_[1]) ? $_[0] : $_[1] }
Subroutine max: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 19 47 66
230  
231 sub print_description {
232 311 ▶ my ($desc,$indent,$bigindent)=@_;
Subroutine print_description: called CC=12 SCAR=24.9
233 311 foreach my $c ($desc->childNodes()) {
234 2007 ▶ if ($c->nodeType == XML::LibXML::XML_ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->nodeType == 1) 848 1159 2007
235 848 ▶ if ($c->nodeName eq 'para') {
Branch: 5 branches
BranchTrueFalseTotal
if ($c->nodeName eq 'para') { } 507 341 848
elsif ($c->nodeName eq 'section') { } 7 334 341
elsif ($c->nodeName eq 'enumerate') { } 3 331 334
elsif ($c->nodeName eq 'code') { } 154 177 331
elsif ($c->nodeName eq 'example') { } 91 86 177
236 507 my $t=get_text($c);
237 507 $t=~s/\s+/ /g;
238 507 print wrap($indent,$bigindent,$t),"\n\n";
239 507 $indent=$bigindent;
240 } elsif ($c->nodeName eq 'section') {
241 7 my ($title)=$c->findnodes('./title');
242 7 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 7 0 7
243 7 my $t=get_text($title);
244 7 print $bigindent.$t,"\n";
245 7 print $bigindent.'-' x length($t),"\n\n";
246 }
247 7 print_description($c,$indent." ",$bigindent." ");
248 } elsif ($c->nodeName eq 'enumerate') {
249 3 my $n=1;
250 3 foreach my $item ($c->findnodes('listitem')) {
251 20 print_description($item,$indent." ".($n++).". ",$bigindent." ");
252 }
253 } elsif ($c->nodeName eq 'code') {
254 154 local $_ = get_text($c);
255 154 s/\n[ ]*/\n$bigindent/mg;
256 154 s/\\\n/\\\n$bigindent /g;
257 154 s/\t/ /g;
258 154 print "$bigindent$_\n";
259 } elsif ($c->nodeName eq 'example') {
260 91 foreach (map { get_text($_) } $c->findnodes('./title')) {
261 66 s/\s+/ /g;
262 66 print wrap("",$bigindent,"Example:"." "x(max(1,length($bigindent)-8))."$_\n");
263 }
264 91 ▶ unless ($c->findnodes('./title')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->findnodes("./title")) 25 66 91
265 25 print "Example:";
266 }
267 91 print "\n";
268 91 print_description($c,$indent,$bigindent);
269 91 print "\n";
270 }
271 }
272 }
273 }
« blib/lib/XML/XSH2/Iterators.pm ↑ Summary tools/gen_commands.pl »