From: Niki Roo Date: Tue, 28 Apr 2020 07:42:51 +0000 (+0200) Subject: Merge branch 'subtree' X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=commitdiff_plain;h=d9691f01bac1ea36d234f240534ae8bb94ab522a;hp=-c Merge branch 'subtree' --- d9691f01bac1ea36d234f240534ae8bb94ab522a diff --combined src/be/nikiroo/fanfix/Instance.java index f48d05b,d0d1c84..d0d1c84 --- a/src/be/nikiroo/fanfix/Instance.java +++ b/src/be/nikiroo/fanfix/Instance.java @@@ -48,8 -48,19 +48,19 @@@ public class Instance /** * Initialise the instance -- if already initialised, nothing will happen. *

- * Before calling this method, you may call {@link Bundles#setDirectory(String)} - * if wanted. + * Before calling this method, you may call + * {@link Bundles#setDirectory(String)} if wanted. + *

+ * Note that this method will honour some environment variables, the 3 most + * important ones probably being: + *

*/ static public void init() { init(false); @@@ -465,7 -476,6 +476,6 @@@ BasicLibrary lib = null; boolean useRemote = config.getBoolean(Config.REMOTE_LIBRARY_ENABLED, false); - if (useRemote) { String host = null; int port = -1; diff --combined src/be/nikiroo/fanfix/supported/Epub.java index f8e4678,90a9f45..90a9f45 --- a/src/be/nikiroo/fanfix/supported/Epub.java +++ b/src/be/nikiroo/fanfix/supported/Epub.java @@@ -42,8 -42,8 +42,8 @@@ class Epub extends InfoText try { return new File(fakeSource.toURI()); } catch (URISyntaxException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException("Cannot get the source file from the info-text URL", e)); + Instance.getInstance().getTraceHandler().error(new IOException( + "Cannot get the source file from the info-text URL", e)); } return null; @@@ -55,7 -55,8 +55,8 @@@ try { fakeIn.reset(); } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(new IOException("Cannot reset the Epub Text stream", e)); + Instance.getInstance().getTraceHandler().error(new IOException( + "Cannot reset the Epub Text stream", e)); } return fakeIn; @@@ -83,7 -84,8 +84,8 @@@ ZipInputStream zipIn = null; try { zipIn = new ZipInputStream(in); - tmpDir = Instance.getInstance().getTempFiles().createTempDir("fanfic-reader-parser"); + tmpDir = Instance.getInstance().getTempFiles() + .createTempDir("fanfic-reader-parser"); File tmp = new File(tmpDir, "file.txt"); File tmpInfo = new File(tmpDir, "file.info"); @@@ -99,8 -101,9 +101,9 @@@ String title = null; String author = null; - for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn - .getNextEntry()) { + for (ZipEntry entry = zipIn + .getNextEntry(); entry != null; entry = zipIn + .getNextEntry()) { if (!entry.isDirectory() && entry.getName().startsWith(getDataPrefix())) { String entryLName = entry.getName().toLowerCase(); @@@ -124,18 -127,25 +127,25 @@@ try { cover = new Image(zipIn); } catch (Exception e) { - Instance.getInstance().getTraceHandler().error(e); + Instance.getInstance().getTraceHandler() + .error(e); } } - } else if (entry.getName().equals(getDataPrefix() + "URL")) { + } else if (entry.getName() + .equals(getDataPrefix() + "URL")) { String[] descArray = StringUtils .unhtml(IOUtils.readSmallStream(zipIn)).trim() .split("\n"); if (descArray.length > 0) { url = descArray[0].trim(); } - } else if (entry.getName().equals( - getDataPrefix() + "SUMMARY")) { + } else if (entry.getName().endsWith(".desc")) { + // // For old files + // if (this.desc != null) { + // this.desc = IOUtils.readSmallStream(zipIn).trim(); + // } + } else if (entry.getName() + .equals(getDataPrefix() + "SUMMARY")) { String[] descArray = StringUtils .unhtml(IOUtils.readSmallStream(zipIn)).trim() .split("\n"); @@@ -149,12 -159,12 +159,12 @@@ skip = 2; } } - this.desc = ""; - for (int i = skip; i < descArray.length; i++) { - this.desc += descArray[i].trim() + "\n"; - } - - this.desc = this.desc.trim(); + // this.desc = ""; + // for (int i = skip; i < descArray.length; i++) { + // this.desc += descArray[i].trim() + "\n"; + // } + // + // this.desc = this.desc.trim(); } else { // Hopefully the data file IOUtils.write(zipIn, tmp); @@@ -198,9 -208,8 +208,8 @@@ if (cover != null) { meta.setCover(cover); } else { - meta.setCover(InfoReader - .getCoverByName(getSourceFileOriginal().toURI() - .toURL())); + meta.setCover(InfoReader.getCoverByName( + getSourceFileOriginal().toURI().toURL())); } } } finally { diff --combined src/be/nikiroo/fanfix/supported/InfoReader.java index 220350e,405b28f..405b28f --- a/src/be/nikiroo/fanfix/supported/InfoReader.java +++ b/src/be/nikiroo/fanfix/supported/InfoReader.java @@@ -19,7 -19,8 +19,8 @@@ import be.nikiroo.utils.streams.Markabl public class InfoReader { static protected BasicSupportHelper bsHelper = new BasicSupportHelper(); // static protected BasicSupportImages bsImages = new BasicSupportImages(); - // static protected BasicSupportPara bsPara = new BasicSupportPara(new BasicSupportHelper(), new BasicSupportImages()); + // static protected BasicSupportPara bsPara = new BasicSupportPara(new + // BasicSupportHelper(), new BasicSupportImages()); public static MetaData readMeta(File infoFile, boolean withCover) throws IOException { @@@ -30,7 -31,79 +31,79 @@@ if (infoFile.exists()) { InputStream in = new MarkableFileInputStream(infoFile); try { - return createMeta(infoFile.toURI().toURL(), in, withCover); + MetaData meta = createMeta(infoFile.toURI().toURL(), in, + withCover); + + // Some old .info files were using UUID for URL... + if (!hasIt(meta.getUrl()) && meta.getUuid() != null + && (meta.getUuid().startsWith("http://") + || meta.getUuid().startsWith("https://"))) { + meta.setUrl(meta.getUuid()); + } + + // Some old .info files don't have those now required fields... + // So we check if we can find the info in another way (many + // formats have a copy of the original text file) + if (!hasIt(meta.getTitle(), meta.getAuthor(), meta.getDate(), + meta.getUrl())) { + + // TODO: not nice, would be better to do it properly... + + String base = infoFile.getPath(); + if (base.endsWith(".info")) { + base = base.substring(0, + base.length() - ".info".length()); + } + File textFile = new File(base); + if (!textFile.exists()) { + textFile = new File(base + ".txt"); + } + if (!textFile.exists()) { + textFile = new File(base + ".text"); + } + + if (textFile.exists()) { + final URL source = textFile.toURI().toURL(); + final MetaData[] superMetaA = new MetaData[1]; + @SuppressWarnings("unused") + Text unused = new Text() { + private boolean loaded = loadDocument(); + + @Override + public SupportType getType() { + return SupportType.TEXT; + } + + protected boolean loadDocument() + throws IOException { + loadDocument(source); + superMetaA[0] = getMeta(); + return true; + } + + @Override + protected Image getCover(File sourceFile) { + return null; + } + }; + + MetaData superMeta = superMetaA[0]; + if (!hasIt(meta.getTitle())) { + meta.setTitle(superMeta.getTitle()); + } + if (!hasIt(meta.getAuthor())) { + meta.setAuthor(superMeta.getAuthor()); + } + if (!hasIt(meta.getDate())) { + meta.setDate(superMeta.getDate()); + } + if (!hasIt(meta.getUrl())) { + meta.setUrl(superMeta.getUrl()); + } + } + } + + return meta; } finally { in.close(); } @@@ -41,6 -114,24 +114,24 @@@ + infoFile.getAbsolutePath()); } + /** + * Check if we have non-empty values for all the given {@link String}s. + * + * @param values + * the values to check + * + * @return TRUE if none of them was NULL or empty + */ + static private boolean hasIt(String... values) { + for (String value : values) { + if (value == null || value.trim().isEmpty()) { + return false; + } + } + + return true; + } + private static MetaData createMeta(URL sourceInfoFile, InputStream in, boolean withCover) throws IOException { MetaData meta = new MetaData(); @@@ -61,8 -152,7 +152,7 @@@ if (withCover) { String infoTag = getInfoTag(in, "COVER"); if (infoTag != null && !infoTag.trim().isEmpty()) { - meta.setCover(bsHelper.getImage(null, sourceInfoFile, - infoTag)); + meta.setCover(bsHelper.getImage(null, sourceInfoFile, infoTag)); } if (meta.getCover() == null) { // Second chance: try to check for a cover next to the info file @@@ -97,8 -187,8 +187,8 @@@ File basefile = new File(sourceInfoFile.getFile()); - String ext = "." - + Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase(); + String ext = "." + Instance.getInstance().getConfig() + .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase(); // Without removing ext cover = bsHelper.getImage(null, sourceInfoFile, @@@ -169,11 -259,21 +259,21 @@@ String value = getLine(in, key, 0); if (value != null && !value.isEmpty()) { value = value.trim().substring(key.length() - 1).trim(); - if (value.startsWith("'") && value.endsWith("'") - || value.startsWith("\"") && value.endsWith("\"")) { + if (value.length() > 1 && // + (value.startsWith("'") && value.endsWith("'") + || value.startsWith("\"") + && value.endsWith("\""))) { value = value.substring(1, value.length() - 1).trim(); } + // Some old files ended up with TITLE="'xxxxx'" + if ("^TITLE=".equals(key)) { + if (value.startsWith("'") && value.endsWith("'") + && value.length() > 1) { + value = value.substring(1, value.length() - 1).trim(); + } + } + return value; } } @@@ -235,8 -335,8 +335,8 @@@ if (index == -1) { if (needle.startsWith("^")) { - if (lines.get(lines.size() - 1).startsWith( - needle.substring(1))) { + if (lines.get(lines.size() - 1) + .startsWith(needle.substring(1))) { index = lines.size() - 1; } diff --combined src/be/nikiroo/fanfix/supported/InfoText.java index 42e2c13,2af8c7e..2af8c7e --- a/src/be/nikiroo/fanfix/supported/InfoText.java +++ b/src/be/nikiroo/fanfix/supported/InfoText.java @@@ -22,30 -22,7 +22,7 @@@ class InfoText extends Text @Override protected MetaData getMeta() throws IOException { - MetaData meta = InfoReader.readMeta(getInfoFile(), true); - - // Some old .info files don't have those now required fields... - String test = meta.getTitle() == null ? "" : meta.getTitle(); - test += meta.getAuthor() == null ? "" : meta.getAuthor(); - test += meta.getDate() == null ? "" : meta.getDate(); - test += meta.getUrl() == null ? "" : meta.getUrl(); - if (test.isEmpty()) { - MetaData superMeta = super.getMeta(); - if (meta.getTitle() == null || meta.getTitle().isEmpty()) { - meta.setTitle(superMeta.getTitle()); - } - if (meta.getAuthor() == null || meta.getAuthor().isEmpty()) { - meta.setAuthor(superMeta.getAuthor()); - } - if (meta.getDate() == null || meta.getDate().isEmpty()) { - meta.setDate(superMeta.getDate()); - } - if (meta.getUrl() == null || meta.getUrl().isEmpty()) { - meta.setUrl(superMeta.getUrl()); - } - } - - return meta; + return InfoReader.readMeta(getInfoFile(), true); } @Override diff --combined src/be/nikiroo/fanfix/supported/Text.java index c54b6a5,e082a73..e082a73 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@@ -97,7 -97,7 +97,7 @@@ class Text extends BasicSupport meta.setType(getType().toString()); meta.setImageDocument(false); meta.setCover(getCover(getSourceFile())); - + return meta; } @@@ -188,7 -188,7 +188,7 @@@ return content; } - private Image getCover(File sourceFile) { + protected Image getCover(File sourceFile) { String path = sourceFile.getName(); for (String ext : new String[] { ".txt", ".text", ".story" }) { @@@ -317,7 -317,7 +317,7 @@@ } /** - * Remove the ".txt" extension if it is present. + * Remove the ".txt" (or ".text") extension if it is present. * * @param file * the file to process @@@ -326,9 -326,11 +326,11 @@@ * was present */ protected File assureNoTxt(File file) { - if (file.getName().endsWith(".txt")) { - file = new File(file.getPath().substring(0, - file.getPath().length() - 4)); + 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;