Support for no-chapter stories or stories with descriiption before Chatper
authorNiki Roo <niki@nikiroo.be>
Sun, 3 May 2020 10:22:05 +0000 (12:22 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 3 May 2020 10:22:05 +0000 (12:22 +0200)
supported/BasicSupport.java
supported/BasicSupportPara.java
supported/BasicSupport_Deprecated.java
supported/Cbz.java
supported/EHentai.java
supported/Text.java

index 0a5ec3686e1be43c52a328d1a1be86e805ad21fe..17cbec5a984b59c5af1e053aefabdccb2e120140 100644 (file)
@@ -374,7 +374,24 @@ public abstract class BasicSupport {
                sourceNode = loadDocument(source);
 
                try {
-                       return doProcess(pg);
+                       Story story = doProcess(pg);
+                       
+                       // Check for "no chapters" stories
+                       if (story.getChapters().isEmpty()
+                                       && story.getMeta().getResume() != null
+                                       && !story.getMeta().getResume().getParagraphs().isEmpty()) {
+                               Chapter resume = story.getMeta().getResume();
+                               resume.setName("");
+                               resume.setNumber(1);
+                               story.getChapters().add(resume);
+
+                               String descChapterName = Instance.getInstance().getTrans()
+                                               .getString(StringId.DESCRIPTION);
+                               resume = new Chapter(0, descChapterName);
+                               story.getMeta().setResume(resume);
+                       }
+                       
+                       return story;
                } finally {
                        close();
                }
@@ -471,7 +488,8 @@ public abstract class BasicSupport {
         *            the chapter name
         * @param content
         *            the content of the chapter
-        * @return the {@link Chapter}
+        *            
+        * @return the {@link Chapter}, never NULL
         * 
         * @throws IOException
         *             in case of I/O error
index 58c363af59b77e1b4f4d1c323fefe2af350ad31b..1dbedc9cc326b0bdcb18e11753c8d78ccc611e22 100644 (file)
@@ -60,7 +60,7 @@ public class BasicSupportPara {
         * @param html
         *            TRUE if the input content is in HTML mode
         * 
-        * @return the {@link Chapter}
+        * @return the {@link Chapter}, never NULL
         * 
         * @throws IOException
         *             in case of I/O error
@@ -202,7 +202,7 @@ public class BasicSupportPara {
         * @param html
         *            TRUE if the input content is in HTML mode
         * 
-        * @return the processed {@link Paragraph}
+        * @return the processed {@link Paragraph}, never NULL
         */
        protected Paragraph processPara(String line, boolean html) {
                if (html) {
@@ -423,7 +423,7 @@ public class BasicSupportPara {
         * @param pg
         *            the optional progress reporter
         * 
-        * @return the {@link Paragraph}s
+        * @return the {@link Paragraph}s (can be empty but never NULL)
         * 
         * @throws IOException
         *             in case of I/O error
@@ -514,7 +514,7 @@ public class BasicSupportPara {
         * @param html
         *            TRUE if the input content is in HTML mode
         * 
-        * @return the {@link Paragraph}
+        * @return the {@link Paragraph}, never NULL
         */
        protected Paragraph makeParagraph(BasicSupport support, URL source,
                        String line, boolean html) {
index bc3738a211bc22b92ac8811e85a549dd17777618..ad030c0ced64825def174a7bfed5d81b4f00c7d9 100644 (file)
@@ -332,8 +332,22 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                                pg.setProgress(80);
                        }
 
-                       return story;
+                       // Check for "no chapters" stories
+                       if (story.getChapters().isEmpty()
+                                       && story.getMeta().getResume() != null
+                                       && !story.getMeta().getResume().getParagraphs().isEmpty()) {
+                               Chapter resume = story.getMeta().getResume();
+                               resume.setName("");
+                               resume.setNumber(1);
+                               story.getChapters().add(resume);
+
+                               String descChapterName = Instance.getInstance().getTrans()
+                                               .getString(StringId.DESCRIPTION);
+                               resume = new Chapter(0, descChapterName);
+                               story.getMeta().setResume(resume);
+                       }
 
+                       return story;
                } finally {
                        close();
 
@@ -373,7 +387,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param pg
         *            the optional progress reporter
         * 
-        * @return the {@link Chapter}
+        * @return the {@link Chapter}, never NULL
         * 
         * @throws IOException
         *             in case of I/O error
@@ -429,7 +443,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param pg
         *            the optional progress reporter
         * 
-        * @return the {@link Paragraph}s
+        * @return the {@link Paragraph}s (can be empty, but never NULL)
         * 
         * @throws IOException
         *             in case of I/O error
@@ -447,7 +461,6 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                }
 
                List<Paragraph> paras = new ArrayList<Paragraph>();
-
                if (content != null && !content.trim().isEmpty()) {
                        if (isHtml()) {
                                String[] tab = content.split("(<p>|</p>|<br>|<br/>)");
@@ -512,7 +525,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param line
         *            the textual content of the paragraph
         * 
-        * @return the {@link Paragraph}
+        * @return the {@link Paragraph}, never NULL
         */
        private Paragraph makeParagraph(URL source, String line) {
                Image image = null;
@@ -862,7 +875,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param line
         *            the raw line
         * 
-        * @return the processed {@link Paragraph}
+        * @return the processed {@link Paragraph}, never NULL
         */
        protected Paragraph processPara(String line) {
                line = ifUnhtml(line).trim();
index a6188ec55085467b1306f5a9ac91e0083bdf014d..7fe496d97ea8acb9e8869bb41c37d6723e19a636 100644 (file)
@@ -168,7 +168,7 @@ class Cbz extends Epub {
                        }
 
                        if (!imagesList.isEmpty()) {
-                               Chapter chap = new Chapter(story.getChapters().size() + 1, null);
+                               Chapter chap = new Chapter(story.getChapters().size() + 1, "");
                                story.getChapters().add(chap);
 
                                for (String uuid : imagesList) {
index 03c1557432194187cb54e2b43e9f4239d42c1668..aea76bbb00cd1033139bc86e2d2ecd90c2e8adfc 100644 (file)
@@ -54,7 +54,7 @@ class EHentai extends BasicSupport_Deprecated {
                // There is no chapters on e621, just pagination...
                Story story = super.process(url, pg);
 
-               Chapter only = new Chapter(1, null);
+               Chapter only = new Chapter(1, "");
                for (Chapter chap : story) {
                        only.getParagraphs().addAll(chap.getParagraphs());
                }
index ade797fbf71ae3182a05ced4a6f0fdbb74b1f118..232eab6ec4f7736b6b470011f51d09548fe733e8 100644 (file)
@@ -102,7 +102,7 @@ class Text extends BasicSupport {
        }
 
        private String getLang() {
-               @SuppressWarnings("resource")
+               @SuppressWarnings("resource") // cannot close, or we loose getInput()!
                Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                scan.next(); // Title
@@ -128,14 +128,14 @@ class Text extends BasicSupport {
        }
 
        private String getTitle() {
-               @SuppressWarnings("resource")
+               @SuppressWarnings("resource") // cannot close, or we loose getInput()!
                Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                return scan.next();
        }
 
        private String getAuthor() {
-               @SuppressWarnings("resource")
+               @SuppressWarnings("resource") // cannot close, or we loose getInput()!
                Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                scan.next();
@@ -151,7 +151,7 @@ class Text extends BasicSupport {
        }
 
        private String getDate() {
-               @SuppressWarnings("resource")
+               @SuppressWarnings("resource") // cannot close, or we loose getInput()!
                Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                scan.next();
@@ -215,12 +215,13 @@ class Text extends BasicSupport {
        protected List<Entry<String, URL>> getChapters(Progress pg)
                        throws IOException {
                List<Entry<String, URL>> chaps = new ArrayList<Entry<String, URL>>();
-               @SuppressWarnings("resource")
+               @SuppressWarnings("resource") // cannot close, or we loose getInput()!
                Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
-               boolean prevLineEmpty = false;
+               String line = "first is not empty";
                while (scan.hasNext()) {
-                       String line = scan.next();
+                       boolean prevLineEmpty = line.trim().isEmpty();
+                       line = scan.next();
                        if (prevLineEmpty && detectChapter(line, chaps.size() + 1) != null) {
                                String chapName = Integer.toString(chaps.size() + 1);
                                int pos = line.indexOf(':');
@@ -232,10 +233,8 @@ class Text extends BasicSupport {
                                                chapName, //
                                                getSourceFile().toURI().toURL()));
                        }
-
-                       prevLineEmpty = line.trim().isEmpty();
                }
-
+               
                return chaps;
        }
 
@@ -243,17 +242,27 @@ class Text extends BasicSupport {
        protected String getChapterContent(URL source, int number, Progress pg)
                        throws IOException {
                StringBuilder builder = new StringBuilder();
-               @SuppressWarnings("resource")
+               @SuppressWarnings("resource") // cannot close, or we loose getInput()!
                Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
-               boolean inChap = false;
+               scan.next(); // title
+               scan.next(); // author
+               scan.next(); // date or empty
+               Boolean inChap = null;
+               String line = "";
                while (scan.hasNext()) {
-                       String line = scan.next();
-                       if (!inChap && detectChapter(line, number) != null) {
+                       if (number == 0 && !line.trim().isEmpty()) {
+                               // We found pre-chapter content, we are checking for
+                               // Chapter 0 (fake chapter) --> keep the content
+                               if (inChap == null)
+                                       inChap = true;
+                       }
+                       line = scan.next();
+                       if ((inChap == null || !inChap) && detectChapter(line, number) != null) {
                                inChap = true;
                        } else if (detectChapter(line, number + 1) != null) {
                                break;
-                       } else if (inChap) {
+                       } else if (inChap != null && inChap) {
                                builder.append(line);
                                builder.append("\n");
                        }