1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.BufferedReader
;
5 import java
.io
.ByteArrayInputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.InputStreamReader
;
10 import java
.net
.MalformedURLException
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Date
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
17 import java
.util
.Map
.Entry
;
18 import java
.util
.Scanner
;
20 import be
.nikiroo
.fanfix
.Instance
;
21 import be
.nikiroo
.fanfix
.bundles
.Config
;
22 import be
.nikiroo
.fanfix
.bundles
.StringId
;
23 import be
.nikiroo
.fanfix
.data
.Chapter
;
24 import be
.nikiroo
.fanfix
.data
.MetaData
;
25 import be
.nikiroo
.fanfix
.data
.Paragraph
;
26 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
27 import be
.nikiroo
.fanfix
.data
.Story
;
28 import be
.nikiroo
.utils
.IOUtils
;
29 import be
.nikiroo
.utils
.Progress
;
30 import be
.nikiroo
.utils
.StringUtils
;
33 * This class is the base class used by the other support classes. It can be
34 * used outside of this package, and have static method that you can use to get
35 * access to the correct support class.
37 * It will be used with 'resources' (usually web pages or files).
41 public abstract class BasicSupport
{
43 * The supported input types for which we can get a {@link BasicSupport}
48 public enum SupportType
{
49 /** EPUB files created with this program */
51 /** Pure text file with some rules */
53 /** TEXT but with associated .info file */
55 /** My Little Pony fanfictions */
57 /** Fanfictions from a lot of different universes */
59 /** Website with lots of Mangas */
61 /** Furry website with comics support */
63 /** Furry website with stories */
65 /** Comics and images groups, mostly but not only NSFW */
73 * A description of this support type (more information than the
74 * {@link BasicSupport#getSourceName()}).
76 * @return the description
78 public String
getDesc() {
79 String desc
= Instance
.getTrans().getStringX(StringId
.INPUT_DESC
,
83 desc
= Instance
.getTrans().getString(StringId
.INPUT_DESC
, this);
90 * The name of this support type (a short version).
94 public String
getSourceName() {
95 BasicSupport support
= BasicSupport
.getSupport(this);
96 if (support
!= null) {
97 return support
.getSourceName();
104 public String
toString() {
105 return super.toString().toLowerCase();
109 * Call {@link SupportType#valueOf(String)} after conversion to upper
113 * the possible type name
115 * @return NULL or the type
117 public static SupportType
valueOfUC(String typeName
) {
118 return SupportType
.valueOf(typeName
== null ?
null : typeName
123 * Call {@link SupportType#valueOf(String)} after conversion to upper
124 * case but return NULL for NULL instead of raising exception.
127 * the possible type name
129 * @return NULL or the type
131 public static SupportType
valueOfNullOkUC(String typeName
) {
132 if (typeName
== null) {
136 return SupportType
.valueOfUC(typeName
);
140 * Call {@link SupportType#valueOf(String)} after conversion to upper
141 * case but return NULL in case of error instead of raising an
145 * the possible type name
147 * @return NULL or the type
149 public static SupportType
valueOfAllOkUC(String typeName
) {
151 return SupportType
.valueOfUC(typeName
);
152 } catch (Exception e
) {
158 private InputStream in
;
159 private SupportType type
;
160 private URL currentReferer
; // with only one 'r', as in 'HTTP'...
163 private char openQuote
= Instance
.getTrans().getCharacter(
164 StringId
.OPEN_SINGLE_QUOTE
);
165 private char closeQuote
= Instance
.getTrans().getCharacter(
166 StringId
.CLOSE_SINGLE_QUOTE
);
167 private char openDoubleQuote
= Instance
.getTrans().getCharacter(
168 StringId
.OPEN_DOUBLE_QUOTE
);
169 private char closeDoubleQuote
= Instance
.getTrans().getCharacter(
170 StringId
.CLOSE_DOUBLE_QUOTE
);
173 * The name of this support class.
177 protected abstract String
getSourceName();
180 * Check if the given resource is supported by this {@link BasicSupport}.
183 * the resource to check for
185 * @return TRUE if it is
187 protected abstract boolean supports(URL url
);
190 * Return TRUE if the support will return HTML encoded content values for
191 * the chapters content.
193 * @return TRUE for HTML
195 protected abstract boolean isHtml();
198 * Return the {@link MetaData} of this story.
201 * the source of the story
203 * the input (the main resource)
205 * @return the associated {@link MetaData}
207 * @throws IOException
208 * in case of I/O error
210 protected abstract MetaData
getMeta(URL source
, InputStream in
)
214 * Return the story description.
217 * the source of the story
219 * the input (the main resource)
221 * @return the description
223 * @throws IOException
224 * in case of I/O error
226 protected abstract String
getDesc(URL source
, InputStream in
)
230 * Return the list of chapters (name and resource).
233 * the source of the story
235 * the input (the main resource)
237 * the optional progress reporter
239 * @return the chapters
241 * @throws IOException
242 * in case of I/O error
244 protected abstract List
<Entry
<String
, URL
>> getChapters(URL source
,
245 InputStream in
, Progress pg
) throws IOException
;
248 * Return the content of the chapter (possibly HTML encoded, if
249 * {@link BasicSupport#isHtml()} is TRUE).
252 * the source of the story
254 * the input (the main resource)
258 * the optional progress reporter
260 * @return the content
262 * @throws IOException
263 * in case of I/O error
265 protected abstract String
getChapterContent(URL source
, InputStream in
,
266 int number
, Progress pg
) throws IOException
;
269 * Log into the support (can be a no-op depending upon the support).
271 * @throws IOException
272 * in case of I/O error
274 public void login() throws IOException
{
279 * Return the list of cookies (values included) that must be used to
280 * correctly fetch the resources.
282 * You are expected to call the super method implementation if you override
285 * @return the cookies
287 * @throws IOException
288 * in case of I/O error
290 public Map
<String
, String
> getCookies() throws IOException
{
291 return new HashMap
<String
, String
>();
295 * Return the canonical form of the main {@link URL}.
298 * the source {@link URL}
300 * @return the canonical form of this {@link URL}
302 * @throws IOException
303 * in case of I/O error
305 public URL
getCanonicalUrl(URL source
) throws IOException
{
310 * Process the given story resource into a partially filled {@link Story}
311 * object containing the name and metadata, except for the description.
316 * @return the {@link Story}
318 * @throws IOException
319 * in case of I/O error
321 public Story
processMeta(URL url
) throws IOException
{
322 return processMeta(url
, true, false, null);
326 * Process the given story resource into a partially filled {@link Story}
327 * object containing the name and metadata.
332 * close "this" and "in" when done
334 * retrieve the description of the story, or not
336 * the optional progress reporter
338 * @return the {@link Story}
340 * @throws IOException
341 * in case of I/O error
343 protected Story
processMeta(URL url
, boolean close
, boolean getDesc
,
344 Progress pg
) throws IOException
{
348 pg
.setMinMax(0, 100);
354 url
= getCanonicalUrl(url
);
356 setCurrentReferer(url
);
364 preprocess(url
, getInput());
367 Story story
= new Story();
368 MetaData meta
= getMeta(url
, getInput());
369 if (meta
.getCreationDate() == null
370 || meta
.getCreationDate().isEmpty()) {
371 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
377 if (meta
!= null && meta
.getCover() == null) {
378 meta
.setCover(getDefaultCover(meta
.getSubject()));
384 String descChapterName
= Instance
.getTrans().getString(
385 StringId
.DESCRIPTION
);
386 story
.getMeta().setResume(
387 makeChapter(url
, 0, descChapterName
,
388 getDesc(url
, getInput()), null));
397 } catch (IOException e
) {
406 setCurrentReferer(null);
411 * Process the given story resource into a fully filled {@link Story}
417 * the optional progress reporter
419 * @return the {@link Story}
421 * @throws IOException
422 * in case of I/O error
424 public Story
process(URL url
, Progress pg
) throws IOException
{
428 pg
.setMinMax(0, 100);
431 url
= getCanonicalUrl(url
);
434 Progress pgMeta
= new Progress();
435 pg
.addProgress(pgMeta
, 10);
436 Story story
= processMeta(url
, false, true, pgMeta
);
437 if (!pgMeta
.isDone()) {
438 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
446 pg
.setName("Retrieving " + story
.getMeta().getTitle());
448 setCurrentReferer(url
);
450 Progress pgGetChapters
= new Progress();
451 pg
.addProgress(pgGetChapters
, 10);
452 story
.setChapters(new ArrayList
<Chapter
>());
453 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
455 if (!pgGetChapters
.isDone()) {
456 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
459 if (chapters
!= null) {
460 Progress pgChaps
= new Progress("Extracting chapters", 0,
461 chapters
.size() * 300);
462 pg
.addProgress(pgChaps
, 80);
466 for (Entry
<String
, URL
> chap
: chapters
) {
467 pgChaps
.setName("Extracting chapter " + i
);
468 setCurrentReferer(chap
.getValue());
469 InputStream chapIn
= Instance
.getCache().open(
470 chap
.getValue(), this, true);
471 pgChaps
.setProgress(i
* 100);
473 Progress pgGetChapterContent
= new Progress();
474 Progress pgMakeChapter
= new Progress();
475 pgChaps
.addProgress(pgGetChapterContent
, 100);
476 pgChaps
.addProgress(pgMakeChapter
, 100);
478 String content
= getChapterContent(url
, chapIn
, i
,
479 pgGetChapterContent
);
480 if (!pgGetChapterContent
.isDone()) {
481 pgGetChapterContent
.setProgress(pgGetChapterContent
485 Chapter cc
= makeChapter(url
, i
, chap
.getKey(),
486 content
, pgMakeChapter
);
487 if (!pgMakeChapter
.isDone()) {
488 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
491 words
+= cc
.getWords();
492 story
.getChapters().add(cc
);
493 if (story
.getMeta() != null) {
494 story
.getMeta().setWords(words
);
503 pgChaps
.setName("Extracting chapters");
513 } catch (IOException e
) {
521 setCurrentReferer(null);
530 public SupportType
getType() {
535 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
536 * the current {@link URL} we work on.
538 * @return the referer
540 public URL
getCurrentReferer() {
541 return currentReferer
;
545 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
546 * the current {@link URL} we work on.
548 * @param currentReferer
551 protected void setCurrentReferer(URL currentReferer
) {
552 this.currentReferer
= currentReferer
;
563 protected BasicSupport
setType(SupportType type
) {
569 * Prepare the support if needed before processing.
572 * the source of the story
574 * the input (the main resource)
576 * @throws IOException
579 protected void preprocess(URL source
, InputStream in
) throws IOException
{
583 * Now that we have processed the {@link Story}, close the resources if any.
585 * @throws IOException
588 protected void close() throws IOException
{
592 * Create a {@link Chapter} object from the given information, formatting
593 * the content as it should be.
596 * the source of the story
602 * the chapter content
604 * the optional progress reporter
606 * @return the {@link Chapter}
608 * @throws IOException
609 * in case of I/O error
611 protected Chapter
makeChapter(URL source
, int number
, String name
,
612 String content
, Progress pg
) throws IOException
{
613 // Chapter name: process it correctly, then remove the possible
614 // redundant "Chapter x: " in front of it, or "-" (as in
615 // "Chapter 5: - Fun!" after the ": " was automatically added)
616 String chapterName
= processPara(name
).getContent().trim();
617 for (String lang
: Instance
.getConfig().getString(Config
.CHAPTER
)
619 String chapterWord
= Instance
.getConfig().getStringX(
620 Config
.CHAPTER
, lang
);
621 if (chapterName
.startsWith(chapterWord
)) {
622 chapterName
= chapterName
.substring(chapterWord
.length())
628 if (chapterName
.startsWith(Integer
.toString(number
))) {
629 chapterName
= chapterName
.substring(
630 Integer
.toString(number
).length()).trim();
633 while (chapterName
.startsWith(":") || chapterName
.startsWith("-")) {
634 chapterName
= chapterName
.substring(1).trim();
638 Chapter chap
= new Chapter(number
, chapterName
);
640 if (content
!= null) {
641 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
643 for (Paragraph para
: paras
) {
644 words
+= para
.getWords();
646 chap
.setParagraphs(paras
);
647 chap
.setWords(words
);
655 * Convert the given content into {@link Paragraph}s.
658 * the source URL of the story
660 * the textual content
662 * the optional progress reporter
664 * @return the {@link Paragraph}s
666 * @throws IOException
667 * in case of I/O error
669 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
670 Progress pg
) throws IOException
{
676 // Special <HR> processing:
677 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
681 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
683 if (content
!= null && !content
.trim().isEmpty()) {
685 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
686 pg
.setMinMax(0, tab
.length
);
688 for (String line
: tab
) {
689 if (line
.startsWith("[") && line
.endsWith("]")) {
690 pg
.setName("Extracting image " + i
);
692 paras
.add(makeParagraph(source
, line
.trim()));
697 List
<String
> lines
= new ArrayList
<String
>();
698 BufferedReader buff
= null;
700 buff
= new BufferedReader(
701 new InputStreamReader(new ByteArrayInputStream(
702 content
.getBytes("UTF-8")), "UTF-8"));
703 for (String line
= buff
.readLine(); line
!= null; line
= buff
705 lines
.add(line
.trim());
713 pg
.setMinMax(0, lines
.size());
715 for (String line
: lines
) {
716 if (line
.startsWith("[") && line
.endsWith("]")) {
717 pg
.setName("Extracting image " + i
);
719 paras
.add(makeParagraph(source
, line
));
725 // Check quotes for "bad" format
726 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
727 for (Paragraph para
: paras
) {
728 newParas
.addAll(requotify(para
));
732 // Remove double blanks/brks
733 fixBlanksBreaks(paras
);
740 * Convert the given line into a single {@link Paragraph}.
743 * the source URL of the story
745 * the textual content of the paragraph
747 * @return the {@link Paragraph}
749 private Paragraph
makeParagraph(URL source
, String line
) {
751 if (line
.startsWith("[") && line
.endsWith("]")) {
752 image
= getImageUrl(this, source
,
753 line
.substring(1, line
.length() - 1).trim());
757 return new Paragraph(image
);
759 return processPara(line
);
764 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
765 * those {@link Paragraph}s.
767 * The resulting list will not contain a starting or trailing blank/break
768 * nor 2 blanks or breaks following each other.
771 * the list of {@link Paragraph}s to fix
773 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
774 boolean space
= false;
776 for (int i
= 0; i
< paras
.size(); i
++) {
777 Paragraph para
= paras
.get(i
);
778 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
779 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
781 if (i
> 0 && space
&& thisBrk
) {
784 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
793 // Remove blank/brk at start
795 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
796 0).getType() == ParagraphType
.BREAK
)) {
800 // Remove blank/brk at end
801 int last
= paras
.size() - 1;
803 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
804 .get(last
).getType() == ParagraphType
.BREAK
)) {
810 * Get the default cover related to this subject (see <tt>.info</tt> files).
815 * @return the cover if any, or NULL
817 static BufferedImage
getDefaultCover(String subject
) {
818 if (subject
!= null && !subject
.isEmpty()
819 && Instance
.getCoverDir() != null) {
821 File fileCover
= new File(Instance
.getCoverDir(), subject
);
822 return getImage(null, fileCover
.toURI().toURL(), subject
);
823 } catch (MalformedURLException e
) {
831 * Return the list of supported image extensions.
833 * @param emptyAllowed
834 * TRUE to allow an empty extension on first place, which can be
835 * used when you may already have an extension in your input but
836 * are not sure about it
838 * @return the extensions
840 static String
[] getImageExt(boolean emptyAllowed
) {
842 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
844 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
849 * Check if the given resource can be a local image or a remote image, then
850 * refresh the cache with it if it is.
855 * the resource to check
857 * @return the image if found, or NULL
860 static BufferedImage
getImage(BasicSupport support
, URL source
, String line
) {
861 URL url
= getImageUrl(support
, source
, line
);
863 InputStream in
= null;
865 in
= Instance
.getCache().open(url
, getSupport(url
), true);
866 return IOUtils
.toImage(in
);
867 } catch (IOException e
) {
872 } catch (IOException e
) {
882 * Check if the given resource can be a local image or a remote image, then
883 * refresh the cache with it if it is.
888 * the resource to check
890 * @return the image URL if found, or NULL
893 static URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
898 if (source
!= null) {
901 String relPath
= null;
902 String absPath
= null;
904 String path
= new File(source
.getFile()).getParent();
905 relPath
= new File(new File(path
), line
.trim())
907 } catch (Exception e
) {
908 // Cannot be converted to path (one possibility to take
909 // into account: absolute path on Windows)
912 absPath
= new File(line
.trim()).getAbsolutePath();
913 } catch (Exception e
) {
914 // Cannot be converted to path (at all)
917 for (String ext
: getImageExt(true)) {
918 if (absPath
!= null && new File(absPath
+ ext
).exists()) {
919 url
= new File(absPath
+ ext
).toURI().toURL();
920 } else if (relPath
!= null
921 && new File(relPath
+ ext
).exists()) {
922 url
= new File(relPath
+ ext
).toURI().toURL();
925 } catch (Exception e
) {
926 // Should not happen since we control the correct arguments
933 for (String ext
: getImageExt(true)) {
934 if (Instance
.getCache().check(new URL(line
+ ext
))) {
935 url
= new URL(line
+ ext
);
942 for (String ext
: getImageExt(true)) {
944 url
= new URL(line
+ ext
);
945 Instance
.getCache().refresh(url
, support
, true);
947 } catch (IOException e
) {
948 // no image with this ext
953 } catch (MalformedURLException e
) {
958 // refresh the cached file
961 Instance
.getCache().refresh(url
, support
, true);
962 } catch (IOException e
) {
963 // woops, broken image
973 * Open the input file that will be used through the support.
976 * the source {@link URL}
978 * @return the {@link InputStream}
980 * @throws IOException
981 * in case of I/O error
983 protected InputStream
openInput(URL source
) throws IOException
{
984 return Instance
.getCache().open(source
, this, false);
988 * Reset the given {@link InputStream} and return it.
991 * the {@link InputStream} to reset
993 * @return the same {@link InputStream} after reset
995 protected InputStream
reset(InputStream in
) {
998 } catch (IOException e
) {
1004 * Reset then return {@link BasicSupport#in}.
1006 * @return {@link BasicSupport#in}
1008 protected InputStream
getInput() {
1013 * Fix the author name if it is prefixed with some "by" {@link String}.
1016 * the author with a possible prefix
1018 * @return the author without prefixes
1020 protected String
fixAuthor(String author
) {
1021 if (author
!= null) {
1022 for (String suffix
: new String
[] { " ", ":" }) {
1023 for (String byString
: Instance
.getConfig()
1024 .getString(Config
.BYS
).split(",")) {
1026 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
1027 author
= author
.substring(byString
.length()).trim();
1032 // Special case (without suffix):
1033 if (author
.startsWith("©")) {
1034 author
= author
.substring(1);
1042 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
1043 * and requotify them (i.e., separate them into QUOTE paragraphs and other
1044 * paragraphs (quotes or not)).
1047 * the paragraph to requotify (not necessarily a quote)
1049 * @return the correctly (or so we hope) quotified paragraphs
1051 protected List
<Paragraph
> requotify(Paragraph para
) {
1052 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
1054 if (para
.getType() == ParagraphType
.QUOTE
1055 && para
.getContent().length() > 2) {
1056 String line
= para
.getContent();
1057 boolean singleQ
= line
.startsWith("" + openQuote
);
1058 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
1060 // Do not try when more than one quote at a time
1061 // (some stories are not easily readable if we do)
1063 && line
.indexOf(closeQuote
, 1) < line
1064 .lastIndexOf(closeQuote
)) {
1069 && line
.indexOf(closeDoubleQuote
, 1) < line
1070 .lastIndexOf(closeDoubleQuote
)) {
1076 if (!singleQ
&& !doubleQ
) {
1077 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
1078 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
1081 char open
= singleQ ? openQuote
: openDoubleQuote
;
1082 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
1085 boolean inQuote
= false;
1087 for (char car
: line
.toCharArray()) {
1090 } else if (car
== close
) {
1092 } else if (car
== '.' && !inQuote
) {
1100 String rest
= line
.substring(posDot
+ 1).trim();
1101 line
= line
.substring(0, posDot
+ 1).trim();
1103 for (char car
: line
.toCharArray()) {
1108 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
1109 if (!rest
.isEmpty()) {
1110 newParas
.addAll(requotify(processPara(rest
)));
1124 * Process a {@link Paragraph} from a raw line of text.
1126 * Will also fix quotes and HTML encoding if needed.
1131 * @return the processed {@link Paragraph}
1133 protected Paragraph
processPara(String line
) {
1134 line
= ifUnhtml(line
).trim();
1136 boolean space
= true;
1138 boolean quote
= false;
1139 boolean tentativeCloseQuote
= false;
1144 StringBuilder builder
= new StringBuilder();
1145 for (char car
: line
.toCharArray()) {
1147 if (dashCount
> 0) {
1148 // dash, ndash and mdash: - – —
1149 // currently: always use mdash
1150 builder
.append(dashCount
== 1 ?
'-' : '—');
1155 if (tentativeCloseQuote
) {
1156 tentativeCloseQuote
= false;
1157 if (Character
.isLetterOrDigit(car
)) {
1158 builder
.append("'");
1160 // handle double-single quotes as double quotes
1162 builder
.append(closeDoubleQuote
);
1165 builder
.append(closeQuote
);
1171 case ' ': // note: unbreakable space
1174 case '\n': // just in case
1175 case '\r': // just in case
1176 if (builder
.length() > 0
1177 && builder
.charAt(builder
.length() - 1) != ' ') {
1180 builder
.append(' ');
1184 if (space
|| (brk
&& quote
)) {
1186 // handle double-single quotes as double quotes
1188 builder
.deleteCharAt(builder
.length() - 1);
1189 builder
.append(openDoubleQuote
);
1191 builder
.append(openQuote
);
1193 } else if (prev
== ' ' || prev
== car
) {
1194 // handle double-single quotes as double quotes
1196 builder
.deleteCharAt(builder
.length() - 1);
1197 builder
.append(openDoubleQuote
);
1199 builder
.append(openQuote
);
1202 // it is a quote ("I'm off") or a 'quote' ("This
1203 // 'good' restaurant"...)
1204 tentativeCloseQuote
= true;
1209 if (space
|| (brk
&& quote
)) {
1211 builder
.append(openDoubleQuote
);
1212 } else if (prev
== ' ') {
1213 builder
.append(openDoubleQuote
);
1215 builder
.append(closeDoubleQuote
);
1240 builder
.append(car
);
1249 if (space
|| (brk
&& quote
)) {
1251 builder
.append(openQuote
);
1253 // handle double-single quotes as double quotes
1255 builder
.deleteCharAt(builder
.length() - 1);
1256 builder
.append(openDoubleQuote
);
1258 builder
.append(openQuote
);
1272 // handle double-single quotes as double quotes
1274 builder
.deleteCharAt(builder
.length() - 1);
1275 builder
.append(closeDoubleQuote
);
1277 builder
.append(closeQuote
);
1286 if (space
|| (brk
&& quote
)) {
1288 builder
.append(openDoubleQuote
);
1290 builder
.append(openDoubleQuote
);
1303 builder
.append(closeDoubleQuote
);
1309 builder
.append(car
);
1316 if (tentativeCloseQuote
) {
1317 tentativeCloseQuote
= false;
1318 builder
.append(closeQuote
);
1321 line
= builder
.toString().trim();
1323 ParagraphType type
= ParagraphType
.NORMAL
;
1325 type
= ParagraphType
.BLANK
;
1327 type
= ParagraphType
.BREAK
;
1329 type
= ParagraphType
.QUOTE
;
1332 return new Paragraph(type
, line
, words
);
1336 * Remove the HTML from the input <b>if</b> {@link BasicSupport#isHtml()} is
1342 * @return the no html version if needed
1344 private String
ifUnhtml(String input
) {
1345 if (isHtml() && input
!= null) {
1346 return StringUtils
.unhtml(input
);
1353 * Return a {@link BasicSupport} implementation supporting the given
1354 * resource if possible.
1357 * the story resource
1359 * @return an implementation that supports it, or NULL
1361 public static BasicSupport
getSupport(URL url
) {
1366 // TEXT and INFO_TEXT always support files (not URLs though)
1367 for (SupportType type
: SupportType
.values()) {
1368 if (type
!= SupportType
.TEXT
&& type
!= SupportType
.INFO_TEXT
) {
1369 BasicSupport support
= getSupport(type
);
1370 if (support
!= null && support
.supports(url
)) {
1376 for (SupportType type
: new SupportType
[] { SupportType
.INFO_TEXT
,
1377 SupportType
.TEXT
}) {
1378 BasicSupport support
= getSupport(type
);
1379 if (support
!= null && support
.supports(url
)) {
1388 * Return a {@link BasicSupport} implementation supporting the given type.
1393 * @return an implementation that supports it, or NULL
1395 public static BasicSupport
getSupport(SupportType type
) {
1398 return new Epub().setType(type
);
1400 return new InfoText().setType(type
);
1402 return new Fimfiction().setType(type
);
1404 return new Fanfiction().setType(type
);
1406 return new Text().setType(type
);
1408 return new MangaFox().setType(type
);
1410 return new E621().setType(type
);
1412 return new YiffStar().setType(type
);
1414 return new EHentai().setType(type
);
1416 return new Cbz().setType(type
);
1418 return new Html().setType(type
);
1425 * Return the first line from the given input which correspond to the given
1431 * a string that must be found inside the target line (also
1432 * supports "^" at start to say "only if it starts with" the
1434 * @param relativeLine
1435 * the line to return based upon the target line position (-1 =
1436 * the line before, 0 = the target line...)
1440 static String
getLine(InputStream in
, String needle
, int relativeLine
) {
1441 return getLine(in
, needle
, relativeLine
, true);
1445 * Return a line from the given input which correspond to the given
1451 * a string that must be found inside the target line (also
1452 * supports "^" at start to say "only if it starts with" the
1454 * @param relativeLine
1455 * the line to return based upon the target line position (-1 =
1456 * the line before, 0 = the target line...)
1458 * takes the first result (as opposed to the last one, which will
1459 * also always spend the input)
1463 static String
getLine(InputStream in
, String needle
, int relativeLine
,
1469 } catch (IOException e
) {
1473 List
<String
> lines
= new ArrayList
<String
>();
1474 @SuppressWarnings("resource")
1475 Scanner scan
= new Scanner(in
, "UTF-8");
1477 scan
.useDelimiter("\\n");
1478 while (scan
.hasNext()) {
1479 lines
.add(scan
.next());
1482 if (needle
.startsWith("^")) {
1483 if (lines
.get(lines
.size() - 1).startsWith(
1484 needle
.substring(1))) {
1485 index
= lines
.size() - 1;
1489 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1490 index
= lines
.size() - 1;
1495 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1496 rep
= lines
.get(index
+ relativeLine
);
1507 * Return the text between the key and the endKey (and optional subKey can
1508 * be passed, in this case we will look for the key first, then take the
1509 * text between the subKey and the endKey).
1511 * Will only match the first line with the given key if more than one are
1512 * possible. Which also means that if the subKey or endKey is not found on
1513 * that line, NULL will be returned.
1518 * the key to match (also supports "^" at start to say
1519 * "only if it starts with" the key)
1521 * the sub key or NULL if none
1523 * the end key or NULL for "up to the end"
1524 * @return the text or NULL if not found
1526 static String
getKeyLine(InputStream in
, String key
, String subKey
,
1528 String result
= null;
1530 String line
= getLine(in
, key
, 0);
1531 if (line
!= null && line
.contains(key
)) {
1532 line
= line
.substring(line
.indexOf(key
) + key
.length());
1533 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1534 if (subKey
!= null) {
1535 line
= line
.substring(line
.indexOf(subKey
)
1538 if (endKey
== null || line
.contains(endKey
)) {
1539 if (endKey
!= null) {
1540 line
= line
.substring(0, line
.indexOf(endKey
));