File: stripsgml

package info (click to toggle)
perlsgml 1996Oct09-6
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,452 kB
  • ctags: 792
  • sloc: perl: 4,639; makefile: 167
file content (368 lines) | stat: -rwxr-xr-x 11,282 bytes parent folder | download | duplicates (5)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#! /usr/local/bin/perl
##---------------------------------------------------------------------------##
##  File:
##      @(#)  stripsgml 1.3 96/10/07 @(#)
##  Author:
##      Earl Hood       ehood@medusa.acs.uci.edu
##  Description:
##	Remove SGML markup.  Reads from STDIN, writes to STDOUT.
##	Character entities are converted to ASCII equivalents.  However,
##	due to the nature of some special characters, some special
##	characters may get lost in the output.
##
##  Usage:
##	% stripsgml [-html] < file.sgml > file.txt
##
##  	The -html option cause URLs in anchor elements in an HTML document
##	be preserved in the text output.
##
##---------------------------------------------------------------------------##
##  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 main;

##---------------------------------------------------------------------------##

($PROG = $0) =~ s/.*\///;
$VERSION = "1.0.0";

##---------------------------------------------------------------------------##
##	Require libraries
##---------------------------------------------------------------------------##
unshift(@INC, 'lib');
require "sgml.pl"	|| die "Unable to require sgml.pl\n";
require "newgetopt.pl"	|| die "Unable to require newgetopt.pl\n";

##---------------------------------------------------------------------------##
##	Associative array for entity substitutions
##---------------------------------------------------------------------------##

