code cleanup / jdoc
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Text.java
index ade797fbf71ae3182a05ced4a6f0fdbb74b1f118..252aca0443fbfe0f71db294c0c8c1d3a816deb72 100644 (file)
@@ -15,9 +15,7 @@ import org.jsoup.nodes.Document;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
-import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
-import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.ImageUtils;
 import be.nikiroo.utils.Progress;
@@ -87,14 +85,11 @@ class Text extends BasicSupport {
                meta.setAuthor(getAuthor());
                meta.setDate(bsHelper.formatDate(getDate()));
                meta.setTags(new ArrayList<String>());
-               meta.setSource(getType().getSourceName());
                meta.setUrl(getSourceFile().toURI().toURL().toString());
-               meta.setPublisher("");
                meta.setUuid(getSourceFile().toString());
                meta.setLuid("");
                meta.setLang(getLang()); // default is EN
                meta.setSubject(getSourceFile().getParentFile().getName());
-               meta.setType(getType().toString());
                meta.setImageDocument(false);
                meta.setCover(getCover(getSourceFile()));
                
@@ -102,7 +97,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 +123,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 +146,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();
@@ -172,20 +167,7 @@ class Text extends BasicSupport {
 
        @Override
        protected String getDesc() throws IOException {
-               String content = getChapterContent(null, 0, null).trim();
-               if (!content.isEmpty()) {
-                       Chapter desc = bsPara.makeChapter(this, null, 0, "Description",
-                                       content, isHtml(), null);
-                       StringBuilder builder = new StringBuilder();
-                       for (Paragraph para : desc) {
-                               if (builder.length() > 0) {
-                                       builder.append("\n");
-                               }
-                               builder.append(para.getContent());
-                       }
-               }
-
-               return content;
+               return getChapterContent(null, 0, null).trim();
        }
 
        protected Image getCover(File sourceFile) {
@@ -215,12 +197,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 +215,8 @@ class Text extends BasicSupport {
                                                chapName, //
                                                getSourceFile().toURI().toURL()));
                        }
-
-                       prevLineEmpty = line.trim().isEmpty();
                }
-
+               
                return chaps;
        }
 
@@ -243,17 +224,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");
                        }