#!/usr/bin/perl -w
# Translate docbook to LaTeX.
# This handles only a subset of Docbook.
# Usage: d2latex < filename.sgml > filename.tex
# (C) 2001 David A. Wheeler.  Licensed under GNU GPL version 2.
# (C) 2003 Junichi Uekawa, modified to get things working.
# See the GPL license for more information.

sub fix_comment {
 my $value = shift;
 chomp($value);
 $value = $value . "\n";
 $value =~ s/([^\n]*)\n/% $1\n/migs;
 return $value;
}

# Read entire file into "$_".
$old_sep = $/;
undef $/;
$_ = <STDIN>;
$/ = $old_sep;


# Junichi Uekawa ; replace \ with \\ for tex
s/\\/\\\\/migs;
s/_/\\_/migs;
s/\$/\\\$/migs;
#s/\{/\\\{/migs;
#s/\}/\\\}/migs;
s/&gt;/\$>\$/migs;
s/&lt;/\$<\$/migs;
s/&amp;/\\and/migs;
# the above escapes need to be un-escaped inside "verbatim".

s/<chapter>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\chapter{$1}/migs;
s/<sect1>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\section{$1}/migs;
s/<sect2>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\subsection{$1}/migs;
s/<sect3>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\subsubsection{$1}/migs;
s/<sect4>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\paragraph{$1}/migs;
s/<sect5>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\subparagraph{$1}/migs;

s/<\/chapter>/\n/migs;
s/<\/sect[1-9]>/\n/migs;

s/<blockquote>/\\begin{quotation}/migs;
s/<\/blockquote>/\\end{quotation}/migs;

s/<screen>/\\begin{verbatim}\n/migs;
s/<\/screen>/\\end{verbatim}\n/migs;

# added by Junichi Uekawa
s/.*<book>\s*<title>\s*([^<]*)\s*<\/title>\s*/\\title{$1}\\begin{document}\\maketitle/migs;
s/<\/book>/\\end\{document\}/migs;

s/<command>/\{\\tt /migs;
s/<\/command>/\}/migs;
s/<address>/\{\\tt /migs;
s/<\/address>/\}/migs;
s/<filename>/\{\\tt /migs;
s/<\/filename>/\}/migs;
#end

s/<itemizedlist>/\\begin{enumerate}/migs;
s/<listitem>\s*/\\item\n/migs;
s/<\/listitem>//migs;
s/<\/itemizedlist>/\\end{enumerate}/migs;

s/<emphasis>([^<]*)\s*<\/emphasis>\s*/\\emph{$1}/migs;


s/<para>/\n/migs;
s/<\/para>/\n/migs;

s/<!--(.*?)-->/&fix_comment($1);/migse;

# Compress multiple blank lines into a single blank line:
s/\n\n\n+/\n\n/migs;

print <<EOF;;
\\documentclass[twocolumn,twoside,draft]{book}

%\\title{TITLE HERE}
\\author{Junichi Uekawa}
\\date{}
%\\maketitle
EOF



print;

print "\n";

