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>\n$footnotes\n</body>" if $footnotes;
118 print "</FictionBook>";
122 return if ($#sections<0) ;
123 if ($data =~ /^\s*<section>/ && $sections[$#sections]->{data} !~ /^\s*<section>/) {
124 $sections[$#sections]->{data} = tag($sections[$#sections]->{data},"section")."\n";
126 $sections[$#sections]->{data}.=$data;
130 my $tag= shift || 'section';
131 my $str = pop @sections;
134 $content = tag($str->{title},"title");
136 if ($str->{data} =~ /^\s*$/s) {
137 $content .= "<p>\n</p>";
139 $content .= $str->{data};
141 if ($#sections >=0) {
142 add_to_section(tag($content,$tag)."\n");
144 print tag($content,$tag);
149 my ($level,$title)=@_;
150 # Find section of $level in the current stack
151 my $found=scalar(@sections);
153 for (my $i=0;$i<=$#sections;$i++) {
154 if ($sections[$i]->{level} eq $level) {
159 # if found, flush everything below
160 while (scalar(@sections) > $found) {
163 push @sections,{level=>$level,title=>$title,data=>""};
167 my ($content,$name) = @_;
168 $content =~s/^\s+//s;
169 return "" unless $content;
170 return "<$name>$content</$name>";
175 s/{\\(em|it|bf)(?:\s+|{})([^{}]+)}/<emphasis>$2<\/emphasis>/g;
176 s/\\(emph|textit|textbf){([^{}]+)}/<emphasis>$2<\/emphasis>/g;
177 s/\\footnote{(.*)}/push_footnote($1)/e;
184 my $id = "note_".(++$idseq);
185 $footnotes.="<section id=\"$id\">".tag(shift,'p')."</section>\n";
186 return "<a l:href=\"#$id\" type=\"note\">$idseq</a>";