]> www.wagner.pp.ru Git - fiction/Kate-the-Empress.git/blobdiff - Tex2fb2
update version and date in the document-info on each compile
[fiction/Kate-the-Empress.git] / Tex2fb2
diff --git a/Tex2fb2 b/Tex2fb2
index 929e9576444bef247c3bd5b14ef17ab39410c098..22d83434cdcc887bf7382299656a993c4ac007ee 100755 (executable)
--- a/Tex2fb2
+++ b/Tex2fb2
@@ -1,5 +1,6 @@
 #!/usr/bin/perl -CDS
 use utf8;
+use POSIX qw(strftime);
 # char-level modes
 my $poetry = 0;
 my $verbatim = 0;
@@ -19,6 +20,16 @@ xmlns:l=\"http://www.w3.org/1999/xlink\">\n";
 my $metadata = shift @ARGV;
 open F,"<",$metadata;
 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";
+       }
        print $_;
 }
 close F;
@@ -81,7 +92,7 @@ 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/\\%/%/g;
@@ -114,12 +125,15 @@ 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 "</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 +144,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 {
@@ -158,7 +176,8 @@ sub pushsection {
 
 sub tag {
        my ($content,$name) = @_;
-       return "" if $content =~ /^\s*$/s;
+       $content =~s/^\s+//s;
+       return "" unless $content;
        return "<$name>$content</$name>";
 }