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
|
##---------------------------------------------------------------------------
## File:
## @(#) sgml.pl 1.3 96/09/30 @(#)
## Author:
## Earl Hood, ehood@medusa.acs.uci.edu
##---------------------------------------------------------------------------
## Copyright (C) 1994-1996 Earl Hood, ehood@medusa.acs.uci.edu
##
## 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., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##
{
package sgml;
$VERSION = '1.0.0';
$namechars = '\w.-';
## Callback functions
$EndTagFunc = '';
$DeclFunc = '';
$CommentFunc = '';
$CdataFunc = '';
$OpenTagFunc = '';
$ProcInsFunc = '';
}
##---------------------------------------------------------------------------
## SGMLread_sgml() reads SGML markup. A callback is called when
## the following occurs:
##
## o An open tag: &$OpenTagFunc($gi, $attribute_list)
## o An end tag: &$EndTagFunc($gi)
## o A comment: &$CommentFunc(*comment_text);
## o Processing instruction:
## &$ProcInsFunc(*pi_text);
## o Character data: &$CdataFunc(*cdata);
##
## Argument descriptions:
## $handle : Filehandle containing the SGML instance.
##
## Notes:
## o read_sgml() is not intended to parse a DTD, or an
## SGML declaration statement, '<!SGML ...>'. It is
## designed to parse SGML instances. If a "<!" sequence
## is encountered (and not part of a comment declaration,
## read_sgml() tries to ignore the declaration.
##
## o Marked sections are not recognized.
##
## o The $CdataFunc may be called consective times for the
## a contiguous character data segment.
##
sub SGMLread_sgml {
package sgml;
local($handle) = shift;
local($data,
$tmp,
$char,
$txt,
$gi,
$left,
$oldhandle,
$oldds);
$oldhandle = select($handle);
$oldds = $/;
$data = '';
PSGML: while (!eof($handle)) {
$txt = '';
$/ = "<";
$data = <$handle>;
$left = chop $data;
&$CdataFunc(*data) if defined (&$CdataFunc);
$char = getc($handle);
if ($char eq '!') { ## Declaration
$char = getc($handle);
if ($char eq '-') {
$char = getc($handle);
while (1) {
$/ = ">";
$tmp = <$handle>;
last if $tmp =~ s/--\s*>$//o;
$txt .= $tmp;
}
$txt .= $tmp;
&$CommentFunc(*txt) if defined (&$CommentFunc);
} else {
$/ = ">";
$txt = <$handle>;
chop $txt;
&$DeclFunc(*txt) if defined (&$DeclFunc);
}
next PSGML;
}
if ($char eq '?') { ## Processing instruction
$/ = ">";
$txt = <$handle>;
chop $txt;
&$ProcInsFunc(*txt) if defined (&$ProcInsFunc);
next PSGML;
}
if ($char eq '/') { ## End tag
$/ = ">";
$txt = <$handle>;
$txt =~ s/[^$namechars]//go;
&$EndTagFunc($txt) if defined (&$EndTagFunc);
next PSGML;
}
if ($char =~ /[$namechars]/o) { ## Open tag
$gi = $char;
while (($char = getc($handle)) =~ /[$namechars]/o) {
$gi .= $char;
}
if ($char ne '>') {
while (!eof($handle)) {
$/ = ">";
$txt .= <$handle>;
last if (&close_notin_lit($txt));
}
chop $txt;
}
&$OpenTagFunc($gi, $txt) if defined (&$OpenTagFunc);
next PSGML;
}
&$CdataFunc(*left) if defined (&$CdataFunc);
} ## End of parse loop
$/ = $oldds;
select($oldhandle);
}
##---------------------------------------------------------------------------##
## Private functions
##---------------------------------------------------------------------------##
package sgml;
##----------------------------------------------------------------------
## Function to check if string has a literla that is open.
## The function returns 1 if it is not. Else it returns 0.
##
sub close_notin_lit {
local($str) = ($_[0]);
local($lit, $after);
while ($str =~ /(['"])/) {
$lit = $1;
$after = $';
if (($lit eq '"' ? ($after =~ /(")/) :
($after =~ /(')/)) ) {
$str = $';
} else {
return 0;
}
}
1;
}
##---------------------------------------------------------------------------##
1;
|