1 package be
.nikiroo
.fanfix
.output
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.OutputStreamWriter
;
9 import be
.nikiroo
.fanfix
.Instance
;
10 import be
.nikiroo
.fanfix
.bundles
.StringId
;
11 import be
.nikiroo
.fanfix
.data
.Chapter
;
12 import be
.nikiroo
.fanfix
.data
.MetaData
;
13 import be
.nikiroo
.fanfix
.data
.Paragraph
;
14 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
17 class Text
extends BasicOutput
{
18 protected BufferedWriter writer
;
19 protected File targetDir
;
20 private boolean nextParaIsCover
= true;
23 public File
process(Story story
, File targetDir
, String targetName
)
25 String targetNameOrig
= targetName
;
26 targetName
+= getDefaultExtension(false);
28 this.targetDir
= targetDir
;
30 File target
= new File(targetDir
, targetName
);
32 writer
= new BufferedWriter(new OutputStreamWriter(
33 new FileOutputStream(target
), "UTF-8"));
35 super.process(story
, targetDir
, targetNameOrig
);
45 public String
getDefaultExtension(boolean readerTarget
) {
50 protected void writeStoryHeader(Story story
) throws IOException
{
55 MetaData meta
= story
.getMeta();
57 title
= meta
.getTitle() == null ?
"" : meta
.getTitle();
58 author
= meta
.getAuthor();
59 date
= meta
.getDate();
64 if (author
!= null && !author
.isEmpty()) {
65 writer
.write(Instance
.getInstance().getTrans().getString(StringId
.BY
) + " " + author
);
67 if (date
!= null && !date
.isEmpty()) {
75 if (meta
!= null && meta
.getResume() != null) {
76 writeChapter(meta
.getResume());
81 protected void writeChapterHeader(Chapter chap
) throws IOException
{
83 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
84 txt
= Instance
.getInstance().getTrans().getString(StringId
.CHAPTER_NAMED
, chap
.getNumber(), chap
.getName());
86 txt
= Instance
.getInstance().getTrans().getString(StringId
.CHAPTER_UNNAMED
, chap
.getNumber());
89 writer
.write("\n" + txt
+ "\n");
90 for (int i
= 0; i
< txt
.length(); i
++) {
97 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
102 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
103 if (para
.getType() == ParagraphType
.IMAGE
) {
104 File file
= new File(targetDir
, getCurrentImageBestName(true));
106 Instance
.getInstance().getCache().saveAsImage(para
.getContentImage(), file
, nextParaIsCover
);
107 } catch (IOException e
) {
108 Instance
.getInstance().getTraceHandler().error(new IOException("Cannot save an image", e
));
112 nextParaIsCover
= false;
116 protected void writeTextLine(ParagraphType type
, String line
)
122 writer
.write("\n* * *\n");
129 writer
.write("[" + getCurrentImageBestName(true) + "]");