-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkanunu.pm
156 lines (124 loc) · 4.17 KB
/
kanunu.pm
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
# PLUGIN STUFF GOES HERE---------------------------------------------------------
sub new {
my $type = shift;
my $self = { };
# Required variables when object is created
$self->{BOOK_ID} = shift;
my $incode = shift;
my $outcode = shift;
$self->{VERBOSE} = shift;
if ($incode) {
$self->{FROM_CODE} = $incode;
}else{
$self->{FROM_CODE} = "utf-8";
};
if ($outcode) {
$self->{TO_CODE} = $outcode
}else{
$self->{TO_CODE} = "utf-8";
};
# Plugin Variables
$self->{PLUGIN_NAME} = "kanunu";
# The name of the site that this plugin is for
$self->{PLUGIN_DESC} = "book.kanunu.com plugin";
# The url to use for the index page (The one that contains all links, or the starting page). %s is replaced with the bookId.
$self->{INDEX_URL} = "http://book.kanunu.org/files/chinese/".$self->{BOOK_ID}.".html";
# The url pattern to match for each chapter. Use groups to parse out additional data from the url.
$self->{RX_CHAPTER_URL} = "/files/chinese/".$self->{BOOK_ID}."/(\\d+).html";
$self->{RX_CHAPTER_URL_INDEX} = "/files/chinese/".$self->{BOOK_ID}.".html";
$self->{RX_CHAPTER_TITLE} = "^(.*?)_";
$self->{RX_BOOK_TITLE} = "<strong>(.*?)</strong>";
$self->{RX_BOOK_ID} = "(\\d+)/(\\d+)";
$self->{USE_INDEX_TITLE_AS_BOOK_TITLE} = 1;
$self->{BOOK_TITLE} = "";
$self->{BOOK_DATA} = {};
return bless $self, $type;
}
sub isMatchingUrl {
my ($self, $url) = @_;
my $rxChapterUrl = $self->{RX_CHAPTER_URL};
if ($url =~ m|$rxChapterUrl|) {
return 1;
}
return 0;
}
sub hasValidBookId {
my $self = shift;
my $rxBookId = $self->{RX_BOOK_ID};
if ($self->{BOOK_ID} =~ m|$rxBookId|) {
return 1;
} else {
return 0;
}
return 1;
}
sub parseIndex {
my ($self, $mech, $origUrl) = @_;
if (($self->{USE_INDEX_TITLE_AS_BOOK_TITLE})) {
my $title = $mech->title();
my $rxBookTitle = $self->{RX_BOOK_TITLE};
if ($title =~ m|$rxBookTitle|) {
if ($1) {
$title = $1;
}
print STDERR "Found book title: $title\n" if ($self->{VERBOSE});
$self->{BOOK_TITLE} = $title;
}
}
$self->parseDocument($mech, $origUrl);
}
sub parseDocument {
my ($self, $mech, $origUrl) = @_;
my $chapterNum;
my $chapterName;
my $content = $mech->content();
my $title = $mech->title();
my $url = $mech->uri();
my $tree = HTML::TreeBuilder::XPath->new();
$tree->parse_content($content);
my $rxChapterUrl = $self->{RX_CHAPTER_URL};
my $rxChapterUrlIndex = $self->{RX_CHAPTER_URL_INDEX};
if ($url =~ m|$rxChapterUrl|) {
print STDERR "Found chapter number: $1\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'number'} = $1;
} elsif ($url =~ m|$rxChapterUrlIndex|) {
print STDERR "Found chapter number: 1\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'number'} = 1;
} else {
print STDERR "Error: Could not parse chapter number from $url using $rxChapterUrl" if ($self->{VERBOSE});
}
my $rxChapterTitle = $self->{RX_CHAPTER_TITLE};
if (($self->{BOOK_DATA}{$url}{'linkText'}) && ($self->{BOOK_DATA}{$url}{'linkText'} =~ m|$rxChapterTitle|)) {
print STDERR "Found chapter number in linkText: $1\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'number'} = $1;
}
my $text;
my $f = HTML::FormatText::WithLinks->new();
foreach my $node ($tree->findnodes_as_string("//tr/td[\@bgcolor='#FFFFFF']/p")) {
#Strip out H2 tags (they contain chapters, which we already have)
#$text =~ s|<h2>.*?</h2>||g;
$text = $text.$f->parse($node);
#$text = $text.$node;
}
#TODO Using this method, the entire site must use the same encoding. Any way for different pages to have different encodings? Can get current page's encoding from HTTP header?
$text = $self->convert_encoding($text);
$self->{BOOK_DATA}{$url}{'content'} = $text;
if ($title =~ m|$rxChapterTitle|) {
if ($1) {
$title = $1;
}
print STDERR "Found chapter title: $title\n" if ($self->{VERBOSE});
$self->{BOOK_DATA}{$url}{'title'} = $title;
}
# return {
# 'content' => $content,
# 'chapterName' => $chapterName,
# 'chapterNum' => $chapterNum,
# }
return 1;
}
sub print_chapter {
my ($self, $bookData) = @_;
return $bookData->{'title'} . " (" . $bookData->{'number'} . ")\n\n" . $bookData->{'content'} . "\n";
}
# PLUGIN STUFF ENDS HERE---------------------------------------------------------