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
.getInstance().getTrans().getCharacter(StringId
.OPEN_SINGLE_QUOTE
);
30 private static char closeQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_SINGLE_QUOTE
);
31 private static char openDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
32 private static char closeDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
34 // used by this class:
35 BasicSupportHelper bsHelper
;
36 BasicSupportImages bsImages
;
38 public BasicSupportPara(BasicSupportHelper bsHelper
, BasicSupportImages bsImages
) {
39 this.bsHelper
= bsHelper
;
40 this.bsImages
= bsImages
;
44 * Create a {@link Chapter} object from the given information, formatting
45 * the content as it should be.
48 * the linked {@link BasicSupport}
50 * the source of the story (for image lookup in the same path if
51 * the source is a file, can be NULL)
59 * the optional progress reporter
61 * TRUE if the input content is in HTML mode
63 * @return the {@link Chapter}, never NULL
66 * in case of I/O error
68 public Chapter
makeChapter(BasicSupport support
, URL source
,
69 int number
, String name
, String content
, boolean html
, Progress pg
)
71 // Chapter name: process it correctly, then remove the possible
72 // redundant "Chapter x: " in front of it, or "-" (as in
73 // "Chapter 5: - Fun!" after the ": " was automatically added)
74 String chapterName
= processPara(name
, false)
76 for (String lang
: Instance
.getInstance().getConfig().getList(Config
.CONF_CHAPTER
)) {
77 String chapterWord
= Instance
.getInstance().getConfig().getStringX(Config
.CONF_CHAPTER
, lang
);
78 if (chapterName
.startsWith(chapterWord
)) {
79 chapterName
= chapterName
.substring(chapterWord
.length())
85 if (chapterName
.startsWith(Integer
.toString(number
))) {
86 chapterName
= chapterName
.substring(
87 Integer
.toString(number
).length()).trim();
90 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
91 chapterName
= chapterName
.substring(1).trim();
95 Chapter chap
= new Chapter(number
, chapterName
);
97 if (content
!= null) {
98 List
<Paragraph
> paras
= makeParagraphs(support
, source
, content
,
101 for (Paragraph para
: paras
) {
102 words
+= para
.getWords();
104 chap
.setParagraphs(paras
);
105 chap
.setWords(words
);
112 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
113 * and requotify them (i.e., separate them into QUOTE paragraphs and other
114 * paragraphs (quotes or not)).
117 * the paragraph to requotify (not necessarily a quote)
119 * TRUE if the input content is in HTML mode
121 * @return the correctly (or so we hope) quotified paragraphs
123 protected List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
124 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
126 if (para
.getType() == ParagraphType
.QUOTE
127 && para
.getContent().length() > 2) {
128 String line
= para
.getContent();
129 boolean singleQ
= line
.startsWith("" + openQuote
);
130 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
132 // Do not try when more than one quote at a time
133 // (some stories are not easily readable if we do)
135 && line
.indexOf(closeQuote
, 1) < line
136 .lastIndexOf(closeQuote
)) {
141 && line
.indexOf(closeDoubleQuote
, 1) < line
142 .lastIndexOf(closeDoubleQuote
)) {
148 if (!singleQ
&& !doubleQ
) {
149 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
150 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
153 char open
= singleQ ? openQuote
: openDoubleQuote
;
154 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
157 boolean inQuote
= false;
159 for (char car
: line
.toCharArray()) {
162 } else if (car
== close
) {
164 } else if (car
== '.' && !inQuote
) {
172 String rest
= line
.substring(posDot
+ 1).trim();
173 line
= line
.substring(0, posDot
+ 1).trim();
175 for (char car
: line
.toCharArray()) {
180 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
181 if (!rest
.isEmpty()) {
182 newParas
.addAll(requotify(processPara(rest
, html
), html
));
196 * Process a {@link Paragraph} from a raw line of text.
198 * Will also fix quotes and HTML encoding if needed.
203 * TRUE if the input content is in HTML mode
205 * @return the processed {@link Paragraph}, never NULL
207 protected Paragraph
processPara(String line
, boolean html
) {
209 line
= StringUtils
.unhtml(line
).trim();
211 boolean space
= true;
213 boolean quote
= false;
214 boolean tentativeCloseQuote
= false;
219 StringBuilder builder
= new StringBuilder();
220 for (char car
: line
.toCharArray()) {
223 // dash, ndash and mdash: - – —
224 // currently: always use mdash
225 builder
.append(dashCount
== 1 ?
'-' : '—');
230 if (tentativeCloseQuote
) {
231 tentativeCloseQuote
= false;
232 if (Character
.isLetterOrDigit(car
)) {
235 // handle double-single quotes as double quotes
237 builder
.append(closeDoubleQuote
);
241 builder
.append(closeQuote
);
246 case ' ': // note: unbreakable space
249 case '\n': // just in case
250 case '\r': // just in case
251 if (builder
.length() > 0
252 && builder
.charAt(builder
.length() - 1) != ' ') {
259 if (space
|| (brk
&& quote
)) {
261 // handle double-single quotes as double quotes
263 builder
.deleteCharAt(builder
.length() - 1);
264 builder
.append(openDoubleQuote
);
266 builder
.append(openQuote
);
268 } else if (prev
== ' ' || prev
== car
) {
269 // handle double-single quotes as double quotes
271 builder
.deleteCharAt(builder
.length() - 1);
272 builder
.append(openDoubleQuote
);
274 builder
.append(openQuote
);
277 // it is a quote ("I'm off") or a 'quote' ("This
278 // 'good' restaurant"...)
279 tentativeCloseQuote
= true;
284 if (space
|| (brk
&& quote
)) {
286 builder
.append(openDoubleQuote
);
287 } else if (prev
== ' ') {
288 builder
.append(openDoubleQuote
);
290 builder
.append(closeDoubleQuote
);
324 if (space
|| (brk
&& quote
)) {
326 builder
.append(openQuote
);
328 // handle double-single quotes as double quotes
330 builder
.deleteCharAt(builder
.length() - 1);
331 builder
.append(openDoubleQuote
);
333 builder
.append(openQuote
);
347 // handle double-single quotes as double quotes
349 builder
.deleteCharAt(builder
.length() - 1);
350 builder
.append(closeDoubleQuote
);
352 builder
.append(closeQuote
);
361 if (space
|| (brk
&& quote
)) {
363 builder
.append(openDoubleQuote
);
365 builder
.append(openDoubleQuote
);
378 builder
.append(closeDoubleQuote
);
391 if (tentativeCloseQuote
) {
392 tentativeCloseQuote
= false;
393 builder
.append(closeQuote
);
396 line
= builder
.toString().trim();
398 ParagraphType type
= ParagraphType
.NORMAL
;
400 type
= ParagraphType
.BLANK
;
402 type
= ParagraphType
.BREAK
;
404 type
= ParagraphType
.QUOTE
;
407 return new Paragraph(type
, line
, words
);
411 * Convert the given content into {@link Paragraph}s.
414 * the linked {@link BasicSupport} (can be NULL), used to
415 * download optional image content in []
417 * the source URL of the story (for image lookup in the same path
418 * if the source is a file, can be NULL)
420 * the textual content
422 * TRUE if the input content is in HTML mode
424 * the optional progress reporter
426 * @return the {@link Paragraph}s (can be empty but never NULL)
428 * @throws IOException
429 * in case of I/O error
431 protected List
<Paragraph
> makeParagraphs(BasicSupport support
,
432 URL source
, String content
, boolean html
, Progress pg
)
439 // Special <HR> processing:
440 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
444 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
446 if (content
!= null && !content
.trim().isEmpty()) {
448 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
449 pg
.setMinMax(0, tab
.length
);
451 for (String line
: tab
) {
452 if (line
.startsWith("[") && line
.endsWith("]")) {
453 pg
.setName("Extracting image " + i
);
455 paras
.add(makeParagraph(support
, source
, line
.trim(), html
));
459 List
<String
> lines
= new ArrayList
<String
>();
460 BufferedReader buff
= null;
462 buff
= new BufferedReader(
463 new InputStreamReader(new ByteArrayInputStream(
464 content
.getBytes("UTF-8")), "UTF-8"));
465 for (String line
= buff
.readLine(); line
!= null; line
= buff
467 lines
.add(line
.trim());
475 pg
.setMinMax(0, lines
.size());
477 for (String line
: lines
) {
478 if (line
.startsWith("[") && line
.endsWith("]")) {
479 pg
.setName("Extracting image " + i
);
481 paras
.add(makeParagraph(support
, source
, line
, html
));
489 // Check quotes for "bad" format
490 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
491 for (Paragraph para
: paras
) {
492 newParas
.addAll(requotify(para
, html
));
496 // Remove double blanks/brks
497 fixBlanksBreaks(paras
);
504 * Convert the given line into a single {@link Paragraph}.
507 * the linked {@link BasicSupport} (can be NULL), used to
508 * download optional image content in []
510 * the source URL of the story (for image lookup in the same path
511 * if the source is a file, can be NULL)
513 * the textual content of the paragraph
515 * TRUE if the input content is in HTML mode
517 * @return the {@link Paragraph}, never NULL
519 protected Paragraph
makeParagraph(BasicSupport support
, URL source
,
520 String line
, boolean html
) {
522 if (line
.startsWith("[") && line
.endsWith("]")) {
523 image
= bsHelper
.getImage(support
, source
, line
524 .substring(1, line
.length() - 1).trim());
528 return new Paragraph(image
);
531 return processPara(line
, html
);
535 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
536 * those {@link Paragraph}s.
538 * The resulting list will not contain a starting or trailing blank/break
539 * nor 2 blanks or breaks following each other.
542 * the list of {@link Paragraph}s to fix
544 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
545 boolean space
= false;
547 for (int i
= 0; i
< paras
.size(); i
++) {
548 Paragraph para
= paras
.get(i
);
549 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
550 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
552 if (i
> 0 && space
&& thisBrk
) {
555 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
564 // Remove blank/brk at start
566 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
567 0).getType() == ParagraphType
.BREAK
)) {
571 // Remove blank/brk at end
572 int last
= paras
.size() - 1;
574 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
575 .get(last
).getType() == ParagraphType
.BREAK
)) {