8 my $idseq = 0; # sequentual number of footnotes
11 # TODO italic paragaphs
15 # print fictionbook header
16 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
17 print "<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\"
18 xmlns:l=\"http://www.w3.org/1999/xlink\">\n";
19 my $metadata = shift @ARGV;
30 if (/\\(begin|end){(\w+)}/) {
33 if ($environ eq 'verbatim') {
35 } elsif ($environ eq 'verse') {
37 pushsection("poem",undef);
39 add_to_section(tag($buffer,'stanza')."\n") if $buffer;
44 } elsif($environ eq 'document' && $begin) {
49 if ((/^$/ || $environ) && $buffer) {
50 #output on empty line (p or stanza) depending on poetry mode
51 add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p")."\n");
54 next LINE if $environ;
56 if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
58 add_to_section(tag(flushbuffer($buffer),$poetry?"stanza":"p"));
61 pushsection($1,tag($2,"p"));
65 add_to_section("<empty-line />");
68 next LINE if /\\pagebreak\b/;
77 s/\\sloppy([^\w])/$1/g;
82 #replace TeX ligatures ~ --- << >> \% with appropriate unicode symbols
90 #replace ' and " with entities
100 $buffer.=tag($_,'v')."\n";
101 } elsif ($verbatim) {
102 add_to_section(tag(tag($_,"code"),"p"));
108 add_to_section(tag(flushbuffer($buffer),"p"));
117 print "<body id=\"notes\">\n$footnotes\n</body>" if $footnotes;
118 print "</FictionBook>";
122 return if ($#sections<0) ;
123 $sections[$#sections]->{data}.=$data;
127 my $tag= shift || 'section';
128 my $str = pop @sections;
131 $content = tag($str->{title},"title");
133 $content .= $str->{data};
134 if ($#sections >=0) {
135 add_to_section(tag($content,$tag)."\n");
137 print tag($content,$tag);
142 my ($level,$title)=@_;
143 # Find section of $level in the current stack
144 my $found=scalar(@sections);
146 for (my $i=0;$i<=$#sections;$i++) {
147 if ($sections[$i]->{level} eq $level) {
152 # if found, flush everything below
153 while (scalar(@sections) > $found) {
156 push @sections,{level=>$level,title=>$title,data=>""};
160 my ($content,$name) = @_;
161 return "" if $content =~ /^\s*$/s;
162 return "<$name>$content</$name>";
167 s/{\\(em|it|bf)(?:\s+|{})([^{}]+)}/<emphasis>$2<\/emphasis>/g;
168 s/\\(emph|textit|textbf){([^{}]+)}/<emphasis>$2<\/emphasis>/g;
169 s/\\footnote{(.*)}/push_footnote($1)/e;
176 my $id = "note_".(++$idseq);
177 $footnotes.="<section id=\"$id\">".tag(shift,'p')."</section>\n";
178 return "<a l:href=\"#$id\" type=\"note\">$idseq</a>";