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 "\n"
62 "\\usepackage{listings}\n"
63 "\\lstset{breaklines=true,breakatwhitespace=true}\n"
64 "\\lstset{prebreak=\\mbox{$\\hookleftarrow$}}\n"
65 "\\lstset{basicstyle=\\footnotesize\\ttfamily}\n"
66 "\n"
67 "\\usepackage[ngerman]{babel}\n"
68 "\\usepackage[latin1]{inputenc}\n"
69 "\\usepackage[T1]{fontenc}\n"
70 "\\usepackage{lmodern}\n"
71 "\n"
72 "\\ifpdf\n"
73 "\\usepackage[a4paper,margin=15mm,top=25mm,bottom=20mm,pdftex]{geometry}\n"
74 "\\else\n"
75 "\\usepackage[a4paper,margin=15mm,top=25mm,bottom=20mm,dvips]{geometry}\n"
76 "\\fi\n"
77 "\n"
78 "\\usepackage{caption}\n"
79 "\\usepackage{url}\n"
80 "\n"
81 "\\setlength{\\parskip}{6pt}\n"
82 "\n"
83 "\\renewcommand{\\headrulewidth}{0.4pt}\n"
84 "\\renewcommand{\\footrulewidth}{0pt}\n"
85 "\n"
86 "\\fancypagestyle{headings}\n"
87 "\\fancyhead{}\n"
88 "\\fancyhead[LO,RE]{}\n"
89 "\\fancyhead[LE,RO]{\\thepage}\n"
90 "\\fancyfoot{}\n"
91 "\n"
92 "\\pagestyle{fancy}\n"
93 "\n");
94 } /* write_tex_preamble */
96 /*
97 * public API
98 */
100 int
101 l2l_convert_file_fh(FILE *in, FILE *out)
102 {
103 yyscan_t scanner;
105 int status;
107 write_tex_preamble(out);
109 l2l_yylex_init(&scanner);
110 l2l_yyset_in(in, scanner);
111 l2l_yyset_out(out, scanner);
113 status = l2l_yylex(scanner);
115 l2l_yylex_destroy(scanner);
116 return status;
117 } /* l2l_convert_file_fh */
119 int
120 l2l_convert_file(const char *infile, const char *outfile)
121 {
122 FILE *in, *out;
124 int status;
126 in = fopen(infile, "r");
127 if (! in)
128 return -1;
130 out = fopen(outfile, "w");
131 if (! out) {
132 fclose(in);
133 return -1;
134 }
136 status = l2l_convert_file_fh(in, out);
137 fclose(in);
138 fclose(out);
139 return status;
140 } /* l2l_convert_file */
142 unsigned int
143 l2l_version(void)
144 {
145 return L2L_VERSION;
146 } /* l2l_version */
148 const char *
149 l2l_version_string(void)
150 {
151 return L2L_VERSION_STRING;
152 } /* l2l_version_string */
154 const char *
155 l2l_version_extra(void)
156 {
157 return L2L_VERSION_EXTRA;
158 } /* l2l_version_extra */
160 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */