();
+ for (Paragraph para : paras) {
+ newParas.addAll(requotify(para));
+ }
+ paras = newParas;
- space = thisSpace;
- brk = thisBrk;
- }
+ // Remove double blanks/brks
+ fixBlanksBreaks(paras);
- // Remove blank/brk at start
- if (paras.size() > 0
- && (paras.get(0).getType() == ParagraphType.BLANK || paras
- .get(0).getType() == ParagraphType.BREAK)) {
- paras.remove(0);
- }
+ return paras;
+ }
- // Remove blank/brk at end
- int last = paras.size() - 1;
- if (paras.size() > 0
- && (paras.get(last).getType() == ParagraphType.BLANK || paras
- .get(last).getType() == ParagraphType.BREAK)) {
- paras.remove(last);
+ /**
+ * Fix the {@link ParagraphType#BLANK}s and {@link ParagraphType#BREAK}s of
+ * those {@link Paragraph}s.
+ *
+ * The resulting list will not contain a starting or trailing blank/break
+ * nor 2 blanks or breaks following each other.
+ *
+ * @param paras
+ * the list of {@link Paragraph}s to fix
+ */
+ protected void fixBlanksBreaks(List paras) {
+ boolean space = false;
+ boolean brk = true;
+ for (int i = 0; i < paras.size(); i++) {
+ Paragraph para = paras.get(i);
+ boolean thisSpace = para.getType() == ParagraphType.BLANK;
+ boolean thisBrk = para.getType() == ParagraphType.BREAK;
+
+ if (i > 0 && space && thisBrk) {
+ paras.remove(i - 1);
+ i--;
+ } else if ((space || brk) && (thisSpace || thisBrk)) {
+ paras.remove(i);
+ i--;
}
- chap.setParagraphs(paras);
+ space = thisSpace;
+ brk = thisBrk;
+ }
- return chap;
- } finally {
- in.close();
+ // Remove blank/brk at start
+ if (paras.size() > 0
+ && (paras.get(0).getType() == ParagraphType.BLANK || paras.get(
+ 0).getType() == ParagraphType.BREAK)) {
+ paras.remove(0);
+ }
+
+ // Remove blank/brk at end
+ int last = paras.size() - 1;
+ if (paras.size() > 0
+ && (paras.get(last).getType() == ParagraphType.BLANK || paras
+ .get(last).getType() == ParagraphType.BREAK)) {
+ paras.remove(last);
}
}
+ /**
+ * Get the default cover related to this subject (see .info files).
+ *
+ * @param subject
+ * the subject
+ *
+ * @return the cover if any, or NULL
+ */
static BufferedImage getDefaultCover(String subject) {
if (subject != null && !subject.isEmpty()
&& Instance.getCoverDir() != null) {
try {
File fileCover = new File(Instance.getCoverDir(), subject);
- return getImage(fileCover.toURI().toURL(), subject);
+ return getImage(null, fileCover.toURI().toURL(), subject);
} catch (MalformedURLException e) {
}
}
@@ -602,13 +671,13 @@ public abstract class BasicSupport {
}
}
- static BufferedImage getImage(URL source, String line) {
- URL url = getImageUrl(source, line);
+ static BufferedImage getImage(BasicSupport support, URL source, String line) {
+ URL url = getImageUrl(support, source, line);
if (url != null) {
InputStream in = null;
try {
in = Instance.getCache().open(url, getSupport(url), true);
- return ImageIO.read(in);
+ return IOUtils.toImage(in);
} catch (IOException e) {
} finally {
if (in != null) {
@@ -635,7 +704,7 @@ public abstract class BasicSupport {
* @return the image URL if found, or NULL
*
*/
- static URL getImageUrl(URL source, String line) {
+ static URL getImageUrl(BasicSupport support, URL source, String line) {
URL url = null;
if (line != null) {
@@ -644,11 +713,11 @@ public abstract class BasicSupport {
if (source != null) {
path = new File(source.getFile()).getParent();
try {
- String urlBase = new File(new File(path), line.trim())
- .toURI().toURL().toString();
+ String basePath = new File(new File(path), line.trim())
+ .getAbsolutePath();
for (String ext : getImageExt(true)) {
- if (new File(urlBase + ext).exists()) {
- url = new File(urlBase + ext).toURI().toURL();
+ if (new File(basePath + ext).exists()) {
+ url = new File(basePath + ext).toURI().toURL();
}
}
} catch (Exception e) {
@@ -662,6 +731,7 @@ public abstract class BasicSupport {
for (String ext : getImageExt(true)) {
if (Instance.getCache().check(new URL(line + ext))) {
url = new URL(line + ext);
+ break;
}
}
@@ -670,8 +740,7 @@ public abstract class BasicSupport {
for (String ext : getImageExt(true)) {
try {
url = new URL(line + ext);
- Instance.getCache().refresh(url,
- getSupport(url), true);
+ Instance.getCache().refresh(url, support, true);
break;
} catch (IOException e) {
// no image with this ext
@@ -687,7 +756,7 @@ public abstract class BasicSupport {
// refresh the cached file
if (url != null) {
try {
- Instance.getCache().refresh(url, getSupport(url), true);
+ Instance.getCache().refresh(url, support, true);
} catch (IOException e) {
// woops, broken image
url = null;
@@ -698,6 +767,21 @@ public abstract class BasicSupport {
return url;
}
+ /**
+ * Open the input file that will be used through the support.
+ *
+ * @param source
+ * the source {@link URL}
+ *
+ * @return the {@link InputStream}
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ protected InputStream openInput(URL source) throws IOException {
+ return Instance.getCache().open(source, this, false);
+ }
+
protected InputStream reset(InputStream in) {
try {
in.reset();
@@ -754,7 +838,7 @@ public abstract class BasicSupport {
*
* @return the correctly (or so we hope) quotified paragraphs
*/
- private List requotify(Paragraph para) {
+ protected List requotify(Paragraph para) {
List newParas = new ArrayList();
if (para.getType() == ParagraphType.QUOTE
@@ -763,15 +847,42 @@ public abstract class BasicSupport {
boolean singleQ = line.startsWith("" + openQuote);
boolean doubleQ = line.startsWith("" + openDoubleQuote);
+ // Do not try when more than one quote at a time
+ // (some stories are not easily readable if we do)
+ if (singleQ
+ && line.indexOf(closeQuote, 1) < line
+ .lastIndexOf(closeQuote)) {
+ newParas.add(para);
+ return newParas;
+ }
+ if (doubleQ
+ && line.indexOf(closeDoubleQuote, 1) < line
+ .lastIndexOf(closeDoubleQuote)) {
+ newParas.add(para);
+ return newParas;
+ }
+ //
+
if (!singleQ && !doubleQ) {
line = openDoubleQuote + line + closeDoubleQuote;
newParas.add(new Paragraph(ParagraphType.QUOTE, line));
} else {
+ char open = singleQ ? openQuote : openDoubleQuote;
char close = singleQ ? closeQuote : closeDoubleQuote;
- int posClose = line.indexOf(close, 1);
- int posDot = line.indexOf(".");
- while (posDot >= 0 && posDot < posClose) {
- posDot = line.indexOf(".", posDot + 1);
+
+ int posDot = -1;
+ boolean inQuote = false;
+ int i = 0;
+ for (char car : line.toCharArray()) {
+ if (car == open) {
+ inQuote = true;
+ } else if (car == close) {
+ inQuote = false;
+ } else if (car == '.' && !inQuote) {
+ posDot = i;
+ break;
+ }
+ i++;
}
if (posDot >= 0) {
@@ -1011,8 +1122,8 @@ public abstract class BasicSupport {
}
}
- for (SupportType type : new SupportType[] { SupportType.TEXT,
- SupportType.INFO_TEXT }) {
+ for (SupportType type : new SupportType[] { SupportType.INFO_TEXT,
+ SupportType.TEXT }) {
BasicSupport support = getSupport(type);
if (support != null && support.supports(url)) {
return support;
@@ -1048,6 +1159,8 @@ public abstract class BasicSupport {
return new E621().setType(type);
case CBZ:
return new Cbz().setType(type);
+ case HTML:
+ return new Html().setType(type);
}
return null;