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 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 BookModel book = context.getBook();
70
71 if ( !context.getOutputDirectory().exists() )
72 {
73 if ( !context.getOutputDirectory().mkdirs() )
74 {
75 throw new BookDoxiaException(
76 "Could not make directory: " + context.getOutputDirectory().getAbsolutePath() + "." );
77 }
78 }
79
80 File bookFile = new File( context.getOutputDirectory(), book.getId() + ".tex" );
81
82 FileWriter fileWriter = null;
83
84 try
85 {
86 fileWriter = new FileWriter( bookFile );
87
88 PrintWriter writer = new PrintWriter( fileWriter );
89
90 writeBook( book, context, writer );
91 }
92 catch ( IOException e )
93 {
94 throw new BookDoxiaException( "Error while opening file.", e );
95 }
96 finally
97 {
98 IOUtil.close( fileWriter );
99 }
100 }
101
102
103
104
105
106
107 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 Map<String, SectionInfo> sectionInfos = new HashMap<String, SectionInfo>();
133
134 for ( Chapter chapter : book.getChapters() )
135 {
136 for ( Section section : chapter.getSections() )
137 {
138 SectionInfo info = writeSection( section, context );
139
140 sectionInfos.put( info.id, info );
141 }
142 }
143
144
145
146
147
148 writer.println( "\\documentclass{book}" );
149 writer.println( "\\title{" + book.getTitle() + "}" );
150
151 if ( StringUtils.isNotEmpty( book.getAuthor() ) )
152 {
153 writer.println( "\\author{" + book.getAuthor() + "}" );
154 }
155
156 if ( StringUtils.isNotEmpty( book.getDate() ) )
157 {
158 writer.println( "\\date{" + book.getDate() + "}" );
159 }
160
161 LatexBookSink sink = new LatexBookSink( writer );
162 sink.defaultBookPreamble();
163
164 writer.println( "\\begin{document}" );
165 writer.println( "\\maketitle" );
166 writer.println( "\\tableofcontents" );
167
168
169 for ( Chapter chapter : book.getChapters() )
170 {
171 writer.println( "\\chapter{" + chapter.getTitle() + "}" );
172
173 for ( Section section : chapter.getSections() )
174 {
175 SectionInfo info = sectionInfos.get( section.getId() );
176
177 writer.println( "\\input{" + info.id + "}" );
178 }
179 }
180
181 writer.println( "\\end{document}" );
182 }
183
184
185
186
187
188
189
190
191
192
193 private SectionInfo writeSection( Section section, BookContext context )
194 throws IOException, BookDoxiaException
195 {
196 File file = new File( context.getOutputDirectory(), ( section.getId() + ".tex" ) );
197
198 Writer writer = WriterFactory.newWriter( file, context.getOutputEncoding() );
199
200 LatexBookSink sink = new LatexBookSink( writer );
201
202 BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() );
203
204 if ( bookFile == null )
205 {
206 throw new BookDoxiaException( "No document that matches section with id="
207 + section.getId() + "." );
208 }
209
210 Reader reader = null;
211 try
212 {
213 reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
214 doxia.parse( reader, bookFile.getParserId(), sink );
215 }
216 catch ( ParserNotFoundException e )
217 {
218 throw new BookDoxiaException( "Parser not found: "
219 + bookFile.getParserId() + ".", e );
220 }
221 catch ( ParseException e )
222 {
223 throw new BookDoxiaException( "Error while parsing document: "
224 + bookFile.getFile().getAbsolutePath() + ".", e );
225 }
226 catch ( FileNotFoundException e )
227 {
228 throw new BookDoxiaException( "Could not find document: "
229 + bookFile.getFile().getAbsolutePath() + ".", e );
230 }
231 finally
232 {
233 sink.flush();
234 sink.close();
235
236 IOUtil.close( reader );
237 IOUtil.close( writer );
238 }
239
240 SectionInfo info = new SectionInfo();
241 info.id = section.getId();
242 info.title = sink.getTitle();
243
244 return info;
245 }
246 }