1 | |
package org.apache.maven.doxia.book.services.renderer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.doxia.book.BookDoxiaException; |
23 | |
import org.apache.maven.doxia.book.services.renderer.latex.LatexBookSink; |
24 | |
import org.apache.maven.doxia.book.context.BookContext; |
25 | |
import org.apache.maven.doxia.book.model.BookModel; |
26 | |
import org.apache.maven.doxia.book.model.Chapter; |
27 | |
import org.apache.maven.doxia.book.model.Section; |
28 | |
import org.apache.maven.doxia.parser.manager.ParserNotFoundException; |
29 | |
import org.apache.maven.doxia.parser.ParseException; |
30 | |
import org.apache.maven.doxia.Doxia; |
31 | |
import org.codehaus.plexus.util.ReaderFactory; |
32 | |
import org.codehaus.plexus.util.StringUtils; |
33 | |
import org.codehaus.plexus.util.IOUtil; |
34 | |
import org.codehaus.plexus.util.WriterFactory; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.io.FileWriter; |
38 | |
import java.io.IOException; |
39 | |
import java.io.PrintWriter; |
40 | |
import java.io.FileNotFoundException; |
41 | |
import java.io.Reader; |
42 | |
import java.io.Writer; |
43 | |
import java.util.Map; |
44 | |
import java.util.HashMap; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 1 | public class LatexBookRenderer |
54 | |
implements BookRenderer |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | |
private Doxia doxia; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public void renderBook( BookContext context ) |
67 | |
throws BookDoxiaException |
68 | |
{ |
69 | 1 | BookModel book = context.getBook(); |
70 | |
|
71 | 1 | if ( !context.getOutputDirectory().exists() ) |
72 | |
{ |
73 | 0 | if ( !context.getOutputDirectory().mkdirs() ) |
74 | |
{ |
75 | 0 | throw new BookDoxiaException( |
76 | |
"Could not make directory: " + context.getOutputDirectory().getAbsolutePath() + "." ); |
77 | |
} |
78 | |
} |
79 | |
|
80 | 1 | File bookFile = new File( context.getOutputDirectory(), book.getId() + ".tex" ); |
81 | |
|
82 | 1 | FileWriter fileWriter = null; |
83 | |
|
84 | |
try |
85 | |
{ |
86 | 1 | fileWriter = new FileWriter( bookFile ); |
87 | |
|
88 | 1 | PrintWriter writer = new PrintWriter( fileWriter ); |
89 | |
|
90 | 1 | writeBook( book, context, writer ); |
91 | |
} |
92 | 0 | catch ( IOException e ) |
93 | |
{ |
94 | 0 | throw new BookDoxiaException( "Error while opening file.", e ); |
95 | |
} |
96 | |
finally |
97 | |
{ |
98 | 1 | IOUtil.close( fileWriter ); |
99 | 1 | } |
100 | 1 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | 4 | static class SectionInfo |
108 | |
{ |
109 | |
|
110 | |
String id; |
111 | |
|
112 | |
|
113 | |
String title; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
private void writeBook( BookModel book, BookContext context, PrintWriter writer ) |
126 | |
throws IOException, BookDoxiaException |
127 | |
{ |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 1 | Map<String, SectionInfo> sectionInfos = new HashMap<String, SectionInfo>(); |
133 | |
|
134 | 1 | for ( Chapter chapter : book.getChapters() ) |
135 | |
{ |
136 | 2 | for ( Section section : chapter.getSections() ) |
137 | |
{ |
138 | 4 | SectionInfo info = writeSection( section, context ); |
139 | |
|
140 | 4 | sectionInfos.put( info.id, info ); |
141 | 4 | } |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | 1 | writer.println( "\\documentclass{book}" ); |
149 | 1 | writer.println( "\\title{" + book.getTitle() + "}" ); |
150 | |
|
151 | 1 | if ( StringUtils.isNotEmpty( book.getAuthor() ) ) |
152 | |
{ |
153 | 0 | writer.println( "\\author{" + book.getAuthor() + "}" ); |
154 | |
} |
155 | |
|
156 | 1 | if ( StringUtils.isNotEmpty( book.getDate() ) ) |
157 | |
{ |
158 | 0 | writer.println( "\\date{" + book.getDate() + "}" ); |
159 | |
} |
160 | |
|
161 | 1 | LatexBookSink sink = new LatexBookSink( writer ); |
162 | 1 | sink.defaultBookPreamble(); |
163 | |
|
164 | 1 | writer.println( "\\begin{document}" ); |
165 | 1 | writer.println( "\\maketitle" ); |
166 | 1 | writer.println( "\\tableofcontents" ); |
167 | |
|
168 | |
|
169 | 1 | for ( Chapter chapter : book.getChapters() ) |
170 | |
{ |
171 | 2 | writer.println( "\\chapter{" + chapter.getTitle() + "}" ); |
172 | |
|
173 | 2 | for ( Section section : chapter.getSections() ) |
174 | |
{ |
175 | 4 | SectionInfo info = sectionInfos.get( section.getId() ); |
176 | |
|
177 | 4 | writer.println( "\\input{" + info.id + "}" ); |
178 | 4 | } |
179 | |
} |
180 | |
|
181 | 1 | writer.println( "\\end{document}" ); |
182 | 1 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
private SectionInfo writeSection( Section section, BookContext context ) |
194 | |
throws IOException, BookDoxiaException |
195 | |
{ |
196 | 4 | File file = new File( context.getOutputDirectory(), ( section.getId() + ".tex" ) ); |
197 | |
|
198 | 4 | Writer writer = WriterFactory.newWriter( file, context.getOutputEncoding() ); |
199 | |
|
200 | 4 | LatexBookSink sink = new LatexBookSink( writer ); |
201 | |
|
202 | 4 | BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() ); |
203 | |
|
204 | 4 | if ( bookFile == null ) |
205 | |
{ |
206 | 0 | throw new BookDoxiaException( "No document that matches section with id=" |
207 | |
+ section.getId() + "." ); |
208 | |
} |
209 | |
|
210 | 4 | Reader reader = null; |
211 | |
try |
212 | |
{ |
213 | 4 | reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() ); |
214 | 4 | doxia.parse( reader, bookFile.getParserId(), sink ); |
215 | |
} |
216 | 0 | catch ( ParserNotFoundException e ) |
217 | |
{ |
218 | 0 | throw new BookDoxiaException( "Parser not found: " |
219 | |
+ bookFile.getParserId() + ".", e ); |
220 | |
} |
221 | 0 | catch ( ParseException e ) |
222 | |
{ |
223 | 0 | throw new BookDoxiaException( "Error while parsing document: " |
224 | |
+ bookFile.getFile().getAbsolutePath() + ".", e ); |
225 | |
} |
226 | 0 | catch ( FileNotFoundException e ) |
227 | |
{ |
228 | 0 | throw new BookDoxiaException( "Could not find document: " |
229 | |
+ bookFile.getFile().getAbsolutePath() + ".", e ); |
230 | |
} |
231 | |
finally |
232 | |
{ |
233 | 4 | sink.flush(); |
234 | 4 | sink.close(); |
235 | |
|
236 | 4 | IOUtil.close( reader ); |
237 | 4 | IOUtil.close( writer ); |
238 | 4 | } |
239 | |
|
240 | 4 | SectionInfo info = new SectionInfo(); |
241 | 4 | info.id = section.getId(); |
242 | 4 | info.title = sink.getTitle(); |
243 | |
|
244 | 4 | return info; |
245 | |
} |
246 | |
} |