File: mk-qthelp.pl

package info (click to toggle)
octave 10.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 659; xml: 192
file content (201 lines) | stat: -rwxr-xr-x 6,182 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

################################################################################
# File    : mk-qthelp.pl
# Purpose : Transform Octave HTML documentation into intermediate formats
#           for Qt Help Project (.qhp) and Qt Help Collection Project (.qhcp).
# Usage   : mk-qthelp.pl input_htmldir output_filename
################################################################################
use warnings;              # report warnings for questionable run-time code
use strict qw(refs subs);  # check at compile-time for bad programming style
use File::Basename;        # For splitting paths between dir and file
use File::Spec;            # For combining dirs and filenames into paths

################################################################################
# Extract command line arguments
if ($#ARGV != 1)
{ die "USAGE: $0 input_htmldir output_filename"; }

$htmldir = basename ($ARGV[0]);
$basedir = File::Spec->rel2abs (dirname ($ARGV[1]));
$fname   = File::Spec->catfile ($basedir, basename ($ARGV[1]));

################################################################################
# Parse index.html to retrieve the table of contents
$htmlfname = File::Spec->catfile ($basedir, $htmldir, "index.html");
open (my $HTML, "<", $htmlfname) or die "Unable to open $htmlfname";

# Skip through preamble of file to find start of list
while (defined ($_ = <$HTML>) and ! /^<div class="contents">/ ) {;}
while (defined ($_ = <$HTML>)
         and ! /^<ul class="(?:no-bullet|toc-numbered-mark)">/ ) {;}

die "index.html: reached EOF without finding data start pattern" if eof ($HTML);

$level = 0;
while (<$HTML>)
{
  if (/^\s*<li>/)
  {
    ($href, $text) = m|href="([^"]*)"[^<>]*>(.*)</a>|;
    # Sanitize text
    $text =~ s/<[^>]*>//g;         # remove xml-looking blocks like <code>
    $text =~ s/&rsquo;/&#8217;/g;  # Code for apostrophe

    push (@toc, { "href" => $href, "text" => $text,
                  "level" => $level, "sectionstart" => 0 });
  }
  elsif (/^\s+<ul /)
  {
    $level++;
    # Get last node and amend it to have a section start
    %node = %{ $toc[-1] };
    $node{sectionstart} = 1;
    $toc[-1] = { %node };
  }
  elsif (m|^\s+</ul>|)
  {  $level--; }
  elsif (m|^</ul>$|)
  {  last;  }
  else
  {  die "error:unrecognized input:$htmlfname:$.:$_"; }
}

close ($HTML);

die "Failed to parse index.html" if ($level != 0);

################################################################################
# Parse Function-Index.html to retrieve the function reference
$htmlfname = File::Spec->catfile ($basedir, $htmldir, "Function-Index.html");
open ($HTML, "<", $htmlfname) or die "Unable to open $htmlfname";

# Skip through preamble of file to find start of list
while (defined ($_ = <$HTML>)
         and ! /^<table class="(?:index-fn|fn-entries)/ ) {;}

die "Function-Index.html: reached EOF without finding data start pattern"
  if eof ($HTML);

while (<$HTML>)
{
  if (m|<a href="([^"]*)"><code>(.*)</code>|)
  {
    $href = $1, $name = $2;
    # Keep only the first link for each entry
    if (! $kword{$name})
    { $kword{$name} = $href; }
  }
  elsif (m|^</table>$|)
  { last; }
}

close ($HTML);

die "Failed to parse Function-Index.html" if (! m|^</table>$|);

################################################################################
# Prepare Qt Help Project document (.qhp file)
open (my $FH, ">", "$fname.qhp") or die "Unable to open $fname.qhcp";

$htmlfname = File::Spec->catfile ($htmldir, "index.html");

# Add header to file
print $FH <<__EOT1__;
<?xml version="1.0" encoding="UTF-8"?>
<!--DO NOT EDIT!  Generated automatically by $0-->
<QtHelpProject version="1.0">
  <namespace>org.octave.interpreter-1.0</namespace>
  <virtualFolder>doc</virtualFolder>
  <customFilter name="Octave Manual">
    <filterAttribute>core</filterAttribute>
    <filterAttribute>manual</filterAttribute>
  </customFilter>
  <customFilter name="Octave C++ API">
    <filterAttribute>core</filterAttribute>
    <filterAttribute>cpp</filterAttribute>
  </customFilter>
  <filterSection>
    <filterAttribute>core</filterAttribute>
    <filterAttribute>manual</filterAttribute>
    <toc>
      <section title="GNU Octave Manual" ref="$htmlfname">
__EOT1__

# Print out an XML block for each TOC section
$level = 0;
$indent = 4;
foreach $hashref (@toc)
{
  %node = %{$hashref};
  while ($node{level} < $level)
  {
    # Unindent and close section
    $level--;
    print $FH "  " x ($indent + $level);
    print $FH "</section>\n";
  }
  $level = $node{level};

  print $FH "  " x ($indent + $node{level});
  print $FH qq|<section title="$node{text}" |;
  print $FH qq|ref="|, File::Spec->catfile ($htmldir, $node{href});
  print $FH ($node{sectionstart} ? qq|">\n| : qq|"/>\n|);
}

# Spacer between TOC and Keywords
print $FH <<__EOT2__;
      </section>
    </toc>
    <keywords>
__EOT2__

# Print out sorted keywords
foreach $kw (sort {lc($a) cmp lc($b)} (keys (%kword)))
{
  print $FH qq|      <keyword name="$kw" id="$kw" |;
  print $FH qq|ref="|, File::Spec->catfile ($htmldir, $kword{$kw}), qq|"/>\n|;
}

# Footer of file
$htmlfiles = File::Spec->catfile ($htmldir, "*.html");
$pngfiles = File::Spec->catfile ($htmldir, "*.png");
$cssfiles = File::Spec->catfile ($htmldir, "*.css");
print $FH <<__EOT3__;
    </keywords>
    <files>
      <file>$htmlfiles</file>
      <file>$pngfiles</file>
      <file>$cssfiles</file>
    </files>
  </filterSection>
</QtHelpProject>
__EOT3__

close ($FH);

################################################################################
# Prepare Qt Help Collection Project document (.qhcp file)
open ($FH, ">", "$fname.qhcp") or die "Unable to open $fname.qhcp";

$qhpfile = basename ($fname) . ".qhp";
$qchfile = basename ($fname) . ".qch";

# This is the entire file
print $FH <<__EOT4__;
<?xml version="1.0" encoding="UTF-8"?>
<!--DO NOT EDIT! Generated automatically by $0-->
<QHelpCollectionProject version="1.0">
  <docFiles>
    <generate>
      <file>
        <input>$qhpfile</input>
        <output>$qchfile</output>
      </file>
    </generate>
    <register>
      <file>$qchfile</file>
    </register>
  </docFiles>
</QHelpCollectionProject>
__EOT4__