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);
74 * The default extension to add to the output files.
77 * the target to point to to read the {@link Story} (for
78 * instance, the main entry point if this {@link Story} is in
81 * @return the extension
83 public String
getDefaultExtension(boolean readerTarget
) {
84 BasicOutput output
= BasicOutput
.getOutput(this, false);
86 return output
.getDefaultExtension(readerTarget
);
93 * Call {@link OutputType#valueOf(String.toUpperCase())}.
96 * the possible type name
98 * @return NULL or the type
100 public static OutputType
valueOfUC(String typeName
) {
101 return OutputType
.valueOf(typeName
== null ?
null : typeName
106 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
107 * for NULL and empty instead of raising an exception.
110 * the possible type name
112 * @return NULL or the type
114 public static OutputType
valueOfNullOkUC(String typeName
) {
115 if (typeName
== null || typeName
.isEmpty()) {
119 return OutputType
.valueOfUC(typeName
);
123 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
124 * in case of error instead of raising an exception.
127 * the possible type name
129 * @return NULL or the type
131 public static OutputType
valueOfAllOkUC(String typeName
) {
133 return OutputType
.valueOfUC(typeName
);
134 } catch (Exception e
) {
140 /** The creator name (this program, by me!) */
141 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
143 /** The current best name for an image */
144 private String imageName
;
145 private File targetDir
;
146 private String targetName
;
147 private OutputType type
;
148 private boolean writeCover
;
149 private boolean writeInfo
;
150 private Progress storyPg
;
151 private Progress chapPg
;
154 * Process the {@link Story} into the given target.
157 * the {@link Story} to export
159 * the target where to save to (will not necessary be taken as is
160 * by the processor, for instance an extension can be added)
162 * the optional progress reporter
164 * @return the actual main target saved, which can be slightly different
167 * @throws IOException
168 * in case of I/O error
170 public File
process(Story story
, String target
, Progress pg
)
174 target
= new File(target
).getAbsolutePath();
175 File targetDir
= new File(target
).getParentFile();
176 String targetName
= new File(target
).getName();
178 String ext
= getDefaultExtension(false);
179 if (ext
!= null && !ext
.isEmpty()) {
180 if (targetName
.toLowerCase().endsWith(ext
)) {
181 targetName
= targetName
.substring(0,
182 targetName
.length() - ext
.length());
186 return process(story
, targetDir
, targetName
);
190 * Process the {@link Story} into the given target.
192 * This method is expected to be overridden in most cases.
195 * the {@link Story} to export
197 * the target dir where to save to
199 * the target filename (will not necessary be taken as is by the
200 * processor, for instance an extension can be added)
203 * @return the actual main target saved, which can be slightly different
206 * @throws IOException
207 * in case of I/O error
209 protected File
process(Story story
, File targetDir
, String targetName
)
211 this.targetDir
= targetDir
;
212 this.targetName
= targetName
;
224 public OutputType
getType() {
234 * TRUE to enable the creation of a .info file and a cover if
239 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
242 this.writeCover
= writeCover
;
243 this.writeInfo
= writeInfo
;
249 * The default extension to add to the output files.
251 * @param readerTarget
252 * the target to point to to read the {@link Story} (for
253 * instance, the main entry point if this {@link Story} is in a
256 * @return the extension
258 public String
getDefaultExtension(boolean readerTarget
) {
262 protected void writeStoryHeader(Story story
) throws IOException
{
265 protected void writeChapterHeader(Chapter chap
) throws IOException
{
268 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
271 protected void writeStoryFooter(Story story
) throws IOException
{
274 protected void writeChapterFooter(Chapter chap
) throws IOException
{
277 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
280 protected void writeStory(Story story
) throws IOException
{
281 if (storyPg
== null) {
282 storyPg
= new Progress(0, story
.getChapters().size() + 2);
284 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
287 String chapterNameNum
= String
.format("%03d", 0);
288 String paragraphNumber
= String
.format("%04d", 0);
289 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
291 if (story
.getMeta() != null) {
292 story
.getMeta().setType("" + getType());
296 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
299 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
302 storyPg
.setProgress(1);
304 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
306 for (Chapter chap
: story
) {
307 chapPg
= new Progress(0, chap
.getParagraphs().size());
308 storyPg
.addProgress(chapPg
, 1);
313 writeStoryHeader(story
);
314 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
315 chapPg
= chapPgs
.get(i
);
316 writeChapter(story
.getChapters().get(i
));
317 chapPg
.setProgress(chapPg
.getMax());
320 writeStoryFooter(story
);
322 storyPg
.setProgress(storyPg
.getMax());
326 protected void writeChapter(Chapter chap
) throws IOException
{
327 String chapterNameNum
;
328 if (chap
.getName() == null || chap
.getName().isEmpty()) {
329 chapterNameNum
= String
.format("%03d", chap
.getNumber());
331 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
332 + chap
.getName().replace(" ", "_");
336 String paragraphNumber
= String
.format("%04d", num
++);
337 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
339 writeChapterHeader(chap
);
341 for (Paragraph para
: chap
) {
342 paragraphNumber
= String
.format("%04d", num
++);
343 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
344 writeParagraph(para
);
345 if (chapPg
!= null) {
346 chapPg
.setProgress(i
++);
349 writeChapterFooter(chap
);
352 protected void writeParagraph(Paragraph para
) throws IOException
{
353 writeParagraphHeader(para
);
354 writeTextLine(para
.getType(), para
.getContent());
355 writeParagraphFooter(para
);
358 protected void writeTextLine(ParagraphType type
, String line
)
363 * Return the current best guess for an image name, based upon the current
364 * {@link Chapter} and {@link Paragraph}.
367 * add the original target name as a prefix
369 * @return the guessed name
371 protected String
getCurrentImageBestName(boolean prefix
) {
373 return targetName
+ "_" + imageName
;
380 * Return the given word or sentence as <b>bold</b>.
385 * @return the bold output
387 protected String
enbold(String word
) {
392 * Return the given word or sentence as <i>italic</i>.
397 * @return the italic output
399 protected String
italize(String word
) {
404 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
405 * according to {@link BasicOutput#enbold(String)} and
406 * {@link BasicOutput#italize(String)}.
411 * @return the decorated output
413 protected String
decorateText(String text
) {
414 StringBuilder builder
= new StringBuilder();
419 for (char car
: text
.toCharArray()) {
422 if (bold
>= 0 && prev
!= ' ') {
423 String data
= builder
.substring(bold
);
424 builder
.setLength(bold
);
425 builder
.append(enbold(data
));
428 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
429 bold
= builder
.length();
436 if (italic
>= 0 && prev
!= ' ') {
437 String data
= builder
.substring(italic
);
438 builder
.setLength(italic
);
439 builder
.append(enbold(data
));
441 } else if (italic
< 0
442 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
443 italic
= builder
.length();
458 builder
.insert(bold
, '*');
462 builder
.insert(italic
, '_');
465 return builder
.toString();
469 * Return a {@link BasicOutput} object compatible with the given
470 * {@link OutputType}.
475 * force the <tt>.info</tt> file and the cover to be saved next
476 * to the main target file
478 * @return the {@link BasicOutput}
480 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
484 return new Epub().setType(type
, infoCover
, infoCover
);
486 return new Text().setType(type
, true, infoCover
);
488 return new InfoText().setType(type
, true, true);
490 return new Sysout().setType(type
, false, false);
492 return new Cbz().setType(type
, infoCover
, infoCover
);
494 return new LaTeX().setType(type
, infoCover
, infoCover
);
496 return new Html().setType(type
, infoCover
, infoCover
);