X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FText.java;h=232eab6ec4f7736b6b470011f51d09548fe733e8;hb=83001824bb702296a45cdf400312e34f69951e91;hp=2dd052470278886f30b715f541ca571d7967ae50;hpb=333f0e7b5e333e8f6222881ce35398f403fc4121;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index 2dd0524..232eab6 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -1,19 +1,27 @@ package be.nikiroo.fanfix.supported; -import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; +import java.util.AbstractMap; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; import java.util.Scanner; +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; +import be.nikiroo.utils.streams.MarkableFileInputStream; /** * Support class for local stories encoded in textual format, with a few rules: @@ -27,58 +35,75 @@ import be.nikiroo.fanfix.data.MetaData; * number *
+ * It must also be a file, not another kind of URL. + * + * @param url + * the {@link URL} to check + * @param info + * TRUE to require the info file, FALSE to forbid the info file + * + * @return TRUE if it is supported + */ + protected boolean supports(URL url, boolean info) { + if (!"file".equals(url.getProtocol())) { + return false; } - return false; + boolean infoPresent = false; + File file; + try { + file = new File(url.toURI()); + file = assureNoTxt(file); + file = new File(file.getPath() + ".info"); + } catch (URISyntaxException e) { + Instance.getInstance().getTraceHandler().error(e); + file = null; + } + + infoPresent = (file != null && file.exists()); + + return infoPresent == info; } /** - * Check if the given line looks like a starting chapter in a supported - * language, and return the language if it does (or NULL if not). + * Remove the ".txt" (or ".text") extension if it is present. * - * @param line - * the line to check + * @param file + * the file to process * - * @return the language or NULL + * @return the same file or a copy of it without the ".txt" extension if it + * was present */ - private String detectChapter(String line) { - return detectChapter(line, null); + protected File assureNoTxt(File file) { + for (String ext : new String[] { ".txt", ".text" }) { + if (file.getName().endsWith(ext)) { + file = new File(file.getPath().substring(0, + file.getPath().length() - ext.length())); + } + } + + return file; } /** @@ -267,31 +351,35 @@ class Text extends BasicSupport { * * @param line * the line to check + * @param number + * the specific chapter number to check for * * @return the language or NULL */ - private String detectChapter(String line, Integer number) { + static private String detectChapter(String line, int number) { line = line.toUpperCase(); - for (String lang : Instance.getConfig().getString(Config.CHAPTER) - .split(",")) { - String chapter = Instance.getConfig().getStringX(Config.CHAPTER, - lang); + for (String lang : Instance.getInstance().getConfig().getList(Config.CONF_CHAPTER)) { + String chapter = Instance.getInstance().getConfig().getStringX(Config.CONF_CHAPTER, lang); if (chapter != null && !chapter.isEmpty()) { chapter = chapter.toUpperCase() + " "; if (line.startsWith(chapter)) { - if (number != null) { - // We want "[CHAPTER] [number]: [name]", with ": [name]" - // optional - String test = line.substring(chapter.length()).trim(); - if (test.startsWith(Integer.toString(number))) { - test = test.substring( - Integer.toString(number).length()).trim(); - if (test.isEmpty() || test.startsWith(":")) { - return lang; - } + // We want "[CHAPTER] [number]: [name]", with ": [name]" + // optional + String test = line.substring(chapter.length()).trim(); + + String possibleNum = test.trim(); + if (possibleNum.indexOf(':') > 0) { + possibleNum = possibleNum.substring(0, + possibleNum.indexOf(':')).trim(); + } + + if (test.startsWith(Integer.toString(number))) { + test = test + .substring(Integer.toString(number).length()) + .trim(); + if (test.isEmpty() || test.startsWith(":")) { + return lang; } - } else { - return lang; } } }