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 * TRUE to point to the main {@link Story} entry point for a
83 * reader (for instance, the main entry point if this
84 * {@link Story} is in a directory bundle), FALSE to point to
85 * the main file even if it is a directory for instance
87 * @return the extension
89 public String
getDefaultExtension(boolean readerTarget
) {
90 BasicOutput output
= BasicOutput
.getOutput(this, false, false);
92 return output
.getDefaultExtension(readerTarget
);
99 * Call {@link OutputType#valueOf(String)} after conversion to upper
103 * the possible type name
105 * @return NULL or the type
107 public static OutputType
valueOfUC(String typeName
) {
108 return OutputType
.valueOf(typeName
== null ?
null : typeName
113 * Call {@link OutputType#valueOf(String)} after conversion to upper
114 * case but return def for NULL and empty instead of raising an
118 * the possible type name
122 * @return NULL or the type
124 public static OutputType
valueOfNullOkUC(String typeName
, OutputType def
) {
125 if (typeName
== null || typeName
.isEmpty()) {
129 return OutputType
.valueOfUC(typeName
);
133 * Call {@link OutputType#valueOf(String)} after conversion to upper
134 * case but return def in case of error instead of raising an exception.
137 * the possible type name
141 * @return NULL or the type
143 public static OutputType
valueOfAllOkUC(String typeName
, OutputType def
) {
145 return OutputType
.valueOfUC(typeName
);
146 } catch (Exception e
) {
152 /** The creator name (this program, by me!) */
153 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
155 /** The current best name for an image */
156 private String imageName
;
157 private File targetDir
;
158 private String targetName
;
159 private OutputType type
;
160 private boolean writeCover
;
161 private boolean writeInfo
;
162 private Progress storyPg
;
163 private Progress chapPg
;
166 * Process the {@link Story} into the given target.
169 * the {@link Story} to export
171 * the target where to save to (will not necessary be taken as is
172 * by the processor, for instance an extension can be added)
174 * the optional progress reporter
176 * @return the actual main target saved, which can be slightly different
179 * @throws IOException
180 * in case of I/O error
182 public File
process(Story story
, String target
, Progress pg
)
186 File targetDir
= null;
187 String targetName
= null;
188 if (target
!= null) {
189 target
= new File(target
).getAbsolutePath();
190 targetDir
= new File(target
).getParentFile();
191 targetName
= new File(target
).getName();
193 String ext
= getDefaultExtension(false);
194 if (ext
!= null && !ext
.isEmpty()) {
195 if (targetName
.toLowerCase().endsWith(ext
)) {
196 targetName
= targetName
.substring(0, targetName
.length()
202 return process(story
, targetDir
, targetName
);
206 * Process the {@link Story} into the given target.
208 * This method is expected to be overridden in most cases.
211 * the {@link Story} to export
213 * the target dir where to save to
215 * the target filename (will not necessary be taken as is by the
216 * processor, for instance an extension can be added)
219 * @return the actual main target saved, which can be slightly different
222 * @throws IOException
223 * in case of I/O error
225 protected File
process(Story story
, File targetDir
, String targetName
)
227 this.targetDir
= targetDir
;
228 this.targetName
= targetName
;
240 public OutputType
getType() {
245 * Enable the creation of a .info file next to the resulting processed file.
247 * @return TRUE to enable it
249 protected boolean isWriteInfo() {
254 * Enable the creation of a cover file next to the resulting processed file
257 * @return TRUE to enable it
259 protected boolean isWriteCover() {
269 * TRUE to enable the creation of a cover if possible
271 * TRUE to enable the creation of a .info file
275 protected BasicOutput
setType(OutputType type
, boolean writeInfo
,
276 boolean writeCover
) {
278 this.writeInfo
= writeInfo
;
279 this.writeCover
= writeCover
;
285 * The default extension to add to the output files.
287 * @param readerTarget
288 * TRUE to point to the main {@link Story} entry point for a
289 * reader (for instance, the main entry point if this
290 * {@link Story} is in a directory bundle), FALSE to point to the
291 * main file even if it is a directory for instance
293 * @return the extension
295 public String
getDefaultExtension(
296 @SuppressWarnings("unused") boolean readerTarget
) {
300 @SuppressWarnings("unused")
301 protected void writeStoryHeader(Story story
) throws IOException
{
304 @SuppressWarnings("unused")
305 protected void writeChapterHeader(Chapter chap
) throws IOException
{
308 @SuppressWarnings("unused")
309 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
312 @SuppressWarnings("unused")
313 protected void writeStoryFooter(Story story
) throws IOException
{
316 @SuppressWarnings("unused")
317 protected void writeChapterFooter(Chapter chap
) throws IOException
{
320 @SuppressWarnings("unused")
321 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
324 protected void writeStory(Story story
) throws IOException
{
325 if (storyPg
== null) {
326 storyPg
= new Progress(0, story
.getChapters().size() + 2);
328 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
331 String chapterNameNum
= String
.format("%03d", 0);
332 String paragraphNumber
= String
.format("%04d", 0);
333 imageName
= paragraphNumber
+ "_" + chapterNameNum
;
335 if (story
.getMeta() != null) {
336 story
.getMeta().setType("" + getType());
339 if (isWriteCover()) {
340 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
343 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
346 storyPg
.setProgress(1);
348 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
350 for (Chapter chap
: story
) {
351 chapPg
= new Progress(0, chap
.getParagraphs().size());
352 storyPg
.addProgress(chapPg
, 1);
357 writeStoryHeader(story
);
358 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
359 chapPg
= chapPgs
.get(i
);
360 writeChapter(story
.getChapters().get(i
));
361 chapPg
.setProgress(chapPg
.getMax());
364 writeStoryFooter(story
);
366 storyPg
.setProgress(storyPg
.getMax());
370 protected void writeChapter(Chapter chap
) throws IOException
{
371 String chapterNameNum
;
372 if (chap
.getName() == null || chap
.getName().isEmpty()) {
373 chapterNameNum
= String
.format("%03d", chap
.getNumber());
375 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
376 + chap
.getName().replace(" ", "_");
380 String paragraphNumber
= String
.format("%04d", num
++);
381 imageName
= chapterNameNum
+ "_" + paragraphNumber
;
383 writeChapterHeader(chap
);
385 for (Paragraph para
: chap
) {
386 paragraphNumber
= String
.format("%04d", num
++);
387 imageName
= chapterNameNum
+ "_" + paragraphNumber
;
388 writeParagraph(para
);
389 if (chapPg
!= null) {
390 chapPg
.setProgress(i
++);
393 writeChapterFooter(chap
);
396 protected void writeParagraph(Paragraph para
) throws IOException
{
397 writeParagraphHeader(para
);
398 writeTextLine(para
.getType(), para
.getContent());
399 writeParagraphFooter(para
);
402 @SuppressWarnings("unused")
403 protected void writeTextLine(ParagraphType type
, String line
)
408 * Return the current best guess for an image name, based upon the current
409 * {@link Chapter} and {@link Paragraph}.
412 * add the original target name as a prefix
414 * @return the guessed name
416 protected String
getCurrentImageBestName(boolean prefix
) {
418 return targetName
+ "_" + imageName
;
425 * Return the given word or sentence as <b>bold</b>.
430 * @return the bold output
432 protected String
enbold(String word
) {
437 * Return the given word or sentence as <i>italic</i>.
442 * @return the italic output
444 protected String
italize(String word
) {
449 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
450 * according to {@link BasicOutput#enbold(String)} and
451 * {@link BasicOutput#italize(String)}.
456 * @return the decorated output
458 protected String
decorateText(String text
) {
459 StringBuilder builder
= new StringBuilder();
464 for (char car
: text
.toCharArray()) {
467 if (bold
>= 0 && prev
!= ' ') {
468 String data
= builder
.substring(bold
);
469 builder
.setLength(bold
);
470 builder
.append(enbold(data
));
473 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
474 bold
= builder
.length();
481 if (italic
>= 0 && prev
!= ' ') {
482 String data
= builder
.substring(italic
);
483 builder
.setLength(italic
);
484 builder
.append(enbold(data
));
486 } else if (italic
< 0
487 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
488 italic
= builder
.length();
503 builder
.insert(bold
, '*');
507 builder
.insert(italic
, '_');
510 return builder
.toString();
514 * Return a {@link BasicOutput} object compatible with the given
515 * {@link OutputType}.
520 * TRUE to enable the creation of a cover if possible to be saved
521 * next to the main target file
523 * TRUE to enable the creation of a .info file to be saved next
524 * to the main target file
526 * @return the {@link BasicOutput}
528 public static BasicOutput
getOutput(OutputType type
, boolean writeInfo
,
529 boolean writeCover
) {
533 return new Epub().setType(type
, writeInfo
, writeCover
);
535 return new Text().setType(type
, writeInfo
, true);
537 return new InfoText().setType(type
, true, true);
539 return new Sysout().setType(type
, false, false);
541 return new Cbz().setType(type
, writeInfo
, writeCover
);
543 return new LaTeX().setType(type
, writeInfo
, writeCover
);
545 return new Html().setType(type
, writeInfo
, writeCover
);