%Entity = (
#-----------------------------
# Numeric and Special Graphic
#-----------------------------
#   Entity      ASCII substitution
#   ----------  ------------------
    "half",	"1/2",	# fraction one-half
    "frac12",	"1/2",
    "#189",	"1/2",
    "frac14",	"1/4",	# fraction one-quarter
    "#188",	"1/4",
    "frac34",	"3/4",	# fraction three-quarters
    "#190",	"3/4",
    "frac18",	"1/8",	# fraction one-eighth
    "frac38",	"3/8",	# fraction three-eighths
    "frac58",	"5/8",	# fraction five-eighths
    "frac78",	"7/8",	# fraction seven-eighths
    "sup1",	"[1]",	# superscript one
    "#185",	"[1]",
    "sup2",	"[2]",	# superscript two
    "#178",	"[2]",
    "sup3",	"[3]",	# superscript three
    "#179",	"[3]",
    "plus",	"+",	# plus sign
    "plusmn",	"+/-",	# plus-or-minus sign
    "lt",	"<",	# less-than sign
    "equals",	"=",	# equals sign
    "gt",	">",	# greater-than sign
    "divide",	"/",	# divide sign
    "times",	"*",	# times sign
    "curren",	"\$",	# general currency sign
    "#164",	"\$",
    "pound",	"L",	# pound sign
    "#163",	"L",
    "dollar",	"\$",	# dollar sign
    "cent",	"c",	# cent sign
    "#162",	"c",
    "yen",	"Y",	# yen sign
    "#165",	"Y",
    "num",	"#",	# number sign
    "percent",	"%", 	# percent sign
    "amp",	"&",	# ampersand
    "ast",	"*",	# asterisk
    "commat",	"@",	# commercial at
    "lsqb",	"[",	# left square bracket
    "bsol",	"\\",	# reverse solidus
    "rsqb",	"]",	# right square bracket
    "lcub",	"{",	# left curly bracket
    "verbar",	"|",	# vertical bar
    "rcub",	"}",	# right curly bracket
    "copy",	"(C)",	# copyright sign
    "#169",	"(C)",
    "reg",	"(R)",	# registered sign
    "#174",	"(R)",
    "trade",	"(TM)",	# tradmark
    "brvbar",	"|",	# broken vertical bar
    "#166",	"|",
    "excl",	"!",	# exclamation mark
    "quot",	'"',	# quotation mark
    "apos",	"'",	# apostrophe
    "lpar",	"(",	# left parenthesis
    "rpar",	")",	# right parenthesis
    "comma",	",",	# comma
    "lowbar",	"_",	# low line
    "hyphen",	"-",	# hyphen
    "period",	".",	# full stop, period
    "sol",	"/",	# solidus
    "colon",	":",	# colon
    "semi",	";",	# semicolon
    "quest",	"?",	# question mark
    "laquo",	"<<",	# angle quotation mark, left
    "&#171",	"<<",
    "raquo",	">>",	# angle quotation mark, right
    "#187",	">>",
    "lsquo",	"'",	# single quotation mark, left
    "rsquo",	"'",	# single quotation mark, right
    "ldquo",	'"',	# double quotation mark, left
    "rdquo",	'"',	# double quotation mark, right
    "nbsp",	" ",	# no break (required) space
    "#160",	" ",
    "shy",	"-",	# soft hyphen
    "#173",	"-",

#---------------
# Added Latin 1
#---------------
#   Entity      ASCII substitution
#   ----------  ------------------
    "aacute",	"a",	# small a, acute accent
    "#225",	"a",
    "Aacute",	"A",	# capital A, acute accent
    "#193",	"A",
    "acirc",	"a",	# small a, circumflex accent
    "#226",	"a",
    "Acirc",	"A",	# capital A, circumflex accent
    "#194",	"A",
    "agrave",	"a",	# small a, grave accent
    "#224",	"a",
    "Agrave",	"A",	# capital A, grave accent
    "#192",	"A",
    "aring",	"a",	# small a, ring
    "#229",	"a",
    "Aring",	"A",	# capital A, ring
    "#197",	"A",
    "atilde",	"a",	# small a, tilde
    "#227",	"a",
    "Atilde",	"A",	# capital A, tilde
    "#195",	"A",
    "auml",	"a",	# small a, dieresis or umlaut mark
    "#228",	"a",
    "Auml",	"A",	# capital A, dieresis or umlaut mark
    "#196",	"A",
    "aelig",	"ae",	# small ae diphthong (ligature)
    "#230",	"ae",
    "AElig",	"AE",	# capital AE diphthong (ligature)
    "#198",	"AE",
    "ccedil",	"c",	# small c, cedilla
    "#231",	"c",
    "Ccedil",	"C",	# capital C, cedilla
    "#199",	"C",
    "eth",	"d",	# small eth, Icelandic
    "#240",	"d",
    "ETH",	"d",	# capital Eth, Icelandic
    "#208",	"d",
    "eacute",	"e",	# small e, acute accent
    "#233",	"e",
    "Eacute",	"E",	# capital E, acute accent
    "#201",	"E",
    "ecirc",	"e",	# small e, circumflex accent
    "#234",	"e",
    "Ecirc",	"E",	# capital E, circumflex accent
    "#202",	"E",
    "egrave",	"e",	# small e, grave accent
    "#232",	"e",
    "Egrave",	"E",	# capital E, grave accent
    "#200",	"E",
    "euml",	"e",	# small e, dieresis or umlaut mark
    "#235",	"e",
    "Euml",	"E",	# capital E, dieresis or umlaut mark
    "#203",	"E",
    "iacute",	"i",	# small i, acute accent
    "#237",	"i",
    "Iacute",	"I",	# capital I, acute accent
    "#205",	"I",
    "icirc",	"i",	# small i, circumflex accent
    "#238",	"i",
    "Icirc",	"I",	# capital I, circumflex accent
    "#206",	"I",
    "igrave",	"i",	# small i, grave accent
    "#236",	"i",
    "Igrave",	"I",	# capital I, grave accent
    "#204",	"I",
    "iuml",	"i",	# small i, dieresis or umlaut mark
    "#239",	"i",
    "Iuml",	"I",	# capital I, dieresis or umlaut mark
    "#207",	"I",
    "ntilde",	"n",	# small n, tilde
    "#241",	"n",
    "Ntilde",	"N",	# capital N, tilde
    "#209",	"N",
    "oacute",	"o",	# small o, acute accent
    "#243",	"o",
    "Oacute",	"O",	# capital O, acute accent
    "#211",	"O",
    "ocirc",	"o",	# small o, circumflex accent
    "#244",	"o",
    "Ocirc",	"O",	# capital O, circumflex accent
    "#212",	"O",
    "ograve",	"o",	# small o, grave accent
    "#242",	"o",
    "Ograve",	"O",	# capital O, grave accent
    "#210",	"O",
    "oslash",	"o",	# small o, slash
    "#248",	"o",
    "Oslash",	"O",	# capital O, slash
    "#216",	"O",
    "otilde",	"o",	# small o, tilde
    "#245",	"o",
    "Otilde",	"O",	# capital O, tilde
    "#213",	"O",
    "ouml",	"o",	# small o, dieresis or umlaut mark
    "#246",	"o",
    "Ouml",	"O",	# capital O, dieresis or umlaut mark
    "#214",	"O",
    "szlig",	"s",	# small sharp s, German (sz ligature)
    "#223",	"s",
    "thorn",	"p",	# small thorn, Icelandic
    "#254",	"p",
    "THORN",	"P",	# capital THORN, Icelandic
    "#222",	"P",
    "uacute",	"u",	# small u, acute accent
    "#250",	"u",
    "Uacute",	"U",	# capital U, acute accent
    "#218",	"U",
    "ucirc",	"u",	# small u, circumflex accent
    "#251",	"u",
    "Ucirc",	"U",	# capital U, circumflex accent
    "#219",	"U",
    "ugrave",	"u",	# small u, grave accent
    "#249",	"u",
    "Ugrave",	"U",	# capital U, grave accent
    "#217",	"U",
    "uuml",	"u",	# small u, dieresis or umlaut mark
    "#252",	"u",
    "Uuml",	"U",	# capital U, dieresis or umlaut mark
    "#220",	"U",
    "yacute",	"y",	# small y, acute accent
    "#253",	"y",
    "Yacute",	"Y",	# capital Y, acute accent
    "#221",	"Y",
    "yuml",	"y",	# small y, dieresis or umlaut mark
    "#255",	"y",
);

