File: help2xml

package info (click to toggle)
elinks 0.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 17,808 kB
  • sloc: ansic: 104,839; sh: 5,343; python: 3,917; perl: 2,175; pascal: 1,562; makefile: 974; javascript: 368; yacc: 295; lisp: 125; ruby: 70; awk: 65
file content (603 lines) | stat: -rwxr-xr-x 20,752 bytes parent folder | download | duplicates (7)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#! /usr/bin/perl
# The copyright notice and license are in the POD at the bottom.

use strict;
use warnings;
use Getopt::Long qw(GetOptions :config bundling gnu_compat);
use autouse 'Pod::Usage' => qw(pod2usage);

sub show_version
{
    # This program has no version number, because it is only useful
    # as part of the ELinks source tree.
    print "help2xml (ELinks)\n";
    pod2usage({-verbose => 99, -sections => "COPYRIGHT AND LICENSE",
	       -exitval => 0});
}

# This script internally stores XML as nested arrays.  Example:
#
# ['element', ['@id', "foo"], ['@dir', "ltr"], "text", ['subelement'], "more"]
# <element id="foo" dir="ltr">text<subelement/>more</element>
#
# A node is one of:
# - A string.  This is just text and will be properly escaped when output.
# - A reference to an array where the first array element is a string that
#   does not begin with '@'.  This array represents an XML element.  The
#   other array elements are the attributes and content of the XML element.
#   The current implementation does not require attributes to be listed
#   before content.
# - A reference to an array where the first array element is a string that
#   begins with '@'.  This array represents an attribute of the parent XML
#   element.  The second array element is the value of the attribute; it
#   must be a string.  There should be no other array elements.
#
# So there is no way to represent XML declarations, processing instructions,
# comments, doctypes, or general entity references.
#
# The names of attributes in these nodes should be written in 'single quotes'
# because "@foo" would make Perl interpolate the value of the @foo array.
# The names of elements are also written in single quotes, by convention.

