]> www.wagner.pp.ru Git - fiction/Kate-the-Empress.git/blobdiff - Tex2fb2
More or less finished fb2 generation
[fiction/Kate-the-Empress.git] / Tex2fb2
diff --git a/Tex2fb2 b/Tex2fb2
index 693149fcdc401644ad80bda7cf08af16a61a47eb..929e9576444bef247c3bd5b14ef17ab39410c098 100755 (executable)
--- a/Tex2fb2
+++ b/Tex2fb2
@@ -5,13 +5,15 @@ my $poetry = 0;
 my $verbatim = 0;
 my @sections;
 my $buffer;
+my $idseq = 0; # sequentual number of footnotes
+my $footnotes="";
 #
 # TODO italic paragaphs
 # footnotes
 # epigraphs
 #
 # print fictionbook header
-print "<?xml version=\"1.0\" encoding=\"UTF-8\">\n";
+print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
 print "<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\"
 xmlns:l=\"http://www.w3.org/1999/xlink\">\n";
 my $metadata = shift @ARGV;
@@ -20,7 +22,7 @@ while (<F>) {
        print $_;
 }
 close F;
-
+print "<body>\n";
 my $header =1;
 LINE:
 while (<>) {
@@ -46,17 +48,17 @@ if (/\\(begin|end){(\w+)}/) {
 next LINE if $header;
 if ((/^$/ || $environ) && $buffer) {
 #output on empty line (p or stanza) depending on poetry mode
-       add_to_section(tag($buffer,$poetry?"stanza":"p")."\n");
+       add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p")."\n");
        $buffer="";
 }
 next LINE if $environ;
 # Section headings
 if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
        if ($buffer) {
-               add_to_section(tag($buffer,$poetry?"stanza":"p"));
+               add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p"));
                $buffer="";
        }
-       pushsection($1,$2);
+       pushsection($1,tag($2,"p"));
        next LINE;
 }
 if (/\\vspace{/) {
@@ -103,7 +105,7 @@ if ($poetry) {
 }
 }
 if ($buffer) {
-       add_to_section(tag($buffer,"p"));
+       add_to_section(tag(flushbuffer($buffer),"p"));
        $buffer="";
 }
 
@@ -111,7 +113,8 @@ while (@sections) {
        flushsection();
 }
 print "</body>\n";
-## FIXME print footnotes
+## print footnotes
+print "<body id=\"notes\">\n$footnotes\n</body>" if $footnotes;
 print "</FictionBook>";
 
 sub add_to_section {
@@ -158,3 +161,19 @@ sub tag {
        return "" if $content =~ /^\s*$/s;
        return "<$name>$content</$name>";
 }
+
+sub flushbuffer {
+       local $_ = shift;
+       s/{\\(em|it|bf)(?:\s+|{})([^{}]+)}/<emphasis>$2<\/emphasis>/g;
+       s/\\(emph|textit|textbf){([^{}]+)}/<emphasis>$2<\/emphasis>/g;
+       s/\\footnote{(.*)}/push_footnote($1)/e;
+       s/[{}]//g;
+       return $_;
+}
+
+
+sub push_footnote {
+       my $id = "note_".(++$idseq);
+       $footnotes.="<section id=\"$id\">".tag(shift,'p')."</section>\n";
+       return "<a l:href=\"#$id\" type=\"note\">$idseq</a>";
+}