001 package org.rakeshv.applications;
002
003 import java.io.*;
004 import org.rakeshv.tex.*;
005
006 /**
007 * A wrapper application to open a text file, and apply some
008 * common translations to the contents to add
009 * L<sub>A</sub>T<sup>E</sup>X markup.
010 */
011 public class ConvertToTeX
012 {
013 /**
014 * Populate the specified StringBuffer with the standard tex header
015 * used to typeset books.
016 *
017 * @param headerBuffer - The StringBuffer to which the
018 * header is to be appended.
019 * @param fileName - The name of the output tex file
020 * @return StringBuffer - The reference to the StringBuffer passed in.
021 * This makes it easy to chain this method call with other standard
022 * StringBuffer methods.
023 */
024 private static final StringBuffer populateHeader( StringBuffer headerBuffer,
025 String fileName )
026 {
027 // Add the standard book header
028 headerBuffer.append( "%File: " ).append( fileName ).append( "\n" );
029 headerBuffer.append( "%%%%%%%%%%%%%%%% For printable versions %%%%%%%%%%%%%%%%%\n" );
030 headerBuffer.append( "\\documentclass[11pt,openany]{memoir}\n" );
031 headerBuffer.append( "\\settrimmedsize{11in}{210mm}{*}\n" );
032 headerBuffer.append( "\\setlength{\\trimtop}{0pt}\n" );
033 headerBuffer.append( "\\setlength{\\trimedge}{\\stockwidth}\n" );
034 headerBuffer.append( "\\addtolength{\\trimedge}{-\\paperwidth}\n" );
035 headerBuffer.append( "\\settypeblocksize{23cm}{16cm}{*}\n" );
036 headerBuffer.append( "\\setulmargins{*}{*}{0.5}\n" );
037 headerBuffer.append( "\\setlrmargins{3.25cm}{*}{*}\n" );
038 headerBuffer.append( "\\setmarginnotes{17pt}{51pt}{\\onelineskip}\n" );
039 headerBuffer.append( "\\setheadfoot{\\onelineskip}{2.5\\onelineskip}\n" );
040 headerBuffer.append( "\\setheaderspaces{*}{2\\onelineskip}{*}\n" );
041 headerBuffer.append( "\\checkandfixthelayout\n" );
042 headerBuffer.append( "\n" );
043 headerBuffer.append( "\\chapterstyle{default}\n" );
044 headerBuffer.append( "\n" );
045 headerBuffer.append( "\\makepagestyle{fancy}\n" );
046 headerBuffer.append( "\\makeoddhead{fancy}{\\rightmark}{}{\\thepage}\n" );
047 headerBuffer.append( "\\makeoddfoot{fancy}{\\small\\textit{Project Gutenberg Etext " ).append( fileName ).append( "}}{}{\\small\\textit{Formatted by \\href{mailto:rakesh@rakeshv.org}{Rakesh Vidyadharan}}}\n" );
048 headerBuffer.append( "\\makeevenhead{fancy}{\\thepage}{}{\\leftmark}\n" );
049 headerBuffer.append( "\\makeevenfoot{fancy}{\\small\\textit{Formatted by \\href{mailto:rakesh@rakeshv.org}{Rakesh Vidyadharan}}}{}{\\small\\textit{Project Gutenberg Etext " ).append( fileName ).append( "}}\n" );
050 headerBuffer.append( "\\makeheadrule{fancy}{\\textwidth}{\\normalrulethickness}\n" );
051 headerBuffer.append( "\\makefootrule{fancy}{\\textwidth}{\\normalrulethickness}{0pt}\n" );
052 headerBuffer.append( "\\pagestyle{fancy}\n" );
053 headerBuffer.append( "\n" );
054 headerBuffer.append( "%\\chapterstyle{section} % Like section with just number and title\n" );
055 headerBuffer.append( "%\\chapterstyle{companion} % Right justified titling\n" );
056 headerBuffer.append( "%\\chapterstyle{demo} % See what it looks like\n" );
057 headerBuffer.append( "\n" );
058 headerBuffer.append( "% Make chapter titles right justified\n" );
059 headerBuffer.append( "\\renewcommand{\\printchaptername}{\\raggedleft \\chapnamefont \\chaptername}\n" );
060 headerBuffer.append( "% Add trailing dots to chapter content entries\n" );
061 headerBuffer.append( "\\renewcommand{\\cftchapterdotsep}{\\cftdotsep}\n" );
062 headerBuffer.append( "\\addtolength{\\cftchapternumwidth}{2em}\n" );
063 headerBuffer.append( "\\setlength{\\topmargin}{-12mm}\n" );
064 headerBuffer.append( "\n" );
065 headerBuffer.append( "% Hyperref settings and PDF properties\n" );
066 headerBuffer.append( "\\usepackage[colorlinks=true, pdfstartview=FitV, linkcolor=blue, citecolor=blue, urlcolor=blue]{hyperref}\n" );
067 headerBuffer.append( "\\hypersetup{%\n" );
068 headerBuffer.append( "pdftitle={" );
069 headerBuffer.append( fileName );
070 headerBuffer.append( "},%\n" );
071 headerBuffer.append( "pdfauthor={Rakesh Vidyadharan},%\n" );
072 headerBuffer.append( "pdfsubject={Formatted version of Books Online file},%\n" );
073 headerBuffer.append( "pdfkeywords={Online Books, ebooks}%\n" );
074 headerBuffer.append( "%pdfpagemode={FullScreen}%\n" );
075 headerBuffer.append( "}\n" );
076 headerBuffer.append( "\\usepackage{memhfixc}\n" );
077 headerBuffer.append( "%%%%%%%%%%%%%%%% End printable versions %%%%%%%%%%%%%%%%%\n" );
078 headerBuffer.append( "\n" );
079 headerBuffer.append( "%%%%%%%%%%%%%%%% For html versions %%%%%%%%%%%%%%%%%\n" );
080 headerBuffer.append( "%\\documentclass[11pt,openany]{book}\n" );
081 headerBuffer.append( "%\\parskip 10pt plus 10pt\n" );
082 headerBuffer.append( "%\\parindent 0pt\n" );
083 headerBuffer.append( "%\\topmargin 0pt\n" );
084 headerBuffer.append( "%\\headheight 10mm\n" );
085 headerBuffer.append( "%\\headsep 5mm\n" );
086 headerBuffer.append( "%\\oddsidemargin 5mm\n" );
087 headerBuffer.append( "%\\evensidemargin 0mm\n" );
088 headerBuffer.append( "%\\textwidth 160mm\n" );
089 headerBuffer.append( "%\\textheight 230mm\n" );
090 headerBuffer.append( "\n" );
091 headerBuffer.append( "%\\usepackage[html,2,sections+]{tex4ht}\n" );
092 headerBuffer.append( "%%%%%%%%%%%%%%%% End html versions %%%%%%%%%%%%%%%%%\n" );
093 headerBuffer.append( "\n" );
094 headerBuffer.append( "\\def\\partname{Book}%\n" );
095 headerBuffer.append( "\\renewcommand{\\thechapter}{\\Roman{chapter}}\n" );
096 headerBuffer.append( "%\\renewcommand{\\thesection}{\\Roman{section}}\n" );
097 headerBuffer.append( "\\renewcommand{\\thefootnote}{\\fnsymbol{footnote}}\n" );
098 headerBuffer.append( "\\newenvironment{centre}{\\begin{center}}{\\end{center}}\n" );
099 headerBuffer.append( "\\newcommand{\\attrib}[1]{%\n" );
100 headerBuffer.append( " \\nopagebreak{\\raggedleft\\footnotesize #1\\par}\n" );
101 headerBuffer.append( "}\n" );
102 headerBuffer.append( "\n" );
103 headerBuffer.append( "\\usefont{T1}{cmr}{m}{n}\n" );
104 headerBuffer.append( "\n" );
105 headerBuffer.append( "\\usepackage{soul,charter,microtype}\n" );
106 headerBuffer.append( "\\usepackage{optional}\n" );
107 headerBuffer.append( "% \\opt{dvi}{DVI specific text}\n" );
108 headerBuffer.append( "% \\opt{html}{HTML specific text}\n" );
109 headerBuffer.append( "% \\opt{pdf}{PDF specific text}\n" );
110 headerBuffer.append( "% \\opt{ps}{PostScript specific text}\n" );
111 headerBuffer.append( "\\newcommand{\\ExplainOptions}{dvi = DVI output, html = HTML output, pdf = PDF output, ps = PostScript output}\n" );
112 headerBuffer.append( "\\AskOption\n" );
113 headerBuffer.append( "\n" );
114 headerBuffer.append( "\\begin{document}\n" );
115 headerBuffer.append( "\n" );
116 headerBuffer.append( "\\maketitle\n" );
117 headerBuffer.append( "\n" );
118 headerBuffer.append( "\\frontmatter\n" );
119 headerBuffer.append( "\\footnotesize\n" );
120 headerBuffer.append( "\n" );
121
122 return headerBuffer;
123 }
124
125 public static void main( String args[] )
126 {
127 if ( args.length != 2 )
128 {
129 System.out.println( "Usage: java -jar texapplication.jar <input text file> <output tex file>" );
130 System.exit( 1 );
131 }
132
133 StringBuffer contentsBuffer = new StringBuffer( 524288 );
134 StringBuffer headerBuffer = new StringBuffer( 2048 );
135 populateHeader( headerBuffer, args[1] );
136
137 try
138 {
139 // Open the file and read its contents
140 BufferedReader buf = new BufferedReader(
141 new FileReader( args[0] )
142 );
143
144 String line = "";
145 while ( ( line = buf.readLine() ) != null )
146 {
147 contentsBuffer.append( line + "\n" );
148 }
149 buf.close();
150 }
151 catch ( IOException ioex )
152 {
153 System.err.println( ioex.toString() );
154 System.exit( 2 );
155 }
156
157 // String to hold modified contents returned by various
158 // static methods in utility classes
159 String contents = contentsBuffer.toString();
160 contentsBuffer = null;
161
162 // Convert any ISO 8859-1 encoded characters to their LaTeX
163 // equivalent tags
164 contents = ISOToTeX.convertString( contents, "Cp1252" );
165
166 // Call any one of the static part generation methods as per
167 // requirements. Uncomment the required call.
168
169 // Case when part has name PART, and has a title associated
170 // and no reset counter is needed
171 //contents = TransformDocumentParts.generatePartTags( contents,
172 //"PART" );
173
174 // Case when part has name PART, and has no title associated
175 // and no reset counter is needed
176 contents = TransformDocumentParts.generatePartTags( contents,
177 "PART", TransformDocumentParts.NO_TITLE );
178
179 // Case when part has name PART, and has no title associated
180 // and needs a reset counter
181 //contents = TransformDocumentParts.generatePartTags( contents,
182 //"PART", TransformDocumentParts.NO_TITLE,
183 //TransformDocumentParts.RESET_COUNTER );
184 // Case when part has name PART, and has a title associated,
185 // and needs a reset counter
186 //contents = TransformDocumentParts.generatePartTags( contents,
187 //"PART", TransformDocumentParts.HAS_TITLE,
188 //TransformDocumentParts.RESET_COUNTER );
189
190 // Call any one of the static chapter generation methods as per
191 // requirements. Uncomment the required call.
192
193 // Case when chapter has name CHAPTER, and has a title associated
194 // and no reset counter is needed
195 //contents = TransformDocumentParts.generateChapterTags( contents,
196 //"CHAPTER" );
197
198 // Case when chapter has name CHAPTER, and has no title associated
199 // and no reset counter is needed
200 //contents = TransformDocumentParts.generateChapterTags( contents,
201 //"CHAPTER", TransformDocumentParts.NO_TITLE );
202
203 // Case when chapter has name CHAPTER, and has no title associated
204 // and needs a reset counter
205 contents = TransformDocumentParts.generateChapterTags( contents,
206 "CHAPTER", TransformDocumentParts.NO_TITLE,
207 TransformDocumentParts.RESET_COUNTER );
208 // Case when chapter has name CHAPTER, and has a title associated,
209 // and needs a reset counter
210 //contents = TransformDocumentParts.generateChapterTags( contents,
211 //"CHAPTER", TransformDocumentParts.HAS_TITLE,
212 //TransformDocumentParts.RESET_COUNTER );
213
214 // Apply common transformations to the text file
215 contents = TransformTextFile.convertText( contents );
216
217 // Add the header and footer to the contents
218 contentsBuffer = new StringBuffer( contents );
219 contentsBuffer.insert( 0, headerBuffer.toString() );
220 contentsBuffer.append( "\n\\end{document}" );
221
222 // Write the results back to the original file
223 try
224 {
225 PrintWriter out = new PrintWriter(
226 new BufferedWriter(
227 new FileWriter( args[1] )
228 )
229 );
230
231 out.print( contentsBuffer.toString() );
232 out.flush();
233 out.close();
234 }
235 catch ( IOException ie )
236 {
237 System.err.println( ie.toString() );
238 System.exit( 3 );
239 }
240 }
241 }