3
# Xdoc to HTML Converter
4
# Copyright (C) 2014 David Rager (ragerdl@cs.utexas.edu)
6
# License: (An MIT/X11-style license)
8
# Permission is hereby granted, free of charge, to any person obtaining a
9
# copy of this software and associated documentation files (the "Software"),
10
# to deal in the Software without restriction, including without limitation
11
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
# and/or sell copies of the Software, and to permit persons to whom the
13
# Software is furnished to do so, subject to the following conditions:
15
# The above copyright notice and this permission notice shall be included in
16
# all copies or substantial portions of the Software.
18
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
# DEALINGS IN THE SOFTWARE.
34
-------------------------------------------------------------------------------
36
xdata2html.pl: Convert xdata.js and xindex.js into HTML files so
37
search engines can crawl our doucmentation.
39
NOTE: Only those who are trying make their Xdoc manual accessible
40
to search engines need to run this script. Otherwise, they
41
should just use index.html.
43
-------------------------------------------------------------------------------
47
print "; Converting xdata.js\n\n";
51
open (my $fh, "<", $filename) or die("Can't open $filename: $!\n");
58
sub wrap_xdoc_fragment {
60
my $wrap = "<!DOCTYPE xdoc [";
61
$wrap .= "<!ENTITY mdash \"—\">";
62
$wrap .= "<!ENTITY rarr \"→\">";
63
$wrap .= "<!ENTITY nbsp \" \">";
65
$wrap .= "<root>$str</root>";
69
if (! -f "xdata.js") {
70
print "Error: xdata.js not found.\n";
74
print "; Reading file\n";
76
my $xdatastr = read_whole_file("xdata.js");
77
print "; Checking xdata file\n";
80
my $start = "var xdata = ";
81
if (length($start) < length($xdatastr)
82
&& substr($xdatastr, 0, length($start)) eq $start
83
&& substr($xdatastr, length($xdatastr)-1, 1) eq ";")
85
my $stop = length($xdatastr) - length($start) - length(";");
86
$json = substr($xdatastr, length($start), $stop);
89
print "Error: xdata.js does not have the expected format\n";
93
print "; Parsing JSON data.\n";
94
my $xsd = new JSON::XS;
95
my $xdata = $xsd->decode($json);
96
if (!(ref($xdata) eq "HASH")) {
97
print "Error: JSON object within xdata.js not a hash?\n";
102
my $xindexstr = read_whole_file("xindex.js");
103
print "; Checking xindex file\n";
105
if (! -f "xindex.js") {
106
print "Error: xindex.js not found.\n";
110
$start = "var xindex = ";
111
if (length($start) < length($xindexstr)
112
&& substr($xindexstr, 0, length($start)) eq $start
113
&& substr($xindexstr, length($xindexstr)-1, 1) eq ";")
115
my $stop = length($xindexstr) - length($start) - length(";");
116
$json = substr($xindexstr, length($start), $stop);
119
print "Error: xindex.js does not have the expected format\n";
123
print "; Parsing JSON index.\n";
124
my $xsi = new JSON::XS;
125
my $xindex = $xsi->decode($json);
126
if (!(ref($xindex) eq "ARRAY")) {
127
print "Error: JSON object within xindex.js not a hash?\n";
132
my %human_readable_names = ();
133
foreach my $entry (@$xindex) {
134
$shorts{$entry->[0]} = $entry->[4];
135
$human_readable_names{$entry->[0]} = $entry->[1];
138
my $xml_parser = XML::LibXML->new;
139
my $xslt_parser = XML::LibXSLT->new;
140
my $xsl = $xml_parser->parse_file("render-html.xsl");
141
my $stylesheet = $xslt_parser->parse_stylesheet($xsl);
144
while(my ($key,$val) = each %$xdata)
146
my $filename = "HTML/$key.html";
147
open (my $fh, ">", $filename) or die "Could not open $filename";
149
my $enc = $xsd->encode($val);
150
my $long = $val->[3];
151
my $human_readable_name = $human_readable_names{$key};
152
my $short = $shorts{$key};
154
$pagexml .= "<p>$short</p>\n";
156
$pagexml = wrap_xdoc_fragment($pagexml);
157
my $xml = $xml_parser->parse_string($pagexml);
159
$results = $stylesheet->transform($xml);
160
my $output = $stylesheet->output_string($results);
163
my $pagehtml .= "<html>\n<head>\n";
164
$pagehtml .= "<meta charset=\"UTF-8\">\n";
165
$pagehtml .= "<title>$human_readable_name</title>\n";
166
# To debug this script, you may need to comment out the
167
# javascript, since it causes the browser to immediately redirect.
168
# Since it's javascript, this redirect should not be run by search
169
# engines, allowing them to crawl this HTML page.
170
$pagehtml .= "<script language=\"javascript\">\n";
171
$pagehtml .= "<!--\n";
172
$pagehtml .= " location.replace(\"../index.html?topic=$key\");\n";
173
$pagehtml .= "//-->\n";
174
$pagehtml .= "</script>\n";
175
$pagehtml .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\"/>\n";
176
$pagehtml .= "</head>\n<body>\n\n<h3>";
178
$pagehtml .= "</h3>\n\n";
179
$pagehtml .= "$output";
180
$pagehtml .= "</body>\n</html>\n";
182
print $fh "$pagehtml";
184
if ($progress % 500 == 0) {
185
print "Done generating $progress HTML files\n";
190
print "; All done.\n\n";