# xml_output($outfh, $node): Write an XML node to a filehandle.
#
# $outfh: A reference to the output filehandle.
#
# $node: An XML node represented as described above.
#
# return: Unspecified.
sub xml_output
{
    no locale;
    my ($outfh, $node) = @_;
    if (ref($node) eq "ARRAY") {
	my $gi = $node->[0];
	print $outfh "<$gi";
	my @content;
	foreach my $child (@{$node}[1..$#$node]) {
	    if (ref($child) eq "ARRAY" and $child->[0] =~ /^@(.*)/) {
		my $attrname = $1;
		my $attrval = $child->[1];
		$attrval =~ s/([&"]|[^\0-~])/"&#".ord($1).";"/ge;
		print $outfh " $attrname=\"$attrval\"";
	    } else {
		push @content, $child;
	    }
	}
	if (@content) {
	    print $outfh ">";
	    foreach my $child (@content) {
		xml_output($outfh, $child);
	    }
	    print $outfh "</$gi>";
	} else {
	    print $outfh "/>";
	}
    } else {
	$node =~ s/([&<>]|[^\0-~])/"&#".ord($1).";"/ge;
	print $outfh $node;
    }
}

# xml_node_is_element($node, $gi): Check whether $node is an element
# that has the general identifier $gi.
sub xml_node_is_element
{
    my ($node, $gi) = @_;
    return ref($node) eq "ARRAY" && $node->[0] eq $gi;
}

# xml_element_attrs($node): Return the attributes of an XML element as
# a list.  In scalar context, return the number of attributes instead.
sub xml_element_attrs
{
    no locale;
    my ($node) = @_;
    # $node->[0] is the general identifier of the element, a
    # string, thus it won't match in the grep.
    return grep { ref($_) eq "ARRAY" && $_->[0] =~ /^@/ } @$node;
}

# xml_element_content($node): Return the content of an XML element as
# a list.  Not recommended for use in scalar context.
sub xml_element_content
{
    no locale;
    my ($node) = @_;
    return grep { ref($_) ne "ARRAY" || $_->[0] !~ /^@/ } @$node[1..$#$node];
}

# apply_rules($node, $rules): Apply a list of transformations to an
# XML node.
#
# $node: An XML node represented as described above.
#
# $rules: A reference to an array of rules.  The function applies the
# rules in order: the output of a rule can be further transformed with
# later rules but not with the same rule or earlier rules.  Each rule
# in the array is a reference to a hash that has at least these keys:
#
# - FIND: A regular expression.  The function recursively searches for
#   matches in the content of $node, but not in names of elements,
#   names of attributes, or contents of attributes.
# - REPLACE: A reference to a subroutine that returns a replacement for
#   the match, as a list of nodes.  This subroutine is called with
#   no arguments, but it can use the $1 etc. variables that are set
#   according to the regular expression.
#
# return: A list of nodes.
sub apply_rules
{
    my ($node, $rules) = @_;
    my @output;
    if (ref($node) eq "ARRAY") {
	if ($node->[0] =~ /^@/) {
	    return $node;
	} else {
	    return [$node->[0],
		    map({ apply_rules($_, $rules) }
			@{$node}[1..$#$node])];
	}
    } else {
	my @rules = @$rules;
	while (@rules) {
	    my $rule = shift @rules;
	    if ($node =~ $rule->{FIND}) {
		# Using $` or $' anywhere in the program slows down all
		# regexp matches.  So get the values via substr instead.
		my $pre = substr($node, 0, $-[0]);
		my $post = substr($node, $+[0]);
		my @replacement = $rule->{REPLACE}->(); # uses $1 etc.
		return grep({ $_ ne "" }
			    map({ apply_rules($_, [@rules]) }
				$pre, @replacement),
			    apply_rules($post, [$rule, @rules]));
	    }
	}
	return $node;
    }
}

# html_splice_p(@nodes): If the first node in @nodes is a paragraph,
# replace it with its content.  The idea is to avoid extraneous
# vertical space in 'dd' and 'li' elements.
#
# return: The new list of nodes.
sub html_splice_p
{
    my @nodes = @_;
    if (@nodes >= 1
	&& xml_node_is_element($nodes[0], 'p')
	&& !xml_element_attrs($nodes[0])) {
	splice(@nodes, 0, 1, xml_element_content($nodes[0]));
    }
    return @nodes;
}

my %TemplatesDocBook = (
    # named DocBook elements
    APPLICATION => sub { ['application', @_] },
    COMMAND => sub { ['command', @_] },
    ENVAR => sub { ['envar', @_] },
    FILENAME => sub { ['filename', @_] },
    GUIBUTTON => sub { ['guibutton', @_] },
    GUILABEL => sub { ['guilabel', @_] },
    LINK => sub { my $linkend = shift; ['link', ['@linkend', $linkend], @_] },
    LITERAL => sub { ['literal', @_] },
    PARAMETER => sub { ['parameter', @_] },
    SIMPARA => sub { ['simpara', @_] },
    ULINK => sub { my $url = shift; ['ulink', ['@url', $url], @_] },
    USERINPUT => sub { ['userinput', @_] },
    VARIABLELIST => sub { ['variablelist', @_] },

    # not named after DocBook elements, but pretty simple anyway
    CMDOPTTYPE => sub { ['replaceable', @_] },
    MANLINK => sub { my ($title, $volnum) = @_;
		     ['citerefentry', ['refentrytitle', $title], ['manvolnum', $volnum]] },
    SGMLATTR => sub { ['sgmltag', ['@class', "attribute"], @_] },
    SGMLELEMENT => sub { ['sgmltag', ['@class', "element"], @_] },
    STRONG => sub { ['emphasis', ['@role', "strong"], @_] },

    # not so simple
    CFGOPTENTRY => sub { my ($name, $type, $default, @children) = @_;
			 ['varlistentry', ['@id', $name],
			  ['term', ['literal', $name], " ", ['type', $type], " $default"],
			  ['listitem', @children]] },
    CMDOPTINFO => sub { my ($info) = @_; " $info" },
    CMDOPTNAME => sub { my $id = shift; ['option', ['@id', $id], @_] },
    CFGOPTTREE => sub { my ($name, $info, @children) = @_;
			['refsect2', ['@id', $name],
			 ['title', ['literal', $name], " ($info)"],
			 "\n", @children] },
    GUIMENUCHOICE => sub { my $item = pop; ['menuchoice', map(['guimenu', $_], @_), ['guimenuitem', $item]] },
    ITEMIZELIST => sub { ['itemizedlist', ['@spacing', "compact"],
			  map { ['listitem', $_], "\n" } @_] },
    USEREXAMPLE => sub { ['informalexample', ['simpara', ['userinput', @_]]], "\n" },
    VARLISTENTRY => sub { my ($termchildren, @itemchildren) = @_;
			  ['varlistentry', ['term', @$termchildren],
			   ['listitem', @itemchildren]] },
);
my %TemplatesHTML = (
    # named DocBook elements
    APPLICATION => sub { ['em', @_] },
    COMMAND => sub { ['kbd', @_] },
    ENVAR => sub { ['tt', @_] },
    FILENAME => sub { ['tt', @_] },
    GUIBUTTON => sub { "[ ", @_, " ]" },
    GUILABEL => sub { @_ },
    LINK => sub { my $linkend = shift; ['a', ['@href', "#$linkend"], @_] },
    LITERAL => sub { @_ },
    PARAMETER => sub { ['var', @_] },
    SIMPARA => sub { ['p', @_] },
    ULINK => sub { my $url = shift; ['a', ['@href', $url], @_] },
    USERINPUT => sub { ['kbd', @_] },
    VARIABLELIST => sub { ['dl', @_] },

    # not named after DocBook elements, but pretty simple anyway
    CMDOPTTYPE => sub { @_ },
    MANLINK => sub { my ($title, $volnum) = @_;
		     ['b', "$title($volnum)"] },
    SGMLATTR => sub { ['code', @_] },
    SGMLELEMENT => sub { ['code', @_] },
    STRONG => sub { ['strong', @_] },

    # not so simple
    CFGOPTENTRY => sub { my ($name, $type, $default, @children) = @_;
			 ['dt', ['@id', $name], "$name $type $default"],
			 ['dd', html_splice_p(@children)] },
    CMDOPTINFO => sub { my ($info) = @_;
			if ($info =~ /^(\(alias for )([\w.]+)(\))$/) {
			    return " $1", ['a', ['@href', "elinks.conf.5.html#$2"], $2], $3;
			} else {
			    return " $info";
			} },
    CMDOPTNAME => sub { my $id = shift; ['span', ['@id', $id], @_] },
    CFGOPTTREE => sub { my ($name, $info, @children) = @_;
			['h3', ['@id', $name], "$name ($info)"],
			"\n", @children },
    GUIMENUCHOICE => sub { ['em', join(" \x{2192} ", @_)] },
    ITEMIZELIST => sub { ['ul', map { ['li', html_splice_p($_)], "\n" } @_] },
    USEREXAMPLE => sub { ['blockquote', ['p', ['kbd', @_]]], "\n" },
    VARLISTENTRY => sub { my ($termchildren, @itemchildren) = @_;
			  ['dt', @$termchildren],
			  ['dd', html_splice_p(@itemchildren)] },
);

sub optiondesc
{
    my ($pipe, $rules, $templates) = @_;
    my @ret;
    my $paragraph_text;

    my $end_paragraph = sub {
	if (defined $paragraph_text) {
	    push @ret, $templates->{SIMPARA}($paragraph_text);
	    undef $paragraph_text;
	}
    };

    while (defined($_) and /^ {12}/) {
	# '            Cookie maximum age (in days):'
	# '            -1 is use cookie's expiration date if any'
	# '            0  is force expiration at the end of session, ignoring cookie's'
	# '               expiration date'
	# '            1+ is use cookie's expiration date, but limit age to the given'
	# '               number of days'
	if (/^ {12}((?:%|[+-]?\d).*)$/) {
	    $end_paragraph->();
	    my @list_paragraphs;
	    do {
		my $paragraph_text = "";
		do {
		    $paragraph_text .= "$1\n";
		    $_ = <$pipe>;
		} while (defined($_) and /^ {12}(\s+\S.*)$/);
		chomp $paragraph_text;
		push @list_paragraphs, $templates->{SIMPARA}($paragraph_text);
	    } while (defined($_) and /^ {12}((?:%|[+-]?\d).*)$/);
	    push @ret, $templates->{ITEMIZELIST}(@list_paragraphs);
	} elsif (/^ {12}\t(\d.*)$/) {
	    $end_paragraph->();
	    my @list_paragraphs;
	    do {
		push @list_paragraphs, $templates->{SIMPARA}($1);
		$_ = <$pipe>;
	    } while (defined($_) and /^ {12}\t(\d.*)$/);
	    push @ret, $templates->{ITEMIZELIST}(@list_paragraphs);
	} elsif (/^ {12}\t(-.*)$/) {
	    $end_paragraph->();
	    push @ret, $templates->{USEREXAMPLE}($1);
	    $_ = <$pipe>;
	} elsif (/^ {12}\t(\w+)(\(.*\))\s+:\s+(\S.*)$/) {
	    $end_paragraph->();
	    my @list_paragraphs;
	    my @remote_param_rules = (
		{ FIND => qr(\b(URL|text)\b),
		  REPLACE => sub { $templates->{PARAMETER}($1) } },
		{ FIND => qr(\b(new-tab|new-window|openBrowser)\b),
		  REPLACE => sub { $templates->{LITERAL}($1) } },
		);
	    do {
		push @list_paragraphs, $templates->{SIMPARA}(
		    $templates->{COMMAND}($1, apply_rules($2, \@remote_param_rules)),
		    ": $3");
		$_ = <$pipe>;
	    } while (defined($_) and /^ {12}\t(\w+)(\(.*\))\s+:\s+(\S.*)$/);
	    push @ret, $templates->{ITEMIZELIST}(@list_paragraphs);
	} elsif (/^ {12}(.*\S.*)$/) {
	    $paragraph_text .= "$1\n";
	    $_ = <$pipe>;
	} else {
	    $end_paragraph->();
	    $_ = <$pipe>;
	}
    }
    $end_paragraph->();
    return map { apply_rules($_, $rules) } @ret;
}

sub cmdopt_id
{
    no locale;
    my ($option) = @_;
    $option =~ s/^-+//;
    $option =~ s/([^A-Za-z0-9-.])/sprintf('_%u', ord($1))/ge;
    return "cmdopt:$option";
}

sub convert_config
{
    my ($outfh, $elinks, $option, $templates) = @_;
    local $_;

    # The rules that apply to most of the output.
    # See &apply_rules for the format.
    my @shared_rules = (
	# files, commands, environment variables
	{ FIND => qr!"vi"!,
	  REPLACE => sub { $templates->{COMMAND}("vi") } },
	{ FIND => qr!\b(xterm)\b!,
	  REPLACE => sub { $templates->{COMMAND}($1) } },
	{ FIND => qr!((?:\$|\b)(?:EDITOR|FTP_PROXY|HOME|HTTP_PROXY|HTTPS_PROXY|MAILCAP|NNTPSERVER|NO_PROXY|TERM|WWW_HOME|X509_CLIENT_CERT))\b!,
	  REPLACE => sub { $templates->{ENVAR}($1) } },
	{ FIND => qr!(~/\.elinks|/dev/urandom|/dev/zero|\bsetup\.h|\bmime\.types)\b!,
	  REPLACE => sub { $templates->{FILENAME}($1) } },
	{ FIND => qr!\b(rename|fsync|strftime)\((\d+)\)!,
	  REPLACE => sub { $templates->{MANLINK}($1, $2) } },

	# the rest
	{ FIND => qr!\b(http[46]?://[\w./+-]+?)(\.?)$!,
	  REPLACE => sub { $templates->{ULINK}($1, $1), $2 } },
	{ FIND => qr!(ELinks bug (\d+))!,
	  REPLACE => sub { $templates->{ULINK}("http://bugzilla.elinks.cz/show_bug.cgi?id=$2", $1) } },
	{ FIND => qr!\b(ELinks)\b!,
	  REPLACE => sub { $templates->{APPLICATION}($1) } },
	);

    my @command_rules = (
	{ FIND => qr!(-default-mime-type text/html)!,
	  REPLACE => sub { $templates->{USERINPUT}($1) } },

	# This rule cannot be shared because the configuration option
	# documentation does not have the anchors for the links.
	{ FIND => qr!(-?config-dir|-dump|-default-mime-type|-touch-files|-no-connect|-session-ring)!,
	  REPLACE => sub { $templates->{LINK}(cmdopt_id($1), $1) } },

	@shared_rules);

    my @config_rules = (
	# non-ASCII characters
	{ FIND => qr!<->!,          REPLACE => sub { "\x{2194}" } },
	{ FIND => qr!(\s)-(\s)!,    REPLACE => sub { "$1\x{2013}$2" } },
	{ FIND => qr!(\s)---?(\s)!, REPLACE => sub { "$1\x{2014}$2" } },

	# user interface
	{ FIND => qr!(Setup) -> (Terminal options)!,
	  REPLACE => sub { $templates->{GUIMENUCHOICE}($1, $2) } },
	{ FIND => qr!\[ (Save) \]!,
	  REPLACE => sub { $templates->{GUIBUTTON}($1) } },
	{ FIND => qr!\b(Goto URL)\b!,
	  REPLACE => sub { $templates->{GUILABEL}($1) } },

	# SGML
	{ FIND => qr!\b(ACCESSKEY|TABINDEX)\b!,
	  REPLACE => sub { $templates->{SGMLATTR}($1) } },
	{ FIND => qr!\b(IMG)\b!,
	  REPLACE => sub { $templates->{SGMLELEMENT}($1) } },
	{ FIND => qr!\b(alt)/(title)\b!,
	  REPLACE => sub { $templates->{SGMLATTR}($1), "/", $templates->{SGMLATTR}($2) } },
	{ FIND => qr!\b(alt)( attribute)!,
	  REPLACE => sub { $templates->{SGMLATTR}($1), $2 } },

	# typography
	{ FIND => qr!\b_(not)_\b!,
	  REPLACE => sub { $templates->{STRONG}($1) } },

	# This rule cannot be shared because the command-line option
	# documentation does not have the anchors for the links.
	{ FIND => qr!\b(connection\.try_ipv6|cookies\.save|document\.browse\.minimum_refresh_time|document\.browse\.links\.color_dirs)\b!,
	  REPLACE => sub { $templates->{LINK}($1, $1) } },

	@shared_rules);

    open my $pipe, "-|", $elinks, $option or die;
    my $version = <$pipe>;
    chomp $version;
    $version =~ s/^ELinks ([-.\w]+).*$/$1/ or die "unusual version: $version";
    my @nodes;
    $_ = <$pipe>;
    while (defined($_)) {
	if (/^$/) {
	    $_ = <$pipe>;
	} elsif (/^Configuration options:$/) {
	    # The "Generated using" line is here at the top, because
	    # DocBook XML does not allow anything else to follow a
	    # refsect2 within a refsect1.
	    push @nodes, $templates->{SIMPARA}(
		"Generated using output from ELinks version $version.");
	    $_ = <$pipe>;
	    while (defined($_)) {
		if (/^$/) {
		    $_ = <$pipe>;
		} elsif (/^ {2}(\S.*): \(([\w.-]+)\)$/) {
		    # '  Browsing: (document.browse)'
		    my ($tree_info, $tree_name) = ($1, $2);
		    my @tree_nodes;
		    $_ = <$pipe>;
		    push @tree_nodes, optiondesc($pipe, \@config_rules, $templates);
		    my @varlistentries;
		    while (defined($_)) {
			if (/^$/) {
			    $_ = <$pipe>;
			} elsif (/^ {4}(\S+) (\S+) (\(.*)$/) {
			    # '    cookies.save [0|1] (default: 1)'
			    my ($optname, $opttype, $optdefault) = ($1, $2, $3);
			    while ($optdefault =~ /^\([^"()]*"[^"]*$/s) {
				# a special hack for document.dump.separator,
				# which has newlines in the default value
				my $contline = <$pipe>;
				last unless defined($contline);
				chomp $contline;
				$optdefault .= "\n$contline";
			    }
			    $_ = <$pipe>;
			    push @varlistentries, $templates->{CFGOPTENTRY}(
				$optname, $opttype, $optdefault,
				optiondesc($pipe, \@config_rules, $templates));
			} else {
			    last;
			}
		    }
		    push @tree_nodes, $templates->{VARIABLELIST}(@varlistentries)
			if @varlistentries;
		    push @nodes, $templates->{CFGOPTTREE}(
			$tree_name, $tree_info, @tree_nodes);
		} else {
		    last;
		}
	    }
	} elsif (/^Usage:/) {
	    $_ = <$pipe>;
	} elsif (/^Options:$/) {
	    $_ = <$pipe>;
	    my @varlistentries;
	    my $name_rules = [
		{ FIND => qr/([^,\s]+)/,
		  REPLACE => sub { $templates->{CMDOPTNAME}(cmdopt_id($1), $1) } },
		];
	    while (defined($_)) {
		if (/^$/) {
		    $_ = <$pipe>;
		} elsif (/^ {4}(\S+(?:,\s+\S+)*)(?:\s+([\[<]\S*))?(?:\s+(\(.*\)))?\s*$/) {
		    my @optnames = apply_rules($1, $name_rules);
		    my (@opttype, @optinfo);
		    @opttype = (" ", $templates->{CMDOPTTYPE}($2)) if defined($2);
		    @optinfo = $templates->{CMDOPTINFO}($3) if defined($3);
		    $_ = <$pipe>;
		    push @varlistentries, $templates->{VARLISTENTRY}(
			[@optnames, @opttype, @optinfo],
			optiondesc($pipe, \@command_rules, $templates));
		} else {
		    last;
		}
	    }
	    push @nodes, $templates->{VARIABLELIST}(@varlistentries)
		if @varlistentries;
	    push @nodes, $templates->{SIMPARA}(
		"Generated using output from ELinks version $version.");
	} else {
	    last;
	}
    }
    die "parsing stopped at $.: $_" if defined($_);
    xml_output($outfh, $_) foreach @nodes;
}

GetOptions("help" => sub { pod2usage({-verbose => 1, -exitval => 0}) },
	   "version" => \&show_version)
    or exit 2;
print(STDERR "$0: wrong number of operands\n"), exit 2 if @ARGV != 2;
my ($ELinks, $Outfname) = @ARGV;

my ($Option, $Templates);
$Option = "--config-help" if $Outfname =~ m(config[^/]*$);
$Option = "--long-help" if $Outfname =~ m(command[^/]*$);
$Templates = \%TemplatesDocBook if $Outfname =~ m(xml[^/]*$);
$Templates = \%TemplatesHTML if $Outfname =~ m(html[^/]*$);
unless ($Option and $Templates) {
    print(STDERR "$0: name of output file does not indicate its content: $Outfname\n");
    exit 2;
}
open my $outfh, ">", $Outfname or die "$Outfname: $!\n";
convert_config $outfh, $ELinks, $Option, $Templates;
close $outfh or die "$Outfname: $!\n";

__END__

=head1 NAME

help2xml - Convert help output from ELinks to DocBook XML or XHTML.

=head1 SYNOPSIS

B<help2xml> F<.../src/elinks> F<.../option-command.frag.xml>

B<help2xml> F<.../src/elinks> F<.../option-config.frag.xml>

B<help2xml> F<.../src/elinks> F<.../option-command.frag.xhtml>

B<help2xml> F<.../src/elinks> F<.../option-config.frag.xhtml>

=head1 DESCRIPTION

B<help2xml> runs B<elinks --long-help> or B<elinks --config-help> to
get the documentation of command-line or configuration options from
the elinks executable, and converts it to a fragment of DocBook XML or
XHTML.  In the build system, these fragments are then included in the
DocBook and XHTML versions of the L<elinks(1)> and L<elinks.conf(5)>
manual pages.

=head1 ARGUMENTS

=over

=item F<.../src/elinks>

The B<elinks> executable file that B<help2xml> runs in order to
get the documentation.

=item F<.../option-command.frag.xml>

=item F<.../option-config.frag.xml>

=item F<.../option-command.frag.xhtml>

=item F<.../option-config.frag.xhtml>

The output file to which B<help2xml> writes the DocBook XML or
XHTML fragment.  The basename of this file must include the word
"command" for command-line options, or "config" for configuration
options.  It must also include "xml" for Docbook XML, or "html" for
XHTML.

=back

=head1 AUTHOR

Kalle Olavi Niemitalo <kon@iki.fi>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2008 Kalle Olavi Niemitalo.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.