X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Fsupported%2FText.java;h=f7ecadad15f8217dd5d1a1c43887d056f2d383e1;hb=1dbed6237611f74292af13f20f9d80c446a60020;hp=f1ee71c1195d6337461dc35e6d9fdbfdde7a3ef2;hpb=08fe2e33007063e30fe22dc1d290f8afaa18eb1d;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index f1ee71c..45b761a 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -5,13 +5,21 @@ 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.MetaData; +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: @@ -25,77 +33,115 @@ import be.nikiroo.fanfix.bundles.Config; * 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 file == null || !file.exists(); + 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; } - return false; + 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; } /** @@ -260,31 +346,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; } } }