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 */
48 public String
toString() {
49 return super.toString().toLowerCase();
53 * A description of this output type.
56 * TRUE for the long description, FALSE for the short one
58 * @return the description
60 public String
getDesc(boolean longDesc
) {
61 StringId id
= longDesc ? StringId
.OUTPUT_DESC
62 : StringId
.OUTPUT_DESC_SHORT
;
64 String desc
= Instance
.getTrans().getStringX(id
, this.name());
67 desc
= Instance
.getTrans().getString(id
, this);
71 desc
= this.toString();
78 * The default extension to add to the output files.
81 * the target to point to to read the {@link Story} (for
82 * instance, the main entry point if this {@link Story} is in
85 * @return the extension
87 public String
getDefaultExtension(boolean readerTarget
) {
88 BasicOutput output
= BasicOutput
.getOutput(this, false);
90 return output
.getDefaultExtension(readerTarget
);
97 * Call {@link OutputType#valueOf(String)} after conversion to upper
101 * the possible type name
103 * @return NULL or the type
105 public static OutputType
valueOfUC(String typeName
) {
106 return OutputType
.valueOf(typeName
== null ?
null : typeName
111 * Call {@link OutputType#valueOf(String)} after conversion to upper
112 * case but return NULL for NULL and empty instead of raising an
116 * the possible type name
118 * @return NULL or the type
120 public static OutputType
valueOfNullOkUC(String typeName
) {
121 if (typeName
== null || typeName
.isEmpty()) {
125 return OutputType
.valueOfUC(typeName
);
129 * Call {@link OutputType#valueOf(String)} after conversion to upper
130 * case but return NULL in case of error instead of raising an
134 * the possible type name
136 * @return NULL or the type
138 public static OutputType
valueOfAllOkUC(String typeName
) {
140 return OutputType
.valueOfUC(typeName
);
141 } catch (Exception e
) {
147 /** The creator name (this program, by me!) */
148 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
150 /** The current best name for an image */
151 private String imageName
;
152 private File targetDir
;
153 private String targetName
;
154 private OutputType type
;
155 private boolean writeCover
;
156 private boolean writeInfo
;
157 private Progress storyPg
;
158 private Progress chapPg
;
161 * Process the {@link Story} into the given target.
164 * the {@link Story} to export
166 * the target where to save to (will not necessary be taken as is
167 * by the processor, for instance an extension can be added)
169 * the optional progress reporter
171 * @return the actual main target saved, which can be slightly different
174 * @throws IOException
175 * in case of I/O error
177 public File
process(Story story
, String target
, Progress pg
)
181 File targetDir
= null;
182 String targetName
= null;
183 if (target
!= null) {
184 target
= new File(target
).getAbsolutePath();
185 targetDir
= new File(target
).getParentFile();
186 targetName
= new File(target
).getName();
188 String ext
= getDefaultExtension(false);
189 if (ext
!= null && !ext
.isEmpty()) {
190 if (targetName
.toLowerCase().endsWith(ext
)) {
191 targetName
= targetName
.substring(0, targetName
.length()
197 return process(story
, targetDir
, targetName
);
201 * Process the {@link Story} into the given target.
203 * This method is expected to be overridden in most cases.
206 * the {@link Story} to export
208 * the target dir where to save to
210 * the target filename (will not necessary be taken as is by the
211 * processor, for instance an extension can be added)
214 * @return the actual main target saved, which can be slightly different
217 * @throws IOException
218 * in case of I/O error
220 protected File
process(Story story
, File targetDir
, String targetName
)
222 this.targetDir
= targetDir
;
223 this.targetName
= targetName
;
235 public OutputType
getType() {
245 * TRUE to enable the creation of a .info file
247 * TRUE to enable the creation of a cover if possible
251 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
254 this.writeCover
= writeCover
;
255 this.writeInfo
= writeInfo
;
261 * The default extension to add to the output files.
263 * @param readerTarget
264 * the target to point to to read the {@link Story} (for
265 * instance, the main entry point if this {@link Story} is in a
268 * @return the extension
270 public String
getDefaultExtension(boolean readerTarget
) {
274 protected void writeStoryHeader(Story story
) throws IOException
{
277 protected void writeChapterHeader(Chapter chap
) throws IOException
{
280 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
283 protected void writeStoryFooter(Story story
) throws IOException
{
286 protected void writeChapterFooter(Chapter chap
) throws IOException
{
289 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
292 protected void writeStory(Story story
) throws IOException
{
293 if (storyPg
== null) {
294 storyPg
= new Progress(0, story
.getChapters().size() + 2);
296 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
299 String chapterNameNum
= String
.format("%03d", 0);
300 String paragraphNumber
= String
.format("%04d", 0);
301 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
303 if (story
.getMeta() != null) {
304 story
.getMeta().setType("" + getType());
308 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
311 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
314 storyPg
.setProgress(1);
316 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
318 for (Chapter chap
: story
) {
319 chapPg
= new Progress(0, chap
.getParagraphs().size());
320 storyPg
.addProgress(chapPg
, 1);
325 writeStoryHeader(story
);
326 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
327 chapPg
= chapPgs
.get(i
);
328 writeChapter(story
.getChapters().get(i
));
329 chapPg
.setProgress(chapPg
.getMax());
332 writeStoryFooter(story
);
334 storyPg
.setProgress(storyPg
.getMax());
338 protected void writeChapter(Chapter chap
) throws IOException
{
339 String chapterNameNum
;
340 if (chap
.getName() == null || chap
.getName().isEmpty()) {
341 chapterNameNum
= String
.format("%03d", chap
.getNumber());
343 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
344 + chap
.getName().replace(" ", "_");
348 String paragraphNumber
= String
.format("%04d", num
++);
349 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
351 writeChapterHeader(chap
);
353 for (Paragraph para
: chap
) {
354 paragraphNumber
= String
.format("%04d", num
++);
355 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
356 writeParagraph(para
);
357 if (chapPg
!= null) {
358 chapPg
.setProgress(i
++);
361 writeChapterFooter(chap
);
364 protected void writeParagraph(Paragraph para
) throws IOException
{
365 writeParagraphHeader(para
);
366 writeTextLine(para
.getType(), para
.getContent());
367 writeParagraphFooter(para
);
370 protected void writeTextLine(ParagraphType type
, String line
)
375 * Return the current best guess for an image name, based upon the current
376 * {@link Chapter} and {@link Paragraph}.
379 * add the original target name as a prefix
381 * @return the guessed name
383 protected String
getCurrentImageBestName(boolean prefix
) {
385 return targetName
+ "_" + imageName
;
392 * Return the given word or sentence as <b>bold</b>.
397 * @return the bold output
399 protected String
enbold(String word
) {
404 * Return the given word or sentence as <i>italic</i>.
409 * @return the italic output
411 protected String
italize(String word
) {
416 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
417 * according to {@link BasicOutput#enbold(String)} and
418 * {@link BasicOutput#italize(String)}.
423 * @return the decorated output
425 protected String
decorateText(String text
) {
426 StringBuilder builder
= new StringBuilder();
431 for (char car
: text
.toCharArray()) {
434 if (bold
>= 0 && prev
!= ' ') {
435 String data
= builder
.substring(bold
);
436 builder
.setLength(bold
);
437 builder
.append(enbold(data
));
440 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
441 bold
= builder
.length();
448 if (italic
>= 0 && prev
!= ' ') {
449 String data
= builder
.substring(italic
);
450 builder
.setLength(italic
);
451 builder
.append(enbold(data
));
453 } else if (italic
< 0
454 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
455 italic
= builder
.length();
470 builder
.insert(bold
, '*');
474 builder
.insert(italic
, '_');
477 return builder
.toString();
481 * Return a {@link BasicOutput} object compatible with the given
482 * {@link OutputType}.
487 * force the <tt>.info</tt> file and the cover to be saved next
488 * to the main target file
490 * @return the {@link BasicOutput}
492 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
496 return new Epub().setType(type
, infoCover
, infoCover
);
498 return new Text().setType(type
, true, infoCover
);
500 return new InfoText().setType(type
, true, true);
502 return new Sysout().setType(type
, false, false);
504 return new Cbz().setType(type
, infoCover
, infoCover
);
506 return new LaTeX().setType(type
, infoCover
, infoCover
);
508 return new Html().setType(type
, infoCover
, infoCover
);