]> www.wagner.pp.ru Git - fiction/Kate-the-Empress.git/blobdiff - Tex2fb2
Typo fixes
[fiction/Kate-the-Empress.git] / Tex2fb2
diff --git a/Tex2fb2 b/Tex2fb2
index 929e9576444bef247c3bd5b14ef17ab39410c098..abc38bba4320338ddd5275b28ab8f23b90609fff 100755 (executable)
--- a/Tex2fb2
+++ b/Tex2fb2
@@ -1,5 +1,8 @@
 #!/usr/bin/perl -CDS
+
 use utf8;
+use POSIX qw(strftime);
+use MIME::Base64;
 # char-level modes
 my $poetry = 0;
 my $verbatim = 0;
@@ -18,7 +21,32 @@ print "<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\"
 xmlns:l=\"http://www.w3.org/1999/xlink\">\n";
 my $metadata = shift @ARGV;
 open F,"<",$metadata;
+my $pics = "";
 while (<F>) {
+       # Replace empty date with current date
+       if (/<date\s+\/>/ || /<date>\s*<\/date>/) {
+               $_ = "<date value=\"".strftime("%Y-%m-%d",localtime())."\">".
+                       strftime("%d/%m/%Y",localtime())."</date>\n";
+       }
+       # Add current to date as fractional part to version
+       if (/<version>(\d*)(.\d*)?<\/version>/) {
+               my $ver = $1+time()/1E10;
+               $_=tag(sprintf("%g",$ver),"version")."\n";
+       }
+       # Если  существует cover.png, добавляем coverpage
+       if (/<coverpage \/>/) {
+               $_="";
+               COVER:
+               for $file ("cover.jpg","cover.png") {
+                       if (-f $file) { 
+                               my $id = $file;
+                               $id=~tr/./_/;
+                               $_ = "<coverpage>\n<image l:href=\"#$id\" />\n</coverpage>\n";
+                               $pics .= mkbinary($file,$id);
+                               last COVER;
+                       }
+               }       
+       }
        print $_;
 }
 close F;
@@ -53,7 +81,7 @@ if ((/^$/ || $environ) && $buffer) {
 }
 next LINE if $environ;
 # Section headings
