-
Notifications
You must be signed in to change notification settings - Fork 0
/
html2fb2.pl
210 lines (176 loc) · 6.51 KB
/
html2fb2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use strict;
use warnings;
use LWP::Simple;
use File::Basename;
use File::Path;
use Cwd;
use Cwd 'abs_path';
use HTML::TreeBuilder;
use XML::LibXML;
# > perl ./html2fb2.pl "http://www.kabbalah.info/eng/layout/set/trans_page/content/view/full/31834" ./
#################################################
# Patterns
#################################################
#################################################
# Global Variables , Definitions; General Verifications
#################################################
my ($baseInput, $baseOutputDir) = @ARGV;
# checking input
unless (defined $baseInput and defined $baseOutputDir) {
die "usage: $0 base_input base_output";
}
# retrieve pathes, in need of case convert to absolute
my($baseInputFileName, $baseInputPath) = fileparse($baseInput, qr/\.[^.]*/);
$baseOutputDir = (abs_path($baseOutputDir)).'/';
#print "$baseInputFileName, $baseInputPath, $baseOutputDir\n";
# my $header = '<?xml version="1.0" encoding="UTF-8"?>
# <!-- edited with XML Spy v4.4 U (http://www.xmlspy.com) by Dmitry Grobov (DDS) -->
# <FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:xlink="http://www.w3.org/1999/xlink">';
# my $tail = '</FictionBook>';
my %html2fb2 = ("p" => "p", #paragraph
"b" => "strong", # bold text
"strong" => "strong", # strong text
"em" => "emphasis", # emphasized text
"i" => "emphasis", # italic text
"sub" => "sub", # subscripted text
"sup" => "sup", # superscripted text
"code" => "code", # computer code text
"cite" => "cite", # citation
"del" => "strikethrough", # deleted text
"table" => "table", # table
"th" => "th", # table header
"tr" => "tr", # table row
"td" => "td", # table cell
"img" => "image", # image
);
#################################################
# Classes
#################################################
#################################################
# Functions
#################################################
sub getAndStore {
my ($inputBase, $outBase, $file) = @_;
my $outFile = $outBase.$file;
my($outName, $outPath) = fileparse($outFile, qr/\.[^.]*/);
# treating output directory
unless (-e $outPath and -d $outPath) {
die "\nFailed mkdir: $outPath ($!)" unless (mkpath $outPath);
}
# store class in the file
my $status = getstore(join("", $inputBase, $file), $outFile);
unless (is_success($status)) {
warn "$0: Error $status on $inputBase$file";
return undef;
}
return $outFile;
}
sub getElemContentR {
my ($elem) = @_;
if (not ref $elem or @{ $elem->content_array_ref() } <= 1) {
if (not ref $elem) {print "not ref:\n $elem\n"}
elsif (@{ $elem->content_array_ref() } <= 1) {print "content_array_ref <= 1\n";}
return $elem;
} else {
my @content = ();
foreach my $elemIn (@{ $elem->content_array_ref() }) {
push (@content, getElemContentR($elemIn));
}
return @content;
}
}
sub isHTMLParagraph {
my ($elem) = @_;
return undef unless (ref ($elem) and ref ($elem) =~ /HTML::Element/);
$elem->tag() eq "p" ? return 1 : return undef;
}
#################################################
# Main
#################################################
my $content = get($baseInput);
# my $content = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
# <body><title>test</title>
# <div id="body">
# <p>aleph & beyt & <em>gimel</em> </p>
# </div>
# </body></html>';
# my $content = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
# <head>
# <title>test</title>
# </head>
# <body>
# <div id="body">
# <p>aleph</p>
# </div>
# </body>
# </html>';
my $tree = HTML::TreeBuilder->new; # empty tree
$tree->parse($content);
$tree->eof();
$tree->elementify();
# print $content ."\n";
# print $tree->as_HTML() . "\n";
my $title = $tree->look_down('_tag', 'title');
print $title->as_text() . "\n";
print $title->as_HTML() . "\n";
my $body_text = $tree->look_down ('id', 'body');
my $dom = XML::LibXML::Document->createDocument( "1.0", "UTF-8" );
my $fb_elem = $dom->createElementNS("http://www.w3.org/1999/xlink", "FictionBook");
$fb_elem->setNamespace( "http://www.gribuser.ru/xml/fictionbook/2.0" , "xlink", 0 );
my $fb_elem_body = $fb_elem->createElement("body");
foreach my $elem (@{$body_text->content_array_ref()}) {
my $fb_cur_elem = $fb_elem->createElement("p");
foreach my $elemIn (getElemContentR($elem)) {
if (ref $elemIn) {
if (ref ($elemIn) !~ /HTML::Element/) {
my $elemInType = ref ($elemIn);
die "Incorrect element type: $elemInType (Must be HTML::Element)";
}
if (not exists $html2fb2{$elemIn->tag()}) { # append to current elem as a text
# $fb_cur_elem = $fb_elem->createElement($html2fb2{$elemIn->tag());
my $elem_text_content = ($elemIn->content_array_ref())[0];
if (ref elem_text_content) {
die "at this stage we expects no sub-elements";
}
$fb_cur_elem->appendText($elem_text_content);
} elsif (isHTMLParagraph $elemIn) { #push prev paragraph elem to the body elem; create new paragraph elem
$fb_elem_body->appendChild($fb_cur_elem) if ($fb_cur_elem->textContent); # ???
$fb_cur_elem = $elemIn;
} else { # create new sub-elem
$fb_cur_elem->push_content($elemIn);
}
} else {
}
}
}
# DEBUG Output
open (FH,,">out.txt");
foreach my $elem (@{ $body_text->content_array_ref() }) {
print FH "="x20 . "\n";
#if (@{ $elem->content_array_ref() } > 1) {
#foreach my $elemIn (@{ $elem->content_array_ref() }) {
foreach my $elemIn (getElemContentR($elem)) {
print FH "="x10 . "\n";
print ref $elemIn;
print "\n";
if (ref $elemIn) {
print FH "==ELEMENT==\n";
print FH $elemIn->as_HTML() . "\n" ;
} else {
print FH "==TEXT==\n";
print FH $elemIn . "\n";
}
}
#} else {
# print FH $elem->as_HTML() . "\n";
#}
}
close FH;
# open (FH,">out.html");
# print FH $tree->as_HTML();
# close FH;
$tree->delete(); # Now that we're done with it, we must destroy it.
exit;
#################################################
# The End
#################################################