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.
55 * @return the description
57 public String
getDesc() {
58 String desc
= Instance
.getTrans().getStringX(StringId
.OUTPUT_DESC
,
62 desc
= Instance
.getTrans()
63 .getString(StringId
.OUTPUT_DESC
, this);
70 * The default extension to add to the output files.
73 * the target to point to to read the {@link Story} (for
74 * instance, the main entry point if this {@link Story} is in
77 * @return the extension
79 public String
getDefaultExtension(boolean readerTarget
) {
80 BasicOutput output
= BasicOutput
.getOutput(this, false);
82 return output
.getDefaultExtension(readerTarget
);
89 * Call {@link OutputType#valueOf(String.toUpperCase())}.
92 * the possible type name
94 * @return NULL or the type
96 public static OutputType
valueOfUC(String typeName
) {
97 return OutputType
.valueOf(typeName
== null ?
null : typeName
102 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
103 * for NULL and empty instead of raising an exception.
106 * the possible type name
108 * @return NULL or the type
110 public static OutputType
valueOfNullOkUC(String typeName
) {
111 if (typeName
== null || typeName
.isEmpty()) {
115 return OutputType
.valueOfUC(typeName
);
119 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
120 * in case of error instead of raising an exception.
123 * the possible type name
125 * @return NULL or the type
127 public static OutputType
valueOfAllOkUC(String typeName
) {
129 return OutputType
.valueOfUC(typeName
);
130 } catch (Exception e
) {
136 /** The creator name (this program, by me!) */
137 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
139 /** The current best name for an image */
140 private String imageName
;
141 private File targetDir
;
142 private String targetName
;
143 private OutputType type
;
144 private boolean writeCover
;
145 private boolean writeInfo
;
146 private Progress storyPg
;
147 private Progress chapPg
;
150 * Process the {@link Story} into the given target.
153 * the {@link Story} to export
155 * the target where to save to (will not necessary be taken as is
156 * by the processor, for instance an extension can be added)
158 * the optional progress reporter
160 * @return the actual main target saved, which can be slightly different
163 * @throws IOException
164 * in case of I/O error
166 public File
process(Story story
, String target
, Progress pg
)
170 target
= new File(target
).getAbsolutePath();
171 File targetDir
= new File(target
).getParentFile();
172 String targetName
= new File(target
).getName();
174 String ext
= getDefaultExtension(false);
175 if (ext
!= null && !ext
.isEmpty()) {
176 if (targetName
.toLowerCase().endsWith(ext
)) {
177 targetName
= targetName
.substring(0,
178 targetName
.length() - ext
.length());
182 return process(story
, targetDir
, targetName
);
186 * Process the {@link Story} into the given target.
188 * This method is expected to be overridden in most cases.
191 * the {@link Story} to export
193 * the target dir where to save to
195 * the target filename (will not necessary be taken as is by the
196 * processor, for instance an extension can be added)
199 * @return the actual main target saved, which can be slightly different
202 * @throws IOException
203 * in case of I/O error
205 protected File
process(Story story
, File targetDir
, String targetName
)
207 this.targetDir
= targetDir
;
208 this.targetName
= targetName
;
220 public OutputType
getType() {
230 * TRUE to enable the creation of a .info file and a cover if
235 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
238 this.writeCover
= writeCover
;
239 this.writeInfo
= writeInfo
;
245 * The default extension to add to the output files.
247 * @param readerTarget
248 * the target to point to to read the {@link Story} (for
249 * instance, the main entry point if this {@link Story} is in a
252 * @return the extension
254 public String
getDefaultExtension(boolean readerTarget
) {
258 protected void writeStoryHeader(Story story
) throws IOException
{
261 protected void writeChapterHeader(Chapter chap
) throws IOException
{
264 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
267 protected void writeStoryFooter(Story story
) throws IOException
{
270 protected void writeChapterFooter(Chapter chap
) throws IOException
{
273 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
276 protected void writeStory(Story story
) throws IOException
{
277 if (storyPg
== null) {
278 storyPg
= new Progress(0, story
.getChapters().size() + 2);
280 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
283 String chapterNameNum
= String
.format("%03d", 0);
284 String paragraphNumber
= String
.format("%04d", 0);
285 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
287 if (story
.getMeta() != null) {
288 story
.getMeta().setType("" + getType());
292 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
295 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
298 storyPg
.setProgress(1);
300 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
302 for (Chapter chap
: story
) {
303 chapPg
= new Progress(0, chap
.getParagraphs().size());
304 storyPg
.addProgress(chapPg
, 1);
309 writeStoryHeader(story
);
310 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
311 chapPg
= chapPgs
.get(i
);
312 writeChapter(story
.getChapters().get(i
));
313 chapPg
.setProgress(chapPg
.getMax());
316 writeStoryFooter(story
);
318 storyPg
.setProgress(storyPg
.getMax());
322 protected void writeChapter(Chapter chap
) throws IOException
{
323 String chapterNameNum
;
324 if (chap
.getName() == null || chap
.getName().isEmpty()) {
325 chapterNameNum
= String
.format("%03d", chap
.getNumber());
327 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
328 + chap
.getName().replace(" ", "_");
332 String paragraphNumber
= String
.format("%04d", num
++);
333 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
335 writeChapterHeader(chap
);
337 for (Paragraph para
: chap
) {
338 paragraphNumber
= String
.format("%04d", num
++);
339 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
340 writeParagraph(para
);
341 if (chapPg
!= null) {
342 chapPg
.setProgress(i
++);
345 writeChapterFooter(chap
);
348 protected void writeParagraph(Paragraph para
) throws IOException
{
349 writeParagraphHeader(para
);
350 writeTextLine(para
.getType(), para
.getContent());
351 writeParagraphFooter(para
);
354 protected void writeTextLine(ParagraphType type
, String line
)
359 * Return the current best guess for an image name, based upon the current
360 * {@link Chapter} and {@link Paragraph}.
363 * add the original target name as a prefix
365 * @return the guessed name
367 protected String
getCurrentImageBestName(boolean prefix
) {
369 return targetName
+ "_" + imageName
;
376 * Return the given word or sentence as <b>bold</b>.
381 * @return the bold output
383 protected String
enbold(String word
) {
388 * Return the given word or sentence as <i>italic</i>.
393 * @return the italic output
395 protected String
italize(String word
) {
400 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
401 * according to {@link BasicOutput#enbold(String)} and
402 * {@link BasicOutput#italize(String)}.
407 * @return the decorated output
409 protected String
decorateText(String text
) {
410 StringBuilder builder
= new StringBuilder();
415 for (char car
: text
.toCharArray()) {
418 if (bold
>= 0 && prev
!= ' ') {
419 String data
= builder
.substring(bold
);
420 builder
.setLength(bold
);
421 builder
.append(enbold(data
));
424 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
425 bold
= builder
.length();
432 if (italic
>= 0 && prev
!= ' ') {
433 String data
= builder
.substring(italic
);
434 builder
.setLength(italic
);
435 builder
.append(enbold(data
));
437 } else if (italic
< 0
438 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
439 italic
= builder
.length();
454 builder
.insert(bold
, '*');
458 builder
.insert(italic
, '_');
461 return builder
.toString();
465 * Return a {@link BasicOutput} object compatible with the given
466 * {@link OutputType}.
471 * force the <tt>.info</tt> file and the cover to be saved next
472 * to the main target file
474 * @return the {@link BasicOutput}
476 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
480 return new Epub().setType(type
, infoCover
, infoCover
);
482 return new Text().setType(type
, true, infoCover
);
484 return new InfoText().setType(type
, true, true);
486 return new Sysout().setType(type
, false, false);
488 return new Cbz().setType(type
, infoCover
, infoCover
);
490 return new LaTeX().setType(type
, infoCover
, infoCover
);
492 return new Html().setType(type
, infoCover
, infoCover
);