##---------------------------------------------------------------------------##
##	Globals
##---------------------------------------------------------------------------##

##	Variable to hold current URL
$Url	= '';

##	Register callbacks to sgml.pl
$sgml'CdataFunc		= "main'cdata_cb";
$sgml'OpenTagFunc	= "main'open_tag_cb";
$sgml'EndTagFunc	= "main'close_tag_cb";

##---------------------------------------------------------------------------##
				##------------##
				## Begin MAIN ##
				##------------##
{

&get_cli_opts();
&SGMLread_sgml(STDIN);
exit 0;

}
				##----------##
				## End MAIN ##
				##----------##
##---------------------------------------------------------------------------##
##	Routines
##---------------------------------------------------------------------------##

sub get_cli_opts {
    &Usage() unless
    &NGetOpt(
	"html",		# Generate compact listing

	"help"    	# Help message
    );
    &Usage()	if defined($opt_help);
    $HTML = 1	if defined($opt_html);
}

sub cdata_cb {
    local(*data) = shift;

    $data =~ s/\&([#\w-._]+);/&expand_ent($1)/ge;
    print STDOUT $data;
}

sub open_tag_cb {
    local($gi, $attr) = ($_[0], $_[1]);

    if ($HTML && ($gi =~ /^A$/i)) {
	if ($attr =~ /href\s*=\s*['"]([^'"]+)['"]/i) {
	    $Url = $1;
	    $Url = ''  if $Url =~ /^#/;		# Ignore relative URLs
	} else {
	    $Url = '';
	}
    }
}

sub close_tag_cb {
    local($gi) = ($_[0]);

    if ($HTML && ($gi =~ /^A$/i) && $Url) {
	print STDOUT " <URL:$Url>";
	$Url = '';
    }
}

sub expand_ent {
    local($name) = shift;
    defined($Entity{$name}) ? $Entity{$name} : "&$name;";
}

##---------------------------------------------------------------------------##
sub Usage {
    print STDOUT <<EndOfUsage;

Usage:  $PROG [<options>] <  file.sgm  > file.txt
Options:
  -html              : Preserve anchor URLs in output
  -help              : This message
 
Description:
  stripsgml strips out SGML tags.  Common entity references for special
  characters are converted to ASCII.  Input is read from standard in, and
  output is sent to standard out.
 
Version: $VERSION
dtd.pl Version: $dtd'VERSION
 
  Copyright (C) 1994-1996  Earl Hood, ehood\@medusa.acs.uci.edu
  stripsgml comes with ABSOLUTELY NO WARRANTY and stripsgml may be copied
  only under the terms of the GNU General Public License (version 2, or
  later), which may be found in the distribution.
EndOfUsage

    exit 0;
}