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);
40 } elsif($environ = 'document' && $begin) {
45 if ((/^$/ || $environ) && $buffer) {
46 #output on empty line (p or stanza) depending on poetry mode
47 add_to_section(tag($buffer,$poetry?"stanza":"p"));
50 next LINE if $environ;
52 if (/\\(part|chapter|section|subsection|subsubsection)\*?{(.*)}/) {
54 add_to_section(tag($buffer,$poetry?"stanza":"p"));
61 add_to_section("<empty-line />");
72 s/\\sloppy([^\w])/$1/g;
73 #replace TeX ligatures ~ --- << >> \% with appropriate unicode symbols
81 #replace ' and " with entities
92 add_to_section(tag(tag($_,"code"),"p"));
98 add_to_section(tag($buffer,"p"));
106 ## FIXME print footnotes
107 print "</FictionBook>";
111 return if ($#sections<0) ;
112 $sections[$#sections]->{data}.=$data;
116 my $tag= shift || 'section';
117 my $str = pop @sections;
120 $content = tag($str->{title},"title");
122 $content .= $str->{data};
123 if ($#sections >=0) {
124 add_to_section(tag($content,$tag));
126 print tag($content,$tag);
131 my ($level,$title)=@_;
132 # Find section of $level in the current stack
133 my $found=scalar(@sections);
135 for (my $i=0;$i<=$#sections;$i++) {
136 if ($sections[$i]->{level} eq $level) {
141 # if found, flush everything below
142 while (scalar(@sections) > $found) {
145 push @sections,{level=>$level,title=>$title,data=>""};
149 my ($content,$name) = @_;
150 return "" if ($content eq "\n");
151 return "<$name>$content</$name>";