From: Niki Roo Date: Sun, 3 May 2020 10:22:05 +0000 (+0200) Subject: Support for no-chapter stories or stories with descriiption before Chatper X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=75a6a3eadc87e42bb0c9808e359d23d03801a9a2;p=nikiroo-utils.git Support for no-chapter stories or stories with descriiption before Chatper --- diff --git a/supported/BasicSupport.java b/supported/BasicSupport.java index 0a5ec36..17cbec5 100644 --- a/supported/BasicSupport.java +++ b/supported/BasicSupport.java @@ -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 diff --git a/supported/BasicSupportPara.java b/supported/BasicSupportPara.java index 58c363a..1dbedc9 100644 --- a/supported/BasicSupportPara.java +++ b/supported/BasicSupportPara.java @@ -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) { diff --git a/supported/BasicSupport_Deprecated.java b/supported/BasicSupport_Deprecated.java index bc3738a..ad030c0 100644 --- a/supported/BasicSupport_Deprecated.java +++ b/supported/BasicSupport_Deprecated.java @@ -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 paras = new ArrayList(); - if (content != null && !content.trim().isEmpty()) { if (isHtml()) { String[] tab = content.split("(

|

|
|
)"); @@ -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(); diff --git a/supported/Cbz.java b/supported/Cbz.java index a6188ec..7fe496d 100644 --- a/supported/Cbz.java +++ b/supported/Cbz.java @@ -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) { diff --git a/supported/EHentai.java b/supported/EHentai.java index 03c1557..aea76bb 100644 --- a/supported/EHentai.java +++ b/supported/EHentai.java @@ -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()); } diff --git a/supported/Text.java b/supported/Text.java index ade797f..232eab6 100644 --- a/supported/Text.java +++ b/supported/Text.java @@ -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> getChapters(Progress pg) throws IOException { List> chaps = new ArrayList>(); - @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"); }