/**
* The meta data associated to a {@link Story} object.
+ * <p>
+ * Note that some earlier version of the program did not save the resume as an
+ * external file; for those stories, the resume is not fetched until the story
+ * is.
+ * <p>
+ * The cover is never fetched until the story is.
*
* @author niki
*/
* The story resume (a.k.a. description).
* <p>
* This can be NULL if we don't have a resume for this {@link Story}.
+ * <p>
+ * Note that some earlier version of the program did not save the resume as
+ * an external file; for those stories, the resume is not fetched until the
+ * story is.
*
* @return the resume
*/
/**
* The story resume (a.k.a. description).
+ * <p>
+ * Note that some earlier version of the program did not save the resume as
+ * an external file; for those stories, the resume is not fetched until the
+ * story is.
*
* @param resume
* the resume to set
/**
* The cover image of the story if any (can be NULL).
+ * <p>
+ * The cover is not fetched until the story is.
*
* @return the cover
*/
/**
* The cover image of the story if any (can be NULL).
+ * <p>
+ * The cover is not fetched until the story is.
*
* @param cover
* the cover to set
/**
* Retrieve a specific {@link Story}.
+ * <p>
+ * Note that it will update both the cover and the resume in <tt>meta</tt>.
*
* @param luid
* the Library UID of the story
/**
* Retrieve a specific {@link Story}.
+ * <p>
+ * Note that it will update both the cover and the resume in <tt>meta</tt>.
*
* @param luid
* the LUID of the story
files.add(coverFile);
}
+ String summaryExt = ".summary";
+ File summaryFile = new File(path + summaryExt);
+ if (!summaryFile.exists()) {
+ summaryFile = new File(
+ path.substring(0, path.length() - fileExt.length())
+ + summaryExt);
+ }
+
+ if (summaryFile.exists()) {
+ files.add(summaryFile);
+ }
+
return files;
}
return "";
}
- @SuppressWarnings("unused")
protected void writeStoryHeader(Story story) throws IOException {
}
- @SuppressWarnings("unused")
protected void writeChapterHeader(Chapter chap) throws IOException {
}
- @SuppressWarnings("unused")
protected void writeParagraphHeader(Paragraph para) throws IOException {
}
- @SuppressWarnings("unused")
protected void writeStoryFooter(Story story) throws IOException {
}
- @SuppressWarnings("unused")
protected void writeChapterFooter(Chapter chap) throws IOException {
}
- @SuppressWarnings("unused")
protected void writeParagraphFooter(Paragraph para) throws IOException {
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
+import java.util.Arrays;
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.Story;
+/**
+ * Helper class to write info, cover and summary (resume) files.
+ *
+ * @author niki
+ */
public class InfoCover {
+ /**
+ * Write both the <tt>.info<tt> and the <tt>.summary</tt> files by taking
+ * the information from the {@link MetaData}.
+ *
+ * @param targetDir
+ * the directory where to write the 2 files
+ * @param targetName
+ * the target name (no extension) to use (so you will get
+ * <tt>targetName.info</tt> and <tt>targetName.summary</tt>)
+ * @param meta
+ * the {@link MetaData} to get the data out of
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
public static void writeInfo(File targetDir, String targetName,
MetaData meta) throws IOException {
File info = new File(targetDir, targetName + ".info");
- BufferedWriter infoWriter = null;
- try {
- infoWriter = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(info), "UTF-8"));
+ if (meta != null) {
+ BufferedWriter infoWriter = null;
+ try {
+ infoWriter = new BufferedWriter(new OutputStreamWriter(
+ new FileOutputStream(info), "UTF-8"));
- if (meta != null) {
String tags = "";
if (meta.getTags() != null) {
for (String tag : meta.getTags()) {
writeMeta(infoWriter, "TYPE", meta.getType());
if (meta.getCover() != null) {
String format = Instance.getInstance().getConfig()
- .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
+ .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER)
+ .toLowerCase();
writeMeta(infoWriter, "COVER", targetName + "." + format);
} else {
writeMeta(infoWriter, "COVER", "");
writeMeta(infoWriter, "CREATION_DATE", meta.getCreationDate());
writeMeta(infoWriter, "FAKE_COVER",
Boolean.toString(meta.isFakeCover()));
+ } finally {
+ if (infoWriter != null) {
+ infoWriter.close();
+ }
}
- } finally {
- if (infoWriter != null) {
- infoWriter.close();
+
+ if (meta.getResume() != null) {
+ Story fakeStory = new Story();
+ fakeStory.setMeta(meta);
+ fakeStory.setChapters(Arrays.asList(meta.getResume()));
+
+ Text summaryText = new Text() {
+ @Override
+ protected boolean isWriteCover() {
+ return false;
+ }
+
+ @Override
+ protected boolean isWriteInfo() {
+ return false; // infinite loop if not!
+ }
+
+ @Override
+ public String getDefaultExtension(boolean readerTarget) {
+ return ".summary";
+ }
+
+ @Override
+ protected void writeStoryHeader(Story story)
+ throws IOException {
+ }
+
+ @Override
+ protected void writeStoryFooter(Story story)
+ throws IOException {
+ }
+
+ @Override
+ protected void writeChapterHeader(Chapter chap)
+ throws IOException {
+ }
+
+ @Override
+ protected void writeChapterFooter(Chapter chap)
+ throws IOException {
+ }
+ };
+
+ summaryText.process(fakeStory, targetDir, targetName);
}
}
}
+ /**
+ * Write the cover file.
+ *
+ * @param targetDir
+ * the target directory
+ * @param targetName
+ * the target name for the cover (the extension will be added)
+ * @param meta
+ * the meta to get the information out of
+ */
public static void writeCover(File targetDir, String targetName,
MetaData meta) {
if (meta != null && meta.getCover() != null) {
BasicSupportHelper bsHelper;
BasicSupportImages bsImages;
+ /**
+ * Create a new {@link BasicSupportPara}.
+ * <p>
+ * Note that you need an instance of both {@link BasicSupportHelper} and
+ * {@link BasicSupportImages} for it to work.
+ *
+ * @param bsHelper
+ * the required {@link BasicSupportHelper}
+ * @param bsImages
+ * the required {@link BasicSupportImages}
+ */
public BasicSupportPara(BasicSupportHelper bsHelper, BasicSupportImages bsImages) {
this.bsHelper = bsHelper;
this.bsImages = bsImages;
* the content as it should be.
*
* @param support
- * the linked {@link BasicSupport}
+ * the linked {@link BasicSupport} (can be NULL)
* @param source
* the source of the story (for image lookup in the same path if
* the source is a file, can be NULL)
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.utils.IOUtils;
import be.nikiroo.utils.Image;
import be.nikiroo.utils.streams.MarkableFileInputStream;
-// not complete: no "description" tag
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 BasicSupportImages bsImages = new BasicSupportImages();
+ static protected BasicSupportPara bsPara = new BasicSupportPara(
+ new BasicSupportHelper(), new BasicSupportImages());
public static MetaData readMeta(File infoFile, boolean withCover)
throws IOException {
if (infoFile == null) {
throw new IOException("File is null");
}
+
+ MetaData meta = null;
if (infoFile.exists()) {
InputStream in = new MarkableFileInputStream(infoFile);
try {
- MetaData meta = createMeta(infoFile.toURI().toURL(), in,
+ meta = createMeta(infoFile.toURI().toURL(), in,
withCover);
// Some old .info files were using UUID for URL...
// 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,
}
completeMeta(textFile, meta);
- //
}
- return meta;
+
} finally {
in.close();
}
}
+ if (meta != null) {
+ try {
+ File summaryFile = new File(infoFile.getAbsolutePath()
+ .replaceFirst("\\.info$", ".summary"));
+ InputStream in = new MarkableFileInputStream(summaryFile);
+ try {
+ String content = IOUtils.readSmallStream(in);
+ Chapter desc = bsPara.makeChapter(null, null, 0,
+ "Description", content, false, null);
+ meta.setResume(desc);
+ } finally {
+ in.close();
+ }
+ } catch (IOException e) {
+ // ignore absent or bad summary
+ }
+
+ return meta;
+ }
+
throw new FileNotFoundException(
"File given as argument does not exists: "
+ infoFile.getAbsolutePath());
*/
static public void completeMeta(File textFile,
MetaData meta) throws IOException {
+ // TODO: not nice, would be better to do it properly...
if (textFile != null && textFile.exists()) {
final URL source = textFile.toURI().toURL();
final MetaData[] superMetaA = new MetaData[1];
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;
@Override
protected String getDesc() throws IOException {
- String content = getChapterContent(null, 0, null).trim();
- if (!content.isEmpty()) {
- Chapter desc = bsPara.makeChapter(this, null, 0, "Description",
- content, isHtml(), null);
- StringBuilder builder = new StringBuilder();
- for (Paragraph para : desc) {
- if (builder.length() > 0) {
- builder.append("\n");
- }
- builder.append(para.getContent());
- }
- }
-
- return content;
+ return getChapterContent(null, 0, null).trim();
}
protected Image getCover(File sourceFile) {