1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStreamReader
;
8 import java
.util
.ArrayList
;
11 import be
.nikiroo
.fanfix
.Instance
;
12 import be
.nikiroo
.fanfix
.bundles
.Config
;
13 import be
.nikiroo
.fanfix
.bundles
.StringId
;
14 import be
.nikiroo
.fanfix
.data
.Chapter
;
15 import be
.nikiroo
.fanfix
.data
.Paragraph
;
16 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
17 import be
.nikiroo
.utils
.Image
;
18 import be
.nikiroo
.utils
.Progress
;
19 import be
.nikiroo
.utils
.StringUtils
;
22 * Helper class for {@link BasicSupport}, mostly dedicated to {@link Paragraph}
23 * and text formating for the {@link BasicSupport} class.
27 public class BasicSupportPara
{
29 private static char openQuote
= Instance
.getTrans().getCharacter(
30 StringId
.OPEN_SINGLE_QUOTE
);
31 private static char closeQuote
= Instance
.getTrans().getCharacter(
32 StringId
.CLOSE_SINGLE_QUOTE
);
33 private static char openDoubleQuote
= Instance
.getTrans().getCharacter(
34 StringId
.OPEN_DOUBLE_QUOTE
);
35 private static char closeDoubleQuote
= Instance
.getTrans().getCharacter(
36 StringId
.CLOSE_DOUBLE_QUOTE
);
38 // used by this class:
39 BasicSupportHelper bsHelper
;
40 BasicSupportImages bsImages
;
42 public BasicSupportPara(BasicSupportHelper bsHelper
, BasicSupportImages bsImages
) {
43 this.bsHelper
= bsHelper
;
44 this.bsImages
= bsImages
;
48 * Create a {@link Chapter} object from the given information, formatting
49 * the content as it should be.
52 * the linked {@link BasicSupport}
54 * the source of the story
62 * the optional progress reporter
64 * TRUE if the input content is in HTML mode
66 * @return the {@link Chapter}
69 * in case of I/O error
71 public Chapter
makeChapter(BasicSupport support
, URL source
,
72 int number
, String name
, String content
, boolean html
, Progress pg
)
74 // Chapter name: process it correctly, then remove the possible
75 // redundant "Chapter x: " in front of it, or "-" (as in
76 // "Chapter 5: - Fun!" after the ": " was automatically added)
77 String chapterName
= processPara(name
, false)
79 for (String lang
: Instance
.getConfig().getList(Config
.CONF_CHAPTER
)) {
80 String chapterWord
= Instance
.getConfig().getStringX(
81 Config
.CONF_CHAPTER
, lang
);
82 if (chapterName
.startsWith(chapterWord
)) {
83 chapterName
= chapterName
.substring(chapterWord
.length())
89 if (chapterName
.startsWith(Integer
.toString(number
))) {
90 chapterName
= chapterName
.substring(
91 Integer
.toString(number
).length()).trim();
94 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
95 chapterName
= chapterName
.substring(1).trim();
99 Chapter chap
= new Chapter(number
, chapterName
);
101 if (content
!= null) {
102 List
<Paragraph
> paras
= makeParagraphs(support
, source
, content
,
105 for (Paragraph para
: paras
) {
106 words
+= para
.getWords();
108 chap
.setParagraphs(paras
);
109 chap
.setWords(words
);
116 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
117 * and requotify them (i.e., separate them into QUOTE paragraphs and other
118 * paragraphs (quotes or not)).
121 * the paragraph to requotify (not necessarily a quote)
123 * TRUE if the input content is in HTML mode
125 * @return the correctly (or so we hope) quotified paragraphs
127 protected List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
128 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
130 if (para
.getType() == ParagraphType
.QUOTE
131 && para
.getContent().length() > 2) {
132 String line
= para
.getContent();
133 boolean singleQ
= line
.startsWith("" + openQuote
);
134 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
136 // Do not try when more than one quote at a time
137 // (some stories are not easily readable if we do)
139 && line
.indexOf(closeQuote
, 1) < line
140 .lastIndexOf(closeQuote
)) {
145 && line
.indexOf(closeDoubleQuote
, 1) < line
146 .lastIndexOf(closeDoubleQuote
)) {
152 if (!singleQ
&& !doubleQ
) {
153 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
154 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
157 char open
= singleQ ? openQuote
: openDoubleQuote
;
158 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
161 boolean inQuote
= false;
163 for (char car
: line
.toCharArray()) {
166 } else if (car
== close
) {
168 } else if (car
== '.' && !inQuote
) {
176 String rest
= line
.substring(posDot
+ 1).trim();
177 line
= line
.substring(0, posDot
+ 1).trim();
179 for (char car
: line
.toCharArray()) {
184 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
185 if (!rest
.isEmpty()) {
186 newParas
.addAll(requotify(processPara(rest
, html
), html
));
200 * Process a {@link Paragraph} from a raw line of text.
202 * Will also fix quotes and HTML encoding if needed.
207 * TRUE if the input content is in HTML mode
209 * @return the processed {@link Paragraph}
211 protected Paragraph
processPara(String line
, boolean html
) {
213 line
= StringUtils
.unhtml(line
).trim();
215 boolean space
= true;
217 boolean quote
= false;
218 boolean tentativeCloseQuote
= false;
223 StringBuilder builder
= new StringBuilder();
224 for (char car
: line
.toCharArray()) {
227 // dash, ndash and mdash: - – —
228 // currently: always use mdash
229 builder
.append(dashCount
== 1 ?
'-' : '—');
234 if (tentativeCloseQuote
) {
235 tentativeCloseQuote
= false;
236 if (Character
.isLetterOrDigit(car
)) {
239 // handle double-single quotes as double quotes
241 builder
.append(closeDoubleQuote
);
245 builder
.append(closeQuote
);
250 case ' ': // note: unbreakable space
253 case '\n': // just in case
254 case '\r': // just in case
255 if (builder
.length() > 0
256 && builder
.charAt(builder
.length() - 1) != ' ') {
263 if (space
|| (brk
&& quote
)) {
265 // handle double-single quotes as double quotes
267 builder
.deleteCharAt(builder
.length() - 1);
268 builder
.append(openDoubleQuote
);
270 builder
.append(openQuote
);
272 } else if (prev
== ' ' || prev
== car
) {
273 // handle double-single quotes as double quotes
275 builder
.deleteCharAt(builder
.length() - 1);
276 builder
.append(openDoubleQuote
);
278 builder
.append(openQuote
);
281 // it is a quote ("I'm off") or a 'quote' ("This
282 // 'good' restaurant"...)
283 tentativeCloseQuote
= true;
288 if (space
|| (brk
&& quote
)) {
290 builder
.append(openDoubleQuote
);
291 } else if (prev
== ' ') {
292 builder
.append(openDoubleQuote
);
294 builder
.append(closeDoubleQuote
);
328 if (space
|| (brk
&& quote
)) {
330 builder
.append(openQuote
);
332 // handle double-single quotes as double quotes
334 builder
.deleteCharAt(builder
.length() - 1);
335 builder
.append(openDoubleQuote
);
337 builder
.append(openQuote
);
351 // handle double-single quotes as double quotes
353 builder
.deleteCharAt(builder
.length() - 1);
354 builder
.append(closeDoubleQuote
);
356 builder
.append(closeQuote
);
365 if (space
|| (brk
&& quote
)) {
367 builder
.append(openDoubleQuote
);
369 builder
.append(openDoubleQuote
);
382 builder
.append(closeDoubleQuote
);
395 if (tentativeCloseQuote
) {
396 tentativeCloseQuote
= false;
397 builder
.append(closeQuote
);
400 line
= builder
.toString().trim();
402 ParagraphType type
= ParagraphType
.NORMAL
;
404 type
= ParagraphType
.BLANK
;
406 type
= ParagraphType
.BREAK
;
408 type
= ParagraphType
.QUOTE
;
411 return new Paragraph(type
, line
, words
);
415 * Convert the given content into {@link Paragraph}s.
418 * the linked {@link BasicSupport} (can be NULL),
419 * used to download optional image content in []
421 * the source URL of the story
423 * the textual content
425 * TRUE if the input content is in HTML mode
427 * the optional progress reporter
429 * @return the {@link Paragraph}s
431 * @throws IOException
432 * in case of I/O error
434 protected List
<Paragraph
> makeParagraphs(BasicSupport support
,
435 URL source
, String content
, boolean html
, Progress pg
)
442 // Special <HR> processing:
443 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
447 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
449 if (content
!= null && !content
.trim().isEmpty()) {
451 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
452 pg
.setMinMax(0, tab
.length
);
454 for (String line
: tab
) {
455 if (line
.startsWith("[") && line
.endsWith("]")) {
456 pg
.setName("Extracting image " + i
);
458 paras
.add(makeParagraph(support
, source
, line
.trim(), html
));
462 List
<String
> lines
= new ArrayList
<String
>();
463 BufferedReader buff
= null;
465 buff
= new BufferedReader(
466 new InputStreamReader(new ByteArrayInputStream(
467 content
.getBytes("UTF-8")), "UTF-8"));
468 for (String line
= buff
.readLine(); line
!= null; line
= buff
470 lines
.add(line
.trim());
478 pg
.setMinMax(0, lines
.size());
480 for (String line
: lines
) {
481 if (line
.startsWith("[") && line
.endsWith("]")) {
482 pg
.setName("Extracting image " + i
);
484 paras
.add(makeParagraph(support
, source
, line
, html
));
492 // Check quotes for "bad" format
493 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
494 for (Paragraph para
: paras
) {
495 newParas
.addAll(requotify(para
, html
));
499 // Remove double blanks/brks
500 fixBlanksBreaks(paras
);
507 * Convert the given line into a single {@link Paragraph}.
510 * the linked {@link BasicSupport} (can be NULL),
511 * used to download optional image content in []
513 * the source URL of the story
515 * the textual content of the paragraph
517 * TRUE if the input content is in HTML mode
519 * @return the {@link Paragraph}
521 protected Paragraph
makeParagraph(BasicSupport support
, URL source
,
522 String line
, boolean html
) {
524 if (line
.startsWith("[") && line
.endsWith("]")) {
525 image
= bsHelper
.getImage(support
, source
, line
526 .substring(1, line
.length() - 1).trim());
530 return new Paragraph(image
);
533 return processPara(line
, html
);
537 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
538 * those {@link Paragraph}s.
540 * The resulting list will not contain a starting or trailing blank/break
541 * nor 2 blanks or breaks following each other.
544 * the list of {@link Paragraph}s to fix
546 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
547 boolean space
= false;
549 for (int i
= 0; i
< paras
.size(); i
++) {
550 Paragraph para
= paras
.get(i
);
551 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
552 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
554 if (i
> 0 && space
&& thisBrk
) {
557 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
566 // Remove blank/brk at start
568 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
569 0).getType() == ParagraphType
.BREAK
)) {
573 // Remove blank/brk at end
574 int last
= paras
.size() - 1;
576 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
577 .get(last
).getType() == ParagraphType
.BREAK
)) {