/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* spldoc.spl: Generate HTML from SPLDOC comments
*/
load "file";
load "encode_xml";
if (not declared parameters or not declared parameters.infile) {
write("Need parameter 'infile' for input file!\n");
exit;
}
if (not declared parameters or not declared parameters.outdir) {
write("Need parameter 'outdir' for output directory!\n");
exit;
}
var input_file = parameters.infile;
var output_dir = parameters.outdir;
var input;
var module_name;
while (input_file =~ /([^,]+)/) {
module_name = $1;
input ~= file_read($1);
input_file =~ s/([^,]+)//;
}
input_file =~ s/,//g;
module_name =~ s/^.*\/(mod_)//;
module_name =~ s/\.(spl|c)$//;
var html_part1;
var html_part2;
var ascii = module_name ~!= "builtins" ?
'Module "$module_name"\n' : 'Builtins Function Library\n';
ascii ~= ascii =~ s/[^\n]/=/Rg;
var isfirst = 1;
var current_object;
var objectre = 'object|interface|namespace';
function text2html(input, currobj)
{
var text = xml::input;
while (text =~ /\[\[([^\]]+)\]\]/)
{
var rawlinkdata = $1;
var href, link;
rawlinkdata =~ /(?P[^:]+:|)(?P[^\.]*\.|)(?P[A-Za-z0-9_]*)(?P.*)/I;
if (d1 =~ /([^:]+)/) {
href="$1.html#";
if ( "$d2$d3$d4" ~== "" )
link ~= '"$1"';
} else href = "${module_name}.html#";
if (d2 ~== ".")
href ~= "$currobj.";
else
if (d2 =~ /([^\.]+)/) {
href ~= d2;
link ~= '$1' ~
'.';
}
href ~= d3;
link ~= '$d3';
if (d4 ~!= "")
link ~= '$d4';
text =~ s/\[\[([^\]]+)\]\]/$link<\/b><\/a>/;
}
return text;
}
function text2ascii(input, currobj)
{
var text = input;
while (text =~ /\[\[([^\]]+)\]\]/)
{
var rawlinkdata = $1;
var href, link;
rawlinkdata =~ /(?P[^:]+:|)(?P[^\.]*\.|)(?P[A-Za-z0-9_]*)(?P.*)/I;
if (d1 =~ /([^:]+)/ and "$d2$d3$d4" ~== "" )
link ~= '"$1"';
if (d2 =~ /([^\.]+)/)
link ~= '$1.';
link ~= d3;
if (d4 ~!= "")
link ~= '$d4';
text =~ s/\[\[([^\]]+)\]\]/$link/;
}
return text;
}
// extract intro
var intro = "No documentation available for this module.";
if (input =~ s/\/\*\*[ \t]*?\n(.*?)\*\///s) {
var rawtext = $1 =~ s/^[ \t]*\* ?//Rmg;
intro = text2html(rawtext, undef);
ascii ~= "\n${text2ascii(rawtext, undef)}";
}
// extract remaining comments
var lasttype;
while (input =~ s/\/\*\*[ \t]*?\n(?P.*?)\*\/[ \t]*\n[ \t\/]*(?P[^\n]+)//s)
{
import $$;
// clear comment signs from text
text =~ s/^[ \t]*\* ?//mg;
var rawtext = text;
// extract type and name from declaration
decl =~ /^(?P\S+)\s+(?P([A-Za-z0-9_]+(,\s+)?)+)(?P[^;\{]*)/I;
var aname = name =~ s/,\s+/_/Rg;
// this is needed for creating links
var current_object2 = current_object;
// clear old object if this is a new one
if (type =~ /^($objectre)$/) {
if (defined current_object)
html_part1 ~= "
\n}\n";
current_object = undef;
current_object2 = name;
} else
rest ~= ";";
// convert text to html
text = text2html(text, current_object2);
// create 1st HTML snippet
html_part1 ~= <>
${ isfirst ? '' : type =~ /^($objectre)$/ || (type ~!= lasttype && lasttype !~ /^($objectre)$/) ? '' : '
' }
${ defined current_object ? ' ' : '' }
$type
$name$rest
${ type =~ /^($objectre)$/ ? "
\n{" : "" }
>;
// create 2nd HTML snippet
html_part2 ~= <>
$type ${
defined current_object ? '$current_object.' : ''
}$name$rest
${ text =~ s/^\s*(.*?)\s*$/$1/Rs }
>;
// create text output
var title = "$type ${ defined current_object ? '$current_object.' : ''}$name$rest";
ascii ~= <>
$title
${ title =~ s/./${ defined current_object ? '~' : '-'}/Rg }
${text2ascii(rawtext, undef)}
>;
// set new object
if (type =~ /^($objectre)$/)
current_object = name;
lasttype = type;
isfirst = 0;
}
if (defined current_object)
html_part1 ~= "
\n}\n";
html_part1 =~ s,\s+
\s+\{\s+
\s+\}, { },g;
var html = <>
${ module_name ~!= "builtins" ?
"SPL Module Reference: $module_name" :
"SPL Builtins Function Library" }
${ module_name ~!= "builtins" ?
"SPL Module Reference: $module_name" :
"SPL Builtins Function Library" }
load "$module_name";
${ intro =~ s/^\s*(.*?)\s*$/$1/Rs }
$html_part1
$html_part2
>;
file_write("${output_dir}/${module_name}.html", html);
file_write("${output_dir}/${module_name}.txt", ascii);