View Javadoc
1   package org.apache.maven.doxia.module.latex;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Writer;
23  
24  import org.apache.maven.doxia.sink.Sink;
25  import org.apache.maven.doxia.sink.impl.AbstractSinkTest;
26  
27  /**
28   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
29   */
30  public class LatexSinkTest
31      extends AbstractSinkTest
32  {
33      /** {@inheritDoc} */
34      protected String outputExtension()
35      {
36          return "tex";
37      }
38  
39      /** {@inheritDoc} */
40      protected Sink createSink( Writer writer )
41      {
42          return new LatexSink( writer );
43      }
44  
45      /** {@inheritDoc} */
46      protected boolean isXmlSink()
47      {
48          return false;
49      }
50  
51      /** {@inheritDoc} */
52      protected String getTitleBlock( String title )
53      {
54          return "\\title{" + LatexSink.escaped( title ) + "}" + EOL;
55      }
56  
57      /** {@inheritDoc} */
58      protected String getAuthorBlock( String author )
59      {
60          return "\\author{" + LatexSink.escaped( author ) + "}" + EOL;
61      }
62  
63      /** {@inheritDoc} */
64      protected String getDateBlock( String date )
65      {
66          return "\\date{" + LatexSink.escaped( date ) + "}" + EOL;
67      }
68  
69      /** {@inheritDoc} */
70      protected String getHeadBlock()
71      {
72          return ( (LatexSink) getSink() ).defaultSinkCommands()
73              + "\\documentclass[a4paper]{article}" + EOL + EOL
74              + ( (LatexSink) getSink() ).defaultPreamble()
75              + "\\begin{document}" + EOL + EOL;
76      }
77  
78      /** {@inheritDoc} */
79      protected String getBodyBlock()
80      {
81          return "\\end{document}" + EOL;
82      }
83  
84      /** {@inheritDoc} */
85      protected String getArticleBlock()
86      {
87          return "";
88      }
89  
90      /** {@inheritDoc} */
91      protected String getNavigationBlock()
92      {
93          return "";
94      }
95  
96      /** {@inheritDoc} */
97      protected String getSidebarBlock()
98      {
99          return "";
100     }
101 
102     /** {@inheritDoc} */
103     protected String getSectionTitleBlock( String title )
104     {
105         return title;
106     }
107 
108     /** {@inheritDoc} */
109     protected String getSection1Block( String title )
110     {
111         return EOL + "\\section{" + title + "}" + EOL;
112     }
113 
114     /** {@inheritDoc} */
115     protected String getSection2Block( String title )
116     {
117         return EOL + "\\subsection{" + title + "}" + EOL;
118     }
119 
120     /** {@inheritDoc} */
121     protected String getSection3Block( String title )
122     {
123         return EOL + "\\subsubsection{" + title + "}" + EOL;
124     }
125 
126     /** {@inheritDoc} */
127     protected String getSection4Block( String title )
128     {
129         return EOL + "\\paragraph{" + title + "}" + EOL;
130     }
131 
132     /** {@inheritDoc} */
133     protected String getSection5Block( String title )
134     {
135         return EOL + "\\subparagraph{" + title + "}" + EOL;
136     }
137 
138     /** {@inheritDoc} */
139     protected String getHeaderBlock()
140     {
141         return "";
142     }
143 
144     /** {@inheritDoc} */
145     protected String getContentBlock()
146     {
147         return "";
148     }
149 
150     /** {@inheritDoc} */
151     protected String getFooterBlock()
152     {
153         return "";
154     }
155 
156     /** {@inheritDoc} */
157     protected String getListBlock( String item )
158     {
159         return EOL + "\\begin{itemize}" + EOL + "\\item " + LatexSink.escaped( item ) + EOL + "\\end{itemize}" + EOL;
160     }
161 
162     /** {@inheritDoc} */
163     protected String getNumberedListBlock( String item )
164     {
165         return EOL + "\\begin{enumerate}" + EOL + "\\renewcommand{\\theenumi}{\\roman{enumi}}" + EOL + "\\item "
166             + LatexSink.escaped( item ) + EOL + "\\end{enumerate}" + EOL;
167     }
168 
169     /** {@inheritDoc} */
170     protected String getDefinitionListBlock( String definum, String definition )
171     {
172         return EOL + "\\begin{description}" + EOL + "\\item[\\mbox{" + definum + "}] "
173                 + definition + EOL + "\\end{description}" + EOL;
174     }
175 
176     /** {@inheritDoc} */
177     protected String getFigureBlock( String source, String caption )
178     {
179         String figureBlock = EOL + "\\begin{figure}[htb]" + EOL + "\\begin{center}" + EOL + "\\includegraphics{" + source + "}" + EOL
180             + "\\end{center}" + EOL;
181         if (caption != null )
182         {
183             figureBlock += "\\caption{Figure\\_caption}" + EOL;
184         }
185         figureBlock += "\\end{figure}" + EOL;
186         return figureBlock;        
187     }
188 
189     /** {@inheritDoc} */
190     protected String getTableBlock( String cell, String caption )
191     {
192         return EOL + "\\begin{table}[htp]" + EOL + "\\begin{center}" + EOL + "\\begin{tabular}{c}" + EOL
193             + "\\begin{tabular}[t]{c}cell\\end{tabular}\\\\" + EOL + "\\end{tabular}" + EOL
194             + "\\end{center}" + EOL + "\\caption{Table\\_caption}" + EOL + "\\end{table}" + EOL;
195     }
196 
197     /** {@inheritDoc} */
198     protected String getParagraphBlock( String text )
199     {
200         return  EOL + EOL + text + EOL;
201     }
202 
203     /** {@inheritDoc} */
204     protected String getDataBlock( String value, String text )
205     {
206         return text;
207     }
208 
209     /** {@inheritDoc} */
210     protected String getTimeBlock( String datetime, String text )
211     {
212         return text;
213     }
214 
215     /** {@inheritDoc} */
216     protected String getAddressBlock( String text )
217     {
218         return text;
219     }
220 
221     /** {@inheritDoc} */
222     protected String getBlockquoteBlock( String text )
223     {
224         return text;
225     }
226 
227     /** {@inheritDoc} */
228     protected String getDivisionBlock( String text )
229     {
230         return text;
231     }
232 
233     /** {@inheritDoc} */
234     protected String getVerbatimBlock( String text )
235     {
236         return EOL + "\\begin{small}" + EOL + "\\begin{Verbatim}[frame=single]" + EOL + text + EOL
237             + "\\end{Verbatim}" + EOL + "\\end{small}" + EOL;
238     }
239 
240     /** {@inheritDoc} */
241     protected String getHorizontalRuleBlock()
242     {
243         return EOL + "\\begin{center}\\rule[0.5ex]{\\linewidth}{1pt}\\end{center}" + EOL;
244     }
245 
246     /** {@inheritDoc} */
247     protected String getPageBreakBlock()
248     {
249         return EOL + "\\newpage" + EOL;
250     }
251 
252     /** {@inheritDoc} */
253     protected String getAnchorBlock( String anchor )
254     {
255         return "\\hypertarget{" + anchor + "}{" + anchor + "}";
256     }
257 
258     /** {@inheritDoc} */
259     protected String getLinkBlock( String link, String text )
260     {
261         return "\\hyperlink{" + link + "}{" + text + "}";
262     }
263 
264     /** {@inheritDoc} */
265     protected String getInlineBlock( String text )
266     {
267         return text;
268     }
269 
270     /** {@inheritDoc} */
271     protected String getInlineItalicBlock( String text )
272     {
273         return "\\textit{" + text + "}";
274     }
275 
276     /** {@inheritDoc} */
277     protected String getInlineBoldBlock( String text )
278     {
279         return "\\textbf{" + text + "}";
280     }
281 
282     /** {@inheritDoc} */
283     protected String getInlineCodeBlock( String text )
284     {
285         return "\\texttt{\\small " + text + "}";
286     }
287 
288     /** {@inheritDoc} */
289     protected String getItalicBlock( String text )
290     {
291         return "\\textit{" + text + "}";
292     }
293 
294     /** {@inheritDoc} */
295     protected String getBoldBlock( String text )
296     {
297         return "\\textbf{" + text + "}";
298     }
299 
300     /** {@inheritDoc} */
301     protected String getMonospacedBlock( String text )
302     {
303         return "\\texttt{\\small " + text + "}";
304     }
305 
306     /** {@inheritDoc} */
307     protected String getLineBreakBlock()
308     {
309         return "\\newline" + EOL;
310     }
311 
312     /** {@inheritDoc} */
313     protected String getLineBreakOpportunityBlock()
314     {
315         return "";
316     }
317 
318     /** {@inheritDoc} */
319     protected String getNonBreakingSpaceBlock()
320     {
321         return "~";
322     }
323 
324     /** {@inheritDoc} */
325     protected String getTextBlock( String text )
326     {
327         // TODO: how to retrieve those outside the sink?
328         return "\\textasciitilde" + EOL + ",\\_=,\\_\\symbol{45},\\_+,\\_*,\\_[,\\_],"
329                 + "\\_\\symbol{60},\\_\\symbol{62},\\_\\{,\\_\\},\\_\\textbackslash";
330     }
331 
332     /** {@inheritDoc} */
333     protected String getRawTextBlock( String text )
334     {
335         return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
336     }
337 
338     /** {@inheritDoc} */
339     protected String getCommentBlock( String text )
340     {
341         return EOL + "%" + text;
342     }
343 }