9 # TODO italic paragaphs
13 # print fictionbook header
14 print "<?xml version=\"1.0\" encoding=\"UTF-8\">\n";
15 print "<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\"
16 xmlns:l=\"http://www.w3.org/1999/xlink\">\n";
17 my $metadata = shift @ARGV;
28 if (/\\(begin|end){(\w+)}/) {
31 if ($environ eq 'verbatim') {
33 } elsif ($environ eq 'verse') {
35 pushsection("poem",undef);
37 add_to_section(tag($buffer,'stanza')."\n") if $buffer;
42 } elsif($environ eq 'document' && $begin) {
47 if ((/^$/ || $environ) && $buffer) {
48 #output on empty line (p or stanza) depending on poetry mode
49 add_to_section(tag($buffer,$poetry?"stanza":"p")."\n");
52 next LINE if $environ;
54 if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
56 add_to_section(tag($buffer,$poetry?"stanza":"p"));
63 add_to_section("<empty-line />");
66 next LINE if /\\pagebreak\b/;
75 s/\\sloppy([^\w])/$1/g;
80 #replace TeX ligatures ~ --- << >> \% with appropriate unicode symbols
88 #replace ' and " with entities
98 $buffer.=tag($_,'v')."\n";
100 add_to_section(tag(tag($_,"code"),"p"));
106 add_to_section(tag($buffer,"p"));
114 ## FIXME print footnotes
115 print "</FictionBook>";
119 return if ($#sections<0) ;
120 $sections[$#sections]->{data}.=$data;
124 my $tag= shift || 'section';
125 my $str = pop @sections;
128 $content = tag($str->{title},"title");
130 $content .= $str->{data};
131 if ($#sections >=0) {
132 add_to_section(tag($content,$tag)."\n");
134 print tag($content,$tag);
139 my ($level,$title)=@_;
140 # Find section of $level in the current stack
141 my $found=scalar(@sections);
143 for (my $i=0;$i<=$#sections;$i++) {
144 if ($sections[$i]->{level} eq $level) {
149 # if found, flush everything below
150 while (scalar(@sections) > $found) {
153 push @sections,{level=>$level,title=>$title,data=>""};
157 my ($content,$name) = @_;
158 return "" if $content =~ /^\s*$/s;
159 return "<$name>$content</$name>";