X-Git-Url: http://www.wagner.pp.ru/gitweb/?p=fiction%2FKate-the-Empress.git;a=blobdiff_plain;f=Tex2fb2;h=96279b391b0062e4b4afdb8b8e21ceddb3366ecd;hp=30988082287d8c4345292a34f511a10b60ca4e66;hb=HEAD;hpb=871e2a219f6f86d618577850a35f45d1ee4248d9 diff --git a/Tex2fb2 b/Tex2fb2 index 3098808..abc38bb 100755 --- a/Tex2fb2 +++ b/Tex2fb2 @@ -1,6 +1,8 @@ #!/usr/bin/perl -CDS + use utf8; use POSIX qw(strftime); +use MIME::Base64; # char-level modes my $poetry = 0; my $verbatim = 0; @@ -19,6 +21,7 @@ print "\n"; my $metadata = shift @ARGV; open F,"<",$metadata; +my $pics = ""; while () { # Replace empty date with current date if (// || /\s*<\/date>/) { @@ -30,6 +33,20 @@ while () { my $ver = $1+time()/1E10; $_=tag(sprintf("%g",$ver),"version")."\n"; } + # Если существует cover.png, добавляем coverpage + if (//) { + $_=""; + COVER: + for $file ("cover.jpg","cover.png") { + if (-f $file) { + my $id = $file; + $id=~tr/./_/; + $_ = "\n\n\n"; + $pics .= mkbinary($file,$id); + last COVER; + } + } + } print $_; } close F; @@ -64,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=""; @@ -72,7 +89,7 @@ if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) { pushsection($1,tag($2,"p")); next LINE; } -if (/\\vspace{/) { +if (/\\vspace\{/) { add_to_section(""); next LINE; } @@ -89,22 +106,16 @@ if (!$verbatim) { 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/\\-/\xAD/g; -s/---/—/g; -s/<>/»/g; -s/\\%/%/g; -s/\\dots/\x{2026}/g; -s/\\verb(.)(.*)\1/$2<\/code>/; +$_ = fix_ligatures($_); } if ($poetry) { @@ -117,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 { @@ -137,6 +147,7 @@ while (@sections) { print "\n"; ## print footnotes print "\n$footnotes\n" if $footnotes; +print $pics; print ""; sub add_to_section { @@ -182,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/\\dots/\x{2026}/g; + s/\\verb(.)(.*)\1/$2<\/code>/; + return $_; +} sub tag { my ($content,$name) = @_; $content =~s/^\s+//s; @@ -194,9 +217,12 @@ sub tag { sub flushbuffer { local $_ = shift; - s/{\\(em|it|bf)(?:\s+|{})([^{}]+)}/$2<\/emphasis>/g; - s/\\(emph|textit|textbf){([^{}]+)}/$2<\/emphasis>/g; - s/\\footnote{(.*)}/push_footnote($1)/e; + + s/\\footnote\{(.*)\}/push_footnote($1)/e; + $_.="}" if (/^\s*\{.*?[^}]$/) ; + s/\{\\(em|it|bf)(?:\s+| \{\})([^{}]+)}/$2<\/emphasis>/g; + s/\{\\(tt)(?:\s+|\{\})([^{}]+)}/$2<\/code>/g; + s/\\(emph|textit|textbf)\{([^{}]+)\}/$2<\/emphasis>/g; s/[{}]//g; return $_; } @@ -204,6 +230,17 @@ sub flushbuffer { sub push_footnote { my $id = "note_".(++$idseq); - $footnotes.="
".tag(shift,'p')."
\n"; + $footnotes.="
".tag(flushbuffer(shift),'p')."
\n"; return "$idseq"; } + +sub mkbinary { + my ($filename,$id) = @_; + my $f; + open $f,"<",$filename; + binmode $f; + local $/; + my $data = encode_base64(<$f>); + return "$data\n"; + close $f; +}