import be.nikiroo.fanfix.bundles.Config;
import be.nikiroo.fanfix.bundles.ConfigBundle;
import be.nikiroo.fanfix.bundles.StringIdBundle;
+import be.nikiroo.fanfix.output.BasicOutput.OutputType;
import be.nikiroo.utils.resources.Bundles;
/**
trans = new StringIdBundle(getLang());
try {
- lib = new Library(getFile(Config.LIBRARY_DIR));
+ lib = new Library(getFile(Config.LIBRARY_DIR),
+ OutputType.INFO_TEXT, OutputType.CBZ);
} catch (Exception e) {
syserr(new IOException("Cannot create library for directory: "
+ getFile(Config.LIBRARY_DIR), e));
}
-
+
debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
coverDir = getFile(Config.DEFAULT_COVERS_DIR);
File tmp = getFile(Config.CACHE_DIR);
private File baseDir;
private Map<MetaData, File> stories;
private int lastId;
+ private OutputType text;
+ private OutputType image;
/**
* Create a new {@link Library} with the given backend directory.
*
* @param dir
- * the directoy where to find the {@link Story} objects
+ * the directory where to find the {@link Story} objects
+ * @param text
+ * the {@link OutputType} to save the text-focused stories into
+ * @param image
+ * the {@link OutputType} to save the images-focused stories into
*/
- public Library(File dir) {
+ public Library(File dir, OutputType text, OutputType image) {
this.baseDir = dir;
this.stories = new HashMap<MetaData, File>();
this.lastId = 0;
+ this.text = text;
+ this.image = image;
dir.mkdirs();
}
return list;
}
+ /**
+ * Retrieve a {@link File} corresponding to the given {@link Story}.
+ *
+ * @param luid
+ * the Library UID of the story
+ *
+ * @return the corresponding {@link Story}
+ */
+ public File getFile(String luid) {
+ if (luid != null) {
+ for (Entry<MetaData, File> entry : getStories().entrySet()) {
+ if (luid.equals(entry.getKey().getLuid())) {
+ return entry.getValue();
+ }
+ }
+ }
+
+ return null;
+ }
+
/**
* Retrieve a specific {@link Story}.
*
throw new IOException("URL not supported: " + url.toString());
}
- getStories(); // refresh lastId
- Story story = support.process(url);
- story.getMeta().setLuid(String.format("%03d", (++lastId)));
- save(story);
-
- return story;
+ return save(support.process(url), null);
}
/**
}
/**
- * Save a story as-is to the {@link Library} -- the LUID <b>must</b> be
- * correct.
+ * Save a {@link Story} to the {@link Library}.
+ *
+ * @param story
+ * the {@link Story} to save
+ *
+ * @return the same {@link Story}, whose LUID may have changed
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ public Story save(Story story) throws IOException {
+ return save(story, null);
+ }
+
+ /**
+ * Save a {@link Story} to the {@link Library} -- the LUID <b>must</b> be
+ * correct, or NULL to get the next free one.
*
* @param story
* the {@link Story} to save
+ * @param luid
+ * the <b>correct</b> LUID or NULL to get the next free one
+ *
+ * @return the same {@link Story}, whose LUID may have changed
*
* @throws IOException
* in case of I/O error
*/
- private void save(Story story) throws IOException {
+ private Story save(Story story, String luid) throws IOException {
MetaData key = story.getMeta();
+ if (luid == null || luid.isEmpty()) {
+ getStories(); // refresh lastId if needed
+ key.setLuid(String.format("%03d", (++lastId)));
+ } else {
+ key.setLuid(luid);
+ }
+
getDir(key).mkdirs();
if (!getDir(key).exists()) {
throw new IOException("Cannot create library dir");
}
OutputType out;
- SupportType in;
if (key != null && key.isImageDocument()) {
- in = SupportType.CBZ;
- out = OutputType.CBZ;
+ out = image;
} else {
- in = SupportType.INFO_TEXT;
- out = OutputType.INFO_TEXT;
+ out = text;
}
+
BasicOutput it = BasicOutput.getOutput(out, true);
File file = it.process(story, getFile(key).getPath());
- getStories().put(
- BasicSupport.getSupport(in).processMeta(file.toURI().toURL())
- .getMeta(), file);
+ getStories().put(story.getMeta(), file);
+
+ return story;
}
/**
if (stories.isEmpty()) {
lastId = 0;
+ String ext = ".info";
for (File dir : baseDir.listFiles()) {
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
try {
- if (file.getPath().toLowerCase().endsWith(".info")) {
+ if (file.getPath().toLowerCase().endsWith(ext)) {
MetaData meta = InfoReader.readMeta(file);
try {
int id = Integer.parseInt(meta.getLuid());
lastId = id;
}
+ // Replace .info with whatever is needed:
+ String path = file.getPath();
+ path = path.substring(0, path.length()
+ - ext.length());
+
+ String newExt = getOutputType(meta)
+ .getDefaultExtension();
+
+ file = new File(path + newExt);
+ //
+
stories.put(meta, file);
} catch (Exception e) {
return stories;
}
+
+ /**
+ * Return the {@link OutputType} for this {@link Story}.
+ *
+ * @param meta
+ * the {@link Story} {@link MetaData}
+ *
+ * @return the type
+ */
+ private OutputType getOutputType(MetaData meta) {
+ if (meta != null && meta.isImageDocument()) {
+ return image;
+ } else {
+ return text;
+ }
+ }
}
LATEX_LANG_EN, //
@Meta(what = "LaTeX output language", where = "LaTeX", format = "", info = "LaTeX full name for French")
LATEX_LANG_FR, //
- @Meta(what = "other 'by' prefixes before author name", where = "", format = "coma-separated list", info = "used to identify the author")
+ @Meta(what = "other 'by' prefixes before author name", where = "", format = "comma-separated list", info = "used to identify the author")
BYS, //
- @Meta(what = "Chapter identification languages", where = "", format = "coma-separated list", info = "used to identify a starting chapter in text mode")
+ @Meta(what = "Chapter identification languages", where = "", format = "comma-separated list", info = "used to identify a starting chapter in text mode")
CHAPTER, //
@Meta(what = "Chapter identification string", where = "", format = "", info = "used to identify a starting chapter in text mode")
CHAPTER_EN, //
# (WHAT: LaTeX output language, WHERE: LaTeX)
# LaTeX full name for French
LATEX_LANG_FR = french
-# (WHAT: other 'by' prefixes before author name, FORMAT: coma-separated list)
+# (WHAT: other 'by' prefixes before author name, FORMAT: comma-separated list)
# used to identify the author
BYS = by,par,de,©,(c)
-# (WHAT: Chapter identification languages, FORMAT: coma-separated list)
+# (WHAT: Chapter identification languages, FORMAT: comma-separated list)
# used to identify a starting chapter in text mode
CHAPTER = EN,FR
# (WHAT: Chapter identification string)
private BufferedImage cover;
private String subject;
private String source;
+ private String url;
private String uuid;
private String luid;
private String lang;
}
/**
- * The source of this story (where it was downloaded from).
+ * The source of this story (which online library it was downloaded from).
*
* @return the source
*/
}
/**
- * The source of this story (where it was downloaded from).
+ * The source of this story (which online library it was downloaded from).
*
* @param source
* the source to set
}
/**
- * A unique value representing the story (it is often an URL).
+ * The original URL from which this {@link Story} was imported.
+ *
+ * @return the url
+ */
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ * The original URL from which this {@link Story} was imported.
+ *
+ * @param url
+ * the new url to set
+ */
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ /**
+ * A unique value representing the story (it is often a URL).
*
* @return the uuid
*/
}
/**
- * A unique value representing the story (it is often an URL).
+ * A unique value representing the story (it is often a URL).
*
* @param uuid
* the uuid to set
/** ZIP with (PNG) images */
CBZ,
/** LaTeX file with "book" template */
- LATEX;
+ LATEX,
+ /** HTML files in a dedicated directory */
+ HTML,
+
+ ;
+
public String toString() {
return super.toString().toLowerCase();
}
return desc;
}
+ /**
+ * The default extension to add to the output files.
+ *
+ * @return the extension
+ */
+ public String getDefaultExtension() {
+ BasicOutput output = BasicOutput.getOutput(this, false);
+ if (output != null) {
+ return output.getDefaultExtension();
+ }
+
+ return null;
+ }
+
/**
* Call {@link OutputType#valueOf(String.toUpperCase())}.
*
/**
* The default extension to add to the output files.
- * <p>
- * Cannot be NULL!
*
* @return the extension
*/
- protected String getDefaultExtension() {
+ public String getDefaultExtension() {
return "";
}
return new Cbz().setType(type, infoCover, infoCover);
case LATEX:
return new LaTeX().setType(type, infoCover, infoCover);
+ case HTML:
+ return new Html().setType(type, false, false);
}
}
}
@Override
- protected String getDefaultExtension() {
+ public String getDefaultExtension() {
return ".cbz";
}
}
@Override
- protected String getDefaultExtension() {
+ public String getDefaultExtension() {
return ".epub";
}
--- /dev/null
+package be.nikiroo.fanfix.output;
+
+//TODO: implement it for LocalReader
+class Html extends Text {
+
+}
}
}
- String lang = meta.getLang();
- if (lang != null) {
- lang = lang.toLowerCase();
- }
-
writeMeta(infoWriter, "TITLE", meta.getTitle());
writeMeta(infoWriter, "AUTHOR", meta.getAuthor());
writeMeta(infoWriter, "DATE", meta.getDate());
writeMeta(infoWriter, "SUBJECT", meta.getSubject());
writeMeta(infoWriter, "SOURCE", meta.getSource());
+ writeMeta(infoWriter, "URL", meta.getUrl());
writeMeta(infoWriter, "TAGS", tags);
writeMeta(infoWriter, "UUID", meta.getUuid());
writeMeta(infoWriter, "LUID", meta.getLuid());
- writeMeta(infoWriter, "LANG", lang);
+ writeMeta(infoWriter, "LANG", meta.getLang() == null ? ""
+ : meta.getLang().toLowerCase());
writeMeta(infoWriter, "IMAGES_DOCUMENT",
meta.isImageDocument() ? "true" : "false");
writeMeta(infoWriter, "TYPE", meta.getType());
StringId.CLOSE_DOUBLE_QUOTE);
@Override
- protected String getDefaultExtension() {
+ public String getDefaultExtension() {
return "";
}
}
@Override
- protected String getDefaultExtension() {
+ public String getDefaultExtension() {
return ".tex";
}
}
@Override
- protected String getDefaultExtension() {
+ public String getDefaultExtension() {
return ".txt";
}
meta.setDate("");
meta.setTags(new ArrayList<String>()); // TODDO ???
meta.setSource(getSourceName());
+ meta.setUrl(source.toString());
meta.setPublisher(getSourceName());
meta.setUuid(source.toString());
meta.setLuid("");
meta.setLang("EN");
meta.setTags(new ArrayList<String>());
meta.setSource(getSourceName());
+ meta.setUrl(source.toString());
}
}
meta.setDate(getDate(reset(in)));
meta.setTags(getTags(reset(in)));
meta.setSource(getSourceName());
+ meta.setUrl(source.toString());
meta.setPublisher(getSourceName());
meta.setUuid(source.toString());
meta.setLuid("");
meta.setDate(getDate(reset(in)));
meta.setTags(getTags(reset(in)));
meta.setSource(getSourceName());
+ meta.setUrl(source.toString());
meta.setPublisher(getSourceName());
meta.setUuid(source.toString());
meta.setLuid("");
meta.setDate(getInfoTag(in, "DATE"));
meta.setTags(getInfoTagList(in, "TAGS", ","));
meta.setSource(getInfoTag(in, "SOURCE"));
+ meta.setUrl(getInfoTag(in, "URL"));
meta.setPublisher(getInfoTag(in, "PUBLISHER"));
meta.setUuid(getInfoTag(in, "UUID"));
meta.setLuid(getInfoTag(in, "LUID"));
meta.setDate(getDate(reset(in)));
meta.setTags(getTags(reset(in)));
meta.setSource(getSourceName());
+ meta.setUrl(source.toString());
meta.setPublisher(getSourceName());
meta.setUuid(source.toString());
meta.setLuid("");
meta.setDate(getDate(reset(in)));
meta.setTags(new ArrayList<String>());
meta.setSource(getSourceName());
- meta.setPublisher(""); // often sourceName
+ meta.setUrl(source.toString());
+ meta.setPublisher("");
meta.setUuid(source.toString());
meta.setLuid("");
meta.setLang(getLang(source, reset(in))); // default is EN