]> www.wagner.pp.ru Git - fiction/Kate-the-Empress.git/commitdiff
Sync Tex2fb2 with spacians version
authorVictor Wagner <vitus@wagner.pp.ru>
Mon, 17 Jul 2017 18:27:48 +0000 (21:27 +0300)
committerVictor Wagner <vitus@wagner.pp.ru>
Mon, 17 Jul 2017 18:27:48 +0000 (21:27 +0300)
Tex2fb2

diff --git a/Tex2fb2 b/Tex2fb2
index 9ed9dd93299d5c906fbac8ffe03bf9dbf4103947..abc38bba4320338ddd5275b28ab8f23b90609fff 100755 (executable)
--- a/Tex2fb2
+++ b/Tex2fb2
@@ -1,4 +1,5 @@
 #!/usr/bin/perl -CDS
+
 use utf8;
 use POSIX qw(strftime);
 use MIME::Base64;
@@ -35,11 +36,16 @@ while (<F>) {
        # Если  существует 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);
-               }
+               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 $_;
 }
@@ -100,6 +106,7 @@ if (!$verbatim) {
 s/([^\\])%.*$/$1/;
 s/^%.*$//;
 # strip \sloppy
+s/\\(\w+)\\sloppy/\\$1/g;
 s/\\sloppy\s+//g;
 s/\\sloppy\{\}//g;
 s/\\sloppy([^\w])/$1/g;
@@ -108,14 +115,7 @@ s/^\s+//;
 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>/;
+$_ = fix_ligatures($_);
 }
 
 if ($poetry) {
@@ -128,9 +128,8 @@ if ($poetry) {
                $poetic_buffer=$_;
                next LINE;
        }
-       s/\\footnote\{(.*)\}/push_footnote($1)/e;
        s/\s*\\\\$//;
-  $buffer.=tag($_,'v')."\n";
+  $buffer.=tag(flushbuffer($_),'v')."\n";
 } elsif ($verbatim) {
        add_to_section(tag(tag($_,"code"),"p"));
 } else {
@@ -194,9 +193,21 @@ 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) = @_;
        $content =~s/^\s+//s;
@@ -206,9 +217,12 @@ sub tag {
 
 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;
+       $_.="}" 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 $_;
 }
@@ -216,7 +230,7 @@ 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>";
 }