1 package be
.nikiroo
.fanfix
.output
;
4 import java
.io
.IOException
;
6 import be
.nikiroo
.fanfix
.Instance
;
7 import be
.nikiroo
.fanfix
.bundles
.StringId
;
8 import be
.nikiroo
.fanfix
.data
.Chapter
;
9 import be
.nikiroo
.fanfix
.data
.Paragraph
;
10 import be
.nikiroo
.fanfix
.data
.Story
;
11 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
14 * This class is the base class used by the other output classes. It can be used
15 * outside of this package, and have static method that you can use to get
16 * access to the correct support class.
20 public abstract class BasicOutput
{
22 * The supported output types for which we can get a {@link BasicOutput}
27 public enum OutputType
{
28 /** EPUB files created with this program */
30 /** Pure text file with some rules */
32 /** TEXT but with associated .info file */
34 /** DEBUG output to console */
36 /** ZIP with (PNG) images */
38 /** LaTeX file with "book" template */
40 public String
toString() {
41 return super.toString().toLowerCase();
45 * A description of this output type.
47 * @return the description
49 public String
getDesc() {
50 String desc
= Instance
.getTrans().getStringX(StringId
.OUTPUT_DESC
,
54 desc
= Instance
.getTrans()
55 .getString(StringId
.OUTPUT_DESC
, this);
62 * Call {@link OutputType#valueOf(String.toUpperCase())}.
65 * the possible type name
67 * @return NULL or the type
69 public static OutputType
valueOfUC(String typeName
) {
70 return OutputType
.valueOf(typeName
== null ?
null : typeName
75 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
76 * for NULL instead of raising exception.
79 * the possible type name
81 * @return NULL or the type
83 public static OutputType
valueOfNullOkUC(String typeName
) {
84 if (typeName
== null) {
88 return OutputType
.valueOfUC(typeName
);
92 * Call {@link OutputType#valueOf(String.toUpperCase())} but return NULL
93 * in case of error instead of raising an exception.
96 * the possible type name
98 * @return NULL or the type
100 public static OutputType
valueOfAllOkUC(String typeName
) {
102 return OutputType
.valueOfUC(typeName
);
103 } catch (Exception e
) {
109 /** The creator name (this program, by me!) */
110 static final String EPUB_CREATOR
= "Fanfix (by Niki)";
112 /** The current best name for an image */
113 private String imageName
;
114 private File targetDir
;
115 private String targetName
;
116 private OutputType type
;
117 private boolean writeCover
;
118 private boolean writeInfo
;
121 * Process the {@link Story} into the given target.
124 * the {@link Story} to export
126 * the target where to save to (will not necessary be taken as is
127 * by the processor, for instance an extension can be added)
129 * @return the actual main target saved, which can be slightly different
132 * @throws IOException
133 * in case of I/O error
135 public File
process(Story story
, String target
) throws IOException
{
136 target
= new File(target
).getAbsolutePath();
137 File targetDir
= new File(target
).getParentFile();
138 String targetName
= new File(target
).getName();
140 String ext
= getDefaultExtension();
141 if (ext
!= null && !ext
.isEmpty()) {
142 if (targetName
.toLowerCase().endsWith(ext
)) {
143 targetName
= targetName
.substring(0,
144 targetName
.length() - ext
.length());
148 return process(story
, targetDir
, targetName
);
152 * Process the {@link Story} into the given target.
154 * This method is expected to be overridden in most cases.
157 * the {@link Story} to export
159 * the target dir where to save to
161 * the target filename (will not necessary be taken as is by the
162 * processor, for instance an extension can be added)
164 * @return the actual main target saved, which can be slightly different
167 * @throws IOException
168 * in case of I/O error
170 protected File
process(Story story
, File targetDir
, String targetName
)
172 this.targetDir
= targetDir
;
173 this.targetName
= targetName
;
185 public OutputType
getType() {
195 * TRUE to enable the creation of a .info file and a cover if
200 protected BasicOutput
setType(OutputType type
, boolean writeCover
,
203 this.writeCover
= writeCover
;
204 this.writeInfo
= writeInfo
;
210 * The default extension to add to the output files.
214 * @return the extension
216 protected String
getDefaultExtension() {
220 protected void writeStoryHeader(Story story
) throws IOException
{
223 protected void writeChapterHeader(Chapter chap
) throws IOException
{
226 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
229 protected void writeStoryFooter(Story story
) throws IOException
{
232 protected void writeChapterFooter(Chapter chap
) throws IOException
{
235 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
238 protected void writeStory(Story story
) throws IOException
{
239 String chapterNameNum
= String
.format("%03d", 0);
240 String paragraphNumber
= String
.format("%04d", 0);
241 imageName
= paragraphNumber
+ "_" + chapterNameNum
+ ".png";
244 InfoCover
.writeCover(targetDir
, targetName
, story
.getMeta());
247 InfoCover
.writeInfo(targetDir
, targetName
, story
.getMeta());
250 writeStoryHeader(story
);
251 for (Chapter chap
: story
) {
254 writeStoryFooter(story
);
257 protected void writeChapter(Chapter chap
) throws IOException
{
258 String chapterNameNum
;
259 if (chap
.getName() == null || chap
.getName().isEmpty()) {
260 chapterNameNum
= String
.format("%03d", chap
.getNumber());
262 chapterNameNum
= String
.format("%03d", chap
.getNumber()) + "_"
263 + chap
.getName().replace(" ", "_");
267 String paragraphNumber
= String
.format("%04d", num
++);
268 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
270 writeChapterHeader(chap
);
271 for (Paragraph para
: chap
) {
272 paragraphNumber
= String
.format("%04d", num
++);
273 imageName
= chapterNameNum
+ "_" + paragraphNumber
+ ".png";
274 writeParagraph(para
);
276 writeChapterFooter(chap
);
279 protected void writeParagraph(Paragraph para
) throws IOException
{
280 writeParagraphHeader(para
);
281 writeTextLine(para
.getType(), para
.getContent());
282 writeParagraphFooter(para
);
285 protected void writeTextLine(ParagraphType type
, String line
)
290 * Return the current best guess for an image name, based upon the current
291 * {@link Chapter} and {@link Paragraph}.
294 * add the original target name as a prefix
296 * @return the guessed name
298 protected String
getCurrentImageBestName(boolean prefix
) {
300 return targetName
+ "_" + imageName
;
307 * Return the given word or sentence as <b>bold</b>.
312 * @return the bold output
314 protected String
enbold(String word
) {
319 * Return the given word or sentence as <i>italic</i>.
324 * @return the italic output
326 protected String
italize(String word
) {
331 * Decorate the given text with <b>bold</b> and <i>italic</i> words,
332 * according to {@link BasicOutput#enbold(String)} and
333 * {@link BasicOutput#italize(String)}.
338 * @return the decorated output
340 protected String
decorateText(String text
) {
341 StringBuilder builder
= new StringBuilder();
346 for (char car
: text
.toCharArray()) {
349 if (bold
>= 0 && prev
!= ' ') {
350 String data
= builder
.substring(bold
);
351 builder
.setLength(bold
);
352 builder
.append(enbold(data
));
355 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
356 bold
= builder
.length();
363 if (italic
>= 0 && prev
!= ' ') {
364 String data
= builder
.substring(italic
);
365 builder
.setLength(italic
);
366 builder
.append(enbold(data
));
368 } else if (italic
< 0
369 && (prev
== ' ' || prev
== '\0' || prev
== '\n')) {
370 italic
= builder
.length();
385 builder
.insert(bold
, '*');
389 builder
.insert(italic
, '_');
392 return builder
.toString();
396 * Return a {@link BasicOutput} object compatible with the given
397 * {@link OutputType}.
402 * force the <tt>.info</tt> file and the cover to be saved next
403 * to the main target file
405 * @return the {@link BasicOutput}
407 public static BasicOutput
getOutput(OutputType type
, boolean infoCover
) {
411 return new Epub().setType(type
, infoCover
, infoCover
);
413 return new Text().setType(type
, true, infoCover
);
415 return new InfoText().setType(type
, true, true);
417 return new Sysout().setType(type
, false, false);
419 return new Cbz().setType(type
, infoCover
, infoCover
);
421 return new LaTeX().setType(type
, infoCover
, infoCover
);