Commit | Line | Data |
---|---|---|
73785268 NR |
1 | package be.nikiroo.gofetch.support; |
2 | ||
3 | import java.io.IOException; | |
3e62b034 NR |
4 | import java.io.InputStream; |
5 | import java.net.URL; | |
b34d1f35 | 6 | import java.text.SimpleDateFormat; |
27008a87 | 7 | import java.util.ArrayList; |
3e62b034 | 8 | import java.util.Arrays; |
b34d1f35 | 9 | import java.util.Date; |
9cf08a99 | 10 | import java.util.HashMap; |
73785268 | 11 | import java.util.List; |
9cf08a99 | 12 | import java.util.Map; |
3e62b034 | 13 | import java.util.Map.Entry; |
73785268 | 14 | |
3e62b034 | 15 | import org.jsoup.helper.DataUtil; |
27008a87 | 16 | import org.jsoup.helper.StringUtil; |
3e62b034 | 17 | import org.jsoup.nodes.Document; |
27008a87 NR |
18 | import org.jsoup.nodes.Element; |
19 | import org.jsoup.nodes.Node; | |
20 | import org.jsoup.nodes.TextNode; | |
27008a87 NR |
21 | import org.jsoup.select.NodeTraversor; |
22 | import org.jsoup.select.NodeVisitor; | |
23 | ||
3e62b034 | 24 | import be.nikiroo.gofetch.data.Comment; |
73785268 | 25 | import be.nikiroo.gofetch.data.Story; |
136ab801 | 26 | import be.nikiroo.utils.Downloader; |
3e62b034 | 27 | import be.nikiroo.utils.StringUtils; |
73785268 | 28 | |
b34d1f35 NR |
29 | /** |
30 | * Base class for website support. | |
31 | * | |
32 | * @author niki | |
33 | */ | |
73785268 | 34 | public abstract class BasicSupport { |
a71d4075 | 35 | /** |
21f1a997 | 36 | * The {@link Downloader} to use for all web sites via |
9cf08a99 | 37 | * {@link BasicSupport#open(URL)} |
a71d4075 NR |
38 | */ |
39 | static private Downloader downloader = new Downloader("gofetcher"); | |
136ab801 | 40 | |
3e62b034 NR |
41 | static private String preselector; |
42 | ||
9cf08a99 NR |
43 | /** |
44 | * The optional cookies to use to get the site data. | |
45 | */ | |
46 | private Map<String, String> cookies = new HashMap<String, String>(); | |
47 | ||
3e62b034 NR |
48 | private Type type; |
49 | ||
9cf08a99 NR |
50 | /** |
51 | * Login on the web site (this method does nothing by default, but can be | |
52 | * overridden if needed). | |
53 | * | |
54 | * @throws IOException | |
55 | * in case of I/O error | |
56 | * | |
57 | */ | |
21f1a997 | 58 | @SuppressWarnings("unused") |
9cf08a99 NR |
59 | public void login() throws IOException { |
60 | } | |
61 | ||
3e62b034 NR |
62 | /** |
63 | * The website textual description, to add in the dispatcher page. | |
64 | * <p> | |
65 | * Should be short. | |
66 | * | |
67 | * @return the description | |
68 | */ | |
69 | abstract public String getDescription(); | |
70 | ||
b34d1f35 | 71 | /** |
3e62b034 NR |
72 | * The gopher "selector" to use for output. |
73 | * <p> | |
74 | * A kind of "URL path", like "/news/" or "/misc/news/" or... | |
75 | * | |
76 | * @return the selector | |
77 | */ | |
78 | public String getSelector() { | |
a71d4075 | 79 | return getSelector(getType()); |
73785268 NR |
80 | } |
81 | ||
20217360 | 82 | /** |
3e62b034 NR |
83 | * The support type. |
84 | * | |
85 | * @return the type | |
86 | */ | |
87 | public Type getType() { | |
88 | return type; | |
27008a87 NR |
89 | } |
90 | ||
20217360 | 91 | /** |
3e62b034 NR |
92 | * List all the recent items, but only assure the ID and internal URL to |
93 | * fetch it later on (until it has been fetched, the rest of the | |
94 | * {@link Story} is not confirmed). | |
20217360 | 95 | * |
3e62b034 NR |
96 | * @return the list of new stories |
97 | * | |
98 | * @throws IOException | |
99 | * in case of I/O | |
20217360 | 100 | */ |
3e62b034 NR |
101 | public List<Story> list() throws IOException { |
102 | List<Story> list = new ArrayList<Story>(); | |
103 | ||
9cf08a99 | 104 | login(); |
3e62b034 NR |
105 | for (Entry<URL, String> entry : getUrls()) { |
106 | URL url = entry.getKey(); | |
107 | String defaultCateg = entry.getValue(); | |
108 | if (defaultCateg == null) { | |
109 | defaultCateg = ""; | |
110 | } | |
20217360 | 111 | |
a71d4075 | 112 | InputStream in = open(url); |
3e62b034 NR |
113 | Document doc = DataUtil.load(in, "UTF-8", url.toString()); |
114 | List<Element> articles = getArticles(doc); | |
115 | for (Element article : articles) { | |
116 | String id = getArticleId(doc, article).trim(); | |
117 | String title = getArticleTitle(doc, article).trim(); | |
118 | String author = getArticleAuthor(doc, article).trim(); | |
119 | String date = getArticleDate(doc, article).trim(); | |
120 | String categ = getArticleCategory(doc, article, defaultCateg) | |
121 | .trim(); | |
122 | String details = getArticleDetails(doc, article).trim(); | |
123 | String intUrl = getArticleIntUrl(doc, article).trim(); | |
124 | String extUrl = getArticleExtUrl(doc, article).trim(); | |
125 | String content = getArticleContent(doc, article).trim(); | |
126 | ||
127 | if (id.isEmpty() && date.isEmpty()) { | |
128 | continue; | |
129 | } | |
20217360 | 130 | |
1ab7ff0a NR |
131 | if (!id.isEmpty()) { |
132 | while (id.length() < 10) { | |
133 | id = "0" + id; | |
134 | } | |
135 | } else { | |
21f1a997 NR |
136 | id = date.replace(":", "_").replace("+", "_") |
137 | .replace("/", "-"); | |
3e62b034 | 138 | } |
21f1a997 | 139 | |
3e62b034 | 140 | date = date(date); |
b9afb12e | 141 | |
3e62b034 NR |
142 | list.add(new Story(getType(), id, title, author, date, categ, |
143 | details, intUrl, extUrl, content)); | |
144 | } | |
b9afb12e | 145 | } |
3e62b034 NR |
146 | |
147 | return list; | |
20217360 NR |
148 | } |
149 | ||
3e62b034 NR |
150 | /** |
151 | * The {@link URL}s to process for this website. | |
152 | * | |
153 | * @return the list of {@link URL}s | |
154 | * | |
155 | * @throws IOException | |
156 | * in case of I/O error | |
157 | */ | |
158 | abstract protected List<Entry<URL, String>> getUrls() throws IOException; | |
73785268 | 159 | |
3e62b034 NR |
160 | /** |
161 | * The article {@link Element}s of this document. | |
162 | * | |
163 | * @param doc | |
164 | * the main document for the current category | |
165 | * | |
166 | * @return the articles | |
167 | */ | |
168 | abstract protected List<Element> getArticles(Document doc); | |
73785268 | 169 | |
100a8395 | 170 | /** |
3e62b034 | 171 | * The ID of the article (defaults to the date element if empty). |
100a8395 | 172 | * |
3e62b034 NR |
173 | * @param doc |
174 | * the main document for the current category | |
175 | * @param article | |
176 | * the article to look into | |
100a8395 | 177 | * |
3e62b034 NR |
178 | * @return the ID |
179 | */ | |
180 | abstract protected String getArticleId(Document doc, Element article); | |
181 | ||
182 | /** | |
183 | * The article title to display. | |
184 | * | |
185 | * @param doc | |
186 | * the main document for the current category | |
187 | * @param article | |
188 | * the article to look into | |
189 | * | |
190 | * @return the title | |
191 | */ | |
192 | abstract protected String getArticleTitle(Document doc, Element article); | |
193 | ||
194 | /** | |
195 | * The optional article author. | |
196 | * | |
197 | * @param doc | |
198 | * the main document for the current category | |
199 | * @param article | |
200 | * the article to look into | |
201 | * | |
202 | * @return the author | |
203 | */ | |
204 | abstract protected String getArticleAuthor(Document doc, Element article); | |
205 | ||
206 | /** | |
207 | * The optional article date. | |
208 | * | |
209 | * @param doc | |
210 | * the main document for the current category | |
211 | * @param article | |
212 | * the article to look into | |
213 | * | |
214 | * @return the date | |
215 | */ | |
216 | abstract protected String getArticleDate(Document doc, Element article); | |
217 | ||
218 | /** | |
219 | * the optional article category. | |
220 | * | |
221 | * @param doc | |
222 | * the main document for the current category | |
223 | * @param article | |
224 | * the article to look into | |
225 | * @param currentCategory | |
226 | * the currently listed category if any (can be NULL) | |
227 | * | |
228 | * @return the category | |
100a8395 | 229 | */ |
3e62b034 NR |
230 | abstract protected String getArticleCategory(Document doc, Element article, |
231 | String currentCategory); | |
232 | ||
233 | /** | |
234 | * the optional details of the article (can replace the date, author and | |
235 | * category, for instance). | |
236 | * | |
237 | * @param doc | |
238 | * the main document for the current category | |
239 | * @param article | |
240 | * the article to look into | |
241 | * | |
242 | * @return the details | |
243 | */ | |
244 | abstract protected String getArticleDetails(Document doc, Element article); | |
245 | ||
246 | /** | |
247 | * The (required) {@link URL} that points to the news page on the supported | |
248 | * website. | |
249 | * | |
250 | * @param doc | |
251 | * the main document for the current category | |
252 | * @param article | |
253 | * the article to look into | |
254 | * | |
255 | * @return the internal {@link URL} | |
256 | */ | |
257 | abstract protected String getArticleIntUrl(Document doc, Element article); | |
258 | ||
259 | /** | |
260 | * the optional {@link URL} that points to an external website for more | |
261 | * information. | |
262 | * | |
263 | * @param doc | |
264 | * the main document for the current category | |
265 | * @param article | |
266 | * the article to look into | |
267 | * | |
268 | * @return the external {@link URL} | |
269 | */ | |
270 | abstract protected String getArticleExtUrl(Document doc, Element article); | |
271 | ||
272 | /** | |
273 | * The optional article short-content (not the full content, that will be | |
274 | * fetched by {@link BasicSupport#fetch(Story)}). | |
275 | * | |
276 | * @param doc | |
277 | * the main document for the current category | |
278 | * @param article | |
279 | * the article to look into | |
280 | * | |
281 | * @return the short content | |
282 | */ | |
283 | abstract protected String getArticleContent(Document doc, Element article); | |
73785268 | 284 | |
5c056aad NR |
285 | /** |
286 | * Fetch the full article content as well as all the comments associated to | |
287 | * this {@link Story}, if any (can be empty, but not NULL). | |
288 | * | |
289 | * @param story | |
290 | * the story to fetch the comments of | |
291 | * | |
292 | * @throws IOException | |
293 | * in case of I/O error | |
294 | */ | |
3e62b034 NR |
295 | public void fetch(Story story) throws IOException { |
296 | String fullContent = ""; | |
297 | ||
298 | URL url = new URL(story.getUrlInternal()); | |
a71d4075 | 299 | InputStream in = open(url); |
3e62b034 NR |
300 | try { |
301 | Document doc = DataUtil.load(in, "UTF-8", url.toString()); | |
302 | Element article = getFullArticle(doc); | |
303 | if (article != null) { | |
e818d449 | 304 | fullContent = getArticleText(article); |
3e62b034 NR |
305 | } |
306 | ||
307 | if (fullContent.isEmpty()) { | |
308 | fullContent = story.getContent(); | |
309 | } | |
310 | ||
311 | story.setFullContent(fullContent); | |
312 | story.setComments(getComments(doc, | |
313 | getFullArticleCommentPosts(doc, url))); | |
314 | } finally { | |
315 | if (in != null) { | |
316 | in.close(); | |
317 | } | |
318 | } | |
319 | } | |
73785268 | 320 | |
e818d449 NR |
321 | /** |
322 | * Return the text from this {@link Element}, using the | |
323 | * {@link BasicSupport#getElementProcessorFullArticle()} processor logic. | |
324 | * | |
325 | * @param article | |
326 | * the element to extract the text from | |
327 | * | |
328 | * @return the text | |
329 | */ | |
330 | protected String getArticleText(Element article) { | |
331 | StringBuilder builder = new StringBuilder(); | |
332 | ElementProcessor eProc = getElementProcessorFullArticle(); | |
333 | if (eProc != null) { | |
334 | for (String line : toLines(article, eProc)) { | |
335 | builder.append(line + "\n"); | |
336 | } | |
337 | } else { | |
338 | builder.append(article.text()); | |
339 | } | |
340 | ||
341 | // Content is too tight with a single break per line: | |
342 | return builder.toString().replace("\n", "\n\n") // | |
343 | .replace("\n\n\n\n", "\n\n") // | |
344 | .replace("\n\n\n\n", "\n\n") // | |
345 | .trim(); | |
346 | } | |
347 | ||
b34d1f35 | 348 | /** |
9cf08a99 NR |
349 | * Return the full article if available (this is the article to retrieve |
350 | * from the newly downloaded page at {@link Story#getUrlInternal()}). | |
b34d1f35 | 351 | * |
3e62b034 NR |
352 | * @param doc |
353 | * the (full article) document to work on | |
354 | * | |
355 | * @return the article or NULL | |
b34d1f35 | 356 | */ |
3e62b034 | 357 | abstract protected Element getFullArticle(Document doc); |
2d95a873 | 358 | |
b34d1f35 | 359 | /** |
3e62b034 NR |
360 | * Return the list of comment {@link Element}s from this optional container |
361 | * -- must <b>NOT</b> return the "container" as a comment {@link Element}. | |
362 | * | |
363 | * @param doc | |
364 | * the (full article) document to work on | |
365 | * @param intUrl | |
366 | * the internal {@link URL} this article wa taken from (the | |
367 | * {@link URL} from the supported website) | |
368 | * | |
369 | * @return the list of comment posts | |
370 | */ | |
371 | abstract protected List<Element> getFullArticleCommentPosts(Document doc, | |
372 | URL intUrl); | |
373 | ||
374 | /** | |
375 | * The {@link ElementProcessor} to use to convert the main article element | |
376 | * (see {@link BasicSupport#getFullArticle(Document)}) into text. | |
b34d1f35 | 377 | * <p> |
3e62b034 NR |
378 | * See {@link BasicElementProcessor} for a working, basic implementation. |
379 | * <p> | |
380 | * Can be NULL to simply use {@link Element#text()}. | |
b34d1f35 | 381 | * |
3e62b034 | 382 | * @return the processor, or NULL |
b34d1f35 | 383 | */ |
3e62b034 | 384 | abstract protected ElementProcessor getElementProcessorFullArticle(); |
73785268 | 385 | |
a71d4075 NR |
386 | /** |
387 | * Open a network resource. | |
388 | * <p> | |
389 | * You need to close the returned {@link InputStream} when done. | |
390 | * | |
391 | * @param url | |
392 | * the source to open | |
393 | * | |
394 | * @return the content | |
395 | * | |
396 | * @throws IOException | |
397 | * in case of I/O error | |
398 | */ | |
399 | protected InputStream open(URL url) throws IOException { | |
9cf08a99 | 400 | return downloader.open(url, url, cookies, null, null, null); |
a71d4075 NR |
401 | } |
402 | ||
b34d1f35 | 403 | /** |
3e62b034 | 404 | * Convert the comment elements into {@link Comment}s |
b34d1f35 | 405 | * |
3e62b034 NR |
406 | * @param doc |
407 | * the document we work on | |
408 | * @param posts | |
409 | * the comment elements | |
410 | * | |
411 | * @return the converted {@link Comment}s | |
b34d1f35 | 412 | */ |
3e62b034 NR |
413 | private List<Comment> getComments(Document doc, List<Element> posts) { |
414 | List<Comment> comments = new ArrayList<Comment>(); | |
415 | if (posts != null) { | |
416 | for (Element post : posts) { | |
417 | String id = getCommentId(post).trim(); | |
418 | String author = getCommentAuthor(post).trim(); | |
419 | String title = getCommentTitle(post).trim(); | |
420 | String date = getCommentDate(post).trim(); | |
421 | ||
422 | List<String> content = new ArrayList<String>(); | |
423 | ||
424 | if (id.isEmpty()) { | |
425 | id = date; | |
426 | } | |
427 | ||
428 | date = date(date); | |
429 | ||
430 | Element contentE = getCommentContentElement(post); | |
431 | if (contentE != null) { | |
432 | ElementProcessor eProc = getElementProcessorComment(); | |
433 | if (eProc != null) { | |
434 | for (String line : toLines(contentE, eProc)) { | |
435 | content.add(line); | |
436 | } | |
437 | } else { | |
438 | content = Arrays.asList(contentE.text().split("\n")); | |
439 | } | |
440 | } | |
441 | ||
442 | Comment comment = new Comment(id, author, title, date, content); | |
443 | comment.addAll(getComments(doc, | |
444 | getCommentCommentPosts(doc, post))); | |
445 | ||
446 | if (!comment.isEmpty()) { | |
447 | comments.add(comment); | |
448 | } | |
449 | } | |
450 | } | |
451 | ||
452 | return comments; | |
73785268 NR |
453 | } |
454 | ||
3e62b034 NR |
455 | /** |
456 | * Return the list of subcomment {@link Element}s from this comment element | |
457 | * -- must <b>NOT</b> return the "container" as a comment {@link Element}. | |
458 | * | |
459 | * @param doc | |
460 | * the (full article) document to work on | |
461 | * @param container | |
462 | * the container (a comment {@link Element}) | |
463 | * | |
464 | * @return the list of comment posts | |
465 | */ | |
466 | abstract protected List<Element> getCommentCommentPosts(Document doc, | |
467 | Element container); | |
468 | ||
469 | /** | |
470 | * Compute the ID of the given comment element. | |
471 | * | |
472 | * @param post | |
473 | * the comment element | |
474 | * | |
475 | * @return the ID | |
476 | */ | |
477 | abstract protected String getCommentId(Element post); | |
478 | ||
479 | /** | |
480 | * Compute the author of the given comment element. | |
481 | * | |
482 | * @param post | |
483 | * the comment element | |
484 | * | |
485 | * @return the author | |
486 | */ | |
487 | abstract protected String getCommentAuthor(Element post); | |
488 | ||
489 | /** | |
490 | * Compute the title of the given comment element. | |
491 | * | |
492 | * @param post | |
493 | * the comment element | |
494 | * | |
495 | * @return the title | |
496 | */ | |
497 | abstract protected String getCommentTitle(Element post); | |
498 | ||
499 | /** | |
500 | * Compute the date of the given comment element. | |
501 | * | |
502 | * @param post | |
503 | * the comment element | |
504 | * | |
505 | * @return the date | |
506 | */ | |
507 | abstract protected String getCommentDate(Element post); | |
508 | ||
509 | /** | |
510 | * Get the main of the given comment element, which can be NULL. | |
511 | * | |
512 | * @param post | |
513 | * the comment element | |
514 | * | |
515 | * @return the element | |
516 | */ | |
517 | abstract protected Element getCommentContentElement(Element post); | |
518 | ||
519 | /** | |
520 | * The {@link ElementProcessor} to use to convert the main comment element | |
521 | * (see {@link BasicSupport#getCommentContentElement(Element)}) into text. | |
522 | * <p> | |
523 | * See {@link BasicElementProcessor} for a working, basic implementation. | |
524 | * <p> | |
525 | * Can be NULL to simply use {@link Element#text()}. | |
526 | * | |
527 | * @return the processor | |
528 | */ | |
529 | abstract protected ElementProcessor getElementProcessorComment(); | |
530 | ||
b34d1f35 NR |
531 | /** |
532 | * The support type. | |
533 | * | |
534 | * @param type | |
535 | * the new type | |
536 | */ | |
73785268 NR |
537 | protected void setType(Type type) { |
538 | this.type = type; | |
539 | } | |
540 | ||
9cf08a99 NR |
541 | /** |
542 | * Add a cookie for all site connections. | |
543 | * | |
544 | * @param name | |
545 | * the cookie name | |
546 | * @param value | |
547 | * the value | |
548 | */ | |
549 | protected void addCookie(String name, String value) { | |
550 | cookies.put(name, value); | |
551 | } | |
552 | ||
73785268 | 553 | /** |
b34d1f35 NR |
554 | * The {@link String} to append to the selector (the selector will be |
555 | * constructed as "this string" then "/type/". | |
556 | * | |
73785268 NR |
557 | * @param preselector |
558 | * the preselector to set | |
559 | */ | |
560 | static public void setPreselector(String preselector) { | |
561 | BasicSupport.preselector = preselector; | |
562 | } | |
563 | ||
20217360 NR |
564 | /** |
565 | * Return a {@link BasicSupport} that is compatible with the given | |
566 | * {@link Type} if it exists (or NULL if not). | |
567 | * | |
568 | * @param type | |
569 | * the type | |
570 | * | |
571 | * @return a compatible {@link BasicSupport} if it exists (or NULL if not) | |
572 | */ | |
73785268 NR |
573 | static public BasicSupport getSupport(Type type) { |
574 | BasicSupport support = null; | |
575 | ||
576 | if (type != null) { | |
577 | switch (type) { | |
578 | case SLASHDOT: | |
579 | support = new Slashdot(); | |
580 | break; | |
eaaeae39 NR |
581 | case LWN: |
582 | support = new LWN(); | |
583 | break; | |
100a8395 NR |
584 | case LEMONDE: |
585 | support = new LeMonde(); | |
586 | break; | |
d28c4aac NR |
587 | case REGISTER: |
588 | support = new TheRegister(); | |
589 | break; | |
b34d1f35 | 590 | case TOO_LINUX: |
cd555a1e NR |
591 | support = new TooLinux(); |
592 | break; | |
31755801 NR |
593 | case ERE_NUMERIQUE: |
594 | support = new EreNumerique(); | |
595 | break; | |
127e065f NR |
596 | case PHORONIX: |
597 | support = new Phoronix(); | |
598 | break; | |
9cf08a99 NR |
599 | case SEPT_SUR_SEPT: |
600 | support = new SeptSurSept(); | |
601 | break; | |
b19b3632 NR |
602 | case REDDIT: |
603 | support = new Reddit(); | |
604 | break; | |
73785268 NR |
605 | } |
606 | ||
607 | if (support != null) { | |
608 | support.setType(type); | |
609 | } | |
610 | } | |
611 | ||
612 | return support; | |
613 | } | |
614 | ||
b34d1f35 NR |
615 | /** |
616 | * The gopher "selector" to use for output for this type, using the | |
617 | * preselector. | |
618 | * <p> | |
619 | * A kind of "URL path", like "/news/" or "/misc/news/" or... | |
620 | * | |
621 | * @param type | |
622 | * the type to get the selector of | |
623 | * | |
624 | * @return the selector | |
625 | */ | |
73785268 NR |
626 | static public String getSelector(Type type) { |
627 | return preselector + "/" + type + "/"; | |
628 | } | |
629 | ||
20217360 NR |
630 | /** |
631 | * Process the given element into text (each line is a text paragraph and | |
632 | * can be prepended with ">" signs to indicate a quote or sub-quote or | |
633 | * sub-sub-quote...). | |
634 | * | |
635 | * @param element | |
636 | * the element to process | |
637 | * @param elementProcessor | |
638 | * the element processor, must not be NULL | |
639 | * | |
640 | * @return text lines, each line is a paragraph | |
641 | */ | |
27008a87 | 642 | static protected List<String> toLines(Element element, |
20217360 | 643 | final ElementProcessor elementProcessor) { |
27008a87 NR |
644 | final List<String> lines = new ArrayList<String>(); |
645 | final StringBuilder currentLine = new StringBuilder(); | |
646 | final List<Integer> quoted = new ArrayList<Integer>(); | |
647 | final List<Node> ignoredNodes = new ArrayList<Node>(); | |
3e62b034 | 648 | final List<String> footnotes = new ArrayList<String>(); |
27008a87 NR |
649 | |
650 | if (element != null) { | |
651 | new NodeTraversor(new NodeVisitor() { | |
652 | @Override | |
653 | public void head(Node node, int depth) { | |
100a8395 | 654 | String manual = null; |
20217360 | 655 | boolean ignore = elementProcessor.ignoreNode(node) |
100a8395 | 656 | || ignoredNodes.contains(node.parentNode()); |
b9afb12e | 657 | // Manual processing |
100a8395 | 658 | if (!ignore) { |
20217360 | 659 | manual = elementProcessor.manualProcessing(node); |
100a8395 NR |
660 | if (manual != null) { |
661 | currentLine.append(manual); | |
662 | ignore = true; | |
663 | } | |
664 | } | |
665 | ||
b9afb12e NR |
666 | // Subtitle check |
667 | if (!ignore) { | |
668 | String subtitle = elementProcessor.isSubtitle(node); | |
669 | if (subtitle != null) { | |
670 | subtitle = subtitle.trim(); | |
671 | currentLine.append("\n[ " + subtitle + " ]\n"); | |
672 | ignore = true; | |
673 | } | |
674 | } | |
675 | ||
3e62b034 NR |
676 | // <pre> check |
677 | if (!ignore) { | |
678 | if (node instanceof Element) { | |
679 | Element el = (Element) node; | |
680 | if ("pre".equals(el.tagName())) { | |
681 | currentLine.append(StringUtils | |
682 | .unhtml(el.text()).trim()); | |
683 | ignore = true; | |
684 | } | |
685 | } | |
686 | } | |
687 | ||
100a8395 | 688 | if (ignore) { |
27008a87 NR |
689 | ignoredNodes.add(node); |
690 | return; | |
691 | } | |
692 | ||
693 | String prep = ""; | |
694 | for (int i = 0; i < quoted.size(); i++) { | |
695 | prep += ">"; | |
696 | } | |
697 | prep += " "; | |
698 | ||
20217360 | 699 | boolean enterQuote = elementProcessor.detectQuote(node); |
27008a87 NR |
700 | boolean leaveQuote = quoted.contains(depth); |
701 | ||
702 | if (enterQuote) { | |
703 | quoted.add(depth); | |
704 | } | |
705 | ||
706 | if (leaveQuote) { | |
707 | quoted.remove(Integer.valueOf(depth)); | |
708 | } | |
709 | ||
710 | if (enterQuote || leaveQuote) { | |
711 | if (currentLine.length() > 0) { | |
712 | if (currentLine.charAt(currentLine.length() - 1) == '\n') { | |
713 | currentLine.setLength(currentLine.length() - 1); | |
714 | } | |
715 | for (String l : currentLine.toString().split("\n")) { | |
716 | lines.add(prep + l); | |
717 | } | |
718 | } | |
719 | currentLine.setLength(0); | |
720 | } | |
721 | ||
722 | if (node instanceof Element) { | |
723 | Element element = (Element) node; | |
724 | boolean block = element.isBlock() | |
725 | || element.tagName().equalsIgnoreCase("br"); | |
726 | if (block && currentLine.length() > 0) { | |
727 | currentLine.append("\n"); | |
728 | } | |
3e62b034 NR |
729 | |
730 | if (!element.absUrl("href").trim().isEmpty()) { | |
731 | footnotes.add(element.absUrl("href")); | |
732 | currentLine.append("[" + footnotes.size() + "]"); | |
733 | } | |
27008a87 NR |
734 | } else if (node instanceof TextNode) { |
735 | TextNode textNode = (TextNode) node; | |
736 | String line = StringUtil.normaliseWhitespace(textNode | |
737 | .getWholeText()); | |
738 | ||
20217360 | 739 | currentLine.append(elementProcessor.processText(line)); |
27008a87 NR |
740 | currentLine.append(" "); |
741 | } | |
742 | } | |
743 | ||
744 | @Override | |
745 | public void tail(Node node, int depth) { | |
746 | } | |
747 | }).traverse(element); | |
748 | } | |
749 | ||
750 | if (currentLine.length() > 0) { | |
751 | String prep = ""; | |
752 | for (int i = 0; i < quoted.size(); i++) { | |
753 | prep += ">"; | |
754 | } | |
755 | prep += " "; | |
756 | if (currentLine.length() > 0) { | |
757 | if (currentLine.charAt(currentLine.length() - 1) == '\n') { | |
758 | currentLine.setLength(currentLine.length() - 1); | |
759 | } | |
760 | for (String l : currentLine.toString().split("\n")) { | |
761 | lines.add(prep + l); | |
762 | } | |
763 | } | |
764 | } | |
765 | ||
3e62b034 NR |
766 | // Fix spaces and nbsp, remove multiple following blank lines |
767 | List<String> linesCopy = new ArrayList<String>(lines.size()); | |
768 | long blanks = 0; | |
27008a87 | 769 | for (int i = 0; i < lines.size(); i++) { |
3e62b034 NR |
770 | String line = lines.get(i).replace(" ", " ") // nbsp -> space |
771 | .replace(" ", " ").trim(); | |
772 | if (line.isEmpty()) { | |
773 | blanks++; | |
774 | } else { | |
775 | blanks = 0; | |
776 | } | |
777 | ||
778 | if (blanks < 2) { | |
779 | linesCopy.add(line); | |
780 | } | |
781 | } | |
782 | ||
783 | // Footnotes insertion | |
784 | if (footnotes.size() > 0) { | |
785 | linesCopy.add(""); | |
786 | linesCopy.add(""); | |
787 | linesCopy.add(""); | |
788 | linesCopy.add(""); | |
789 | for (int i = 0; i < footnotes.size(); i++) { | |
790 | linesCopy.add("[" + (i + 1) + "] " + footnotes.get(i)); | |
791 | } | |
27008a87 NR |
792 | } |
793 | ||
3e62b034 | 794 | return linesCopy; |
b34d1f35 NR |
795 | } |
796 | ||
797 | /** | |
798 | * Reformat the date if possible. | |
799 | * | |
800 | * @param date | |
801 | * the input date | |
802 | * | |
803 | * @return the reformated date, or the same value if it was not parsable | |
804 | */ | |
3e62b034 | 805 | static private String date(String date) { |
b34d1f35 NR |
806 | SimpleDateFormat out = new SimpleDateFormat("yyyy/MM/dd"); |
807 | ||
808 | long epoch = 0; | |
809 | try { | |
c9cffa91 | 810 | epoch = Long.parseLong(date.trim()); |
b34d1f35 NR |
811 | } catch (Exception e) { |
812 | epoch = 0; | |
880740c4 NR |
813 | } |
814 | ||
b34d1f35 NR |
815 | if (epoch > 0) { |
816 | return out.format(new Date(1000 * epoch)); | |
817 | } | |
818 | ||
819 | try { | |
820 | Date dat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") | |
821 | .parse(date.trim()); | |
822 | return out.format(dat); | |
1197ec1a | 823 | } catch (Exception e) { |
b34d1f35 NR |
824 | return date; |
825 | } | |
27008a87 | 826 | } |
73785268 | 827 | } |