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.toUpperCase())}.
100 * the possible type name
102 * @return NULL or the type
104 public static OutputType
valueOfUC(String typeName
) {
105 return OutputType
.valueOf(typeName
== null ?
null : typeName
110 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
111 * for NULL and empty instead of raising an exception.
114 * the possible type name
116 * @return NULL or the type
118 public static OutputType
valueOfNullOkUC(String typeName
) {
119 if (typeName
== null || typeName
.isEmpty()) {
123 return OutputType
.valueOfUC(typeName
);
127 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
128 * in case of error instead of raising an exception.
131 * the possible type name
133 * @return NULL or the type
135 public static OutputType
valueOfAllOkUC(String typeName
) {
137 return OutputType
.valueOfUC(typeName
);
138 } catch (Exception e
) {
144 /** The creator name (this program, by me!) */
145 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
147 /** The current best name for an image */
148 private String imageName
;
149 private File targetDir
;
150 private String targetName
;
151 private OutputType type
;
152 private boolean writeCover
;
153 private boolean writeInfo
;
154 private Progress storyPg
;
155 private Progress chapPg
;
158 * Process the {@link Story} into the given target.
161 * the {@link Story} to export
163 * the target where to save to (will not necessary be taken as is
164 * by the processor, for instance an extension can be added)
166 * the optional progress reporter
168 * @return the actual main target saved, which can be slightly different
171 * @throws IOException
172 * in case of I/O error
174 public File
process(Story story
, String target
, Progress pg
)
178 File targetDir
= null;
179 String targetName
= null;
180 if (target
!= null) {
181 target
= new File(target
).getAbsolutePath();
182 targetDir
= new File(target
).getParentFile();
183 targetName
= new File(target
).getName();
185 String ext
= getDefaultExtension(false);
186 if (ext
!= null && !ext
.isEmpty()) {
187 if (targetName
.toLowerCase().endsWith(ext
)) {
188 targetName
= targetName
.substring(0, targetName
.length()
194 return process(story
, targetDir
, targetName
);
198 * Process the {@link Story} into the given target.
200 * This method is expected to be overridden in most cases.
203 * the {@link Story} to export
205 * the target dir where to save to
207 * the target filename (will not necessary be taken as is by the
208 * processor, for instance an extension can be added)
211 * @return the actual main target saved, which can be slightly different
214 * @throws IOException
215 * in case of I/O error
217 protected File
process(Story story
, File targetDir
, String targetName
)
219 this.targetDir
= targetDir
;
220 this.targetName
= targetName
;
232 public OutputType
getType() {
242 * TRUE to enable the creation of a .info file and a cover if
247 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
250 this.writeCover
= writeCover
;
251 this.writeInfo
= writeInfo
;
257 * The default extension to add to the output files.
259 * @param readerTarget
260 * the target to point to to read the {@link Story} (for
261 * instance, the main entry point if this {@link Story} is in a
264 * @return the extension
266 public String
getDefaultExtension(boolean readerTarget
) {
270 protected void writeStoryHeader(Story story
) throws IOException
{
273 protected void writeChapterHeader(Chapter chap
) throws IOException
{
276 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
279 protected void writeStoryFooter(Story story
) throws IOException
{
282 protected void writeChapterFooter(Chapter chap
) throws IOException
{
285 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
288 protected void writeStory(Story story
) throws IOException
{
289 if (storyPg
== null) {
290 storyPg
= new Progress(0, story
.getChapters().size() + 2);
292 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
295 String chapterNameNum
= String
.format("%03d", 0);
296 String paragraphNumber
= String
.format("%04d", 0);
297 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
299 if (story
.getMeta() != null) {
300 story
.getMeta().setType("" + getType());
304 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
307 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
310 storyPg
.setProgress(1);
312 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
314 for (Chapter chap
: story
) {
315 chapPg
= new Progress(0, chap
.getParagraphs().size());
316 storyPg
.addProgress(chapPg
, 1);
321 writeStoryHeader(story
);
322 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
323 chapPg
= chapPgs
.get(i
);
324 writeChapter(story
.getChapters().get(i
));
325 chapPg
.setProgress(chapPg
.getMax());
328 writeStoryFooter(story
);
330 storyPg
.setProgress(storyPg
.getMax());
334 protected void writeChapter(Chapter chap
) throws IOException
{
335 String chapterNameNum
;
336 if (chap
.getName() == null || chap
.getName().isEmpty()) {
337 chapterNameNum
= String
.format("%03d", chap
.getNumber());
339 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
340 + chap
.getName().replace(" ", "_");
344 String paragraphNumber
= String
.format("%04d", num
++);
345 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
347 writeChapterHeader(chap
);
349 for (Paragraph para
: chap
) {
350 paragraphNumber
= String
.format("%04d", num
++);
351 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
352 writeParagraph(para
);
353 if (chapPg
!= null) {
354 chapPg
.setProgress(i
++);
357 writeChapterFooter(chap
);
360 protected void writeParagraph(Paragraph para
) throws IOException
{
361 writeParagraphHeader(para
);
362 writeTextLine(para
.getType(), para
.getContent());
363 writeParagraphFooter(para
);
366 protected void writeTextLine(ParagraphType type
, String line
)
371 * Return the current best guess for an image name, based upon the current
372 * {@link Chapter} and {@link Paragraph}.
375 * add the original target name as a prefix
377 * @return the guessed name
379 protected String
getCurrentImageBestName(boolean prefix
) {
381 return targetName
+ "_" + imageName
;
388 * Return the given word or sentence as <b>bold</b>.
393 * @return the bold output
395 protected String
enbold(String word
) {
400 * Return the given word or sentence as <i>italic</i>.
405 * @return the italic output
407 protected String
italize(String word
) {
412 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
413 * according to {@link BasicOutput#enbold(String)} and
414 * {@link BasicOutput#italize(String)}.
419 * @return the decorated output
421 protected String
decorateText(String text
) {
422 StringBuilder builder
= new StringBuilder();
427 for (char car
: text
.toCharArray()) {
430 if (bold
>= 0 && prev
!= ' ') {
431 String data
= builder
.substring(bold
);
432 builder
.setLength(bold
);
433 builder
.append(enbold(data
));
436 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
437 bold
= builder
.length();
444 if (italic
>= 0 && prev
!= ' ') {
445 String data
= builder
.substring(italic
);
446 builder
.setLength(italic
);
447 builder
.append(enbold(data
));
449 } else if (italic
< 0
450 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
451 italic
= builder
.length();
466 builder
.insert(bold
, '*');
470 builder
.insert(italic
, '_');
473 return builder
.toString();
477 * Return a {@link BasicOutput} object compatible with the given
478 * {@link OutputType}.
483 * force the <tt>.info</tt> file and the cover to be saved next
484 * to the main target file
486 * @return the {@link BasicOutput}
488 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
492 return new Epub().setType(type
, infoCover
, infoCover
);
494 return new Text().setType(type
, true, infoCover
);
496 return new InfoText().setType(type
, true, true);
498 return new Sysout().setType(type
, false, false);
500 return new Cbz().setType(type
, infoCover
, infoCover
);
502 return new LaTeX().setType(type
, infoCover
, infoCover
);
504 return new Html().setType(type
, infoCover
, infoCover
);