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 (for image lookup in the same path if
55 * the source is a file, can be NULL)
63 * the optional progress reporter
65 * TRUE if the input content is in HTML mode
67 * @return the {@link Chapter}
70 * in case of I/O error
72 public Chapter
makeChapter(BasicSupport support
, URL source
,
73 int number
, String name
, String content
, boolean html
, Progress pg
)
75 // Chapter name: process it correctly, then remove the possible
76 // redundant "Chapter x: " in front of it, or "-" (as in
77 // "Chapter 5: - Fun!" after the ": " was automatically added)
78 String chapterName
= processPara(name
, false)
80 for (String lang
: Instance
.getConfig().getList(Config
.CONF_CHAPTER
)) {
81 String chapterWord
= Instance
.getConfig().getStringX(
82 Config
.CONF_CHAPTER
, lang
);
83 if (chapterName
.startsWith(chapterWord
)) {
84 chapterName
= chapterName
.substring(chapterWord
.length())
90 if (chapterName
.startsWith(Integer
.toString(number
))) {
91 chapterName
= chapterName
.substring(
92 Integer
.toString(number
).length()).trim();
95 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
96 chapterName
= chapterName
.substring(1).trim();
100 Chapter chap
= new Chapter(number
, chapterName
);
102 if (content
!= null) {
103 List
<Paragraph
> paras
= makeParagraphs(support
, source
, content
,
106 for (Paragraph para
: paras
) {
107 words
+= para
.getWords();
109 chap
.setParagraphs(paras
);
110 chap
.setWords(words
);
117 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
118 * and requotify them (i.e., separate them into QUOTE paragraphs and other
119 * paragraphs (quotes or not)).
122 * the paragraph to requotify (not necessarily a quote)
124 * TRUE if the input content is in HTML mode
126 * @return the correctly (or so we hope) quotified paragraphs
128 protected List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
129 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
131 if (para
.getType() == ParagraphType
.QUOTE
132 && para
.getContent().length() > 2) {
133 String line
= para
.getContent();
134 boolean singleQ
= line
.startsWith("" + openQuote
);
135 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
137 // Do not try when more than one quote at a time
138 // (some stories are not easily readable if we do)
140 && line
.indexOf(closeQuote
, 1) < line
141 .lastIndexOf(closeQuote
)) {
146 && line
.indexOf(closeDoubleQuote
, 1) < line
147 .lastIndexOf(closeDoubleQuote
)) {
153 if (!singleQ
&& !doubleQ
) {
154 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
155 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
158 char open
= singleQ ? openQuote
: openDoubleQuote
;
159 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
162 boolean inQuote
= false;
164 for (char car
: line
.toCharArray()) {
167 } else if (car
== close
) {
169 } else if (car
== '.' && !inQuote
) {
177 String rest
= line
.substring(posDot
+ 1).trim();
178 line
= line
.substring(0, posDot
+ 1).trim();
180 for (char car
: line
.toCharArray()) {
185 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
186 if (!rest
.isEmpty()) {
187 newParas
.addAll(requotify(processPara(rest
, html
), html
));
201 * Process a {@link Paragraph} from a raw line of text.
203 * Will also fix quotes and HTML encoding if needed.
208 * TRUE if the input content is in HTML mode
210 * @return the processed {@link Paragraph}
212 protected Paragraph
processPara(String line
, boolean html
) {
214 line
= StringUtils
.unhtml(line
).trim();
216 boolean space
= true;
218 boolean quote
= false;
219 boolean tentativeCloseQuote
= false;
224 StringBuilder builder
= new StringBuilder();
225 for (char car
: line
.toCharArray()) {
228 // dash, ndash and mdash: - â â
229 // currently: always use mdash
230 builder
.append(dashCount
== 1 ?
'-' : 'â');
235 if (tentativeCloseQuote
) {
236 tentativeCloseQuote
= false;
237 if (Character
.isLetterOrDigit(car
)) {
240 // handle double-single quotes as double quotes
242 builder
.append(closeDoubleQuote
);
246 builder
.append(closeQuote
);
251 case 'Â ': // note: unbreakable space
254 case '\n': // just in case
255 case '\r': // just in case
256 if (builder
.length() > 0
257 && builder
.charAt(builder
.length() - 1) != ' ') {
264 if (space
|| (brk
&& quote
)) {
266 // handle double-single quotes as double quotes
268 builder
.deleteCharAt(builder
.length() - 1);
269 builder
.append(openDoubleQuote
);
271 builder
.append(openQuote
);
273 } else if (prev
== ' ' || prev
== car
) {
274 // handle double-single quotes as double quotes
276 builder
.deleteCharAt(builder
.length() - 1);
277 builder
.append(openDoubleQuote
);
279 builder
.append(openQuote
);
282 // it is a quote ("I'm off") or a 'quote' ("This
283 // 'good' restaurant"...)
284 tentativeCloseQuote
= true;
289 if (space
|| (brk
&& quote
)) {
291 builder
.append(openDoubleQuote
);
292 } else if (prev
== ' ') {
293 builder
.append(openDoubleQuote
);
295 builder
.append(closeDoubleQuote
);
329 if (space
|| (brk
&& quote
)) {
331 builder
.append(openQuote
);
333 // handle double-single quotes as double quotes
335 builder
.deleteCharAt(builder
.length() - 1);
336 builder
.append(openDoubleQuote
);
338 builder
.append(openQuote
);
352 // handle double-single quotes as double quotes
354 builder
.deleteCharAt(builder
.length() - 1);
355 builder
.append(closeDoubleQuote
);
357 builder
.append(closeQuote
);
366 if (space
|| (brk
&& quote
)) {
368 builder
.append(openDoubleQuote
);
370 builder
.append(openDoubleQuote
);
383 builder
.append(closeDoubleQuote
);
396 if (tentativeCloseQuote
) {
397 tentativeCloseQuote
= false;
398 builder
.append(closeQuote
);
401 line
= builder
.toString().trim();
403 ParagraphType type
= ParagraphType
.NORMAL
;
405 type
= ParagraphType
.BLANK
;
407 type
= ParagraphType
.BREAK
;
409 type
= ParagraphType
.QUOTE
;
412 return new Paragraph(type
, line
, words
);
416 * Convert the given content into {@link Paragraph}s.
419 * the linked {@link BasicSupport} (can be NULL), used to
420 * download optional image content in []
422 * the source URL of the story (for image lookup in the same path
423 * if the source is a file, can be NULL)
425 * the textual content
427 * TRUE if the input content is in HTML mode
429 * the optional progress reporter
431 * @return the {@link Paragraph}s
433 * @throws IOException
434 * in case of I/O error
436 protected List
<Paragraph
> makeParagraphs(BasicSupport support
,
437 URL source
, String content
, boolean html
, Progress pg
)
444 // Special <HR> processing:
445 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
449 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
451 if (content
!= null && !content
.trim().isEmpty()) {
453 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
454 pg
.setMinMax(0, tab
.length
);
456 for (String line
: tab
) {
457 if (line
.startsWith("[") && line
.endsWith("]")) {
458 pg
.setName("Extracting image " + i
);
460 paras
.add(makeParagraph(support
, source
, line
.trim(), html
));
464 List
<String
> lines
= new ArrayList
<String
>();
465 BufferedReader buff
= null;
467 buff
= new BufferedReader(
468 new InputStreamReader(new ByteArrayInputStream(
469 content
.getBytes("UTF-8")), "UTF-8"));
470 for (String line
= buff
.readLine(); line
!= null; line
= buff
472 lines
.add(line
.trim());
480 pg
.setMinMax(0, lines
.size());
482 for (String line
: lines
) {
483 if (line
.startsWith("[") && line
.endsWith("]")) {
484 pg
.setName("Extracting image " + i
);
486 paras
.add(makeParagraph(support
, source
, line
, html
));
494 // Check quotes for "bad" format
495 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
496 for (Paragraph para
: paras
) {
497 newParas
.addAll(requotify(para
, html
));
501 // Remove double blanks/brks
502 fixBlanksBreaks(paras
);
509 * Convert the given line into a single {@link Paragraph}.
512 * the linked {@link BasicSupport} (can be NULL), used to
513 * download optional image content in []
515 * the source URL of the story (for image lookup in the same path
516 * if the source is a file, can be NULL)
518 * the textual content of the paragraph
520 * TRUE if the input content is in HTML mode
522 * @return the {@link Paragraph}
524 protected Paragraph
makeParagraph(BasicSupport support
, URL source
,
525 String line
, boolean html
) {
527 if (line
.startsWith("[") && line
.endsWith("]")) {
528 image
= bsHelper
.getImage(support
, source
, line
529 .substring(1, line
.length() - 1).trim());
533 return new Paragraph(image
);
536 return processPara(line
, html
);
540 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
541 * those {@link Paragraph}s.
543 * The resulting list will not contain a starting or trailing blank/break
544 * nor 2 blanks or breaks following each other.
547 * the list of {@link Paragraph}s to fix
549 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
550 boolean space
= false;
552 for (int i
= 0; i
< paras
.size(); i
++) {
553 Paragraph para
= paras
.get(i
);
554 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
555 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
557 if (i
> 0 && space
&& thisBrk
) {
560 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
569 // Remove blank/brk at start
571 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
572 0).getType() == ParagraphType
.BREAK
)) {
576 // Remove blank/brk at end
577 int last
= paras
.size() - 1;
579 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
580 .get(last
).getType() == ParagraphType
.BREAK
)) {