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
.getTrans().getCharacter(
23 StringId
.OPEN_SINGLE_QUOTE
);
24 private char closeQuote
= Instance
.getTrans().getCharacter(
25 StringId
.CLOSE_SINGLE_QUOTE
);
26 private char openDoubleQuote
= Instance
.getTrans().getCharacter(
27 StringId
.OPEN_DOUBLE_QUOTE
);
28 private char closeDoubleQuote
= Instance
.getTrans().getCharacter(
29 StringId
.CLOSE_DOUBLE_QUOTE
);
32 public File
process(Story story
, File targetDir
, String targetName
)
34 String targetNameOrig
= targetName
;
35 targetName
+= getDefaultExtension(false);
37 File target
= new File(targetDir
, targetName
);
39 writer
= new BufferedWriter(new OutputStreamWriter(
40 new FileOutputStream(target
), "UTF-8"));
42 super.process(story
, targetDir
, targetNameOrig
);
52 public String
getDefaultExtension(boolean readerTarget
) {
57 protected void writeStoryHeader(Story story
) throws IOException
{
60 String title
= "\\title{}";
62 if (story
.getMeta() != null) {
63 MetaData meta
= story
.getMeta();
64 title
= "\\title{" + latexEncode(meta
.getTitle()) + "}";
65 date
= "\\date{" + latexEncode(meta
.getDate()) + "}";
66 author
= "\\author{" + latexEncode(meta
.getAuthor()) + "}";
67 lang
= meta
.getLang().toLowerCase();
68 if (lang
!= null && !lang
.isEmpty()) {
69 lang
= Instance
.getConfig().getStringX(Config
.LATEX_LANG
, lang
);
71 System
.err
.println(Instance
.getTrans().getString(
72 StringId
.LATEX_LANG_UNKNOWN
, lang
));
78 writer
.append("% This LaTeX document was auto-generated by Fanfic Reader, created by Niki.\n");
79 writer
.append("%\n\n");
80 writer
.append("\\documentclass[a4paper]{book}\n");
81 if (lang
!= null && !lang
.isEmpty()) {
82 writer
.append("\\usepackage[" + lang
+ "]{babel}\n");
84 writer
.append("\\usepackage[utf8]{inputenc}\n");
85 writer
.append("\\usepackage[T1]{fontenc}\n");
86 writer
.append("\\usepackage{lmodern}\n");
87 writer
.append("\\newcommand{\\br}{\\vspace{10 mm}}\n");
88 writer
.append("\\newcommand{\\say}{--- \\noindent\\emph}\n");
89 writer
.append("\\hyphenpenalty=1000\n");
90 writer
.append("\\tolerance=5000\n");
91 writer
.append("\\begin{document}\n");
92 if (story
.getMeta() != null && story
.getMeta().getDate() != null)
93 writer
.append(date
+ "\n");
94 writer
.append(title
+ "\n");
95 writer
.append(author
+ "\n");
96 writer
.append("\\maketitle\n");
103 protected void writeStoryFooter(Story story
) throws IOException
{
104 writer
.append("\\end{document}\n");
108 protected void writeChapterHeader(Chapter chap
) throws IOException
{
109 writer
.append("\n\n\\chapter{" + latexEncode(chap
.getName()) + "}"
114 protected void writeChapterFooter(Chapter chap
) throws IOException
{
119 protected String
enbold(String word
) {
120 return "\\textsc{" + word
+ "}";
124 protected String
italize(String word
) {
125 return "\\emph{" + word
+ "}";
129 protected void writeTextLine(ParagraphType type
, String line
)
132 line
= decorateText(latexEncode(line
));
137 lastWasQuote
= false;
140 writer
.write("\n\\br");
142 lastWasQuote
= false;
147 lastWasQuote
= false;
150 writer
.write("\n\\say{" + line
+ "}\n");
152 writer
.write("\n\\noindent{}");
162 private String
latexEncode(String input
) {
163 StringBuilder builder
= new StringBuilder();
164 for (char car
: input
.toCharArray()) {
165 // TODO: check restricted chars?
166 if (car
== '^' || car
== '$' || car
== '\\' || car
== '#'
168 builder
.append('\\');
170 } else if (car
== openQuote
) {
172 } else if (car
== closeQuote
) {
173 builder
.append('\'');
174 } else if (car
== openDoubleQuote
) {
175 builder
.append("``");
176 } else if (car
== closeDoubleQuote
) {
177 builder
.append("''");
183 return builder
.toString();