commandref_join.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/perl
  2. # MAXWI
  3. # With pre: 1320, without 1020 (content only)
  4. # pre { white-space: pre-wrap; } : 900
  5. use strict;
  6. use warnings;
  7. # $Id: commandref_join.pl 13245 2017-01-26 19:11:27Z rudolfkoenig $
  8. my $noWarnings = grep $_ eq '-noWarnings', @ARGV;
  9. my ($verify) = grep $_ =~ /\.pm$/ , @ARGV;
  10. use constant TAGS => qw{ul li code b i u table tr td div};
  11. sub generateModuleCommandref($$;$);
  12. my %mods;
  13. my %modIdx;
  14. my @modDir = ("FHEM");
  15. my @lang = ("EN", "DE");
  16. if(!$verify) {
  17. foreach my $modDir (@modDir) {
  18. opendir(DH, $modDir) || die "Cant open $modDir: $!\n";
  19. while(my $l = readdir DH) {
  20. next if($l !~ m/^\d\d_.*\.pm$/);
  21. my $of = $l;
  22. $l =~ s/.pm$//;
  23. $l =~ s/^[0-9][0-9]_//;
  24. $mods{$l} = "$modDir/$of";
  25. $modIdx{$l} = "device";
  26. open(MOD, "$modDir/$of") || die("Cant open $modDir/$l");
  27. while(my $cl = <MOD>) {
  28. if($cl =~ m/^=item\s+(helper|command|device)/) {
  29. $modIdx{$l} = $1;
  30. last;
  31. }
  32. }
  33. close(MOD);
  34. }
  35. }
  36. if(-f "configDB.pm") {
  37. $mods{configDB} = "configDB.pm";
  38. $modIdx{configDB} = "helper";
  39. }
  40. } else { # check for syntax only
  41. my $modname = $verify;
  42. $modname =~ s/^.*[\/\\](?:\d\d_)?(.+).pm$/$1/;
  43. $mods{$modname} = $verify;
  44. foreach my $lang (@lang) {
  45. generateModuleCommandref($modname, $lang);
  46. }
  47. exit;
  48. }
  49. sub
  50. printList($)
  51. {
  52. for my $i (sort { "\L$a" cmp "\L$b" } keys %modIdx) {
  53. print OUT " <a href=\"#$i\">$i</a> &nbsp;\n"
  54. if($modIdx{$i} eq $_[0]);
  55. }
  56. while(my $l = <IN>) {
  57. next if($l =~ m/href=/);
  58. print OUT $l;
  59. last;
  60. }
  61. }
  62. foreach my $lang (@lang) {
  63. my $suffix = ($lang eq "EN" ? "" : "_$lang");
  64. my $docIn = "docs/commandref_frame$suffix.html";
  65. my $docOut = "docs/commandref$suffix.html";
  66. open(IN, "$docIn") || die "Cant open $docIn: $!\n";
  67. open(OUT, ">$docOut") || die "Cant open $docOut: $!\n";
  68. if(!$suffix) { # First run: remember commands/helper module
  69. my $modType;
  70. while(my $l = <IN>) {
  71. $modType = "command" if($l =~ m/>FHEM commands</);
  72. $modType = "device" if($l =~ m/>Devices</);
  73. $modType = "helper" if($l =~ m/>Helper modules</);
  74. $modIdx{$1} = $modType
  75. if($modType && $l =~ m/href="#(.*?)">/ && $1 ne "global");
  76. last if($l =~ m/<!-- header end -->/);
  77. }
  78. seek(IN,0,0);
  79. }
  80. # Second run: create the file
  81. while(my $l = <IN>) { # Header
  82. last if($l =~ m/name="perl"/);
  83. print OUT $l;
  84. printList($1) if($l =~ m/<!-- header:(.*) -->/);
  85. }
  86. # Copy the doc part from the module
  87. foreach my $mod (sort keys %mods) {
  88. generateModuleCommandref($mod,$lang, \*OUT);
  89. }
  90. # Copy the tail
  91. print OUT '<a name="perl"></a>',"\n";
  92. while(my $l = <IN>) {
  93. print OUT $l;
  94. }
  95. close(OUT);
  96. }
  97. #############################
  98. # read a module file and check/print the commandref
  99. sub generateModuleCommandref($$;$)
  100. {
  101. my ($mod, $lang, $fh) = @_;
  102. my $tag;
  103. my $suffix = ($lang eq "EN" ? "" : "_$lang");
  104. my %tagcount= ();
  105. my %llwct = (); # Last line with closed tag
  106. open(MOD, $mods{$mod}) || die("Cant open $mods{$mod}:$!\n");
  107. my $skip = 1;
  108. my $line = 0;
  109. my $docCount = 0;
  110. my $hasLink = 0;
  111. my $dosMode = 0;
  112. while(my $l = <MOD>) {
  113. $line++;
  114. $dosMode = 1 if($l =~ m/^=begin html$suffix.*\r/);
  115. if($l =~ m/^=begin html$suffix$/) {
  116. $l = <MOD>; # skip one line, to be able to repeat join+split
  117. print "*** $lang $mod: nonempty line after =begin html ignored\n"
  118. if($l =~ m/^...*$/);
  119. $skip = 0; $line++;
  120. } elsif($l =~ m/^=end html$suffix$/) {
  121. $skip = 1;
  122. } elsif(!$skip) {
  123. print $fh $l if($fh);
  124. $docCount++;
  125. $hasLink = ($l =~ m/<a name="$mod"/) if(!$hasLink);
  126. foreach $tag (TAGS) {
  127. $tagcount{$tag} +=()= ($l =~ /<$tag>/gi);
  128. $tagcount{$tag} -=()= ($l =~ /<\/$tag>/gi);
  129. $llwct{$tag} = $line if(!$tagcount{$tag});
  130. }
  131. }
  132. }
  133. close(MOD);
  134. print "*** $lang $mods{$mod}: ignoring text due to DOS encoding\n"
  135. if($dosMode);
  136. print "*** $lang $mods{$mod}: No document text found\n"
  137. if(!$suffix && !$docCount && !$dosMode && $mods{$mod} !~ m,/99_,);
  138. if($suffix && !$docCount && !$dosMode) {
  139. if($lang eq "DE" && $fh) {
  140. print $fh <<EOF;
  141. <a name="$mod"></a>
  142. <h3>$mod</h3>
  143. <ul>
  144. Leider keine deutsche Dokumentation vorhanden. Die englische Version gibt es
  145. hier: <a href='commandref.html#$mod'>$mod</a><br/>
  146. </ul>
  147. EOF
  148. }
  149. }
  150. print "*** $lang $mods{$mod}: No a-tag with name=\"$mod\" \n"
  151. if(!$suffix && $docCount && !$hasLink && !$noWarnings);
  152. foreach $tag (TAGS) {
  153. print("*** $lang $mods{$mod}: Unbalanced $tag ".
  154. "($tagcount{$tag}, last line ok: $llwct{$tag})\n")
  155. if($tagcount{$tag} && !$noWarnings);
  156. }
  157. }