Fix some bad line breaks on HTML supports
[fanfix.git] / src / be / nikiroo / fanfix / supported / BasicSupport.java
index b528cac819acea887acfc28f861b9c32221d1971..18113188b8ac358b19af20ff62a845a91d7be9b1 100644 (file)
@@ -10,6 +10,7 @@ import java.io.InputStreamReader;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -333,6 +334,10 @@ public abstract class BasicSupport {
 
                        Story story = new Story();
                        MetaData meta = getMeta(url, getInput());
+                       if (meta.getCreationDate() == null
+                                       || meta.getCreationDate().isEmpty()) {
+                               meta.setCreationDate(StringUtils.fromTime(new Date().getTime()));
+                       }
                        story.setMeta(meta);
 
                        if (meta != null && meta.getCover() == null) {
@@ -396,6 +401,8 @@ public abstract class BasicSupport {
                                return null;
                        }
 
+                       pg.setName("Retrieving " + story.getMeta().getTitle());
+
                        setCurrentReferer(url);
 
                        story.setChapters(new ArrayList<Chapter>());
@@ -408,14 +415,19 @@ public abstract class BasicSupport {
                                Progress pgChaps = new Progress(0, chapters.size());
                                pg.addProgress(pgChaps, 80);
 
+                               long words = 0;
                                for (Entry<String, URL> chap : chapters) {
                                        setCurrentReferer(chap.getValue());
                                        InputStream chapIn = Instance.getCache().open(
                                                        chap.getValue(), this, true);
                                        try {
-                                               story.getChapters().add(
-                                                               makeChapter(url, i, chap.getKey(),
-                                                                               getChapterContent(url, chapIn, i)));
+                                               Chapter cc = makeChapter(url, i, chap.getKey(),
+                                                               getChapterContent(url, chapIn, i));
+                                               words += cc.getWords();
+                                               story.getChapters().add(cc);
+                                               if (story.getMeta() != null) {
+                                                       story.getMeta().setWords(words);
+                                               }
                                        } finally {
                                                chapIn.close();
                                        }
@@ -554,7 +566,13 @@ public abstract class BasicSupport {
                Chapter chap = new Chapter(number, chapterName);
 
                if (content != null) {
-                       chap.setParagraphs(makeParagraphs(source, content));
+                       List<Paragraph> paras = makeParagraphs(source, content);
+                       long words = 0;
+                       for (Paragraph para : paras) {
+                               words += para.getWords();
+                       }
+                       chap.setParagraphs(paras);
+                       chap.setWords(words);
                }
 
                return chap;
@@ -579,57 +597,71 @@ public abstract class BasicSupport {
                if (isHtml()) {
                        // Special <HR> processing:
                        content = content.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)",
-                                       "\n* * *\n");
+                                       "<br/>* * *<br/>");
                }
 
                List<Paragraph> paras = new ArrayList<Paragraph>();
-               InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));
-               try {
-                       BufferedReader buff = new BufferedReader(new InputStreamReader(in,
-                                       "UTF-8"));
-
-                       for (String encodedLine = buff.readLine(); encodedLine != null; encodedLine = buff
-                                       .readLine()) {
-                               String lines[];
-                               if (isHtml()) {
-                                       lines = encodedLine.split("(<p>|</p>|<br>|<br/>|\\n)");
-                               } else {
-                                       lines = new String[] { encodedLine };
-                               }
 
-                               for (String aline : lines) {
-                                       String line = aline.trim();
-
-                                       URL image = null;
-                                       if (line.startsWith("[") && line.endsWith("]")) {
-                                               image = getImageUrl(this, source,
-                                                               line.substring(1, line.length() - 1).trim());
+               if (content != null && !content.trim().isEmpty()) {
+                       if (isHtml()) {
+                               for (String line : content.split("(<p>|</p>|<br>|<br/>)")) {
+                                       paras.add(makeParagraph(source, line.trim()));
+                               }
+                       } else {
+                               BufferedReader buff = null;
+                               try {
+                                       buff = new BufferedReader(
+                                                       new InputStreamReader(new ByteArrayInputStream(
+                                                                       content.getBytes("UTF-8")), "UTF-8"));
+                                       for (String line = buff.readLine(); line != null; line = buff
+                                                       .readLine()) {
+                                               paras.add(makeParagraph(source, line.trim()));
                                        }
-
-                                       if (image != null) {
-                                               paras.add(new Paragraph(image));
-                                       } else {
-                                               paras.add(processPara(line));
+                               } finally {
+                                       if (buff != null) {
+                                               buff.close();
                                        }
                                }
                        }
-               } finally {
-                       in.close();
-               }
 
-               // Check quotes for "bad" format
-               List<Paragraph> newParas = new ArrayList<Paragraph>();
-               for (Paragraph para : paras) {
-                       newParas.addAll(requotify(para));
-               }
-               paras = newParas;
+                       // Check quotes for "bad" format
+                       List<Paragraph> newParas = new ArrayList<Paragraph>();
+                       for (Paragraph para : paras) {
+                               newParas.addAll(requotify(para));
+                       }
+                       paras = newParas;
 
-               // Remove double blanks/brks
-               fixBlanksBreaks(paras);
+                       // Remove double blanks/brks
+                       fixBlanksBreaks(paras);
+               }
 
                return paras;
        }
 
+       /**
+        * Convert the given line into a single {@link Paragraph}.
+        * 
+        * @param source
+        *            the source URL of the story
+        * @param line
+        *            the textual content of the paragraph
+        * 
+        * @return the {@link Paragraph}
+        */
+       private Paragraph makeParagraph(URL source, String line) {
+               URL image = null;
+               if (line.startsWith("[") && line.endsWith("]")) {
+                       image = getImageUrl(this, source,
+                                       line.substring(1, line.length() - 1).trim());
+               }
+
+               if (image != null) {
+                       return new Paragraph(image);
+               } else {
+                       return processPara(line);
+               }
+       }
+
        /**
         * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
         * those {@link Paragraph}s.
@@ -929,7 +961,8 @@ public abstract class BasicSupport {
 
                        if (!singleQ && !doubleQ) {
                                line = openDoubleQuote + line + closeDoubleQuote;
-                               newParas.add(new Paragraph(ParagraphType.QUOTE, line));
+                               newParas.add(new Paragraph(ParagraphType.QUOTE, line, para
+                                               .getWords()));
                        } else {
                                char open = singleQ ? openQuote : openDoubleQuote;
                                char close = singleQ ? closeQuote : closeDoubleQuote;
@@ -952,7 +985,13 @@ public abstract class BasicSupport {
                                if (posDot >= 0) {
                                        String rest = line.substring(posDot + 1).trim();
                                        line = line.substring(0, posDot + 1).trim();
-                                       newParas.add(new Paragraph(ParagraphType.QUOTE, line));
+                                       long words = 1;
+                                       for (char car : line.toCharArray()) {
+                                               if (car == ' ') {
+                                                       words++;
+                                               }
+                                       }
+                                       newParas.add(new Paragraph(ParagraphType.QUOTE, line, words));
                                        if (!rest.isEmpty()) {
                                                newParas.addAll(requotify(processPara(rest)));
                                        }
@@ -986,6 +1025,7 @@ public abstract class BasicSupport {
                boolean tentativeCloseQuote = false;
                char prev = '\0';
                int dashCount = 0;
+               long words = 1;
 
                StringBuilder builder = new StringBuilder();
                for (char car : line.toCharArray()) {
@@ -1019,6 +1059,10 @@ public abstract class BasicSupport {
                        case '\t':
                        case '\n': // just in case
                        case '\r': // just in case
+                               if (builder.length() > 0
+                                               && builder.charAt(builder.length() - 1) != ' ') {
+                                       words++;
+                               }
                                builder.append(' ');
                                break;
 
@@ -1171,7 +1215,7 @@ public abstract class BasicSupport {
                        type = ParagraphType.QUOTE;
                }
 
-               return new Paragraph(type, line);
+               return new Paragraph(type, line, words);
        }
 
        /**