utility to write docbook from text sources

This commit is contained in:
david 2002-01-12 21:31:26 +00:00
parent 2aa4aeea1d
commit 4976f7a258
3 changed files with 255 additions and 0 deletions

34
LDP/txt2db/README Normal file
View File

@ -0,0 +1,34 @@
This is a utility to convert text files in a specific format into valid
DocBook. Just pass it the input filename on the commmand line and you'll
get a .sgml file out. It won't be a complete valid document, as it will
have no header information or dtd specification. It's just a DocBook
fragment, not a complete document.
The following constructs are currently supported. If you need support for
an addition construct, write discuss@linuxdoc.org if you're subscribed,
or feedback@linuxdoc.org if you're not.
Or just add it in the cvs. :-)
=Title= <sect1><title>Title</title>
<sect1>
==Title== <sect2><title>Title</title>
</sect2>
===Title=== <sect3><title>Title</title>
</sect3>
Foo <para>Foo</para>
#Foo <orderedlist>
#Bar <listitem><para>Foo</para></listitem>
#Baz <listitem><para>Bar</para></listitem>
<listitem><para>Baz</para></listitem>
</orderedlist>
*Foo <simplelist>
*Bar <listitem><para>Foo</para></listitem>
*Baz <listitem><para>Bar</para></listitem>
<listitem><para>Baz</para></listitem>
</simplelist>

32
LDP/txt2db/sample.txt Normal file
View File

@ -0,0 +1,32 @@
=Introduction=
Numbered List
#item
#item
#item
Another to make sure the numbers restart at one.
#item
#item
#item
Simple List
*item
*item
*item
=Bar=
Just another section.
==Level 2==
===Level 3===
=Level 1 Again=
All previous sections should be properly closed.

189
LDP/txt2db/txt2db.pl Executable file
View File

@ -0,0 +1,189 @@
#!/usr/bin/perl
#
#Converts txt files into docbook.
#
#Usage: txt2db <text file>
my($txtfile, $dbfile) = '';
my($level1,$level2,$level3,$orderedlist,$listitem,$para);
# read in cmd-line arguments
#
while (1) {
if($ARGV[0] eq "-o") {
$dbfile = $ARGV[0];
shift(@ARGV);
} elsif($ARGV[0] eq "-h") {
&usage;
} else {
$txtfile = $ARGV[0];
shift(@ARGV);
}
if ($ARGV[0] eq '') {
last;
}
}
# abort if no input file given
#
if($txtfile eq '') {
print "txt2db: ERROR text file not specified.\n\n";
&usage();
} elsif( !(-r $txtfile) ) {
print "txt2db: ERROR cannot read $f ($!)\n\n";
&usage();
}
if( $dbfile eq '') {
$txtfile =~ /([^\/]+)\.txt$/i;
$dbfile = $1 . ".sgml";
}
$buf = '';
&proc_txt($txtfile);
open(DB, "> $dbfile") || die "txt2db: cannot write to $dbfile ($!)\n";
print DB $buf, "\n";
close(DB);
print "txt2db: created $dbfile from $txtfile\n";
exit(0);
# -----------------------------------------------------------
sub usage {
print "Usage: txt2db {-o <sgml file>} <text file>\n";
exit(0);
}
sub proc_txt {
my($line);
my($f) = @_;
# read in the text file
#
open(TXT, "$f") || die "txt2db: cannot open $f ($!)\n";
while (<TXT>) {
$line = $_;
$line =~ s/\s+$//;
$line =~ s/^\s+//;
# blank lines
if ($line eq '') {
&closepara;
next;
}
if ($line =~ /^=\w/) {
&close1;
$level1 = 1;
$line =~ s/^=/<sect1><title>/;
$line =~ s/=$/<\/title>\n/;
} elsif ($line =~ /^==\w/) {
&close2;
$level2 = 1;
$line =~ s/^==/\n<sect2><title>/;
$line =~ s/==$/<\/title>\n/;
} elsif ($line =~ /^===\w/) {
&close3;
$level3 = 1;
$line =~ s/^===/\n<sect3><title>/;
$line =~ s/===$/<\/title>\n/;
} elsif ($line =~ /^#/) {
if ($orderedlist == 0) {
$orderedlist = 1;
$buf .= "\n<orderedlist>\n";
}
&closelistitem;
$listitem = 1;
$line =~ s/^#//;
$line =~ s/^/\n<listitem><para>/;
$para = 1;
} elsif ($line =~ /^\*/) {
if ($list == 0) {
$list = 1;
$buf .= "\n<simplelist>\n";
}
&closelistitem;
$listitem = 1;
$line =~ s/^\*//;
$line =~ s/^/\n<listitem><para>/;
$para = 1;
}
else {
&closeorderedlist;
if ( $para == 0 ) {
$para = 1;
$line =~ s/^/<para>/;
} else {
$line .= " ";
}
}
$buf .= $line;
}
# close nesting
#
&close1;
}
sub close1 {
&close2;
if ($level1 == 1) {
$buf .= "</sect1>\n";
$level1 = 0;
}
}
sub close2 {
&close3;
if ($level2 == 1) {
$buf .= "</sect2>\n";
$level2 = 0;
}
}
sub close3 {
&closeorderedlist;
&closelist;
&closepara;
if ($level3 == 1) {
$buf .= "</sect3>\n";
$level3 = 0;
}
}
sub closeorderedlist {
&closepara;
&closelistitem;
if ($orderedlist == 1 ) {
$buf .= "</orderedlist>\n";
$orderedlist = 0;
}
}
sub closelist {
&closepara;
&closelistitem;
if ($list == 1 ) {
$buf .= "</simplelist>\n";
$list = 0;
}
}
sub closelistitem {
&closepara;
if ($listitem == 1 ) {
$buf .= "</listitem>\n";
$listitem = 0;
}
}
sub closepara {
if ($para == 1) {
$buf .= "</para>\n";
$para = 0;
}
}