X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=Tex2fb2;h=96279b391b0062e4b4afdb8b8e21ceddb3366ecd;hb=b1d6f57b35a64e7454d8552d6ab0fade4fd5713b;hp=693149fcdc401644ad80bda7cf08af16a61a47eb;hpb=90f2749e4d60b2700566294a2dc75370dcf356d2;p=fiction%2FKate-the-Empress.git
diff --git a/Tex2fb2 b/Tex2fb2
index 693149f..96279b3 100755
--- a/Tex2fb2
+++ b/Tex2fb2
@@ -1,26 +1,50 @@
#!/usr/bin/perl -CDS
use utf8;
+use POSIX qw(strftime);
+use MIME::Base64;
# char-level modes
my $poetry = 0;
my $verbatim = 0;
my @sections;
my $buffer;
+my $idseq = 0; # sequentual number of footnotes
+my $footnotes="";
#
# TODO italic paragaphs
# footnotes
# epigraphs
#
# print fictionbook header
-print "\n";
+print "\n";
print "\n";
my $metadata = shift @ARGV;
open F,"<",$metadata;
+my $pics = "";
while () {
+ # Replace empty date with current date
+ if (// || /\s*<\/date>/) {
+ $_ = "".
+ strftime("%d/%m/%Y",localtime())."\n";
+ }
+ # Add current to date as fractional part to version
+ if (/(\d*)(.\d*)?<\/version>/) {
+ my $ver = $1+time()/1E10;
+ $_=tag(sprintf("%g",$ver),"version")."\n";
+ }
+ # ÐÑли ÑÑÑеÑÑвÑÐµÑ cover.png, добавлÑем coverpage
+ if (//) {
+ $_="";
+ if (-f "cover.png") {
+ my $id = "cover_png";
+ $_ = "\n\n\n";
+ $pics .= mkbinary("cover.png",$id);
+ }
+ }
print $_;
}
close F;
-
+print "\n";
my $header =1;
LINE:
while (<>) {
@@ -46,17 +70,17 @@ if (/\\(begin|end){(\w+)}/) {
next LINE if $header;
if ((/^$/ || $environ) && $buffer) {
#output on empty line (p or stanza) depending on poetry mode
- add_to_section(tag($buffer,$poetry?"stanza":"p")."\n");
+ add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p")."\n");
$buffer="";
}
next LINE if $environ;
# Section headings
if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
if ($buffer) {
- add_to_section(tag($buffer,$poetry?"stanza":"p"));
+ add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p"));
$buffer="";
}
- pushsection($1,$2);
+ pushsection($1,tag($2,"p"));
next LINE;
}
if (/\\vspace{/) {
@@ -64,6 +88,12 @@ if (/\\vspace{/) {
next LINE;
}
next LINE if /\\pagebreak\b/;
+#replace ' and " with entities
+s/&/&/g;
+s/'/'/g;
+s/"/"/g;
+s/</g;
+s/>/>/g;
#normal mode:
if (!$verbatim) {
#strip TeX comments
@@ -79,21 +109,26 @@ s/\s+$//;
s/(\s)\s+/$1/g;
#replace TeX ligatures ~ --- << >> \% with appropriate unicode symbols
s/~/\xA0/g;
-s/---/-/g;
+s/\\-/\xAD/g;
+s/---/â/g;
s/<«/g;
s/>>/»/g;
s/\\%/%/g;
s/\\dots/\x{2026}/g;
+s/\\verb(.)(.*)\1/$2<\/code>/;
}
-#replace ' and " with entities
-s/&/&/g;
-s/'/'/g;
-s/"/"/g;
-s/</g;
-s/>/>/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) {
@@ -103,7 +138,7 @@ if ($poetry) {
}
}
if ($buffer) {
- add_to_section(tag($buffer,"p"));
+ add_to_section(tag(flushbuffer($buffer),"p"));
$buffer="";
}
@@ -111,12 +146,17 @@ while (@sections) {
flushsection();
}
print "\n";
-## FIXME print footnotes
+## print footnotes
+print "\n$footnotes\n" if $footnotes;
+print $pics;
print "
";
sub add_to_section {
my $data = shift;
return if ($#sections<0) ;
+ if ($data =~ /^\s*/ && $sections[$#sections]->{data} !~ /^\s*/) {
+ $sections[$#sections]->{data} = tag($sections[$#sections]->{data},"section")."\n";
+ }
$sections[$#sections]->{data}.=$data;
}
@@ -127,7 +167,11 @@ sub flushsection {
if ($str->{title}) {
$content = tag($str->{title},"title");
}
- $content .= $str->{data};
+ if ($str->{data} =~ /^\s*$/s) {
+ $content .= "\n
";
+ } else {
+ $content .= $str->{data};
+ }
if ($#sections >=0) {
add_to_section(tag($content,$tag)."\n");
} else {
@@ -155,6 +199,34 @@ sub pushsection {
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+|{})([^{}]+)}/$2<\/emphasis>/g;
+ s/\\(emph|textit|textbf){([^{}]+)}/$2<\/emphasis>/g;
+ s/\\footnote{(.*)}/push_footnote($1)/e;
+ s/[{}]//g;
+ return $_;
+}
+
+
+sub push_footnote {
+ my $id = "note_".(++$idseq);
+ $footnotes.="\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;
+}