-if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
+if (/\\(part|chapter|section|subsection|subsubsection)\*?\{(.*)\}/) {
        if ($buffer) {
                add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p"));
                $buffer="";
@@ -61,43 +89,47 @@ if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
        pushsection($1,tag($2,"p"));
        next LINE;
 }
-if (/\\vspace{/) {
+if (/\\vspace\{/) {
        add_to_section("<empty-line />");
        next LINE;
 }
 next LINE if /\\pagebreak\b/;
+#replace ' and " with entities
+s/&/&amp;/g;
+s/'/&apos;/g;
+s/"/&quot;/g;
+s/</&lt;/g;
+s/>/&gt;/g;
 #normal mode: 
 if (!$verbatim) {
 #strip TeX comments 
 s/([^\\])%.*$/$1/;
 s/^%.*$//;
 # strip \sloppy
+s/\\(\w+)\\sloppy/\\$1/g;
 s/\\sloppy\s+//g;
-s/\\sloppy{}//g;
+s/\\sloppy\{\}//g;
 s/\\sloppy([^\w])/$1/g;
 # strip extra space
 s/^\s+//;
 s/\s+$//;
 s/(\s)\s+/$1/g;
 #replace TeX ligatures ~ --- << >> \% with appropriate unicode symbols
-s/~/\xA0/g;
-s/---/-/g;
-s/<</«/g;
-s/>>/»/g;
-s/\\%/%/g;
-s/\\dots/\x{2026}/g;
+$_ = fix_ligatures($_);
 }
-#replace ' and " with entities
-s/&/&amp;/g;
-s/'/&apos;/g;
-s/"/&quot;/g;
-s/</&lt;/g;
-s/>/&gt;/g;
 
 if ($poetry) {
        chomp;
+       if ($poetic_buffer) {
+               $_ = $poetic_buffer." ".$_;
+               $poetic_buffer = undef;
+       }
+       if (/{[^}]+$/) {
+               $poetic_buffer=$_;
+               next LINE;
+       }
        s/\s*\\\\$//;
-  $buffer.=tag($_,'v')."\n";
+  $buffer.=tag(flushbuffer($_),'v')."\n";
 } elsif ($verbatim) {
        add_to_section(tag(tag($_,"code"),"p"));
 } else {
@@ -114,12 +146,16 @@ while (@sections) {
 }
 print "</body>\n";
 ## print footnotes
-print "<body id=\"notes\">\n$footnotes\n</body>" if $footnotes;
+print "<body>\n$footnotes\n</body>" if $footnotes;
+print $pics;
 print "</FictionBook>";
 
 sub add_to_section {
        my $data = shift;
        return if ($#sections<0) ;
+       if ($data =~ /^\s*<section>/ && $sections[$#sections]->{data} !~ /^\s*<section>/) {
+               $sections[$#sections]->{data} = tag($sections[$#sections]->{data},"section")."\n";
+       }       
        $sections[$#sections]->{data}.=$data;
 }
 
@@ -130,7 +166,11 @@ sub flushsection {
        if ($str->{title}) {
                $content = tag($str->{title},"title");
        }
-       $content .=  $str->{data};
+       if ($str->{data} =~ /^\s*$/s) {
+               $content .= "<p>\n</p>";
+       } else {
+               $content .=  $str->{data};
+       }
        if ($#sections >=0) {
                add_to_section(tag($content,$tag)."\n");
        } else {
@@ -153,20 +193,36 @@ sub pushsection {
        while (scalar(@sections) > $found) {
                flushsection;
        }
-       push @sections,{level=>$level,title=>$title,data=>""};
+       push @sections,{level=>$level,title=>fix_ligatures($title),data=>""};
 }
 
+sub fix_ligatures {
+       local $_=shift;
+       s/~/\xA0/g;
+       s/\\-/\xAD/g;
+       s/---/—/g;
+       s/<</«/g;
+       s/>>/»/g;
+       s/\\%/%/g;
+       s/\\dots/\x{2026}/g;
+       s/\\verb(.)(.*)\1/<code>$2<\/code>/;
+       return $_;
+}      
 sub tag {
        my ($content,$name) = @_;
-       return "" if $content =~ /^\s*$/s;
+       $content =~s/^\s+//s;
+       return "" unless $content;
        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/\\footnote\{(.*)\}/push_footnote($1)/e;
+       $_.="}" if (/^\s*\{.*?[^}]$/) ;
+       s/\{\\(em|it|bf)(?:\s+| \{\})([^{}]+)}/<emphasis>$2<\/emphasis>/g;
+       s/\{\\(tt)(?:\s+|\{\})([^{}]+)}/<code>$2<\/code>/g;
+       s/\\(emph|textit|textbf)\{([^{}]+)\}/<emphasis>$2<\/emphasis>/g;
        s/[{}]//g;
        return $_;
 }
@@ -174,6 +230,17 @@ sub flushbuffer {
 
 sub push_footnote {
        my $id = "note_".(++$idseq);
-       $footnotes.="<section id=\"$id\">".tag(shift,'p')."</section>\n";
+       $footnotes.="<section id=\"$id\">".tag(flushbuffer(shift),'p')."</section>\n";
        return "<a l:href=\"#$id\" type=\"note\">$idseq</a>";
 }
+
+sub mkbinary {
+       my ($filename,$id) = @_;
+       my $f;
+       open $f,"<",$filename;
+       binmode $f;
+       local $/;
+       my $data = encode_base64(<$f>);
+       return "<binary id=\"$id\" content-type=\"image/png\">$data</binary>\n";
+       close $f;
+}