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
.Config
;
11 import be
.nikiroo
.fanfix
.bundles
.StringId
;
12 import be
.nikiroo
.fanfix
.data
.Chapter
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
15 import be
.nikiroo
.fanfix
.data
.Story
;
17 class LaTeX
extends BasicOutput
{
18 protected BufferedWriter writer
;
19 private boolean lastWasQuote
= false;
22 private char openQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_SINGLE_QUOTE
);
23 private char closeQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_SINGLE_QUOTE
);
24 private char openDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
25 private char closeDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
28 public File
process(Story story
, File targetDir
, String targetName
)
30 String targetNameOrig
= targetName
;
31 targetName
+= getDefaultExtension(false);
33 File target
= new File(targetDir
, targetName
);
35 writer
= new BufferedWriter(new OutputStreamWriter(
36 new FileOutputStream(target
), "UTF-8"));
38 super.process(story
, targetDir
, targetNameOrig
);
48 public String
getDefaultExtension(boolean readerTarget
) {
53 protected void writeStoryHeader(Story story
) throws IOException
{
56 String title
= "\\title{}";
58 if (story
.getMeta() != null) {
59 MetaData meta
= story
.getMeta();
60 title
= "\\title{" + latexEncode(meta
.getTitle()) + "}";
61 date
= "\\date{" + latexEncode(meta
.getDate()) + "}";
62 author
= "\\author{" + latexEncode(meta
.getAuthor()) + "}";
63 lang
= meta
.getLang().toLowerCase();
64 if (lang
!= null && !lang
.isEmpty()) {
65 lang
= Instance
.getInstance().getConfig().getStringX(Config
.CONF_LATEX_LANG
, lang
);
67 System
.err
.println(Instance
.getInstance().getTrans().getString(StringId
.LATEX_LANG_UNKNOWN
, lang
));
73 writer
.append("% This LaTeX document was auto-generated by Fanfic Reader, created by Niki.\n");
74 writer
.append("%\n\n");
75 writer
.append("\\documentclass[a4paper]{book}\n");
76 if (lang
!= null && !lang
.isEmpty()) {
77 writer
.append("\\usepackage[" + lang
+ "]{babel}\n");
79 writer
.append("\\usepackage[utf8]{inputenc}\n");
80 writer
.append("\\usepackage[T1]{fontenc}\n");
81 writer
.append("\\usepackage{lmodern}\n");
82 writer
.append("\\newcommand{\\br}{\\vspace{10 mm}}\n");
83 writer
.append("\\newcommand{\\say}{--- \\noindent\\emph}\n");
84 writer
.append("\\hyphenpenalty=1000\n");
85 writer
.append("\\tolerance=5000\n");
86 writer
.append("\\begin{document}\n");
87 if (story
.getMeta() != null && story
.getMeta().getDate() != null)
88 writer
.append(date
+ "\n");
89 writer
.append(title
+ "\n");
90 writer
.append(author
+ "\n");
91 writer
.append("\\maketitle\n");
98 protected void writeStoryFooter(Story story
) throws IOException
{
99 writer
.append("\\end{document}\n");
103 protected void writeChapterHeader(Chapter chap
) throws IOException
{
104 writer
.append("\n\n\\chapter{" + latexEncode(chap
.getName()) + "}"
109 protected void writeChapterFooter(Chapter chap
) throws IOException
{
114 protected String
enbold(String word
) {
115 return "\\textsc{" + word
+ "}";
119 protected String
italize(String word
) {
120 return "\\emph{" + word
+ "}";
124 protected void writeTextLine(ParagraphType type
, String line
)
127 line
= decorateText(latexEncode(line
));
132 lastWasQuote
= false;
135 writer
.write("\n\\br");
137 lastWasQuote
= false;
142 lastWasQuote
= false;
145 writer
.write("\n\\say{" + line
+ "}\n");
147 writer
.write("\n\\noindent{}");
152 // TODO what about images in LaTeX?
157 private String
latexEncode(String input
) {
158 StringBuilder builder
= new StringBuilder();
159 for (char car
: input
.toCharArray()) {
160 // TODO: check restricted chars?
161 if (car
== '^' || car
== '$' || car
== '\\' || car
== '#'
163 builder
.append('\\');
165 } else if (car
== openQuote
) {
167 } else if (car
== closeQuote
) {
168 builder
.append('\'');
169 } else if (car
== openDoubleQuote
) {
170 builder
.append("``");
171 } else if (car
== closeDoubleQuote
) {
172 builder
.append("''");
178 return builder
.toString();