1 /*
2 * lm2latex - src/l2l.c
3 * Copyright (C) 2010 Sebastian Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 /*
29 * A Linux-Magazin markup to LaTeX converter -- core library.
30 */
32 #include "l2l.h"
33 #include "l2l_scanner.h"
34 #include "l2l_features.h"
36 #include <assert.h>
38 #include <stdio.h>
40 /*
41 * private helper functions
42 */
44 static void
45 write_tex_preamble(FILE *out)
46 {
47 assert(out);
49 fprintf(out,
50 "\\documentclass[11pt,a4paper,twoside]{article}\n"
51 "\n"
52 "\\usepackage{ifpdf}\n"
53 "\n"
54 "\\usepackage{a4}\n"
55 "\\usepackage{fancyhdr}\n"
56 "\\ifpdf\n"
57 "\\usepackage[pdftex]{graphicx}\n"
58 "\\else\n"
59 "\\usepackage[dvips]{graphicx}\n"
60 "\\fi\n"
61 "\\usepackage[svgnames]{xcolor}\n"
62 "\n"
63 "\\usepackage{listings}\n"
64 "\\lstset{breaklines=true,breakatwhitespace=true}\n"
65 "\\lstset{prebreak=\\mbox{$\\hookleftarrow$}}\n"
66 "\\lstset{basicstyle=\\footnotesize\\ttfamily}\n"
67 "\n"
68 "\\usepackage[ngerman]{babel}\n"
69 "\\usepackage[latin1]{inputenc}\n"
70 "\\usepackage[T1]{fontenc}\n"
71 "\\usepackage{lmodern}\n"
72 "\n"
73 "\\ifpdf\n"
74 "\\usepackage[a4paper,margin=15mm,top=25mm,bottom=20mm,pdftex]{geometry}\n"
75 "\\else\n"
76 "\\usepackage[a4paper,margin=15mm,top=25mm,bottom=20mm,dvips]{geometry}\n"
77 "\\fi\n"
78 "\n"
79 "\\usepackage{caption}\n"
80 "\\usepackage{float}\n"
81 "\\usepackage{url}\n"
82 "\n"
83 "\\floatstyle{ruled}\n"
84 "\\newfloat{l2lbox}{tbp}{lof}\n"
85 "\n"
86 "\\setlength{\\parskip}{6pt}\n"
87 "\n"
88 "\\renewcommand{\\headrulewidth}{0.4pt}\n"
89 "\\renewcommand{\\footrulewidth}{0pt}\n"
90 "\n"
91 "\\fancypagestyle{headings}\n"
92 "\\fancyhead{}\n"
93 "\\fancyhead[LO,RE]{}\n"
94 "\\fancyhead[LE,RO]{\\thepage}\n"
95 "\\fancyfoot{}\n"
96 "\n"
97 "\\pagestyle{fancy}\n"
98 "\n");
99 } /* write_tex_preamble */
101 /*
102 * public API
103 */
105 int
106 l2l_convert_file_fh(FILE *in, FILE *out)
107 {
108 yyscan_t scanner;
110 int status;
112 write_tex_preamble(out);
114 l2l_yylex_init(&scanner);
115 l2l_yyset_in(in, scanner);
116 l2l_yyset_out(out, scanner);
118 status = l2l_yylex(scanner);
120 l2l_yylex_destroy(scanner);
121 return status;
122 } /* l2l_convert_file_fh */
124 int
125 l2l_convert_file(const char *infile, const char *outfile)
126 {
127 FILE *in, *out;
129 int status;
131 in = fopen(infile, "r");
132 if (! in)
133 return -1;
135 out = fopen(outfile, "w");
136 if (! out) {
137 fclose(in);
138 return -1;
139 }
141 status = l2l_convert_file_fh(in, out);
142 fclose(in);
143 fclose(out);
144 return status;
145 } /* l2l_convert_file */
147 unsigned int
148 l2l_version(void)
149 {
150 return L2L_VERSION;
151 } /* l2l_version */
153 const char *
154 l2l_version_string(void)
155 {
156 return L2L_VERSION_STRING;
157 } /* l2l_version_string */
159 const char *
160 l2l_version_extra(void)
161 {
162 return L2L_VERSION_EXTRA;
163 } /* l2l_version_extra */
165 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */