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 itself (not its
28 class BasicSupportPara
{
30 private static char openQuote
= Instance
.getTrans().getCharacter(
31 StringId
.OPEN_SINGLE_QUOTE
);
32 private static char closeQuote
= Instance
.getTrans().getCharacter(
33 StringId
.CLOSE_SINGLE_QUOTE
);
34 private static char openDoubleQuote
= Instance
.getTrans().getCharacter(
35 StringId
.OPEN_DOUBLE_QUOTE
);
36 private static char closeDoubleQuote
= Instance
.getTrans().getCharacter(
37 StringId
.CLOSE_DOUBLE_QUOTE
);
40 * Create a {@link Chapter} object from the given information, formatting
41 * the content as it should be.
44 * the linked {@link BasicSupport}
46 * the source of the story
54 * the optional progress reporter
56 * TRUE if the input content is in HTML mode
58 * @return the {@link Chapter}
61 * in case of I/O error
63 public static Chapter
makeChapter(BasicSupport support
, URL source
,
64 int number
, String name
, String content
, boolean html
, Progress pg
)
66 // Chapter name: process it correctly, then remove the possible
67 // redundant "Chapter x: " in front of it, or "-" (as in
68 // "Chapter 5: - Fun!" after the ": " was automatically added)
69 String chapterName
= BasicSupportPara
.processPara(name
, false)
71 for (String lang
: Instance
.getConfig().getList(Config
.CONF_CHAPTER
)) {
72 String chapterWord
= Instance
.getConfig().getStringX(
73 Config
.CONF_CHAPTER
, lang
);
74 if (chapterName
.startsWith(chapterWord
)) {
75 chapterName
= chapterName
.substring(chapterWord
.length())
81 if (chapterName
.startsWith(Integer
.toString(number
))) {
82 chapterName
= chapterName
.substring(
83 Integer
.toString(number
).length()).trim();
86 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
87 chapterName
= chapterName
.substring(1).trim();
91 Chapter chap
= new Chapter(number
, chapterName
);
93 if (content
!= null) {
94 List
<Paragraph
> paras
= makeParagraphs(support
, source
, content
,
97 for (Paragraph para
: paras
) {
98 words
+= para
.getWords();
100 chap
.setParagraphs(paras
);
101 chap
.setWords(words
);
108 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
109 * and requotify them (i.e., separate them into QUOTE paragraphs and other
110 * paragraphs (quotes or not)).
113 * the paragraph to requotify (not necessarily a quote)
115 * TRUE if the input content is in HTML mode
117 * @return the correctly (or so we hope) quotified paragraphs
119 private static List
<Paragraph
> requotify(Paragraph para
, boolean html
) {
120 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
122 if (para
.getType() == ParagraphType
.QUOTE
123 && para
.getContent().length() > 2) {
124 String line
= para
.getContent();
125 boolean singleQ
= line
.startsWith("" + openQuote
);
126 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
128 // Do not try when more than one quote at a time
129 // (some stories are not easily readable if we do)
131 && line
.indexOf(closeQuote
, 1) < line
132 .lastIndexOf(closeQuote
)) {
137 && line
.indexOf(closeDoubleQuote
, 1) < line
138 .lastIndexOf(closeDoubleQuote
)) {
144 if (!singleQ
&& !doubleQ
) {
145 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
146 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
149 char open
= singleQ ? openQuote
: openDoubleQuote
;
150 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
153 boolean inQuote
= false;
155 for (char car
: line
.toCharArray()) {
158 } else if (car
== close
) {
160 } else if (car
== '.' && !inQuote
) {
168 String rest
= line
.substring(posDot
+ 1).trim();
169 line
= line
.substring(0, posDot
+ 1).trim();
171 for (char car
: line
.toCharArray()) {
176 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
177 if (!rest
.isEmpty()) {
178 newParas
.addAll(requotify(processPara(rest
, html
), html
));
192 * Process a {@link Paragraph} from a raw line of text.
194 * Will also fix quotes and HTML encoding if needed.
199 * TRUE if the input content is in HTML mode
201 * @return the processed {@link Paragraph}
203 private static Paragraph
processPara(String line
, boolean html
) {
205 line
= StringUtils
.unhtml(line
).trim();
207 boolean space
= true;
209 boolean quote
= false;
210 boolean tentativeCloseQuote
= false;
215 StringBuilder builder
= new StringBuilder();
216 for (char car
: line
.toCharArray()) {
219 // dash, ndash and mdash: - – —
220 // currently: always use mdash
221 builder
.append(dashCount
== 1 ?
'-' : '—');
226 if (tentativeCloseQuote
) {
227 tentativeCloseQuote
= false;
228 if (Character
.isLetterOrDigit(car
)) {
231 // handle double-single quotes as double quotes
233 builder
.append(closeDoubleQuote
);
237 builder
.append(closeQuote
);
242 case ' ': // note: unbreakable space
245 case '\n': // just in case
246 case '\r': // just in case
247 if (builder
.length() > 0
248 && builder
.charAt(builder
.length() - 1) != ' ') {
255 if (space
|| (brk
&& quote
)) {
257 // handle double-single quotes as double quotes
259 builder
.deleteCharAt(builder
.length() - 1);
260 builder
.append(openDoubleQuote
);
262 builder
.append(openQuote
);
264 } else if (prev
== ' ' || prev
== car
) {
265 // handle double-single quotes as double quotes
267 builder
.deleteCharAt(builder
.length() - 1);
268 builder
.append(openDoubleQuote
);
270 builder
.append(openQuote
);
273 // it is a quote ("I'm off") or a 'quote' ("This
274 // 'good' restaurant"...)
275 tentativeCloseQuote
= true;
280 if (space
|| (brk
&& quote
)) {
282 builder
.append(openDoubleQuote
);
283 } else if (prev
== ' ') {
284 builder
.append(openDoubleQuote
);
286 builder
.append(closeDoubleQuote
);
320 if (space
|| (brk
&& quote
)) {
322 builder
.append(openQuote
);
324 // handle double-single quotes as double quotes
326 builder
.deleteCharAt(builder
.length() - 1);
327 builder
.append(openDoubleQuote
);
329 builder
.append(openQuote
);
343 // handle double-single quotes as double quotes
345 builder
.deleteCharAt(builder
.length() - 1);
346 builder
.append(closeDoubleQuote
);
348 builder
.append(closeQuote
);
357 if (space
|| (brk
&& quote
)) {
359 builder
.append(openDoubleQuote
);
361 builder
.append(openDoubleQuote
);
374 builder
.append(closeDoubleQuote
);
387 if (tentativeCloseQuote
) {
388 tentativeCloseQuote
= false;
389 builder
.append(closeQuote
);
392 line
= builder
.toString().trim();
394 ParagraphType type
= ParagraphType
.NORMAL
;
396 type
= ParagraphType
.BLANK
;
398 type
= ParagraphType
.BREAK
;
400 type
= ParagraphType
.QUOTE
;
403 return new Paragraph(type
, line
, words
);
407 * Convert the given content into {@link Paragraph}s.
410 * the linked {@link BasicSupport}
412 * the source URL of the story
414 * the textual content
416 * TRUE if the input content is in HTML mode
418 * the optional progress reporter
420 * @return the {@link Paragraph}s
422 * @throws IOException
423 * in case of I/O error
425 private static List
<Paragraph
> makeParagraphs(BasicSupport support
,
426 URL source
, String content
, boolean html
, Progress pg
)
433 // Special <HR> processing:
434 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
438 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
440 if (content
!= null && !content
.trim().isEmpty()) {
442 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
443 pg
.setMinMax(0, tab
.length
);
445 for (String line
: tab
) {
446 if (line
.startsWith("[") && line
.endsWith("]")) {
447 pg
.setName("Extracting image " + i
);
449 paras
.add(makeParagraph(support
, source
, line
.trim(), html
));
453 List
<String
> lines
= new ArrayList
<String
>();
454 BufferedReader buff
= null;
456 buff
= new BufferedReader(
457 new InputStreamReader(new ByteArrayInputStream(
458 content
.getBytes("UTF-8")), "UTF-8"));
459 for (String line
= buff
.readLine(); line
!= null; line
= buff
461 lines
.add(line
.trim());
469 pg
.setMinMax(0, lines
.size());
471 for (String line
: lines
) {
472 if (line
.startsWith("[") && line
.endsWith("]")) {
473 pg
.setName("Extracting image " + i
);
475 paras
.add(makeParagraph(support
, source
, line
, html
));
483 // Check quotes for "bad" format
484 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
485 for (Paragraph para
: paras
) {
486 newParas
.addAll(BasicSupportPara
.requotify(para
, html
));
490 // Remove double blanks/brks
491 fixBlanksBreaks(paras
);
498 * Convert the given line into a single {@link Paragraph}.
501 * the linked {@link BasicSupport}
503 * the source URL of the story
505 * the textual content of the paragraph
507 * TRUE if the input content is in HTML mode
509 * @return the {@link Paragraph}
511 private static Paragraph
makeParagraph(BasicSupport support
, URL source
,
512 String line
, boolean html
) {
514 if (line
.startsWith("[") && line
.endsWith("]")) {
515 image
= BasicSupportHelper
.getImage(support
, source
, line
516 .substring(1, line
.length() - 1).trim());
520 return new Paragraph(image
);
523 return BasicSupportPara
.processPara(line
, html
);
527 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
528 * those {@link Paragraph}s.
530 * The resulting list will not contain a starting or trailing blank/break
531 * nor 2 blanks or breaks following each other.
534 * the list of {@link Paragraph}s to fix
536 private static void fixBlanksBreaks(List
<Paragraph
> paras
) {
537 boolean space
= false;
539 for (int i
= 0; i
< paras
.size(); i
++) {
540 Paragraph para
= paras
.get(i
);
541 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
542 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
544 if (i
> 0 && space
&& thisBrk
) {
547 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
556 // Remove blank/brk at start
558 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
559 0).getType() == ParagraphType
.BREAK
)) {
563 // Remove blank/brk at end
564 int last
= paras
.size() - 1;
566 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
567 .get(last
).getType() == ParagraphType
.BREAK
)) {