]> 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 12f50da94cbca1d0b11758a5f6f08b85455a621d..96279b391b0062e4b4afdb8b8e21ceddb3366ecd 100755 (executable)
--- a/Tex2fb2
+++ b/Tex2fb2
@@ -1,5 +1,7 @@
 #!/usr/bin/perl -CDS
 use utf8;
+use POSIX qw(strftime);
+use MIME::Base64;
 # char-level modes
 my $poetry = 0;
 my $verbatim = 0;
@@ -18,7 +20,27 @@ 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 \/>/) {
+               $_="";
+               if (-f "cover.png") { 
+                       my $id = "cover_png";
+                       $_ = "<coverpage>\n<image l:href=\"#$id\" />\n</coverpage>\n";
+                       $pics .= mkbinary("cover.png",$id);
+               }
+       }
        print $_;
 }
 close F;
@@ -66,6 +88,12 @@ if (/\\vspace{/) {
        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 
@@ -81,21 +109,26 @@ s/\s+$//;
 s/(\s)\s+/$1/g;
 #replace TeX ligatures ~ --- << >> \% with appropriate unicode symbols
 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>/;
 }
-#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/\\footnote{(.*)}/push_footnote($1)/e;
        s/\s*\\\\$//;
   $buffer.=tag($_,'v')."\n";
 } elsif ($verbatim) {
@@ -115,6 +148,7 @@ while (@sections) {
 print "</body>\n";
 ## print footnotes
 print "<body>\n$footnotes\n</body>" if $footnotes;
+print $pics;
 print "</FictionBook>";
 
 sub add_to_section {
@@ -185,3 +219,14 @@ sub push_footnote {
        $footnotes.="<section id=\"$id\">".tag(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;
+}