-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjjwxc.pm
220 lines (180 loc) · 5.93 KB
/
jjwxc.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
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
211
212
213
214
215
216
217
218
219
220
# 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} = "jjwxc";
# The name of the site that this plugin is for
$self->{PLUGIN_DESC} = "www.jjwxc.net 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://www.jjwxc.net/onebook.php?novelid=".$self->{BOOK_ID};
# The url pattern to match for each chapter. Use groups to parse out additional data from the url.
$self->{RX_CHAPTER_URL} = "/onebook\\.php\\?novelid=".$self->{BOOK_ID}."&chapterid=(\\d+)\$";
$self->{RX_CHAPTER_URL_INDEX} = "/onebook\\.php\\?novelid=".$self->{BOOK_ID};
$self->{RX_CHAPTER_TITLE} = "\\x{2c7}(.*?)\\x{2c7}"; # ˇ = \x{2c7}
#$self->{RX_CHAPTER_TITLE} = "\\p{Close_Punctuation}.*? ˇ(.*?)ˇ"; #This was too aggressive. Finding the first one in ^s seems to work.
$self->{RX_BOOK_TITLE} = "\\p{Open_Punctuation}(.*?)\\p{Close_Punctuation}";
$self->{RX_BOOK_ID} = "(\\d+)";
$self->{USE_INDEX_TITLE_AS_BOOK_TITLE} = 1;
$self->{BOOK_TITLE} = "";
$self->{BOOK_DATA} = {};
return bless $self, $type;
}
sub isMatchingUrl {
my ($self, $url) = @_;
#use re 'debug';
my $rxChapterUrl = $self->{RX_CHAPTER_URL};
if ($url =~ m|$rxChapterUrl|) {
return 1;
}
print STDERR "URL $url does not match $rxChapterUrl\n" if ($self->{VERBOSE} > 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);
return 1;
}
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});
}
#use re 'debug';
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;
use HTML::TokeParser;
my $tree = HTML::TokeParser->new(\$content);
while (my $tag = $tree->get_tag("div")) {
if (($tag->[1]{class}) && ($tag->[1]{class} eq "noveltext")) {
while (my $contents = $tree->get_text("font", "/div", "div") ) {
$text = $text."$contents\n";
$tree->get_text("/font");
}
}
}
my $f = HTML::FormatText::WithLinks->new();
$text = $f->parse($text);
#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;
#use re 'debug';
print STDERR "TITLE is $title\n" if ($self->{VERBOSE} > 1);
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---------------------------------------------------------package myplugin;
sub plugin_sort ($$) {
# # Sort first alphabetically, then numerically
# my ($aa, $ab, $na, $nb);
# if ($_[0] =~ m/([a-z]+)/) { $aa = $1; }
# if ($_[1] =~ m/([a-z]+)/) { $ab = $1; }
# if ($_[0] =~ m/([0-9]+)/) { $na = $1; }
# if ($_[1] =~ m/([0-9]+)/) { $nb = $1; }
# #$aa = join('', grep(/[a-z]+/, $_[0]));
# #$ab = join('', grep(/[a-z]+/, $_[1]));
# #$na = join('', grep(/[0-9]+/, $_[0]));
# #$nb = join('', grep(/[0-9]+/, $_[1]));
# print "DEBUG: $aa $ab $na $nb\n";
#
# if ( uc($aa) eq uc($ab)) {
# print "DEBUG: alpha compared ".uc($aa)." with ".uc($ab)."\n";
# $na <=> $nb;
# } else {
# print "DEBUG: num compared ".uc($na)." with ".uc($nb)."\n";
# uc($aa) cmp uc($ab);
# }
my $x = uc( shift );
my $y = uc( shift );
if( !($x =~ /\d+(\.\d+)?/) ) {
return $x cmp $y;
}
my $xBefore = $`;
my $xMatch = $&;
my $xAfter = $';
if( !($y =~ /\d+(\.\d+)?/) ) {
return $x cmp $y;
}
if( $xBefore eq $` ) {
if( $xMatch == $& ) {
return naturalSortInner( $xAfter, $' );
} else {
return $xMatch <=> $&;
}
} else {
return $x cmp $y;
}
print "\n<before: '$xBefore', match: '$xMatch', after: '$xAfter'>\
+n";
}
# PLUGIN STUFF ENDS HERE---------------------------------------------------------