1 package be
.nikiroo
.fanfix
.output
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
8 import be
.nikiroo
.fanfix
.Instance
;
9 import be
.nikiroo
.fanfix
.bundles
.StringId
;
10 import be
.nikiroo
.fanfix
.data
.Chapter
;
11 import be
.nikiroo
.fanfix
.data
.Paragraph
;
12 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.utils
.Progress
;
17 * This class is the base class used by the other output classes. It can be used
18 * outside of this package, and have static method that you can use to get
19 * access to the correct support class.
23 public abstract class BasicOutput
{
25 * The supported output types for which we can get a {@link BasicOutput}
30 public enum OutputType
{
31 /** EPUB files created with this program */
33 /** Pure text file with some rules */
35 /** TEXT but with associated .info file */
37 /** DEBUG output to console */
39 /** ZIP with (PNG) images */
41 /** LaTeX file with "book" template */
43 /** HTML files in a dedicated directory */
49 public String
toString() {
50 return super.toString().toLowerCase();
54 * A description of this output type.
57 * TRUE for the long description, FALSE for the short one
59 * @return the description
61 public String
getDesc(boolean longDesc
) {
62 StringId id
= longDesc ? StringId
.OUTPUT_DESC
63 : StringId
.OUTPUT_DESC_SHORT
;
65 String desc
= Instance
.getTrans().getStringX(id
, this.name());
68 desc
= Instance
.getTrans().getString(id
, this);
72 desc
= this.toString();
79 * The default extension to add to the output files.
82 * the target to point to to read the {@link Story} (for
83 * instance, the main entry point if this {@link Story} is in
86 * @return the extension
88 public String
getDefaultExtension(boolean readerTarget
) {
89 BasicOutput output
= BasicOutput
.getOutput(this, false);
91 return output
.getDefaultExtension(readerTarget
);
98 * Call {@link OutputType#valueOf(String)} after conversion to upper
102 * the possible type name
104 * @return NULL or the type
106 public static OutputType
valueOfUC(String typeName
) {
107 return OutputType
.valueOf(typeName
== null ?
null : typeName
112 * Call {@link OutputType#valueOf(String)} after conversion to upper
113 * case but return NULL for NULL and empty instead of raising an
117 * the possible type name
119 * @return NULL or the type
121 public static OutputType
valueOfNullOkUC(String typeName
) {
122 if (typeName
== null || typeName
.isEmpty()) {
126 return OutputType
.valueOfUC(typeName
);
130 * Call {@link OutputType#valueOf(String)} after conversion to upper
131 * case but return NULL in case of error instead of raising an
135 * the possible type name
137 * @return NULL or the type
139 public static OutputType
valueOfAllOkUC(String typeName
) {
141 return OutputType
.valueOfUC(typeName
);
142 } catch (Exception e
) {
148 /** The creator name (this program, by me!) */
149 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
151 /** The current best name for an image */
152 private String imageName
;
153 private File targetDir
;
154 private String targetName
;
155 private OutputType type
;
156 private boolean writeCover
;
157 private boolean writeInfo
;
158 private Progress storyPg
;
159 private Progress chapPg
;
162 * Process the {@link Story} into the given target.
165 * the {@link Story} to export
167 * the target where to save to (will not necessary be taken as is
168 * by the processor, for instance an extension can be added)
170 * the optional progress reporter
172 * @return the actual main target saved, which can be slightly different
175 * @throws IOException
176 * in case of I/O error
178 public File
process(Story story
, String target
, Progress pg
)
182 File targetDir
= null;
183 String targetName
= null;
184 if (target
!= null) {
185 target
= new File(target
).getAbsolutePath();
186 targetDir
= new File(target
).getParentFile();
187 targetName
= new File(target
).getName();
189 String ext
= getDefaultExtension(false);
190 if (ext
!= null && !ext
.isEmpty()) {
191 if (targetName
.toLowerCase().endsWith(ext
)) {
192 targetName
= targetName
.substring(0, targetName
.length()
198 return process(story
, targetDir
, targetName
);
202 * Process the {@link Story} into the given target.
204 * This method is expected to be overridden in most cases.
207 * the {@link Story} to export
209 * the target dir where to save to
211 * the target filename (will not necessary be taken as is by the
212 * processor, for instance an extension can be added)
215 * @return the actual main target saved, which can be slightly different
218 * @throws IOException
219 * in case of I/O error
221 protected File
process(Story story
, File targetDir
, String targetName
)
223 this.targetDir
= targetDir
;
224 this.targetName
= targetName
;
236 public OutputType
getType() {
246 * TRUE to enable the creation of a .info file
248 * TRUE to enable the creation of a cover if possible
252 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
255 this.writeCover
= writeCover
;
256 this.writeInfo
= writeInfo
;
262 * The default extension to add to the output files.
264 * @param readerTarget
265 * the target to point to to read the {@link Story} (for
266 * instance, the main entry point if this {@link Story} is in a
269 * @return the extension
271 public String
getDefaultExtension(
272 @SuppressWarnings("unused") boolean readerTarget
) {
276 @SuppressWarnings("unused")
277 protected void writeStoryHeader(Story story
) throws IOException
{
280 @SuppressWarnings("unused")
281 protected void writeChapterHeader(Chapter chap
) throws IOException
{
284 @SuppressWarnings("unused")
285 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
288 @SuppressWarnings("unused")
289 protected void writeStoryFooter(Story story
) throws IOException
{
292 @SuppressWarnings("unused")
293 protected void writeChapterFooter(Chapter chap
) throws IOException
{
296 @SuppressWarnings("unused")
297 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
300 protected void writeStory(Story story
) throws IOException
{
301 if (storyPg
== null) {
302 storyPg
= new Progress(0, story
.getChapters().size() + 2);
304 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
307 String chapterNameNum
= String
.format("%03d", 0);
308 String paragraphNumber
= String
.format("%04d", 0);
309 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
311 if (story
.getMeta() != null) {
312 story
.getMeta().setType("" + getType());
316 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
319 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
322 storyPg
.setProgress(1);
324 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
326 for (Chapter chap
: story
) {
327 chapPg
= new Progress(0, chap
.getParagraphs().size());
328 storyPg
.addProgress(chapPg
, 1);
333 writeStoryHeader(story
);
334 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
335 chapPg
= chapPgs
.get(i
);
336 writeChapter(story
.getChapters().get(i
));
337 chapPg
.setProgress(chapPg
.getMax());
340 writeStoryFooter(story
);
342 storyPg
.setProgress(storyPg
.getMax());
346 protected void writeChapter(Chapter chap
) throws IOException
{
347 String chapterNameNum
;
348 if (chap
.getName() == null || chap
.getName().isEmpty()) {
349 chapterNameNum
= String
.format("%03d", chap
.getNumber());
351 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
352 + chap
.getName().replace(" ", "_");
356 String paragraphNumber
= String
.format("%04d", num
++);
357 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
359 writeChapterHeader(chap
);
361 for (Paragraph para
: chap
) {
362 paragraphNumber
= String
.format("%04d", num
++);
363 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
364 writeParagraph(para
);
365 if (chapPg
!= null) {
366 chapPg
.setProgress(i
++);
369 writeChapterFooter(chap
);
372 protected void writeParagraph(Paragraph para
) throws IOException
{
373 writeParagraphHeader(para
);
374 writeTextLine(para
.getType(), para
.getContent());
375 writeParagraphFooter(para
);
378 @SuppressWarnings("unused")
379 protected void writeTextLine(ParagraphType type
, String line
)
384 * Return the current best guess for an image name, based upon the current
385 * {@link Chapter} and {@link Paragraph}.
388 * add the original target name as a prefix
390 * @return the guessed name
392 protected String
getCurrentImageBestName(boolean prefix
) {
394 return targetName
+ "_" + imageName
;
401 * Return the given word or sentence as <b>bold</b>.
406 * @return the bold output
408 protected String
enbold(String word
) {
413 * Return the given word or sentence as <i>italic</i>.
418 * @return the italic output
420 protected String
italize(String word
) {
425 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
426 * according to {@link BasicOutput#enbold(String)} and
427 * {@link BasicOutput#italize(String)}.
432 * @return the decorated output
434 protected String
decorateText(String text
) {
435 StringBuilder builder
= new StringBuilder();
440 for (char car
: text
.toCharArray()) {
443 if (bold
>= 0 && prev
!= ' ') {
444 String data
= builder
.substring(bold
);
445 builder
.setLength(bold
);
446 builder
.append(enbold(data
));
449 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
450 bold
= builder
.length();
457 if (italic
>= 0 && prev
!= ' ') {
458 String data
= builder
.substring(italic
);
459 builder
.setLength(italic
);
460 builder
.append(enbold(data
));
462 } else if (italic
< 0
463 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
464 italic
= builder
.length();
479 builder
.insert(bold
, '*');
483 builder
.insert(italic
, '_');
486 return builder
.toString();
490 * Return a {@link BasicOutput} object compatible with the given
491 * {@link OutputType}.
496 * force the <tt>.info</tt> file and the cover to be saved next
497 * to the main target file
499 * @return the {@link BasicOutput}
501 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
505 return new Epub().setType(type
, infoCover
, infoCover
);
507 return new Text().setType(type
, true, infoCover
);
509 return new InfoText().setType(type
, true, true);
511 return new Sysout().setType(type
, false, false);
513 return new Cbz().setType(type
, infoCover
, infoCover
);
515 return new LaTeX().setType(type
, infoCover
, infoCover
);
517 return new Html().setType(type
, infoCover
, infoCover
);