001package org.apache.maven.doxia.module.latex;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.Writer;
023
024import org.apache.maven.doxia.sink.Sink;
025import org.apache.maven.doxia.sink.AbstractSinkTest;
026
027/**
028 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
029 * @version $Id: LatexSinkTest.html 905940 2014-04-12 16:27:29Z hboutemy $
030 */
031public class LatexSinkTest
032    extends AbstractSinkTest
033{
034    /** {@inheritDoc} */
035    protected String outputExtension()
036    {
037        return "tex";
038    }
039
040    /** {@inheritDoc} */
041    protected Sink createSink( Writer writer )
042    {
043        return new LatexSink( writer );
044    }
045
046    /** {@inheritDoc} */
047    protected boolean isXmlSink()
048    {
049        return false;
050    }
051
052    /** {@inheritDoc} */
053    protected String getTitleBlock( String title )
054    {
055        return "\\title{" + LatexSink.escaped( title ) + "}" + EOL;
056    }
057
058    /** {@inheritDoc} */
059    protected String getAuthorBlock( String author )
060    {
061        return "\\author{" + LatexSink.escaped( author ) + "}" + EOL;
062    }
063
064    /** {@inheritDoc} */
065    protected String getDateBlock( String date )
066    {
067        return "\\date{" + LatexSink.escaped( date ) + "}" + EOL;
068    }
069
070    /** {@inheritDoc} */
071    protected String getHeadBlock()
072    {
073        return ( (LatexSink) getSink() ).defaultSinkCommands()
074            + "\\documentclass[a4paper]{article}" + EOL + EOL
075            + ( (LatexSink) getSink() ).defaultPreamble()
076            + "\\begin{document}" + EOL + EOL;
077    }
078
079    /** {@inheritDoc} */
080    protected String getBodyBlock()
081    {
082        return "\\end{document}" + EOL;
083    }
084
085    /** {@inheritDoc} */
086    protected String getSectionTitleBlock( String title )
087    {
088        return title;
089    }
090
091    /** {@inheritDoc} */
092    protected String getSection1Block( String title )
093    {
094        return EOL + "\\section{" + title + "}" + EOL;
095    }
096
097    /** {@inheritDoc} */
098    protected String getSection2Block( String title )
099    {
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}