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 target
= new File(target
).getAbsolutePath();
179 File targetDir
= new File(target
).getParentFile();
180 String targetName
= new File(target
).getName();
182 String ext
= getDefaultExtension(false);
183 if (ext
!= null && !ext
.isEmpty()) {
184 if (targetName
.toLowerCase().endsWith(ext
)) {
185 targetName
= targetName
.substring(0,
186 targetName
.length() - ext
.length());
190 return process(story
, targetDir
, targetName
);
194 * Process the {@link Story} into the given target.
196 * This method is expected to be overridden in most cases.
199 * the {@link Story} to export
201 * the target dir where to save to
203 * the target filename (will not necessary be taken as is by the
204 * processor, for instance an extension can be added)
207 * @return the actual main target saved, which can be slightly different
210 * @throws IOException
211 * in case of I/O error
213 protected File
process(Story story
, File targetDir
, String targetName
)
215 this.targetDir
= targetDir
;
216 this.targetName
= targetName
;
228 public OutputType
getType() {
238 * TRUE to enable the creation of a .info file and a cover if
243 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
246 this.writeCover
= writeCover
;
247 this.writeInfo
= writeInfo
;
253 * The default extension to add to the output files.
255 * @param readerTarget
256 * the target to point to to read the {@link Story} (for
257 * instance, the main entry point if this {@link Story} is in a
260 * @return the extension
262 public String
getDefaultExtension(boolean readerTarget
) {
266 protected void writeStoryHeader(Story story
) throws IOException
{
269 protected void writeChapterHeader(Chapter chap
) throws IOException
{
272 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
275 protected void writeStoryFooter(Story story
) throws IOException
{
278 protected void writeChapterFooter(Chapter chap
) throws IOException
{
281 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
284 protected void writeStory(Story story
) throws IOException
{
285 if (storyPg
== null) {
286 storyPg
= new Progress(0, story
.getChapters().size() + 2);
288 storyPg
.setMinMax(0, story
.getChapters().size() + 2);
291 String chapterNameNum
= String
.format("%03d", 0);
292 String paragraphNumber
= String
.format("%04d", 0);
293 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
295 if (story
.getMeta() != null) {
296 story
.getMeta().setType("" + getType());
300 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
303 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
306 storyPg
.setProgress(1);
308 List
<Progress
> chapPgs
= new ArrayList
<Progress
>(story
.getChapters()
310 for (Chapter chap
: story
) {
311 chapPg
= new Progress(0, chap
.getParagraphs().size());
312 storyPg
.addProgress(chapPg
, 1);
317 writeStoryHeader(story
);
318 for (int i
= 0; i
< story
.getChapters().size(); i
++) {
319 chapPg
= chapPgs
.get(i
);
320 writeChapter(story
.getChapters().get(i
));
321 chapPg
.setProgress(chapPg
.getMax());
324 writeStoryFooter(story
);
326 storyPg
.setProgress(storyPg
.getMax());
330 protected void writeChapter(Chapter chap
) throws IOException
{
331 String chapterNameNum
;
332 if (chap
.getName() == null || chap
.getName().isEmpty()) {
333 chapterNameNum
= String
.format("%03d", chap
.getNumber());
335 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
336 + chap
.getName().replace(" ", "_");
340 String paragraphNumber
= String
.format("%04d", num
++);
341 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
343 writeChapterHeader(chap
);
345 for (Paragraph para
: chap
) {
346 paragraphNumber
= String
.format("%04d", num
++);
347 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
348 writeParagraph(para
);
349 if (chapPg
!= null) {
350 chapPg
.setProgress(i
++);
353 writeChapterFooter(chap
);
356 protected void writeParagraph(Paragraph para
) throws IOException
{
357 writeParagraphHeader(para
);
358 writeTextLine(para
.getType(), para
.getContent());
359 writeParagraphFooter(para
);
362 protected void writeTextLine(ParagraphType type
, String line
)
367 * Return the current best guess for an image name, based upon the current
368 * {@link Chapter} and {@link Paragraph}.
371 * add the original target name as a prefix
373 * @return the guessed name
375 protected String
getCurrentImageBestName(boolean prefix
) {
377 return targetName
+ "_" + imageName
;
384 * Return the given word or sentence as <b>bold</b>.
389 * @return the bold output
391 protected String
enbold(String word
) {
396 * Return the given word or sentence as <i>italic</i>.
401 * @return the italic output
403 protected String
italize(String word
) {
408 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
409 * according to {@link BasicOutput#enbold(String)} and
410 * {@link BasicOutput#italize(String)}.
415 * @return the decorated output
417 protected String
decorateText(String text
) {
418 StringBuilder builder
= new StringBuilder();
423 for (char car
: text
.toCharArray()) {
426 if (bold
>= 0 && prev
!= ' ') {
427 String data
= builder
.substring(bold
);
428 builder
.setLength(bold
);
429 builder
.append(enbold(data
));
432 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
433 bold
= builder
.length();
440 if (italic
>= 0 && prev
!= ' ') {
441 String data
= builder
.substring(italic
);
442 builder
.setLength(italic
);
443 builder
.append(enbold(data
));
445 } else if (italic
< 0
446 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
447 italic
= builder
.length();
462 builder
.insert(bold
, '*');
466 builder
.insert(italic
, '_');
469 return builder
.toString();
473 * Return a {@link BasicOutput} object compatible with the given
474 * {@link OutputType}.
479 * force the <tt>.info</tt> file and the cover to be saved next
480 * to the main target file
482 * @return the {@link BasicOutput}
484 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
488 return new Epub().setType(type
, infoCover
, infoCover
);
490 return new Text().setType(type
, true, infoCover
);
492 return new InfoText().setType(type
, true, true);
494 return new Sysout().setType(type
, false, false);
496 return new Cbz().setType(type
, infoCover
, infoCover
);
498 return new LaTeX().setType(type
, infoCover
, infoCover
);
500 return new Html().setType(type
, infoCover
, infoCover
);