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 */
71 * A description of this support type (more information than the
72 * {@link BasicSupport#getSourceName()}).
74 * @return the description
76 public String
getDesc() {
77 String desc
= Instance
.getTrans().getStringX(StringId
.INPUT_DESC
,
81 desc
= Instance
.getTrans().getString(StringId
.INPUT_DESC
, this);
88 * The name of this support type (a short version).
92 public String
getSourceName() {
93 BasicSupport support
= BasicSupport
.getSupport(this);
94 if (support
!= null) {
95 return support
.getSourceName();
102 public String
toString() {
103 return super.toString().toLowerCase();
107 * Call {@link SupportType#valueOf(String.toUpperCase())}.
110 * the possible type name
112 * @return NULL or the type
114 public static SupportType
valueOfUC(String typeName
) {
115 return SupportType
.valueOf(typeName
== null ?
null : typeName
120 * Call {@link SupportType#valueOf(String.toUpperCase())} but return
121 * NULL for NULL instead of raising exception.
124 * the possible type name
126 * @return NULL or the type
128 public static SupportType
valueOfNullOkUC(String typeName
) {
129 if (typeName
== null) {
133 return SupportType
.valueOfUC(typeName
);
137 * Call {@link SupportType#valueOf(String.toUpperCase())} but return
138 * NULL in case of error instead of raising an exception.
141 * the possible type name
143 * @return NULL or the type
145 public static SupportType
valueOfAllOkUC(String typeName
) {
147 return SupportType
.valueOfUC(typeName
);
148 } catch (Exception e
) {
154 private InputStream in
;
155 private SupportType type
;
156 private URL currentReferer
; // with only one 'r', as in 'HTTP'...
159 private char openQuote
= Instance
.getTrans().getChar(
160 StringId
.OPEN_SINGLE_QUOTE
);
161 private char closeQuote
= Instance
.getTrans().getChar(
162 StringId
.CLOSE_SINGLE_QUOTE
);
163 private char openDoubleQuote
= Instance
.getTrans().getChar(
164 StringId
.OPEN_DOUBLE_QUOTE
);
165 private char closeDoubleQuote
= Instance
.getTrans().getChar(
166 StringId
.CLOSE_DOUBLE_QUOTE
);
169 * The name of this support class.
173 protected abstract String
getSourceName();
176 * Check if the given resource is supported by this {@link BasicSupport}.
179 * the resource to check for
181 * @return TRUE if it is
183 protected abstract boolean supports(URL url
);
186 * Return TRUE if the support will return HTML encoded content values for
187 * the chapters content.
189 * @return TRUE for HTML
191 protected abstract boolean isHtml();
193 protected abstract MetaData
getMeta(URL source
, InputStream in
)
197 * Return the story description.
200 * the source of the story
202 * the input (the main resource)
204 * @return the description
206 * @throws IOException
207 * in case of I/O error
209 protected abstract String
getDesc(URL source
, InputStream in
)
213 * Return the list of chapters (name and resource).
216 * the source of the story
218 * the input (the main resource)
220 * the optional progress reporter
222 * @return the chapters
224 * @throws IOException
225 * in case of I/O error
227 protected abstract List
<Entry
<String
, URL
>> getChapters(URL source
,
228 InputStream in
, Progress pg
) throws IOException
;
231 * Return the content of the chapter (possibly HTML encoded, if
232 * {@link BasicSupport#isHtml()} is TRUE).
235 * the source of the story
237 * the input (the main resource)
241 * the optional progress reporter
243 * @return the content
245 * @throws IOException
246 * in case of I/O error
248 protected abstract String
getChapterContent(URL source
, InputStream in
,
249 int number
, Progress pg
) throws IOException
;
252 * Log into the support (can be a no-op depending upon the support).
254 * @throws IOException
255 * in case of I/O error
257 public void login() throws IOException
{
262 * Return the list of cookies (values included) that must be used to
263 * correctly fetch the resources.
265 * You are expected to call the super method implementation if you override
268 * @return the cookies
270 * @throws IOException
271 * in case of I/O error
273 public Map
<String
, String
> getCookies() throws IOException
{
274 return new HashMap
<String
, String
>();
278 * Return the canonical form of the main {@link URL}.
281 * the source {@link URL}
283 * @return the canonical form of this {@link URL}
285 * @throws IOException
286 * in case of I/O error
288 public URL
getCanonicalUrl(URL source
) throws IOException
{
293 * Process the given story resource into a partially filled {@link Story}
294 * object containing the name and metadata, except for the description.
299 * @return the {@link Story}
301 * @throws IOException
302 * in case of I/O error
304 public Story
processMeta(URL url
) throws IOException
{
305 return processMeta(url
, true, false, null);
309 * Process the given story resource into a partially filled {@link Story}
310 * object containing the name and metadata.
316 * close "this" and "in" when done
318 * the optional progress reporter
320 * @return the {@link Story}
322 * @throws IOException
323 * in case of I/O error
325 protected Story
processMeta(URL url
, boolean close
, boolean getDesc
,
326 Progress pg
) throws IOException
{
330 pg
.setMinMax(0, 100);
336 url
= getCanonicalUrl(url
);
338 setCurrentReferer(url
);
346 preprocess(url
, getInput());
349 Story story
= new Story();
350 MetaData meta
= getMeta(url
, getInput());
351 if (meta
.getCreationDate() == null
352 || meta
.getCreationDate().isEmpty()) {
353 meta
.setCreationDate(StringUtils
.fromTime(new Date().getTime()));
359 if (meta
!= null && meta
.getCover() == null) {
360 meta
.setCover(getDefaultCover(meta
.getSubject()));
366 String descChapterName
= Instance
.getTrans().getString(
367 StringId
.DESCRIPTION
);
368 story
.getMeta().setResume(
369 makeChapter(url
, 0, descChapterName
,
370 getDesc(url
, getInput()), null));
379 } catch (IOException e
) {
388 setCurrentReferer(null);
393 * Process the given story resource into a fully filled {@link Story}
399 * the optional progress reporter
401 * @return the {@link Story}
403 * @throws IOException
404 * in case of I/O error
406 public Story
process(URL url
, Progress pg
) throws IOException
{
410 pg
.setMinMax(0, 100);
413 url
= getCanonicalUrl(url
);
416 Progress pgMeta
= new Progress();
417 pg
.addProgress(pgMeta
, 10);
418 Story story
= processMeta(url
, false, true, pgMeta
);
419 if (!pgMeta
.isDone()) {
420 pgMeta
.setProgress(pgMeta
.getMax()); // 10%
428 pg
.setName("Retrieving " + story
.getMeta().getTitle());
430 setCurrentReferer(url
);
432 Progress pgGetChapters
= new Progress();
433 pg
.addProgress(pgGetChapters
, 10);
434 story
.setChapters(new ArrayList
<Chapter
>());
435 List
<Entry
<String
, URL
>> chapters
= getChapters(url
, getInput(),
437 if (!pgGetChapters
.isDone()) {
438 pgGetChapters
.setProgress(pgGetChapters
.getMax()); // 20%
441 if (chapters
!= null) {
442 Progress pgChaps
= new Progress("Extracting chapters", 0,
443 chapters
.size() * 300);
444 pg
.addProgress(pgChaps
, 80);
448 for (Entry
<String
, URL
> chap
: chapters
) {
449 pgChaps
.setName("Extracting chapter " + i
);
450 setCurrentReferer(chap
.getValue());
451 InputStream chapIn
= Instance
.getCache().open(
452 chap
.getValue(), this, true);
453 pgChaps
.setProgress(i
* 100);
455 Progress pgGetChapterContent
= new Progress();
456 Progress pgMakeChapter
= new Progress();
457 pgChaps
.addProgress(pgGetChapterContent
, 100);
458 pgChaps
.addProgress(pgMakeChapter
, 100);
460 String content
= getChapterContent(url
, chapIn
, i
,
461 pgGetChapterContent
);
462 if (!pgGetChapterContent
.isDone()) {
463 pgGetChapterContent
.setProgress(pgGetChapterContent
467 Chapter cc
= makeChapter(url
, i
, chap
.getKey(),
468 content
, pgMakeChapter
);
469 if (!pgMakeChapter
.isDone()) {
470 pgMakeChapter
.setProgress(pgMakeChapter
.getMax());
473 words
+= cc
.getWords();
474 story
.getChapters().add(cc
);
475 if (story
.getMeta() != null) {
476 story
.getMeta().setWords(words
);
485 pgChaps
.setName("Extracting chapters");
495 } catch (IOException e
) {
503 setCurrentReferer(null);
512 public SupportType
getType() {
517 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
518 * the current {@link URL} we work on.
520 * @return the referer
522 public URL
getCurrentReferer() {
523 return currentReferer
;
527 * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e.,
528 * the current {@link URL} we work on.
530 * @param currentReferer
533 protected void setCurrentReferer(URL currentReferer
) {
534 this.currentReferer
= currentReferer
;
545 protected BasicSupport
setType(SupportType type
) {
551 * Prepare the support if needed before processing.
554 * the source of the story
556 * the input (the main resource)
558 * @throws IOException
561 protected void preprocess(URL source
, InputStream in
) throws IOException
{
565 * Now that we have processed the {@link Story}, close the resources if any.
567 * @throws IOException
570 protected void close() throws IOException
{
574 * Create a {@link Chapter} object from the given information, formatting
575 * the content as it should be.
582 * the chapter content
584 * the optional progress reporter
586 * @return the {@link Chapter}
588 * @throws IOException
589 * in case of I/O error
591 protected Chapter
makeChapter(URL source
, int number
, String name
,
592 String content
, Progress pg
) throws IOException
{
593 // Chapter name: process it correctly, then remove the possible
594 // redundant "Chapter x: " in front of it
595 String chapterName
= processPara(name
).getContent().trim();
596 for (String lang
: Instance
.getConfig().getString(Config
.CHAPTER
)
598 String chapterWord
= Instance
.getConfig().getStringX(
599 Config
.CHAPTER
, lang
);
600 if (chapterName
.startsWith(chapterWord
)) {
601 chapterName
= chapterName
.substring(chapterWord
.length())
607 if (chapterName
.startsWith(Integer
.toString(number
))) {
608 chapterName
= chapterName
.substring(
609 Integer
.toString(number
).length()).trim();
612 if (chapterName
.startsWith(":")) {
613 chapterName
= chapterName
.substring(1).trim();
617 Chapter chap
= new Chapter(number
, chapterName
);
619 if (content
!= null) {
620 List
<Paragraph
> paras
= makeParagraphs(source
, content
, pg
);
622 for (Paragraph para
: paras
) {
623 words
+= para
.getWords();
625 chap
.setParagraphs(paras
);
626 chap
.setWords(words
);
634 * Convert the given content into {@link Paragraph}s.
637 * the source URL of the story
639 * the textual content
641 * the optional progress reporter
643 * @return the {@link Paragraph}s
645 * @throws IOException
646 * in case of I/O error
648 protected List
<Paragraph
> makeParagraphs(URL source
, String content
,
649 Progress pg
) throws IOException
{
655 // Special <HR> processing:
656 content
= content
.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
660 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>();
662 if (content
!= null && !content
.trim().isEmpty()) {
664 String
[] tab
= content
.split("(<p>|</p>|<br>|<br/>)");
665 pg
.setMinMax(0, tab
.length
);
667 for (String line
: tab
) {
668 if (line
.startsWith("[") && line
.endsWith("]")) {
669 pg
.setName("Extracting image " + i
);
671 paras
.add(makeParagraph(source
, line
.trim()));
676 List
<String
> lines
= new ArrayList
<String
>();
677 BufferedReader buff
= null;
679 buff
= new BufferedReader(
680 new InputStreamReader(new ByteArrayInputStream(
681 content
.getBytes("UTF-8")), "UTF-8"));
682 for (String line
= buff
.readLine(); line
!= null; line
= buff
684 lines
.add(line
.trim());
692 pg
.setMinMax(0, lines
.size());
694 for (String line
: lines
) {
695 if (line
.startsWith("[") && line
.endsWith("]")) {
696 pg
.setName("Extracting image " + i
);
698 paras
.add(makeParagraph(source
, line
));
704 // Check quotes for "bad" format
705 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
706 for (Paragraph para
: paras
) {
707 newParas
.addAll(requotify(para
));
711 // Remove double blanks/brks
712 fixBlanksBreaks(paras
);
719 * Convert the given line into a single {@link Paragraph}.
722 * the source URL of the story
724 * the textual content of the paragraph
726 * @return the {@link Paragraph}
728 private Paragraph
makeParagraph(URL source
, String line
) {
730 if (line
.startsWith("[") && line
.endsWith("]")) {
731 image
= getImageUrl(this, source
,
732 line
.substring(1, line
.length() - 1).trim());
736 return new Paragraph(image
);
738 return processPara(line
);
743 * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
744 * those {@link Paragraph}s.
746 * The resulting list will not contain a starting or trailing blank/break
747 * nor 2 blanks or breaks following each other.
750 * the list of {@link Paragraph}s to fix
752 protected void fixBlanksBreaks(List
<Paragraph
> paras
) {
753 boolean space
= false;
755 for (int i
= 0; i
< paras
.size(); i
++) {
756 Paragraph para
= paras
.get(i
);
757 boolean thisSpace
= para
.getType() == ParagraphType
.BLANK
;
758 boolean thisBrk
= para
.getType() == ParagraphType
.BREAK
;
760 if (i
> 0 && space
&& thisBrk
) {
763 } else if ((space
|| brk
) && (thisSpace
|| thisBrk
)) {
772 // Remove blank/brk at start
774 && (paras
.get(0).getType() == ParagraphType
.BLANK
|| paras
.get(
775 0).getType() == ParagraphType
.BREAK
)) {
779 // Remove blank/brk at end
780 int last
= paras
.size() - 1;
782 && (paras
.get(last
).getType() == ParagraphType
.BLANK
|| paras
783 .get(last
).getType() == ParagraphType
.BREAK
)) {
789 * Get the default cover related to this subject (see <tt>.info</tt> files).
794 * @return the cover if any, or NULL
796 static BufferedImage
getDefaultCover(String subject
) {
797 if (subject
!= null && !subject
.isEmpty()
798 && Instance
.getCoverDir() != null) {
800 File fileCover
= new File(Instance
.getCoverDir(), subject
);
801 return getImage(null, fileCover
.toURI().toURL(), subject
);
802 } catch (MalformedURLException e
) {
810 * Return the list of supported image extensions.
812 * @param emptyAllowed
813 * TRUE to allow an empty extension on first place, which can be
814 * used when you may already have an extension in your input but
815 * are not sure about it
817 * @return the extensions
819 static String
[] getImageExt(boolean emptyAllowed
) {
821 return new String
[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
823 return new String
[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
828 * Check if the given resource can be a local image or a remote image, then
829 * refresh the cache with it if it is.
834 * the resource to check
836 * @return the image if found, or NULL
839 static BufferedImage
getImage(BasicSupport support
, URL source
, String line
) {
840 URL url
= getImageUrl(support
, source
, line
);
842 InputStream in
= null;
844 in
= Instance
.getCache().open(url
, getSupport(url
), true);
845 return IOUtils
.toImage(in
);
846 } catch (IOException e
) {
851 } catch (IOException e
) {
861 * Check if the given resource can be a local image or a remote image, then
862 * refresh the cache with it if it is.
867 * the resource to check
869 * @return the image URL if found, or NULL
872 static URL
getImageUrl(BasicSupport support
, URL source
, String line
) {
878 if (source
!= null) {
879 path
= new File(source
.getFile()).getParent();
881 String basePath
= new File(new File(path
), line
.trim())
883 for (String ext
: getImageExt(true)) {
884 if (new File(basePath
+ ext
).exists()) {
885 url
= new File(basePath
+ ext
).toURI().toURL();
888 } catch (Exception e
) {
889 // Nothing to do here
896 for (String ext
: getImageExt(true)) {
897 if (Instance
.getCache().check(new URL(line
+ ext
))) {
898 url
= new URL(line
+ ext
);
905 for (String ext
: getImageExt(true)) {
907 url
= new URL(line
+ ext
);
908 Instance
.getCache().refresh(url
, support
, true);
910 } catch (IOException e
) {
911 // no image with this ext
916 } catch (MalformedURLException e
) {
921 // refresh the cached file
924 Instance
.getCache().refresh(url
, support
, true);
925 } catch (IOException e
) {
926 // woops, broken image
936 * Open the input file that will be used through the support.
939 * the source {@link URL}
941 * @return the {@link InputStream}
943 * @throws IOException
944 * in case of I/O error
946 protected InputStream
openInput(URL source
) throws IOException
{
947 return Instance
.getCache().open(source
, this, false);
951 * Reset the given {@link InputStream} and return it.
954 * the {@link InputStream} to reset
956 * @return the same {@link InputStream} after reset
958 protected InputStream
reset(InputStream in
) {
961 } catch (IOException e
) {
967 * Reset then return {@link BasicSupport#in}.
969 * @return {@link BasicSupport#in}
971 protected InputStream
getInput() {
976 * Fix the author name if it is prefixed with some "by" {@link String}.
979 * the author with a possible prefix
981 * @return the author without prefixes
983 protected String
fixAuthor(String author
) {
984 if (author
!= null) {
985 for (String suffix
: new String
[] { " ", ":" }) {
986 for (String byString
: Instance
.getConfig()
987 .getString(Config
.BYS
).split(",")) {
989 if (author
.toUpperCase().startsWith(byString
.toUpperCase())) {
990 author
= author
.substring(byString
.length()).trim();
995 // Special case (without suffix):
996 if (author
.startsWith("©")) {
997 author
= author
.substring(1);
1005 * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
1006 * and requotify them (i.e., separate them into QUOTE paragraphs and other
1007 * paragraphs (quotes or not)).
1010 * the paragraph to requotify (not necessarily a quote)
1012 * @return the correctly (or so we hope) quotified paragraphs
1014 protected List
<Paragraph
> requotify(Paragraph para
) {
1015 List
<Paragraph
> newParas
= new ArrayList
<Paragraph
>();
1017 if (para
.getType() == ParagraphType
.QUOTE
1018 && para
.getContent().length() > 2) {
1019 String line
= para
.getContent();
1020 boolean singleQ
= line
.startsWith("" + openQuote
);
1021 boolean doubleQ
= line
.startsWith("" + openDoubleQuote
);
1023 // Do not try when more than one quote at a time
1024 // (some stories are not easily readable if we do)
1026 && line
.indexOf(closeQuote
, 1) < line
1027 .lastIndexOf(closeQuote
)) {
1032 && line
.indexOf(closeDoubleQuote
, 1) < line
1033 .lastIndexOf(closeDoubleQuote
)) {
1039 if (!singleQ
&& !doubleQ
) {
1040 line
= openDoubleQuote
+ line
+ closeDoubleQuote
;
1041 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, para
1044 char open
= singleQ ? openQuote
: openDoubleQuote
;
1045 char close
= singleQ ? closeQuote
: closeDoubleQuote
;
1048 boolean inQuote
= false;
1050 for (char car
: line
.toCharArray()) {
1053 } else if (car
== close
) {
1055 } else if (car
== '.' && !inQuote
) {
1063 String rest
= line
.substring(posDot
+ 1).trim();
1064 line
= line
.substring(0, posDot
+ 1).trim();
1066 for (char car
: line
.toCharArray()) {
1071 newParas
.add(new Paragraph(ParagraphType
.QUOTE
, line
, words
));
1072 if (!rest
.isEmpty()) {
1073 newParas
.addAll(requotify(processPara(rest
)));
1087 * Process a {@link Paragraph} from a raw line of text.
1089 * Will also fix quotes and HTML encoding if needed.
1094 * @return the processed {@link Paragraph}
1096 protected Paragraph
processPara(String line
) {
1097 line
= ifUnhtml(line
).trim();
1099 boolean space
= true;
1101 boolean quote
= false;
1102 boolean tentativeCloseQuote
= false;
1107 StringBuilder builder
= new StringBuilder();
1108 for (char car
: line
.toCharArray()) {
1110 if (dashCount
> 0) {
1111 // dash, ndash and mdash: - – —
1112 // currently: always use mdash
1113 builder
.append(dashCount
== 1 ?
'-' : '—');
1118 if (tentativeCloseQuote
) {
1119 tentativeCloseQuote
= false;
1120 if (Character
.isLetterOrDigit(car
)) {
1121 builder
.append("'");
1123 // handle double-single quotes as double quotes
1125 builder
.append(closeDoubleQuote
);
1128 builder
.append(closeQuote
);
1134 case ' ': // note: unbreakable space
1137 case '\n': // just in case
1138 case '\r': // just in case
1139 if (builder
.length() > 0
1140 && builder
.charAt(builder
.length() - 1) != ' ') {
1143 builder
.append(' ');
1147 if (space
|| (brk
&& quote
)) {
1149 // handle double-single quotes as double quotes
1151 builder
.deleteCharAt(builder
.length() - 1);
1152 builder
.append(openDoubleQuote
);
1154 builder
.append(openQuote
);
1156 } else if (prev
== ' ' || prev
== car
) {
1157 // handle double-single quotes as double quotes
1159 builder
.deleteCharAt(builder
.length() - 1);
1160 builder
.append(openDoubleQuote
);
1162 builder
.append(openQuote
);
1165 // it is a quote ("I'm off") or a 'quote' ("This
1166 // 'good' restaurant"...)
1167 tentativeCloseQuote
= true;
1172 if (space
|| (brk
&& quote
)) {
1174 builder
.append(openDoubleQuote
);
1175 } else if (prev
== ' ') {
1176 builder
.append(openDoubleQuote
);
1178 builder
.append(closeDoubleQuote
);
1203 builder
.append(car
);
1212 if (space
|| (brk
&& quote
)) {
1214 builder
.append(openQuote
);
1216 // handle double-single quotes as double quotes
1218 builder
.deleteCharAt(builder
.length() - 1);
1219 builder
.append(openDoubleQuote
);
1221 builder
.append(openQuote
);
1235 // handle double-single quotes as double quotes
1237 builder
.deleteCharAt(builder
.length() - 1);
1238 builder
.append(closeDoubleQuote
);
1240 builder
.append(closeQuote
);
1249 if (space
|| (brk
&& quote
)) {
1251 builder
.append(openDoubleQuote
);
1253 builder
.append(openDoubleQuote
);
1266 builder
.append(closeDoubleQuote
);
1272 builder
.append(car
);
1279 if (tentativeCloseQuote
) {
1280 tentativeCloseQuote
= false;
1281 builder
.append(closeQuote
);
1284 line
= builder
.toString().trim();
1286 ParagraphType type
= ParagraphType
.NORMAL
;
1288 type
= ParagraphType
.BLANK
;
1290 type
= ParagraphType
.BREAK
;
1292 type
= ParagraphType
.QUOTE
;
1295 return new Paragraph(type
, line
, words
);
1299 * Remove the HTML from the input <b>if</b> {@link BasicSupport#isHtml()} is
1305 * @return the no html version if needed
1307 private String
ifUnhtml(String input
) {
1308 if (isHtml() && input
!= null) {
1309 return StringUtils
.unhtml(input
);
1316 * Return a {@link BasicSupport} implementation supporting the given
1317 * resource if possible.
1320 * the story resource
1322 * @return an implementation that supports it, or NULL
1324 public static BasicSupport
getSupport(URL url
) {
1329 // TEXT and INFO_TEXT always support files (not URLs though)
1330 for (SupportType type
: SupportType
.values()) {
1331 if (type
!= SupportType
.TEXT
&& type
!= SupportType
.INFO_TEXT
) {
1332 BasicSupport support
= getSupport(type
);
1333 if (support
!= null && support
.supports(url
)) {
1339 for (SupportType type
: new SupportType
[] { SupportType
.INFO_TEXT
,
1340 SupportType
.TEXT
}) {
1341 BasicSupport support
= getSupport(type
);
1342 if (support
!= null && support
.supports(url
)) {
1351 * Return a {@link BasicSupport} implementation supporting the given type.
1356 * @return an implementation that supports it, or NULL
1358 public static BasicSupport
getSupport(SupportType type
) {
1361 return new Epub().setType(type
);
1363 return new InfoText().setType(type
);
1365 return new Fimfiction().setType(type
);
1367 return new Fanfiction().setType(type
);
1369 return new Text().setType(type
);
1371 return new MangaFox().setType(type
);
1373 return new E621().setType(type
);
1375 return new YiffStar().setType(type
);
1377 return new Cbz().setType(type
);
1379 return new Html().setType(type
);
1386 * Return the first line from the given input which correspond to the given
1392 * a string that must be found inside the target line (also
1393 * supports "^" at start to say "only if it starts with" the
1395 * @param relativeLine
1396 * the line to return based upon the target line position (-1 =
1397 * the line before, 0 = the target line...)
1401 static String
getLine(InputStream in
, String needle
, int relativeLine
) {
1402 return getLine(in
, needle
, relativeLine
, true);
1406 * Return a 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...)
1419 * takes the first result (as opposed to the last one, which will
1420 * also always spend the input)
1424 static String
getLine(InputStream in
, String needle
, int relativeLine
,
1430 } catch (IOException e
) {
1434 List
<String
> lines
= new ArrayList
<String
>();
1435 @SuppressWarnings("resource")
1436 Scanner scan
= new Scanner(in
, "UTF-8");
1438 scan
.useDelimiter("\\n");
1439 while (scan
.hasNext()) {
1440 lines
.add(scan
.next());
1443 if (needle
.startsWith("^")) {
1444 if (lines
.get(lines
.size() - 1).startsWith(
1445 needle
.substring(1))) {
1446 index
= lines
.size() - 1;
1450 if (lines
.get(lines
.size() - 1).contains(needle
)) {
1451 index
= lines
.size() - 1;
1456 if (index
>= 0 && index
+ relativeLine
< lines
.size()) {
1457 rep
= lines
.get(index
+ relativeLine
);