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{float}\n"
80 "\\usepackage{url}\n"
81 "\n"
82 "\\floatstyle{ruled}\n"
83 "\\newfloat{l2lbox}{tbp}{lof}\n"
84 "\n"
85 "\\setlength{\\parskip}{6pt}\n"
86 "\n"
87 "\\renewcommand{\\headrulewidth}{0.4pt}\n"
88 "\\renewcommand{\\footrulewidth}{0pt}\n"
89 "\n"
90 "\\fancypagestyle{headings}\n"
91 "\\fancyhead{}\n"
92 "\\fancyhead[LO,RE]{}\n"
93 "\\fancyhead[LE,RO]{\\thepage}\n"
94 "\\fancyfoot{}\n"
95 "\n"
96 "\\pagestyle{fancy}\n"
97 "\n");
98 } /* write_tex_preamble */
100 /*
101 * public API
102 */
104 int
105 l2l_convert_file_fh(FILE *in, FILE *out)
106 {
107 yyscan_t scanner;
109 int status;
111 write_tex_preamble(out);
113 l2l_yylex_init(&scanner);
114 l2l_yyset_in(in, scanner);
115 l2l_yyset_out(out, scanner);
117 status = l2l_yylex(scanner);
119 l2l_yylex_destroy(scanner);
120 return status;
121 } /* l2l_convert_file_fh */
123 int
124 l2l_convert_file(const char *infile, const char *outfile)
125 {
126 FILE *in, *out;
128 int status;
130 in = fopen(infile, "r");
131 if (! in)
132 return -1;
134 out = fopen(outfile, "w");
135 if (! out) {
136 fclose(in);
137 return -1;
138 }
140 status = l2l_convert_file_fh(in, out);
141 fclose(in);
142 fclose(out);
143 return status;
144 } /* l2l_convert_file */
146 unsigned int
147 l2l_version(void)
148 {
149 return L2L_VERSION;
150 } /* l2l_version */
152 const char *
153 l2l_version_string(void)
154 {
155 return L2L_VERSION_STRING;
156 } /* l2l_version_string */
158 const char *
159 l2l_version_extra(void)
160 {
161 return L2L_VERSION_EXTRA;
162 } /* l2l_version_extra */
164 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */