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.AbstractSinkTest;
26  
27  /**
28   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
29   * @version $Id: LatexSinkTest.java 1467294 2013-04-12 14:51:42Z rfscholte $
30   */
31  public class LatexSinkTest
32      extends AbstractSinkTest
33  {
34      /** {@inheritDoc} */
35      protected String outputExtension()
36      {
37          return "tex";
38      }
39  
40      /** {@inheritDoc} */
41      protected Sink createSink( Writer writer )
42      {
43          return new LatexSink( writer );
44      }
45  
46      /** {@inheritDoc} */
47      protected boolean isXmlSink()
48      {
49          return false;
50      }
51  
52      /** {@inheritDoc} */
53      protected String getTitleBlock( String title )
54      {
55          return "\\title{" + LatexSink.escaped( title ) + "}" + EOL;
56      }
57  
58      /** {@inheritDoc} */
59      protected String getAuthorBlock( String author )
60      {
61          return "\\author{" + LatexSink.escaped( author ) + "}" + EOL;
62      }
63  
64      /** {@inheritDoc} */
65      protected String getDateBlock( String date )
66      {
67          return "\\date{" + LatexSink.escaped( date ) + "}" + EOL;
68      }
69  
70      /** {@inheritDoc} */
71      protected String getHeadBlock()
72      {
73          return ( (LatexSink) getSink() ).defaultSinkCommands()
74              + "\\documentclass[a4paper]{article}" + EOL + EOL
75              + ( (LatexSink) getSink() ).defaultPreamble()
76              + "\\begin{document}" + EOL + EOL;
77      }
78  
79      /** {@inheritDoc} */
80      protected String getBodyBlock()
81      {
82          return "\\end{document}" + EOL;
83      }
84  
85      /** {@inheritDoc} */
86      protected String getSectionTitleBlock( String title )
87      {
88          return title;
89      }
90  
91      /** {@inheritDoc} */
92      protected String getSection1Block( String title )
93      {
94          return EOL + "\\section{" + title + "}" + EOL;
95      }
96  
97      /** {@inheritDoc} */
98      protected String getSection2Block( String title )
99      {
100         return EOL + "\\subsection{" + title + "}" + EOL;
101     }
102 
103     /** {@inheritDoc} */
104     protected String getSection3Block( String title )
105     {
106         return EOL + "\\subsubsection{" + title + "}" + EOL;
107     }
108 
109     /** {@inheritDoc} */
110     protected String getSection4Block( String title )
111     {
112         return EOL + "\\paragraph{" + title + "}" + EOL;
113     }
114 
115     /** {@inheritDoc} */
116     protected String getSection5Block( String title )
117     {
118         return EOL + "\\subparagraph{" + title + "}" + EOL;
119     }
120 
121     /** {@inheritDoc} */
122     protected String getListBlock( String item )
123     {
124         return EOL + "\\begin{itemize}" + EOL + "\\item " + LatexSink.escaped( item ) + EOL + "\\end{itemize}" + EOL;
125     }
126 
127     /** {@inheritDoc} */
128     protected String getNumberedListBlock( String item )
129     {
130         return EOL + "\\begin{enumerate}" + EOL + "\\renewcommand{\\theenumi}{\\roman{enumi}}" + EOL + "\\item "
131             + LatexSink.escaped( item ) + EOL + "\\end{enumerate}" + EOL;
132     }
133 
134     /** {@inheritDoc} */
135     protected String getDefinitionListBlock( String definum, String definition )
136     {
137         return EOL + "\\begin{description}" + EOL + "\\item[\\mbox{" + definum + "}] "
138                 + definition + EOL + "\\end{description}" + EOL;
139     }
140 
141     /** {@inheritDoc} */
142     protected String getFigureBlock( String source, String caption )
143     {
144         String figureBlock = EOL + "\\begin{figure}[htb]" + EOL + "\\begin{center}" + EOL + "\\includegraphics{" + source + "}" + EOL
145             + "\\end{center}" + EOL;
146         if (caption != null )
147         {
148             figureBlock += "\\caption{Figure\\_caption}" + EOL;
149         }
150         figureBlock += "\\end{figure}" + EOL;
151         return figureBlock;        
152     }
153 
154     /** {@inheritDoc} */
155     protected String getTableBlock( String cell, String caption )
156     {
157         return EOL + "\\begin{table}[htp]" + EOL + "\\begin{center}" + EOL + "\\begin{tabular}{c}" + EOL
158             + "\\begin{tabular}[t]{c}cell\\end{tabular}\\\\" + EOL + "\\end{tabular}" + EOL
159             + "\\end{center}" + EOL + "\\caption{Table\\_caption}" + EOL + "\\end{table}" + EOL;
160     }
161 
162     /** {@inheritDoc} */
163     protected String getParagraphBlock( String text )
164     {
165         return  EOL + EOL + text + EOL;
166     }
167 
168     /** {@inheritDoc} */
169     protected String getVerbatimBlock( String text )
170     {
171         return EOL + "\\begin{small}" + EOL + "\\begin{Verbatim}[frame=single]" + EOL + text + EOL
172             + "\\end{Verbatim}" + EOL + "\\end{small}" + EOL;
173     }
174 
175     /** {@inheritDoc} */
176     protected String getHorizontalRuleBlock()
177     {
178         return EOL + "\\begin{center}\\rule[0.5ex]{\\linewidth}{1pt}\\end{center}" + EOL;
179     }
180 
181     /** {@inheritDoc} */
182     protected String getPageBreakBlock()
183     {
184         return EOL + "\\newpage" + EOL;
185     }
186 
187     /** {@inheritDoc} */
188     protected String getAnchorBlock( String anchor )
189     {
190         return "\\hypertarget{" + anchor + "}{" + anchor + "}";
191     }
192 
193     /** {@inheritDoc} */
194     protected String getLinkBlock( String link, String text )
195     {
196         return "\\hyperlink{" + link + "}{" + text + "}";
197     }
198 
199     /** {@inheritDoc} */
200     protected String getItalicBlock( String text )
201     {
202         return "\\textit{" + text + "}";
203     }
204 
205     /** {@inheritDoc} */
206     protected String getBoldBlock( String text )
207     {
208         return "\\textbf{" + text + "}";
209     }
210 
211     /** {@inheritDoc} */
212     protected String getMonospacedBlock( String text )
213     {
214         return "\\texttt{\\small " + text + "}";
215     }
216 
217     /** {@inheritDoc} */
218     protected String getLineBreakBlock()
219     {
220         return "\\newline" + EOL;
221     }
222 
223     /** {@inheritDoc} */
224     protected String getNonBreakingSpaceBlock()
225     {
226         return "~";
227     }
228 
229     /** {@inheritDoc} */
230     protected String getTextBlock( String text )
231     {
232         // TODO: how to retrieve those outside the sink?
233         return "\\textasciitilde" + EOL + ",\\_=,\\_\\symbol{45},\\_+,\\_*,\\_[,\\_],"
234                 + "\\_\\symbol{60},\\_\\symbol{62},\\_\\{,\\_\\},\\_\\textbackslash";
235     }
236 
237     /** {@inheritDoc} */
238     protected String getRawTextBlock( String text )
239     {
240         return "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
241     }
242 
243     /** {@inheritDoc} */
244     protected String getCommentBlock( String text )
245     {
246         return EOL + "% Simple comment with ----";
247     }
248 }