Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
68686a37 | 3 | import java.awt.image.BufferedImage; |
68e370a4 | 4 | import java.io.BufferedReader; |
08fe2e33 NR |
5 | import java.io.ByteArrayInputStream; |
6 | import java.io.File; | |
7 | import java.io.IOException; | |
8 | import java.io.InputStream; | |
68e370a4 | 9 | import java.io.InputStreamReader; |
08fe2e33 NR |
10 | import java.net.MalformedURLException; |
11 | import java.net.URL; | |
08fe2e33 | 12 | import java.util.ArrayList; |
793f1071 | 13 | import java.util.Date; |
08fe2e33 NR |
14 | import java.util.HashMap; |
15 | import java.util.List; | |
16 | import java.util.Map; | |
17 | import java.util.Map.Entry; | |
18 | import java.util.Scanner; | |
19 | ||
20 | import be.nikiroo.fanfix.Instance; | |
21 | import be.nikiroo.fanfix.bundles.Config; | |
22 | import be.nikiroo.fanfix.bundles.StringId; | |
23 | import be.nikiroo.fanfix.data.Chapter; | |
24 | import be.nikiroo.fanfix.data.MetaData; | |
25 | import be.nikiroo.fanfix.data.Paragraph; | |
08fe2e33 | 26 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
9252c65e | 27 | import be.nikiroo.fanfix.data.Story; |
595dfa7a | 28 | import be.nikiroo.utils.IOUtils; |
3b2b638f | 29 | import be.nikiroo.utils.Progress; |
08fe2e33 NR |
30 | import be.nikiroo.utils.StringUtils; |
31 | ||
32 | /** | |
33 | * This class is the base class used by the other support classes. It can be | |
34 | * used outside of this package, and have static method that you can use to get | |
35 | * access to the correct support class. | |
36 | * <p> | |
37 | * It will be used with 'resources' (usually web pages or files). | |
38 | * | |
39 | * @author niki | |
40 | */ | |
41 | public abstract class BasicSupport { | |
42 | /** | |
43 | * The supported input types for which we can get a {@link BasicSupport} | |
44 | * object. | |
45 | * | |
46 | * @author niki | |
47 | */ | |
48 | public enum SupportType { | |
49 | /** EPUB files created with this program */ | |
50 | EPUB, | |
51 | /** Pure text file with some rules */ | |
52 | TEXT, | |
53 | /** TEXT but with associated .info file */ | |
54 | INFO_TEXT, | |
55 | /** My Little Pony fanfictions */ | |
56 | FIMFICTION, | |
57 | /** Fanfictions from a lot of different universes */ | |
58 | FANFICTION, | |
59 | /** Website with lots of Mangas */ | |
60 | MANGAFOX, | |
61 | /** Furry website with comics support */ | |
62 | E621, | |
a4143cd7 NR |
63 | /** Furry website with stories */ |
64 | YIFFSTAR, | |
f0608ab1 NR |
65 | /** Comics and images groups, mostly but not only NSFW */ |
66 | E_HENTAI, | |
08fe2e33 | 67 | /** CBZ files */ |
373da363 NR |
68 | CBZ, |
69 | /** HTML files */ | |
70 | HTML; | |
08fe2e33 NR |
71 | |
72 | /** | |
73 | * A description of this support type (more information than the | |
74 | * {@link BasicSupport#getSourceName()}). | |
75 | * | |
76 | * @return the description | |
77 | */ | |
78 | public String getDesc() { | |
79 | String desc = Instance.getTrans().getStringX(StringId.INPUT_DESC, | |
80 | this.name()); | |
81 | ||
82 | if (desc == null) { | |
83 | desc = Instance.getTrans().getString(StringId.INPUT_DESC, this); | |
84 | } | |
85 | ||
86 | return desc; | |
87 | } | |
88 | ||
89 | /** | |
90 | * The name of this support type (a short version). | |
91 | * | |
92 | * @return the name | |
93 | */ | |
94 | public String getSourceName() { | |
95 | BasicSupport support = BasicSupport.getSupport(this); | |
96 | if (support != null) { | |
97 | return support.getSourceName(); | |
98 | } | |
99 | ||
100 | return null; | |
101 | } | |
102 | ||
103 | @Override | |
104 | public String toString() { | |
105 | return super.toString().toLowerCase(); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Call {@link SupportType#valueOf(String.toUpperCase())}. | |
110 | * | |
111 | * @param typeName | |
112 | * the possible type name | |
113 | * | |
114 | * @return NULL or the type | |
115 | */ | |
116 | public static SupportType valueOfUC(String typeName) { | |
117 | return SupportType.valueOf(typeName == null ? null : typeName | |
118 | .toUpperCase()); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Call {@link SupportType#valueOf(String.toUpperCase())} but return | |
123 | * NULL for NULL instead of raising exception. | |
124 | * | |
125 | * @param typeName | |
126 | * the possible type name | |
127 | * | |
128 | * @return NULL or the type | |
129 | */ | |
130 | public static SupportType valueOfNullOkUC(String typeName) { | |
131 | if (typeName == null) { | |
132 | return null; | |
133 | } | |
134 | ||
135 | return SupportType.valueOfUC(typeName); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Call {@link SupportType#valueOf(String.toUpperCase())} but return | |
140 | * NULL in case of error instead of raising an exception. | |
141 | * | |
142 | * @param typeName | |
143 | * the possible type name | |
144 | * | |
145 | * @return NULL or the type | |
146 | */ | |
147 | public static SupportType valueOfAllOkUC(String typeName) { | |
148 | try { | |
149 | return SupportType.valueOfUC(typeName); | |
150 | } catch (Exception e) { | |
151 | return null; | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
08fe2e33 NR |
156 | private InputStream in; |
157 | private SupportType type; | |
22848428 | 158 | private URL currentReferer; // with only one 'r', as in 'HTTP'... |
08fe2e33 NR |
159 | |
160 | // quote chars | |
e8eeea0a | 161 | private char openQuote = Instance.getTrans().getCharacter( |
08fe2e33 | 162 | StringId.OPEN_SINGLE_QUOTE); |
e8eeea0a | 163 | private char closeQuote = Instance.getTrans().getCharacter( |
08fe2e33 | 164 | StringId.CLOSE_SINGLE_QUOTE); |
e8eeea0a | 165 | private char openDoubleQuote = Instance.getTrans().getCharacter( |
08fe2e33 | 166 | StringId.OPEN_DOUBLE_QUOTE); |
e8eeea0a | 167 | private char closeDoubleQuote = Instance.getTrans().getCharacter( |
08fe2e33 NR |
168 | StringId.CLOSE_DOUBLE_QUOTE); |
169 | ||
170 | /** | |
171 | * The name of this support class. | |
172 | * | |
173 | * @return the name | |
174 | */ | |
175 | protected abstract String getSourceName(); | |
176 | ||
177 | /** | |
178 | * Check if the given resource is supported by this {@link BasicSupport}. | |
179 | * | |
180 | * @param url | |
181 | * the resource to check for | |
182 | * | |
183 | * @return TRUE if it is | |
184 | */ | |
185 | protected abstract boolean supports(URL url); | |
186 | ||
187 | /** | |
188 | * Return TRUE if the support will return HTML encoded content values for | |
189 | * the chapters content. | |
190 | * | |
191 | * @return TRUE for HTML | |
192 | */ | |
193 | protected abstract boolean isHtml(); | |
194 | ||
68686a37 | 195 | protected abstract MetaData getMeta(URL source, InputStream in) |
08fe2e33 NR |
196 | throws IOException; |
197 | ||
198 | /** | |
199 | * Return the story description. | |
200 | * | |
201 | * @param source | |
202 | * the source of the story | |
203 | * @param in | |
204 | * the input (the main resource) | |
205 | * | |
206 | * @return the description | |
207 | * | |
208 | * @throws IOException | |
209 | * in case of I/O error | |
210 | */ | |
211 | protected abstract String getDesc(URL source, InputStream in) | |
212 | throws IOException; | |
213 | ||
08fe2e33 NR |
214 | /** |
215 | * Return the list of chapters (name and resource). | |
216 | * | |
217 | * @param source | |
218 | * the source of the story | |
219 | * @param in | |
220 | * the input (the main resource) | |
ed08c171 NR |
221 | * @param pg |
222 | * the optional progress reporter | |
08fe2e33 NR |
223 | * |
224 | * @return the chapters | |
225 | * | |
226 | * @throws IOException | |
227 | * in case of I/O error | |
228 | */ | |
229 | protected abstract List<Entry<String, URL>> getChapters(URL source, | |
ed08c171 | 230 | InputStream in, Progress pg) throws IOException; |
08fe2e33 NR |
231 | |
232 | /** | |
233 | * Return the content of the chapter (possibly HTML encoded, if | |
234 | * {@link BasicSupport#isHtml()} is TRUE). | |
235 | * | |
236 | * @param source | |
237 | * the source of the story | |
238 | * @param in | |
239 | * the input (the main resource) | |
240 | * @param number | |
241 | * the chapter number | |
ed08c171 NR |
242 | * @param pg |
243 | * the optional progress reporter | |
08fe2e33 NR |
244 | * |
245 | * @return the content | |
246 | * | |
247 | * @throws IOException | |
248 | * in case of I/O error | |
249 | */ | |
250 | protected abstract String getChapterContent(URL source, InputStream in, | |
ed08c171 | 251 | int number, Progress pg) throws IOException; |
08fe2e33 | 252 | |
6e06d2cc NR |
253 | /** |
254 | * Log into the support (can be a no-op depending upon the support). | |
255 | * | |
256 | * @throws IOException | |
257 | * in case of I/O error | |
258 | */ | |
259 | public void login() throws IOException { | |
260 | ||
261 | } | |
262 | ||
08fe2e33 NR |
263 | /** |
264 | * Return the list of cookies (values included) that must be used to | |
265 | * correctly fetch the resources. | |
266 | * <p> | |
267 | * You are expected to call the super method implementation if you override | |
268 | * it. | |
269 | * | |
270 | * @return the cookies | |
6e06d2cc NR |
271 | * |
272 | * @throws IOException | |
273 | * in case of I/O error | |
08fe2e33 | 274 | */ |
6e06d2cc | 275 | public Map<String, String> getCookies() throws IOException { |
08fe2e33 NR |
276 | return new HashMap<String, String>(); |
277 | } | |
278 | ||
a4143cd7 NR |
279 | /** |
280 | * Return the canonical form of the main {@link URL}. | |
281 | * | |
282 | * @param source | |
283 | * the source {@link URL} | |
284 | * | |
285 | * @return the canonical form of this {@link URL} | |
286 | * | |
287 | * @throws IOException | |
288 | * in case of I/O error | |
289 | */ | |
290 | public URL getCanonicalUrl(URL source) throws IOException { | |
291 | return source; | |
292 | } | |
293 | ||
08fe2e33 NR |
294 | /** |
295 | * Process the given story resource into a partially filled {@link Story} | |
296 | * object containing the name and metadata, except for the description. | |
297 | * | |
298 | * @param url | |
299 | * the story resource | |
300 | * | |
301 | * @return the {@link Story} | |
302 | * | |
303 | * @throws IOException | |
304 | * in case of I/O error | |
305 | */ | |
306 | public Story processMeta(URL url) throws IOException { | |
ed08c171 | 307 | return processMeta(url, true, false, null); |
08fe2e33 NR |
308 | } |
309 | ||
310 | /** | |
311 | * Process the given story resource into a partially filled {@link Story} | |
312 | * object containing the name and metadata. | |
313 | * | |
314 | * @param url | |
315 | * the story resource | |
316 | * | |
317 | * @param close | |
318 | * close "this" and "in" when done | |
ed08c171 NR |
319 | * @param pg |
320 | * the optional progress reporter | |
08fe2e33 NR |
321 | * |
322 | * @return the {@link Story} | |
323 | * | |
324 | * @throws IOException | |
325 | * in case of I/O error | |
326 | */ | |
ed08c171 NR |
327 | protected Story processMeta(URL url, boolean close, boolean getDesc, |
328 | Progress pg) throws IOException { | |
329 | if (pg == null) { | |
330 | pg = new Progress(); | |
331 | } else { | |
332 | pg.setMinMax(0, 100); | |
333 | } | |
334 | ||
6e06d2cc | 335 | login(); |
ed08c171 | 336 | pg.setProgress(10); |
6e06d2cc | 337 | |
a4143cd7 NR |
338 | url = getCanonicalUrl(url); |
339 | ||
340 | setCurrentReferer(url); | |
341 | ||
373da363 | 342 | in = openInput(url); |
08fe2e33 NR |
343 | if (in == null) { |
344 | return null; | |
345 | } | |
346 | ||
347 | try { | |
68686a37 | 348 | preprocess(url, getInput()); |
ed08c171 | 349 | pg.setProgress(30); |
08fe2e33 NR |
350 | |
351 | Story story = new Story(); | |
68686a37 | 352 | MetaData meta = getMeta(url, getInput()); |
793f1071 NR |
353 | if (meta.getCreationDate() == null |
354 | || meta.getCreationDate().isEmpty()) { | |
355 | meta.setCreationDate(StringUtils.fromTime(new Date().getTime())); | |
356 | } | |
68686a37 NR |
357 | story.setMeta(meta); |
358 | ||
ed08c171 NR |
359 | pg.setProgress(50); |
360 | ||
68686a37 NR |
361 | if (meta != null && meta.getCover() == null) { |
362 | meta.setCover(getDefaultCover(meta.getSubject())); | |
363 | } | |
08fe2e33 | 364 | |
ed08c171 NR |
365 | pg.setProgress(60); |
366 | ||
08fe2e33 NR |
367 | if (getDesc) { |
368 | String descChapterName = Instance.getTrans().getString( | |
369 | StringId.DESCRIPTION); | |
370 | story.getMeta().setResume( | |
371 | makeChapter(url, 0, descChapterName, | |
ed08c171 | 372 | getDesc(url, getInput()), null)); |
08fe2e33 NR |
373 | } |
374 | ||
ed08c171 | 375 | pg.setProgress(100); |
08fe2e33 NR |
376 | return story; |
377 | } finally { | |
378 | if (close) { | |
379 | try { | |
380 | close(); | |
381 | } catch (IOException e) { | |
382 | Instance.syserr(e); | |
383 | } | |
384 | ||
385 | if (in != null) { | |
386 | in.close(); | |
387 | } | |
388 | } | |
a4143cd7 NR |
389 | |
390 | setCurrentReferer(null); | |
08fe2e33 NR |
391 | } |
392 | } | |
393 | ||
394 | /** | |
395 | * Process the given story resource into a fully filled {@link Story} | |
396 | * object. | |
397 | * | |
398 | * @param url | |
399 | * the story resource | |
92fb0719 NR |
400 | * @param pg |
401 | * the optional progress reporter | |
08fe2e33 NR |
402 | * |
403 | * @return the {@link Story} | |
404 | * | |
405 | * @throws IOException | |
406 | * in case of I/O error | |
407 | */ | |
92fb0719 NR |
408 | public Story process(URL url, Progress pg) throws IOException { |
409 | if (pg == null) { | |
410 | pg = new Progress(); | |
411 | } else { | |
412 | pg.setMinMax(0, 100); | |
413 | } | |
414 | ||
a4143cd7 | 415 | url = getCanonicalUrl(url); |
92fb0719 | 416 | pg.setProgress(1); |
08fe2e33 | 417 | try { |
ed08c171 NR |
418 | Progress pgMeta = new Progress(); |
419 | pg.addProgress(pgMeta, 10); | |
420 | Story story = processMeta(url, false, true, pgMeta); | |
421 | if (!pgMeta.isDone()) { | |
422 | pgMeta.setProgress(pgMeta.getMax()); // 10% | |
423 | } | |
424 | ||
08fe2e33 | 425 | if (story == null) { |
ed08c171 | 426 | pg.setProgress(90); |
08fe2e33 NR |
427 | return null; |
428 | } | |
429 | ||
754a5bc2 NR |
430 | pg.setName("Retrieving " + story.getMeta().getTitle()); |
431 | ||
a4143cd7 NR |
432 | setCurrentReferer(url); |
433 | ||
ed08c171 NR |
434 | Progress pgGetChapters = new Progress(); |
435 | pg.addProgress(pgGetChapters, 10); | |
08fe2e33 | 436 | story.setChapters(new ArrayList<Chapter>()); |
ed08c171 NR |
437 | List<Entry<String, URL>> chapters = getChapters(url, getInput(), |
438 | pgGetChapters); | |
439 | if (!pgGetChapters.isDone()) { | |
440 | pgGetChapters.setProgress(pgGetChapters.getMax()); // 20% | |
441 | } | |
08fe2e33 | 442 | |
08fe2e33 | 443 | if (chapters != null) { |
ed08c171 NR |
444 | Progress pgChaps = new Progress("Extracting chapters", 0, |
445 | chapters.size() * 300); | |
92fb0719 NR |
446 | pg.addProgress(pgChaps, 80); |
447 | ||
793f1071 | 448 | long words = 0; |
ed08c171 | 449 | int i = 1; |
08fe2e33 | 450 | for (Entry<String, URL> chap : chapters) { |
ed08c171 | 451 | pgChaps.setName("Extracting chapter " + i); |
08fe2e33 NR |
452 | setCurrentReferer(chap.getValue()); |
453 | InputStream chapIn = Instance.getCache().open( | |
454 | chap.getValue(), this, true); | |
ed08c171 | 455 | pgChaps.setProgress(i * 100); |
08fe2e33 | 456 | try { |
ed08c171 NR |
457 | Progress pgGetChapterContent = new Progress(); |
458 | Progress pgMakeChapter = new Progress(); | |
459 | pgChaps.addProgress(pgGetChapterContent, 100); | |
460 | pgChaps.addProgress(pgMakeChapter, 100); | |
461 | ||
462 | String content = getChapterContent(url, chapIn, i, | |
463 | pgGetChapterContent); | |
464 | if (!pgGetChapterContent.isDone()) { | |
465 | pgGetChapterContent.setProgress(pgGetChapterContent | |
466 | .getMax()); | |
467 | } | |
468 | ||
793f1071 | 469 | Chapter cc = makeChapter(url, i, chap.getKey(), |
ed08c171 NR |
470 | content, pgMakeChapter); |
471 | if (!pgMakeChapter.isDone()) { | |
472 | pgMakeChapter.setProgress(pgMakeChapter.getMax()); | |
473 | } | |
474 | ||
793f1071 NR |
475 | words += cc.getWords(); |
476 | story.getChapters().add(cc); | |
477 | if (story.getMeta() != null) { | |
478 | story.getMeta().setWords(words); | |
479 | } | |
08fe2e33 NR |
480 | } finally { |
481 | chapIn.close(); | |
482 | } | |
a6395bef | 483 | |
ed08c171 | 484 | i++; |
08fe2e33 | 485 | } |
ed08c171 NR |
486 | |
487 | pgChaps.setName("Extracting chapters"); | |
92fb0719 | 488 | } else { |
ed08c171 | 489 | pg.setProgress(80); |
08fe2e33 NR |
490 | } |
491 | ||
492 | return story; | |
493 | ||
494 | } finally { | |
495 | try { | |
496 | close(); | |
497 | } catch (IOException e) { | |
498 | Instance.syserr(e); | |
499 | } | |
500 | ||
501 | if (in != null) { | |
502 | in.close(); | |
503 | } | |
504 | ||
a4143cd7 | 505 | setCurrentReferer(null); |
08fe2e33 NR |
506 | } |
507 | } | |
508 | ||
509 | /** | |
a4143cd7 | 510 | * The support type. |
08fe2e33 NR |
511 | * |
512 | * @return the type | |
513 | */ | |
514 | public SupportType getType() { | |
515 | return type; | |
516 | } | |
517 | ||
518 | /** | |
519 | * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e., | |
520 | * the current {@link URL} we work on. | |
521 | * | |
522 | * @return the referer | |
523 | */ | |
524 | public URL getCurrentReferer() { | |
525 | return currentReferer; | |
526 | } | |
527 | ||
528 | /** | |
529 | * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e., | |
530 | * the current {@link URL} we work on. | |
531 | * | |
532 | * @param currentReferer | |
533 | * the new referer | |
534 | */ | |
535 | protected void setCurrentReferer(URL currentReferer) { | |
536 | this.currentReferer = currentReferer; | |
537 | } | |
538 | ||
539 | /** | |
540 | * The support type. | |
541 | * | |
542 | * @param type | |
543 | * the new type | |
544 | * | |
545 | * @return this | |
546 | */ | |
547 | protected BasicSupport setType(SupportType type) { | |
548 | this.type = type; | |
549 | return this; | |
550 | } | |
551 | ||
552 | /** | |
68686a37 | 553 | * Prepare the support if needed before processing. |
08fe2e33 NR |
554 | * |
555 | * @param source | |
556 | * the source of the story | |
557 | * @param in | |
558 | * the input (the main resource) | |
559 | * | |
08fe2e33 NR |
560 | * @throws IOException |
561 | * on I/O error | |
562 | */ | |
68686a37 | 563 | protected void preprocess(URL source, InputStream in) throws IOException { |
08fe2e33 NR |
564 | } |
565 | ||
566 | /** | |
567 | * Now that we have processed the {@link Story}, close the resources if any. | |
568 | * | |
569 | * @throws IOException | |
570 | * on I/O error | |
571 | */ | |
572 | protected void close() throws IOException { | |
573 | } | |
574 | ||
575 | /** | |
576 | * Create a {@link Chapter} object from the given information, formatting | |
577 | * the content as it should be. | |
578 | * | |
579 | * @param number | |
580 | * the chapter number | |
581 | * @param name | |
582 | * the chapter name | |
583 | * @param content | |
584 | * the chapter content | |
ed08c171 NR |
585 | * @param pg |
586 | * the optional progress reporter | |
08fe2e33 NR |
587 | * |
588 | * @return the {@link Chapter} | |
589 | * | |
590 | * @throws IOException | |
591 | * in case of I/O error | |
592 | */ | |
593 | protected Chapter makeChapter(URL source, int number, String name, | |
ed08c171 | 594 | String content, Progress pg) throws IOException { |
08fe2e33 NR |
595 | // Chapter name: process it correctly, then remove the possible |
596 | // redundant "Chapter x: " in front of it | |
597 | String chapterName = processPara(name).getContent().trim(); | |
598 | for (String lang : Instance.getConfig().getString(Config.CHAPTER) | |
599 | .split(",")) { | |
600 | String chapterWord = Instance.getConfig().getStringX( | |
601 | Config.CHAPTER, lang); | |
602 | if (chapterName.startsWith(chapterWord)) { | |
603 | chapterName = chapterName.substring(chapterWord.length()) | |
604 | .trim(); | |
605 | break; | |
606 | } | |
607 | } | |
608 | ||
609 | if (chapterName.startsWith(Integer.toString(number))) { | |
610 | chapterName = chapterName.substring( | |
611 | Integer.toString(number).length()).trim(); | |
612 | } | |
613 | ||
614 | if (chapterName.startsWith(":")) { | |
615 | chapterName = chapterName.substring(1).trim(); | |
616 | } | |
617 | // | |
618 | ||
619 | Chapter chap = new Chapter(number, chapterName); | |
620 | ||
68e370a4 | 621 | if (content != null) { |
ed08c171 | 622 | List<Paragraph> paras = makeParagraphs(source, content, pg); |
793f1071 NR |
623 | long words = 0; |
624 | for (Paragraph para : paras) { | |
625 | words += para.getWords(); | |
626 | } | |
627 | chap.setParagraphs(paras); | |
628 | chap.setWords(words); | |
08fe2e33 NR |
629 | } |
630 | ||
68e370a4 NR |
631 | return chap; |
632 | ||
633 | } | |
634 | ||
635 | /** | |
636 | * Convert the given content into {@link Paragraph}s. | |
637 | * | |
638 | * @param source | |
639 | * the source URL of the story | |
640 | * @param content | |
641 | * the textual content | |
ed08c171 NR |
642 | * @param pg |
643 | * the optional progress reporter | |
68e370a4 NR |
644 | * |
645 | * @return the {@link Paragraph}s | |
646 | * | |
647 | * @throws IOException | |
648 | * in case of I/O error | |
649 | */ | |
ed08c171 NR |
650 | protected List<Paragraph> makeParagraphs(URL source, String content, |
651 | Progress pg) throws IOException { | |
652 | if (pg == null) { | |
653 | pg = new Progress(); | |
654 | } | |
655 | ||
08fe2e33 NR |
656 | if (isHtml()) { |
657 | // Special <HR> processing: | |
658 | content = content.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)", | |
d5a7153c | 659 | "<br/>* * *<br/>"); |
08fe2e33 NR |
660 | } |
661 | ||
68e370a4 | 662 | List<Paragraph> paras = new ArrayList<Paragraph>(); |
08fe2e33 | 663 | |
d5a7153c NR |
664 | if (content != null && !content.trim().isEmpty()) { |
665 | if (isHtml()) { | |
ed08c171 NR |
666 | String[] tab = content.split("(<p>|</p>|<br>|<br/>)"); |
667 | pg.setMinMax(0, tab.length); | |
668 | int i = 1; | |
669 | for (String line : tab) { | |
670 | if (line.startsWith("[") && line.endsWith("]")) { | |
671 | pg.setName("Extracting image " + i); | |
672 | } | |
d5a7153c | 673 | paras.add(makeParagraph(source, line.trim())); |
ed08c171 | 674 | pg.setProgress(i++); |
d5a7153c | 675 | } |
ed08c171 | 676 | pg.setName(null); |
d5a7153c | 677 | } else { |
ed08c171 | 678 | List<String> lines = new ArrayList<String>(); |
d5a7153c NR |
679 | BufferedReader buff = null; |
680 | try { | |
681 | buff = new BufferedReader( | |
682 | new InputStreamReader(new ByteArrayInputStream( | |
683 | content.getBytes("UTF-8")), "UTF-8")); | |
684 | for (String line = buff.readLine(); line != null; line = buff | |
685 | .readLine()) { | |
ed08c171 | 686 | lines.add(line.trim()); |
68e370a4 | 687 | } |
d5a7153c NR |
688 | } finally { |
689 | if (buff != null) { | |
690 | buff.close(); | |
68e370a4 | 691 | } |
08fe2e33 | 692 | } |
ed08c171 NR |
693 | |
694 | pg.setMinMax(0, lines.size()); | |
695 | int i = 0; | |
696 | for (String line : lines) { | |
697 | if (line.startsWith("[") && line.endsWith("]")) { | |
698 | pg.setName("Extracting image " + i); | |
699 | } | |
700 | paras.add(makeParagraph(source, line)); | |
701 | pg.setProgress(i++); | |
702 | } | |
703 | pg.setName(null); | |
08fe2e33 NR |
704 | } |
705 | ||
d5a7153c NR |
706 | // Check quotes for "bad" format |
707 | List<Paragraph> newParas = new ArrayList<Paragraph>(); | |
708 | for (Paragraph para : paras) { | |
709 | newParas.addAll(requotify(para)); | |
710 | } | |
711 | paras = newParas; | |
08fe2e33 | 712 | |
d5a7153c NR |
713 | // Remove double blanks/brks |
714 | fixBlanksBreaks(paras); | |
715 | } | |
08fe2e33 | 716 | |
68e370a4 NR |
717 | return paras; |
718 | } | |
08fe2e33 | 719 | |
d5a7153c NR |
720 | /** |
721 | * Convert the given line into a single {@link Paragraph}. | |
722 | * | |
723 | * @param source | |
724 | * the source URL of the story | |
725 | * @param line | |
726 | * the textual content of the paragraph | |
727 | * | |
728 | * @return the {@link Paragraph} | |
729 | */ | |
730 | private Paragraph makeParagraph(URL source, String line) { | |
731 | URL image = null; | |
732 | if (line.startsWith("[") && line.endsWith("]")) { | |
733 | image = getImageUrl(this, source, | |
734 | line.substring(1, line.length() - 1).trim()); | |
735 | } | |
736 | ||
737 | if (image != null) { | |
738 | return new Paragraph(image); | |
739 | } else { | |
740 | return processPara(line); | |
741 | } | |
742 | } | |
743 | ||
68e370a4 NR |
744 | /** |
745 | * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of | |
746 | * those {@link Paragraph}s. | |
747 | * <p> | |
748 | * The resulting list will not contain a starting or trailing blank/break | |
749 | * nor 2 blanks or breaks following each other. | |
750 | * | |
751 | * @param paras | |
752 | * the list of {@link Paragraph}s to fix | |
753 | */ | |
754 | protected void fixBlanksBreaks(List<Paragraph> paras) { | |
755 | boolean space = false; | |
756 | boolean brk = true; | |
757 | for (int i = 0; i < paras.size(); i++) { | |
758 | Paragraph para = paras.get(i); | |
759 | boolean thisSpace = para.getType() == ParagraphType.BLANK; | |
760 | boolean thisBrk = para.getType() == ParagraphType.BREAK; | |
761 | ||
762 | if (i > 0 && space && thisBrk) { | |
763 | paras.remove(i - 1); | |
764 | i--; | |
765 | } else if ((space || brk) && (thisSpace || thisBrk)) { | |
766 | paras.remove(i); | |
767 | i--; | |
08fe2e33 NR |
768 | } |
769 | ||
68e370a4 NR |
770 | space = thisSpace; |
771 | brk = thisBrk; | |
772 | } | |
08fe2e33 | 773 | |
68e370a4 NR |
774 | // Remove blank/brk at start |
775 | if (paras.size() > 0 | |
776 | && (paras.get(0).getType() == ParagraphType.BLANK || paras.get( | |
777 | 0).getType() == ParagraphType.BREAK)) { | |
778 | paras.remove(0); | |
779 | } | |
780 | ||
781 | // Remove blank/brk at end | |
782 | int last = paras.size() - 1; | |
783 | if (paras.size() > 0 | |
784 | && (paras.get(last).getType() == ParagraphType.BLANK || paras | |
785 | .get(last).getType() == ParagraphType.BREAK)) { | |
786 | paras.remove(last); | |
08fe2e33 NR |
787 | } |
788 | } | |
789 | ||
68e370a4 NR |
790 | /** |
791 | * Get the default cover related to this subject (see <tt>.info</tt> files). | |
792 | * | |
793 | * @param subject | |
794 | * the subject | |
795 | * | |
796 | * @return the cover if any, or NULL | |
797 | */ | |
68686a37 NR |
798 | static BufferedImage getDefaultCover(String subject) { |
799 | if (subject != null && !subject.isEmpty() | |
800 | && Instance.getCoverDir() != null) { | |
801 | try { | |
802 | File fileCover = new File(Instance.getCoverDir(), subject); | |
333f0e7b | 803 | return getImage(null, fileCover.toURI().toURL(), subject); |
68686a37 NR |
804 | } catch (MalformedURLException e) { |
805 | } | |
806 | } | |
807 | ||
808 | return null; | |
809 | } | |
810 | ||
08fe2e33 NR |
811 | /** |
812 | * Return the list of supported image extensions. | |
813 | * | |
a4143cd7 NR |
814 | * @param emptyAllowed |
815 | * TRUE to allow an empty extension on first place, which can be | |
816 | * used when you may already have an extension in your input but | |
817 | * are not sure about it | |
818 | * | |
08fe2e33 NR |
819 | * @return the extensions |
820 | */ | |
68686a37 | 821 | static String[] getImageExt(boolean emptyAllowed) { |
08fe2e33 NR |
822 | if (emptyAllowed) { |
823 | return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
824 | } else { | |
825 | return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
826 | } | |
827 | } | |
828 | ||
a4143cd7 NR |
829 | /** |
830 | * Check if the given resource can be a local image or a remote image, then | |
831 | * refresh the cache with it if it is. | |
832 | * | |
833 | * @param source | |
834 | * the story source | |
835 | * @param line | |
836 | * the resource to check | |
837 | * | |
838 | * @return the image if found, or NULL | |
839 | * | |
840 | */ | |
333f0e7b NR |
841 | static BufferedImage getImage(BasicSupport support, URL source, String line) { |
842 | URL url = getImageUrl(support, source, line); | |
68686a37 NR |
843 | if (url != null) { |
844 | InputStream in = null; | |
845 | try { | |
846 | in = Instance.getCache().open(url, getSupport(url), true); | |
595dfa7a | 847 | return IOUtils.toImage(in); |
68686a37 NR |
848 | } catch (IOException e) { |
849 | } finally { | |
850 | if (in != null) { | |
851 | try { | |
852 | in.close(); | |
853 | } catch (IOException e) { | |
854 | } | |
855 | } | |
856 | } | |
857 | } | |
858 | ||
859 | return null; | |
860 | } | |
861 | ||
08fe2e33 NR |
862 | /** |
863 | * Check if the given resource can be a local image or a remote image, then | |
864 | * refresh the cache with it if it is. | |
865 | * | |
866 | * @param source | |
867 | * the story source | |
868 | * @param line | |
869 | * the resource to check | |
870 | * | |
871 | * @return the image URL if found, or NULL | |
872 | * | |
873 | */ | |
333f0e7b | 874 | static URL getImageUrl(BasicSupport support, URL source, String line) { |
08fe2e33 NR |
875 | URL url = null; |
876 | ||
68686a37 NR |
877 | if (line != null) { |
878 | // try for files | |
68686a37 | 879 | if (source != null) { |
68686a37 | 880 | try { |
2ab2e40a NR |
881 | |
882 | String relPath = null; | |
883 | String absPath = null; | |
884 | try { | |
885 | String path = new File(source.getFile()).getParent(); | |
886 | relPath = new File(new File(path), line.trim()) | |
887 | .getAbsolutePath(); | |
888 | } catch (Exception e) { | |
889 | // Cannot be converted to path (one possibility to take | |
890 | // into account: absolute path on Windows) | |
891 | } | |
892 | try { | |
893 | absPath = new File(line.trim()).getAbsolutePath(); | |
894 | } catch (Exception e) { | |
895 | // Cannot be converted to path (at all) | |
896 | } | |
897 | ||
68686a37 | 898 | for (String ext : getImageExt(true)) { |
2ab2e40a NR |
899 | if (absPath != null && new File(absPath + ext).exists()) { |
900 | url = new File(absPath + ext).toURI().toURL(); | |
901 | } else if (relPath != null | |
902 | && new File(relPath + ext).exists()) { | |
903 | url = new File(relPath + ext).toURI().toURL(); | |
68686a37 | 904 | } |
08fe2e33 | 905 | } |
68686a37 | 906 | } catch (Exception e) { |
2ab2e40a | 907 | // Should not happen since we control the correct arguments |
08fe2e33 | 908 | } |
68686a37 | 909 | } |
08fe2e33 | 910 | |
68686a37 NR |
911 | if (url == null) { |
912 | // try for URLs | |
913 | try { | |
08fe2e33 | 914 | for (String ext : getImageExt(true)) { |
68686a37 | 915 | if (Instance.getCache().check(new URL(line + ext))) { |
08fe2e33 | 916 | url = new URL(line + ext); |
333f0e7b | 917 | break; |
08fe2e33 NR |
918 | } |
919 | } | |
68686a37 NR |
920 | |
921 | // try out of cache | |
922 | if (url == null) { | |
923 | for (String ext : getImageExt(true)) { | |
924 | try { | |
925 | url = new URL(line + ext); | |
333f0e7b | 926 | Instance.getCache().refresh(url, support, true); |
68686a37 NR |
927 | break; |
928 | } catch (IOException e) { | |
929 | // no image with this ext | |
930 | url = null; | |
931 | } | |
932 | } | |
933 | } | |
934 | } catch (MalformedURLException e) { | |
935 | // Not an url | |
08fe2e33 | 936 | } |
08fe2e33 | 937 | } |
08fe2e33 | 938 | |
68686a37 NR |
939 | // refresh the cached file |
940 | if (url != null) { | |
941 | try { | |
333f0e7b | 942 | Instance.getCache().refresh(url, support, true); |
68686a37 NR |
943 | } catch (IOException e) { |
944 | // woops, broken image | |
945 | url = null; | |
946 | } | |
08fe2e33 NR |
947 | } |
948 | } | |
949 | ||
950 | return url; | |
951 | } | |
952 | ||
373da363 NR |
953 | /** |
954 | * Open the input file that will be used through the support. | |
955 | * | |
956 | * @param source | |
957 | * the source {@link URL} | |
958 | * | |
959 | * @return the {@link InputStream} | |
960 | * | |
961 | * @throws IOException | |
962 | * in case of I/O error | |
963 | */ | |
964 | protected InputStream openInput(URL source) throws IOException { | |
965 | return Instance.getCache().open(source, this, false); | |
966 | } | |
967 | ||
a4143cd7 NR |
968 | /** |
969 | * Reset the given {@link InputStream} and return it. | |
970 | * | |
971 | * @param in | |
972 | * the {@link InputStream} to reset | |
973 | * | |
974 | * @return the same {@link InputStream} after reset | |
975 | */ | |
68686a37 NR |
976 | protected InputStream reset(InputStream in) { |
977 | try { | |
978 | in.reset(); | |
979 | } catch (IOException e) { | |
980 | } | |
981 | return in; | |
982 | } | |
983 | ||
08fe2e33 NR |
984 | /** |
985 | * Reset then return {@link BasicSupport#in}. | |
986 | * | |
987 | * @return {@link BasicSupport#in} | |
08fe2e33 | 988 | */ |
68686a37 NR |
989 | protected InputStream getInput() { |
990 | return reset(in); | |
08fe2e33 NR |
991 | } |
992 | ||
993 | /** | |
994 | * Fix the author name if it is prefixed with some "by" {@link String}. | |
995 | * | |
996 | * @param author | |
997 | * the author with a possible prefix | |
998 | * | |
999 | * @return the author without prefixes | |
1000 | */ | |
68686a37 | 1001 | protected String fixAuthor(String author) { |
08fe2e33 NR |
1002 | if (author != null) { |
1003 | for (String suffix : new String[] { " ", ":" }) { | |
1004 | for (String byString : Instance.getConfig() | |
1005 | .getString(Config.BYS).split(",")) { | |
1006 | byString += suffix; | |
1007 | if (author.toUpperCase().startsWith(byString.toUpperCase())) { | |
1008 | author = author.substring(byString.length()).trim(); | |
1009 | } | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | // Special case (without suffix): | |
1014 | if (author.startsWith("©")) { | |
1015 | author = author.substring(1); | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | return author; | |
1020 | } | |
1021 | ||
1022 | /** | |
1023 | * Check quotes for bad format (i.e., quotes with normal paragraphs inside) | |
1024 | * and requotify them (i.e., separate them into QUOTE paragraphs and other | |
1025 | * paragraphs (quotes or not)). | |
1026 | * | |
1027 | * @param para | |
a4143cd7 | 1028 | * the paragraph to requotify (not necessarily a quote) |
08fe2e33 NR |
1029 | * |
1030 | * @return the correctly (or so we hope) quotified paragraphs | |
1031 | */ | |
68e370a4 | 1032 | protected List<Paragraph> requotify(Paragraph para) { |
08fe2e33 NR |
1033 | List<Paragraph> newParas = new ArrayList<Paragraph>(); |
1034 | ||
68686a37 NR |
1035 | if (para.getType() == ParagraphType.QUOTE |
1036 | && para.getContent().length() > 2) { | |
08fe2e33 NR |
1037 | String line = para.getContent(); |
1038 | boolean singleQ = line.startsWith("" + openQuote); | |
1039 | boolean doubleQ = line.startsWith("" + openDoubleQuote); | |
1040 | ||
b4dc6ab5 NR |
1041 | // Do not try when more than one quote at a time |
1042 | // (some stories are not easily readable if we do) | |
1043 | if (singleQ | |
1044 | && line.indexOf(closeQuote, 1) < line | |
1045 | .lastIndexOf(closeQuote)) { | |
1046 | newParas.add(para); | |
1047 | return newParas; | |
1048 | } | |
1049 | if (doubleQ | |
1050 | && line.indexOf(closeDoubleQuote, 1) < line | |
1051 | .lastIndexOf(closeDoubleQuote)) { | |
1052 | newParas.add(para); | |
1053 | return newParas; | |
1054 | } | |
1055 | // | |
1056 | ||
08fe2e33 NR |
1057 | if (!singleQ && !doubleQ) { |
1058 | line = openDoubleQuote + line + closeDoubleQuote; | |
793f1071 NR |
1059 | newParas.add(new Paragraph(ParagraphType.QUOTE, line, para |
1060 | .getWords())); | |
08fe2e33 | 1061 | } else { |
a6395bef | 1062 | char open = singleQ ? openQuote : openDoubleQuote; |
08fe2e33 | 1063 | char close = singleQ ? closeQuote : closeDoubleQuote; |
a6395bef NR |
1064 | |
1065 | int posDot = -1; | |
1066 | boolean inQuote = false; | |
1067 | int i = 0; | |
1068 | for (char car : line.toCharArray()) { | |
1069 | if (car == open) { | |
1070 | inQuote = true; | |
1071 | } else if (car == close) { | |
1072 | inQuote = false; | |
1073 | } else if (car == '.' && !inQuote) { | |
1074 | posDot = i; | |
1075 | break; | |
1076 | } | |
1077 | i++; | |
08fe2e33 NR |
1078 | } |
1079 | ||
1080 | if (posDot >= 0) { | |
1081 | String rest = line.substring(posDot + 1).trim(); | |
1082 | line = line.substring(0, posDot + 1).trim(); | |
793f1071 NR |
1083 | long words = 1; |
1084 | for (char car : line.toCharArray()) { | |
1085 | if (car == ' ') { | |
1086 | words++; | |
1087 | } | |
1088 | } | |
1089 | newParas.add(new Paragraph(ParagraphType.QUOTE, line, words)); | |
68686a37 NR |
1090 | if (!rest.isEmpty()) { |
1091 | newParas.addAll(requotify(processPara(rest))); | |
1092 | } | |
08fe2e33 NR |
1093 | } else { |
1094 | newParas.add(para); | |
1095 | } | |
1096 | } | |
1097 | } else { | |
1098 | newParas.add(para); | |
1099 | } | |
1100 | ||
1101 | return newParas; | |
1102 | } | |
1103 | ||
1104 | /** | |
1105 | * Process a {@link Paragraph} from a raw line of text. | |
1106 | * <p> | |
1107 | * Will also fix quotes and HTML encoding if needed. | |
1108 | * | |
1109 | * @param line | |
1110 | * the raw line | |
1111 | * | |
1112 | * @return the processed {@link Paragraph} | |
1113 | */ | |
22848428 | 1114 | protected Paragraph processPara(String line) { |
08fe2e33 NR |
1115 | line = ifUnhtml(line).trim(); |
1116 | ||
1117 | boolean space = true; | |
1118 | boolean brk = true; | |
1119 | boolean quote = false; | |
1120 | boolean tentativeCloseQuote = false; | |
1121 | char prev = '\0'; | |
1122 | int dashCount = 0; | |
793f1071 | 1123 | long words = 1; |
08fe2e33 NR |
1124 | |
1125 | StringBuilder builder = new StringBuilder(); | |
1126 | for (char car : line.toCharArray()) { | |
1127 | if (car != '-') { | |
1128 | if (dashCount > 0) { | |
1129 | // dash, ndash and mdash: - – — | |
1130 | // currently: always use mdash | |
1131 | builder.append(dashCount == 1 ? '-' : '—'); | |
1132 | } | |
1133 | dashCount = 0; | |
1134 | } | |
1135 | ||
1136 | if (tentativeCloseQuote) { | |
1137 | tentativeCloseQuote = false; | |
22848428 | 1138 | if (Character.isLetterOrDigit(car)) { |
08fe2e33 NR |
1139 | builder.append("'"); |
1140 | } else { | |
22848428 NR |
1141 | // handle double-single quotes as double quotes |
1142 | if (prev == car) { | |
1143 | builder.append(closeDoubleQuote); | |
1144 | continue; | |
1145 | } else { | |
1146 | builder.append(closeQuote); | |
1147 | } | |
08fe2e33 NR |
1148 | } |
1149 | } | |
1150 | ||
1151 | switch (car) { | |
1152 | case ' ': // note: unbreakable space | |
1153 | case ' ': | |
1154 | case '\t': | |
1155 | case '\n': // just in case | |
1156 | case '\r': // just in case | |
793f1071 NR |
1157 | if (builder.length() > 0 |
1158 | && builder.charAt(builder.length() - 1) != ' ') { | |
1159 | words++; | |
1160 | } | |
08fe2e33 NR |
1161 | builder.append(' '); |
1162 | break; | |
1163 | ||
1164 | case '\'': | |
1165 | if (space || (brk && quote)) { | |
1166 | quote = true; | |
22848428 NR |
1167 | // handle double-single quotes as double quotes |
1168 | if (prev == car) { | |
1169 | builder.deleteCharAt(builder.length() - 1); | |
1170 | builder.append(openDoubleQuote); | |
1171 | } else { | |
1172 | builder.append(openQuote); | |
1173 | } | |
1174 | } else if (prev == ' ' || prev == car) { | |
1175 | // handle double-single quotes as double quotes | |
1176 | if (prev == car) { | |
1177 | builder.deleteCharAt(builder.length() - 1); | |
1178 | builder.append(openDoubleQuote); | |
1179 | } else { | |
1180 | builder.append(openQuote); | |
1181 | } | |
08fe2e33 NR |
1182 | } else { |
1183 | // it is a quote ("I'm off") or a 'quote' ("This | |
1184 | // 'good' restaurant"...) | |
1185 | tentativeCloseQuote = true; | |
1186 | } | |
1187 | break; | |
1188 | ||
1189 | case '"': | |
1190 | if (space || (brk && quote)) { | |
1191 | quote = true; | |
1192 | builder.append(openDoubleQuote); | |
1193 | } else if (prev == ' ') { | |
1194 | builder.append(openDoubleQuote); | |
1195 | } else { | |
1196 | builder.append(closeDoubleQuote); | |
1197 | } | |
1198 | break; | |
1199 | ||
1200 | case '-': | |
1201 | if (space) { | |
1202 | quote = true; | |
1203 | } else { | |
1204 | dashCount++; | |
1205 | } | |
1206 | space = false; | |
1207 | break; | |
1208 | ||
1209 | case '*': | |
1210 | case '~': | |
1211 | case '/': | |
1212 | case '\\': | |
1213 | case '<': | |
1214 | case '>': | |
1215 | case '=': | |
1216 | case '+': | |
1217 | case '_': | |
1218 | case '–': | |
1219 | case '—': | |
1220 | space = false; | |
1221 | builder.append(car); | |
1222 | break; | |
1223 | ||
1224 | case '‘': | |
1225 | case '`': | |
1226 | case '‹': | |
1227 | case '﹁': | |
1228 | case '〈': | |
1229 | case '「': | |
1230 | if (space || (brk && quote)) { | |
1231 | quote = true; | |
1232 | builder.append(openQuote); | |
1233 | } else { | |
22848428 NR |
1234 | // handle double-single quotes as double quotes |
1235 | if (prev == car) { | |
1236 | builder.deleteCharAt(builder.length() - 1); | |
1237 | builder.append(openDoubleQuote); | |
1238 | } else { | |
1239 | builder.append(openQuote); | |
1240 | } | |
08fe2e33 NR |
1241 | } |
1242 | space = false; | |
1243 | brk = false; | |
1244 | break; | |
1245 | ||
1246 | case '’': | |
1247 | case '›': | |
1248 | case '﹂': | |
1249 | case '〉': | |
1250 | case '」': | |
1251 | space = false; | |
1252 | brk = false; | |
22848428 NR |
1253 | // handle double-single quotes as double quotes |
1254 | if (prev == car) { | |
1255 | builder.deleteCharAt(builder.length() - 1); | |
1256 | builder.append(closeDoubleQuote); | |
1257 | } else { | |
1258 | builder.append(closeQuote); | |
1259 | } | |
08fe2e33 NR |
1260 | break; |
1261 | ||
1262 | case '«': | |
1263 | case '“': | |
1264 | case '﹃': | |
1265 | case '《': | |
1266 | case '『': | |
1267 | if (space || (brk && quote)) { | |
1268 | quote = true; | |
1269 | builder.append(openDoubleQuote); | |
1270 | } else { | |
1271 | builder.append(openDoubleQuote); | |
1272 | } | |
1273 | space = false; | |
1274 | brk = false; | |
1275 | break; | |
1276 | ||
1277 | case '»': | |
1278 | case '”': | |
1279 | case '﹄': | |
1280 | case '》': | |
1281 | case '』': | |
1282 | space = false; | |
1283 | brk = false; | |
1284 | builder.append(closeDoubleQuote); | |
1285 | break; | |
1286 | ||
1287 | default: | |
1288 | space = false; | |
1289 | brk = false; | |
1290 | builder.append(car); | |
1291 | break; | |
1292 | } | |
1293 | ||
1294 | prev = car; | |
1295 | } | |
1296 | ||
1297 | if (tentativeCloseQuote) { | |
1298 | tentativeCloseQuote = false; | |
1299 | builder.append(closeQuote); | |
1300 | } | |
1301 | ||
1302 | line = builder.toString().trim(); | |
1303 | ||
1304 | ParagraphType type = ParagraphType.NORMAL; | |
1305 | if (space) { | |
1306 | type = ParagraphType.BLANK; | |
1307 | } else if (brk) { | |
1308 | type = ParagraphType.BREAK; | |
1309 | } else if (quote) { | |
1310 | type = ParagraphType.QUOTE; | |
1311 | } | |
1312 | ||
793f1071 | 1313 | return new Paragraph(type, line, words); |
08fe2e33 NR |
1314 | } |
1315 | ||
1316 | /** | |
a4143cd7 | 1317 | * Remove the HTML from the input <b>if</b> {@link BasicSupport#isHtml()} is |
08fe2e33 NR |
1318 | * true. |
1319 | * | |
1320 | * @param input | |
1321 | * the input | |
1322 | * | |
1323 | * @return the no html version if needed | |
1324 | */ | |
1325 | private String ifUnhtml(String input) { | |
1326 | if (isHtml() && input != null) { | |
1327 | return StringUtils.unhtml(input); | |
1328 | } | |
1329 | ||
1330 | return input; | |
1331 | } | |
1332 | ||
1333 | /** | |
1334 | * Return a {@link BasicSupport} implementation supporting the given | |
1335 | * resource if possible. | |
1336 | * | |
1337 | * @param url | |
1338 | * the story resource | |
1339 | * | |
1340 | * @return an implementation that supports it, or NULL | |
1341 | */ | |
1342 | public static BasicSupport getSupport(URL url) { | |
1343 | if (url == null) { | |
1344 | return null; | |
1345 | } | |
1346 | ||
1347 | // TEXT and INFO_TEXT always support files (not URLs though) | |
1348 | for (SupportType type : SupportType.values()) { | |
1349 | if (type != SupportType.TEXT && type != SupportType.INFO_TEXT) { | |
1350 | BasicSupport support = getSupport(type); | |
1351 | if (support != null && support.supports(url)) { | |
1352 | return support; | |
1353 | } | |
1354 | } | |
1355 | } | |
1356 | ||
373da363 NR |
1357 | for (SupportType type : new SupportType[] { SupportType.INFO_TEXT, |
1358 | SupportType.TEXT }) { | |
08fe2e33 NR |
1359 | BasicSupport support = getSupport(type); |
1360 | if (support != null && support.supports(url)) { | |
1361 | return support; | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | return null; | |
1366 | } | |
1367 | ||
1368 | /** | |
1369 | * Return a {@link BasicSupport} implementation supporting the given type. | |
1370 | * | |
1371 | * @param type | |
1372 | * the type | |
1373 | * | |
1374 | * @return an implementation that supports it, or NULL | |
1375 | */ | |
1376 | public static BasicSupport getSupport(SupportType type) { | |
1377 | switch (type) { | |
1378 | case EPUB: | |
1379 | return new Epub().setType(type); | |
1380 | case INFO_TEXT: | |
1381 | return new InfoText().setType(type); | |
1382 | case FIMFICTION: | |
1383 | return new Fimfiction().setType(type); | |
1384 | case FANFICTION: | |
1385 | return new Fanfiction().setType(type); | |
1386 | case TEXT: | |
1387 | return new Text().setType(type); | |
1388 | case MANGAFOX: | |
1389 | return new MangaFox().setType(type); | |
1390 | case E621: | |
1391 | return new E621().setType(type); | |
a4143cd7 NR |
1392 | case YIFFSTAR: |
1393 | return new YiffStar().setType(type); | |
f0608ab1 NR |
1394 | case E_HENTAI: |
1395 | return new EHentai().setType(type); | |
08fe2e33 NR |
1396 | case CBZ: |
1397 | return new Cbz().setType(type); | |
373da363 NR |
1398 | case HTML: |
1399 | return new Html().setType(type); | |
08fe2e33 NR |
1400 | } |
1401 | ||
1402 | return null; | |
1403 | } | |
68686a37 NR |
1404 | |
1405 | /** | |
1406 | * Return the first line from the given input which correspond to the given | |
1407 | * selectors. | |
1408 | * | |
1409 | * @param in | |
1410 | * the input | |
1411 | * @param needle | |
1412 | * a string that must be found inside the target line (also | |
1413 | * supports "^" at start to say "only if it starts with" the | |
1414 | * needle) | |
1415 | * @param relativeLine | |
1416 | * the line to return based upon the target line position (-1 = | |
1417 | * the line before, 0 = the target line...) | |
1418 | * | |
1419 | * @return the line | |
1420 | */ | |
1421 | static String getLine(InputStream in, String needle, int relativeLine) { | |
1422 | return getLine(in, needle, relativeLine, true); | |
1423 | } | |
1424 | ||
1425 | /** | |
1426 | * Return a line from the given input which correspond to the given | |
1427 | * selectors. | |
1428 | * | |
1429 | * @param in | |
1430 | * the input | |
1431 | * @param needle | |
1432 | * a string that must be found inside the target line (also | |
1433 | * supports "^" at start to say "only if it starts with" the | |
1434 | * needle) | |
1435 | * @param relativeLine | |
1436 | * the line to return based upon the target line position (-1 = | |
1437 | * the line before, 0 = the target line...) | |
1438 | * @param first | |
1439 | * takes the first result (as opposed to the last one, which will | |
1440 | * also always spend the input) | |
1441 | * | |
1442 | * @return the line | |
1443 | */ | |
1444 | static String getLine(InputStream in, String needle, int relativeLine, | |
1445 | boolean first) { | |
1446 | String rep = null; | |
1447 | ||
1448 | try { | |
1449 | in.reset(); | |
1450 | } catch (IOException e) { | |
1451 | Instance.syserr(e); | |
1452 | } | |
1453 | ||
1454 | List<String> lines = new ArrayList<String>(); | |
1455 | @SuppressWarnings("resource") | |
1456 | Scanner scan = new Scanner(in, "UTF-8"); | |
1457 | int index = -1; | |
1458 | scan.useDelimiter("\\n"); | |
1459 | while (scan.hasNext()) { | |
1460 | lines.add(scan.next()); | |
1461 | ||
1462 | if (index == -1) { | |
1463 | if (needle.startsWith("^")) { | |
1464 | if (lines.get(lines.size() - 1).startsWith( | |
1465 | needle.substring(1))) { | |
1466 | index = lines.size() - 1; | |
1467 | } | |
1468 | ||
1469 | } else { | |
1470 | if (lines.get(lines.size() - 1).contains(needle)) { | |
1471 | index = lines.size() - 1; | |
1472 | } | |
1473 | } | |
1474 | } | |
1475 | ||
1476 | if (index >= 0 && index + relativeLine < lines.size()) { | |
1477 | rep = lines.get(index + relativeLine); | |
1478 | if (first) { | |
1479 | break; | |
1480 | } | |
1481 | } | |
1482 | } | |
1483 | ||
1484 | return rep; | |
1485 | } | |
f0608ab1 NR |
1486 | |
1487 | /** | |
1488 | * Return the text between the key and the endKey (and optional subKey can | |
1489 | * be passed, in this case we will look for the key first, then take the | |
1490 | * text between the subKey and the endKey). | |
1491 | * <p> | |
1492 | * Will only match the first line with the given key if more than one are | |
1493 | * possible. Which also means that if the subKey or endKey is not found on | |
1494 | * that line, NULL will be returned. | |
1495 | * | |
1496 | * @param in | |
1497 | * the input | |
1498 | * @param key | |
27dc7179 NR |
1499 | * the key to match (also supports "^" at start to say |
1500 | * "only if it starts with" the key) | |
f0608ab1 NR |
1501 | * @param subKey |
1502 | * the sub key or NULL if none | |
1503 | * @param endKey | |
1504 | * the end key or NULL for "up to the end" | |
1505 | * @return the text or NULL if not found | |
1506 | */ | |
1507 | static String getKeyLine(InputStream in, String key, String subKey, | |
1508 | String endKey) { | |
1509 | String result = null; | |
1510 | ||
1511 | String line = getLine(in, key, 0); | |
1512 | if (line != null && line.contains(key)) { | |
1513 | line = line.substring(line.indexOf(key) + key.length()); | |
1514 | if (subKey == null || subKey.isEmpty() || line.contains(subKey)) { | |
1515 | if (subKey != null) { | |
1516 | line = line.substring(line.indexOf(subKey) | |
1517 | + subKey.length()); | |
1518 | } | |
1519 | if (endKey == null || line.contains(endKey)) { | |
1520 | if (endKey != null) { | |
1521 | line = line.substring(0, line.indexOf(endKey)); | |
1522 | result = line; | |
1523 | } | |
1524 | } | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | return result; | |
1529 | } | |
08fe2e33 | 1530 | } |