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.toUpperCase())}.
112 * the possible type name
114 * @return NULL or the type
116 public static SupportType
valueOfUC(String typeName
) {
117 return SupportType
.valueOf(typeName
== null ?
null : typeName
122 * Call {@link SupportType#valueOf(String.toUpperCase())} but return
123 * NULL for NULL instead of raising exception.
126 * the possible type name
128 * @return NULL or the type
130 public static SupportType
valueOfNullOkUC(String typeName
) {
131 if (typeName
== null) {
135 return SupportType
.valueOfUC(typeName
);
139 * Call {@link SupportType#valueOf(String.toUpperCase())} but return
140 * NULL in case of error instead of raising an exception.
143 * the possible type name
145 * @return NULL or the type
147 public static SupportType
valueOfAllOkUC(String typeName
) {
149 return SupportType
.valueOfUC(typeName
);
150 } catch (Exception e
) {
156 private InputStream in
;
157 private SupportType type
;
158 private URL currentReferer
; // with only one 'r', as in 'HTTP'...
161 private char openQuote
= Instance
.getTrans().getCharacter(
162 StringId
.OPEN_SINGLE_QUOTE
);
163 private char closeQuote
= Instance
.getTrans().getCharacter(
164 StringId
.CLOSE_SINGLE_QUOTE
);
165 private char openDoubleQuote
= Instance
.getTrans().getCharacter(
166 StringId
.OPEN_DOUBLE_QUOTE
);
167 private char closeDoubleQuote
= Instance
.getTrans().getCharacter(
168 StringId
.CLOSE_DOUBLE_QUOTE
);
171 * The name of this support class.
175 protected abstract String
getSourceName();
178 * Check if the given resource is supported by this {@link BasicSupport}.
181 * the resource to check for
183 * @return TRUE if it is
185 protected abstract boolean supports(URL url
);
188 * Return TRUE if the support will return HTML encoded content values for
189 * the chapters content.
191 * @return TRUE for HTML
193 protected abstract boolean isHtml();
195 protected abstract MetaData
getMeta(URL source
, InputStream in
)
199 * Return the story description.
202 * the source of the story
204 * the input (the main resource)
206 * @return the description
208 * @throws IOException
209 * in case of I/O error
211 protected abstract String
getDesc(URL source
, InputStream in
)
215 * Return the list of chapters (name and resource).
218 * the source of the story
220 * the input (the main resource)
222 * the optional progress reporter
224 * @return the chapters
226 * @throws IOException
227 * in case of I/O error
229 protected abstract List
<Entry
<String
, URL
>> getChapters(URL source
,
230 InputStream in
, Progress pg
) throws IOException
;
233 * Return the content of the chapter (possibly HTML encoded, if
234 * {@link BasicSupport#isHtml()} is TRUE).
237 * the source of the story
239 * the input (the main resource)
243 * the optional progress reporter
245 * @return the content
247 * @throws IOException
248 * in case of I/O error
250 protected abstract String
getChapterContent(URL source
, InputStream in
,
251 int number
, Progress pg
) throws IOException
;
254 * Log into the support (can be a no-op depending upon the support).
256 * @throws IOException
257 * in case of I/O error
259 public void login() throws IOException
{
264 * Return the list of cookies (values included) that must be used to
265 * correctly fetch the resources.
267 * You are expected to call the super method implementation if you override
270 * @return the cookies
272 * @throws IOException
273 * in case of I/O error
275 public Map
<String
, String
> getCookies() throws IOException
{
276 return new HashMap
<String
, String
>();
280 * Return the canonical form of the main {@link URL}.
283 * the source {@link URL}
285 * @return the canonical form of this {@link URL}
287 * @throws IOException
288 * in case of I/O error
290 public URL
getCanonicalUrl(URL source
) throws IOException
{
295 * Process the given story resource into a partially filled {@link Story}
296 * object containing the name and metadata, except for the description.
301 * @return the {@link Story}
303 * @throws IOException
304 * in case of I/O error
306 public Story
processMeta(URL url
) throws IOException
{
307 return processMeta(url
, true, false, null);
311 * Process the given story resource into a partially filled {@link Story}
312 * object containing the name and metadata.
318 * close "this" and "in" when done
320 * the optional progress reporter
322 * @return the {@link Story}
324 * @throws IOException
325 * in case of I/O error
327 protected Story
processMeta(URL url
, boolean close
, boolean getDesc
,
328 Progress pg
) throws IOException
{
332 pg
.setMinMax(0, 100);
338 url
= getCanonicalUrl(url
);
340 setCurrentReferer(url
);
348 preprocess(url
, getInput());
351 Story story
= new Story();
352 MetaData meta
= getMeta(url
, getInput());
353 if (meta
.getCreationDate() == null
354 || meta
.getCreationDate().isEmpty()) {
355 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
361 if (meta
!= null && meta
.getCover() == null) {
362 meta
.setCover(getDefaultCover(meta
.getSubject()));
368 String descChapterName
= Instance
.getTrans().getString(
369 StringId
.DESCRIPTION
);
370 story
.getMeta().setResume(
371 makeChapter(url
, 0, descChapterName
,
372 getDesc(url
, getInput()), null));
381 } catch (IOException e
) {
390 setCurrentReferer(null);
395 * Process the given story resource into a fully filled {@link Story}
401 * the optional progress reporter
403 * @return the {@link Story}
405 * @throws IOException
406 * in case of I/O error
408 public Story
process(URL url
, Progress pg
) throws IOException
{
412 pg
.setMinMax(0, 100);
415 url
= getCanonicalUrl(url
);
418 Progress pgMeta
= new Progress();
419 pg
.addProgress(pgMeta
, 10);
420 Story story
= processMeta(url
, false, true, pgMeta
);
421 if (!pgMeta
.isDone()) {
422 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
430 pg
.setName("Retrieving " + story
.getMeta().getTitle());
432 setCurrentReferer(url
);
434 Progress pgGetChapters
= new Progress();
435 pg
.addProgress(pgGetChapters
, 10);
436 story
.setChapters(new ArrayList
<Chapter
>());
437 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
439 if (!pgGetChapters
.isDone()) {
440 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
443 if (chapters
!= null) {
444 Progress pgChaps
= new Progress("Extracting chapters", 0,
445 chapters
.size() * 300);
446 pg
.addProgress(pgChaps
, 80);
450 for (Entry
<String
, URL
> chap
: chapters
) {
451 pgChaps
.setName("Extracting chapter " + i
);
452 setCurrentReferer(chap
.getValue());
453 InputStream chapIn
= Instance
.getCache().open(
454 chap
.getValue(), this, true);
455 pgChaps
.setProgress(i
* 100);
457 Progress pgGetChapterContent
= new Progress();
458 Progress pgMakeChapter
= new Progress();
459 pgChaps
.addProgress(pgGetChapterContent
, 100);
460 pgChaps
.addProgress(pgMakeChapter
, 100);
462 String content
= getChapterContent(url
, chapIn
, i
,
463 pgGetChapterContent
);
464 if (!pgGetChapterContent
.isDone()) {
465 pgGetChapterContent
.setProgress(pgGetChapterContent
469 Chapter cc
= makeChapter(url
, i
, chap
.getKey(),
470 content
, pgMakeChapter
);
471 if (!pgMakeChapter
.isDone()) {
472 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
475 words
+= cc
.getWords();
476 story
.getChapters().add(cc
);
477 if (story
.getMeta() != null) {
478 story
.getMeta().setWords(words
);
487 pgChaps
.setName("Extracting chapters");
497 } catch (IOException e
) {
505 setCurrentReferer(null);
514 public SupportType
getType() {
519 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
520 * the current {@link URL} we work on.
522 * @return the referer
524 public URL
getCurrentReferer() {
525 return currentReferer
;
529 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
530 * the current {@link URL} we work on.
532 * @param currentReferer
535 protected void setCurrentReferer(URL currentReferer
) {
536 this.currentReferer
= currentReferer
;
547 protected BasicSupport
setType(SupportType type
) {
553 * Prepare the support if needed before processing.
556 * the source of the story
558 * the input (the main resource)
560 * @throws IOException
563 protected void preprocess(URL source
, InputStream in
) throws IOException
{
567 * Now that we have processed the {@link Story}, close the resources if any.
569 * @throws IOException
572 protected void close() throws IOException
{
576 * Create a {@link Chapter} object from the given information, formatting
577 * the content as it should be.
584 * the chapter content
586 * the optional progress reporter
588 * @return the {@link Chapter}
590 * @throws IOException
591 * in case of I/O error
593 protected Chapter
makeChapter(URL source
, int number
, String name
,
594 String content
, Progress pg
) throws IOException
{
595 // Chapter name: process it correctly, then remove the possible
596 // redundant "Chapter x: " in front of it
597 String chapterName
= processPara(name
).getContent().trim();
598 for (String lang
: Instance
.getConfig().getString(Config
.CHAPTER
)
600 String chapterWord
= Instance
.getConfig().getStringX(
601 Config
.CHAPTER
, lang
);
602 if (chapterName
.startsWith(chapterWord
)) {
603 chapterName
= chapterName
.substring(chapterWord
.length())
609 if (chapterName
.startsWith(Integer
.toString(number
))) {
610 chapterName
= chapterName
.substring(
611 Integer
.toString(number
).length()).trim();
614 if (chapterName
.startsWith(":")) {
615 chapterName
= chapterName
.substring(1).trim();
619 Chapter chap
= new Chapter(number
, chapterName
);
621 if (content
!= null) {
622 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
624 for (Paragraph para
: paras
) {
625 words
+= para
.getWords();
627 chap
.setParagraphs(paras
);
628 chap
.setWords(words
);
636 * Convert the given content into {@link Paragraph}s.
639 * the source URL of the story
641 * the textual content
643 * the optional progress reporter
645 * @return the {@link Paragraph}s
647 * @throws IOException
648 * in case of I/O error
650 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
651 Progress pg
) throws IOException
{
657 // Special <HR> processing:
658 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
662 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
664 if (content
!= null && !content
.trim().isEmpty()) {
666 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
667 pg
.setMinMax(0, tab
.length
);
669 for (String line
: tab
) {
670 if (line
.startsWith("[") && line
.endsWith("]")) {
671 pg
.setName("Extracting image " + i
);
673 paras
.add(makeParagraph(source
, line
.trim()));
678 List
<String
> lines
= new ArrayList
<String
>();
679 BufferedReader buff
= null;
681 buff
= new BufferedReader(
682 new InputStreamReader(new ByteArrayInputStream(
683 content
.getBytes("UTF-8")), "UTF-8"));
684 for (String line
= buff
.readLine(); line
!= null; line
= buff
686 lines
.add(line
.trim());
694 pg
.setMinMax(0, lines
.size());
696 for (String line
: lines
) {
697 if (line
.startsWith("[") && line
.endsWith("]")) {
698 pg
.setName("Extracting image " + i
);
700 paras
.add(makeParagraph(source
, line
));
706 // Check quotes for "bad" format
707 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
708 for (Paragraph para
: paras
) {
709 newParas
.addAll(requotify(para
));
713 // Remove double blanks/brks
714 fixBlanksBreaks(paras
);
721 * Convert the given line into a single {@link Paragraph}.
724 * the source URL of the story
726 * the textual content of the paragraph
728 * @return the {@link Paragraph}
730 private Paragraph
makeParagraph(URL source
, String line
) {
732 if (line
.startsWith("[") && line
.endsWith("]")) {
733 image
= getImageUrl(this, source
,
734 line
.substring(1, line
.length() - 1).trim());
738 return new Paragraph(image
);
740 return processPara(line
);
745 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
746 * those {@link Paragraph}s.
748 * The resulting list will not contain a starting or trailing blank/break
749 * nor 2 blanks or breaks following each other.
752 * the list of {@link Paragraph}s to fix
754 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
755 boolean space
= false;
757 for (int i
= 0; i
< paras
.size(); i
++) {
758 Paragraph para
= paras
.get(i
);
759 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
760 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
762 if (i
> 0 && space
&& thisBrk
) {
765 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
774 // Remove blank/brk at start
776 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
777 0).getType() == ParagraphType
.BREAK
)) {
781 // Remove blank/brk at end
782 int last
= paras
.size() - 1;
784 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
785 .get(last
).getType() == ParagraphType
.BREAK
)) {
791 * Get the default cover related to this subject (see <tt>.info</tt> files).
796 * @return the cover if any, or NULL
798 static BufferedImage
getDefaultCover(String subject
) {
799 if (subject
!= null && !subject
.isEmpty()
800 && Instance
.getCoverDir() != null) {
802 File fileCover
= new File(Instance
.getCoverDir(), subject
);
803 return getImage(null, fileCover
.toURI().toURL(), subject
);
804 } catch (MalformedURLException e
) {
812 * Return the list of supported image extensions.
814 * @param emptyAllowed
815 * TRUE to allow an empty extension on first place, which can be
816 * used when you may already have an extension in your input but
817 * are not sure about it
819 * @return the extensions
821 static String
[] getImageExt(boolean emptyAllowed
) {
823 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
825 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
830 * Check if the given resource can be a local image or a remote image, then
831 * refresh the cache with it if it is.
836 * the resource to check
838 * @return the image if found, or NULL
841 static BufferedImage
getImage(BasicSupport support
, URL source
, String line
) {
842 URL url
= getImageUrl(support
, source
, line
);
844 InputStream in
= null;
846 in
= Instance
.getCache().open(url
, getSupport(url
), true);
847 return IOUtils
.toImage(in
);
848 } catch (IOException e
) {
853 } catch (IOException e
) {
863 * Check if the given resource can be a local image or a remote image, then
864 * refresh the cache with it if it is.
869 * the resource to check
871 * @return the image URL if found, or NULL
874 static URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
879 if (source
!= null) {
882 String relPath
= null;
883 String absPath
= null;
885 String path
= new File(source
.getFile()).getParent();
886 relPath
= new File(new File(path
), line
.trim())
888 } catch (Exception e
) {
889 // Cannot be converted to path (one possibility to take
890 // into account: absolute path on Windows)
893 absPath
= new File(line
.trim()).getAbsolutePath();
894 } catch (Exception e
) {
895 // Cannot be converted to path (at all)
898 for (String ext
: getImageExt(true)) {
899 if (absPath
!= null && new File(absPath
+ ext
).exists()) {
900 url
= new File(absPath
+ ext
).toURI().toURL();
901 } else if (relPath
!= null
902 && new File(relPath
+ ext
).exists()) {
903 url
= new File(relPath
+ ext
).toURI().toURL();
906 } catch (Exception e
) {
907 // Should not happen since we control the correct arguments
914 for (String ext
: getImageExt(true)) {
915 if (Instance
.getCache().check(new URL(line
+ ext
))) {
916 url
= new URL(line
+ ext
);
923 for (String ext
: getImageExt(true)) {
925 url
= new URL(line
+ ext
);
926 Instance
.getCache().refresh(url
, support
, true);
928 } catch (IOException e
) {
929 // no image with this ext
934 } catch (MalformedURLException e
) {
939 // refresh the cached file
942 Instance
.getCache().refresh(url
, support
, true);
943 } catch (IOException e
) {
944 // woops, broken image
954 * Open the input file that will be used through the support.
957 * the source {@link URL}
959 * @return the {@link InputStream}
961 * @throws IOException
962 * in case of I/O error
964 protected InputStream
openInput(URL source
) throws IOException
{
965 return Instance
.getCache().open(source
, this, false);
969 * Reset the given {@link InputStream} and return it.
972 * the {@link InputStream} to reset
974 * @return the same {@link InputStream} after reset
976 protected InputStream
reset(InputStream in
) {
979 } catch (IOException e
) {
985 * Reset then return {@link BasicSupport#in}.
987 * @return {@link BasicSupport#in}
989 protected InputStream
getInput() {
994 * Fix the author name if it is prefixed with some "by" {@link String}.
997 * the author with a possible prefix
999 * @return the author without prefixes
1001 protected String
fixAuthor(String author
) {
1002 if (author
!= null) {
1003 for (String suffix
: new String
[] { " ", ":" }) {
1004 for (String byString
: Instance
.getConfig()
1005 .getString(Config
.BYS
).split(",")) {
1007 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
1008 author
= author
.substring(byString
.length()).trim();
1013 // Special case (without suffix):
1014 if (author
.startsWith("©")) {
1015 author
= author
.substring(1);
1023 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
1024 * and requotify them (i.e., separate them into QUOTE paragraphs and other
1025 * paragraphs (quotes or not)).
1028 * the paragraph to requotify (not necessarily a quote)
1030 * @return the correctly (or so we hope) quotified paragraphs
1032 protected List
<Paragraph
> requotify(Paragraph para
) {
1033 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
1035 if (para
.getType() == ParagraphType
.QUOTE
1036 && para
.getContent().length() > 2) {
1037 String line
= para
.getContent();
1038 boolean singleQ
= line
.startsWith("" + openQuote
);
1039 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
1041 // Do not try when more than one quote at a time
1042 // (some stories are not easily readable if we do)
1044 && line
.indexOf(closeQuote
, 1) < line
1045 .lastIndexOf(closeQuote
)) {
1050 && line
.indexOf(closeDoubleQuote
, 1) < line
1051 .lastIndexOf(closeDoubleQuote
)) {
1057 if (!singleQ
&& !doubleQ
) {
1058 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
1059 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
1062 char open
= singleQ ? openQuote
: openDoubleQuote
;
1063 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
1066 boolean inQuote
= false;
1068 for (char car
: line
.toCharArray()) {
1071 } else if (car
== close
) {
1073 } else if (car
== '.' && !inQuote
) {
1081 String rest
= line
.substring(posDot
+ 1).trim();
1082 line
= line
.substring(0, posDot
+ 1).trim();
1084 for (char car
: line
.toCharArray()) {
1089 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
1090 if (!rest
.isEmpty()) {
1091 newParas
.addAll(requotify(processPara(rest
)));
1105 * Process a {@link Paragraph} from a raw line of text.
1107 * Will also fix quotes and HTML encoding if needed.
1112 * @return the processed {@link Paragraph}
1114 protected Paragraph
processPara(String line
) {
1115 line
= ifUnhtml(line
).trim();
1117 boolean space
= true;
1119 boolean quote
= false;
1120 boolean tentativeCloseQuote
= false;
1125 StringBuilder builder
= new StringBuilder();
1126 for (char car
: line
.toCharArray()) {
1128 if (dashCount
> 0) {
1129 // dash, ndash and mdash: - – —
1130 // currently: always use mdash
1131 builder
.append(dashCount
== 1 ?
'-' : '—');
1136 if (tentativeCloseQuote
) {
1137 tentativeCloseQuote
= false;
1138 if (Character
.isLetterOrDigit(car
)) {
1139 builder
.append("'");
1141 // handle double-single quotes as double quotes
1143 builder
.append(closeDoubleQuote
);
1146 builder
.append(closeQuote
);
1152 case ' ': // note: unbreakable space
1155 case '\n': // just in case
1156 case '\r': // just in case
1157 if (builder
.length() > 0
1158 && builder
.charAt(builder
.length() - 1) != ' ') {
1161 builder
.append(' ');
1165 if (space
|| (brk
&& quote
)) {
1167 // handle double-single quotes as double quotes
1169 builder
.deleteCharAt(builder
.length() - 1);
1170 builder
.append(openDoubleQuote
);
1172 builder
.append(openQuote
);
1174 } else if (prev
== ' ' || prev
== car
) {
1175 // handle double-single quotes as double quotes
1177 builder
.deleteCharAt(builder
.length() - 1);
1178 builder
.append(openDoubleQuote
);
1180 builder
.append(openQuote
);
1183 // it is a quote ("I'm off") or a 'quote' ("This
1184 // 'good' restaurant"...)
1185 tentativeCloseQuote
= true;
1190 if (space
|| (brk
&& quote
)) {
1192 builder
.append(openDoubleQuote
);
1193 } else if (prev
== ' ') {
1194 builder
.append(openDoubleQuote
);
1196 builder
.append(closeDoubleQuote
);
1221 builder
.append(car
);
1230 if (space
|| (brk
&& quote
)) {
1232 builder
.append(openQuote
);
1234 // handle double-single quotes as double quotes
1236 builder
.deleteCharAt(builder
.length() - 1);
1237 builder
.append(openDoubleQuote
);
1239 builder
.append(openQuote
);
1253 // handle double-single quotes as double quotes
1255 builder
.deleteCharAt(builder
.length() - 1);
1256 builder
.append(closeDoubleQuote
);
1258 builder
.append(closeQuote
);
1267 if (space
|| (brk
&& quote
)) {
1269 builder
.append(openDoubleQuote
);
1271 builder
.append(openDoubleQuote
);
1284 builder
.append(closeDoubleQuote
);
1290 builder
.append(car
);
1297 if (tentativeCloseQuote
) {
1298 tentativeCloseQuote
= false;
1299 builder
.append(closeQuote
);
1302 line
= builder
.toString().trim();
1304 ParagraphType type
= ParagraphType
.NORMAL
;
1306 type
= ParagraphType
.BLANK
;
1308 type
= ParagraphType
.BREAK
;
1310 type
= ParagraphType
.QUOTE
;
1313 return new Paragraph(type
, line
, words
);
1317 * Remove the HTML from the input <b>if</b> {@link BasicSupport#isHtml()} is
1323 * @return the no html version if needed
1325 private String
ifUnhtml(String input
) {
1326 if (isHtml() && input
!= null) {
1327 return StringUtils
.unhtml(input
);
1334 * Return a {@link BasicSupport} implementation supporting the given
1335 * resource if possible.
1338 * the story resource
1340 * @return an implementation that supports it, or NULL
1342 public static BasicSupport
getSupport(URL url
) {
1347 // TEXT and INFO_TEXT always support files (not URLs though)
1348 for (SupportType type
: SupportType
.values()) {
1349 if (type
!= SupportType
.TEXT
&& type
!= SupportType
.INFO_TEXT
) {
1350 BasicSupport support
= getSupport(type
);
1351 if (support
!= null && support
.supports(url
)) {
1357 for (SupportType type
: new SupportType
[] { SupportType
.INFO_TEXT
,
1358 SupportType
.TEXT
}) {
1359 BasicSupport support
= getSupport(type
);
1360 if (support
!= null && support
.supports(url
)) {
1369 * Return a {@link BasicSupport} implementation supporting the given type.
1374 * @return an implementation that supports it, or NULL
1376 public static BasicSupport
getSupport(SupportType type
) {
1379 return new Epub().setType(type
);
1381 return new InfoText().setType(type
);
1383 return new Fimfiction().setType(type
);
1385 return new Fanfiction().setType(type
);
1387 return new Text().setType(type
);
1389 return new MangaFox().setType(type
);
1391 return new E621().setType(type
);
1393 return new YiffStar().setType(type
);
1395 return new EHentai().setType(type
);
1397 return new Cbz().setType(type
);
1399 return new Html().setType(type
);
1406 * Return the first line from the given input which correspond to the given
1412 * a string that must be found inside the target line (also
1413 * supports "^" at start to say "only if it starts with" the
1415 * @param relativeLine
1416 * the line to return based upon the target line position (-1 =
1417 * the line before, 0 = the target line...)
1421 static String
getLine(InputStream in
, String needle
, int relativeLine
) {
1422 return getLine(in
, needle
, relativeLine
, true);
1426 * Return a line from the given input which correspond to the given
1432 * a string that must be found inside the target line (also
1433 * supports "^" at start to say "only if it starts with" the
1435 * @param relativeLine
1436 * the line to return based upon the target line position (-1 =
1437 * the line before, 0 = the target line...)
1439 * takes the first result (as opposed to the last one, which will
1440 * also always spend the input)
1444 static String
getLine(InputStream in
, String needle
, int relativeLine
,
1450 } catch (IOException e
) {
1454 List
<String
> lines
= new ArrayList
<String
>();
1455 @SuppressWarnings("resource")
1456 Scanner scan
= new Scanner(in
, "UTF-8");
1458 scan
.useDelimiter("\\n");
1459 while (scan
.hasNext()) {
1460 lines
.add(scan
.next());
1463 if (needle
.startsWith("^")) {
1464 if (lines
.get(lines
.size() - 1).startsWith(
1465 needle
.substring(1))) {
1466 index
= lines
.size() - 1;
1470 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1471 index
= lines
.size() - 1;
1476 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1477 rep
= lines
.get(index
+ relativeLine
);
1488 * Return the text between the key and the endKey (and optional subKey can
1489 * be passed, in this case we will look for the key first, then take the
1490 * text between the subKey and the endKey).
1492 * Will only match the first line with the given key if more than one are
1493 * possible. Which also means that if the subKey or endKey is not found on
1494 * that line, NULL will be returned.
1501 * the sub key or NULL if none
1503 * the end key or NULL for "up to the end"
1504 * @return the text or NULL if not found
1506 static String
getKeyLine(InputStream in
, String key
, String subKey
,
1508 String result
= null;
1510 String line
= getLine(in
, key
, 0);
1511 if (line
!= null && line
.contains(key
)) {
1512 line
= line
.substring(line
.indexOf(key
) + key
.length());
1513 if (subKey
== null || subKey
.isEmpty() || line
.contains(subKey
)) {
1514 if (subKey
!= null) {
1515 line
= line
.substring(line
.indexOf(subKey
)
1518 if (endKey
== null || line
.contains(endKey
)) {
1519 if (endKey
!= null) {
1520 line
= line
.substring(0, line
.indexOf(endKey
));