Code

499ed7f039d91e5866e99c132862e4d5773d8698
[lm2latex.git] / src / l2l.c
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{\\captionfont}{\\footnotesize}\n"
89 "\\setcounter{secnumdepth}{-1}\n"
90 "\n"
91 "\\renewcommand{\\headrulewidth}{0.4pt}\n"
92 "\\renewcommand{\\footrulewidth}{0pt}\n"
93 "\n"
94 "\\fancypagestyle{headings}\n"
95 "\\fancyhead{}\n"
96 "\\fancyhead[LO,RE]{}\n"
97 "\\fancyhead[LE,RO]{\\thepage}\n"
98 "\\fancyfoot{}\n"
99 "\n"
100 "\\pagestyle{fancy}\n"
101 "\n");
102 } /* write_tex_preamble */
104 /*
105  * public API
106  */
108 int
109 l2l_convert_file_fh(FILE *in, FILE *out)
111         yyscan_t scanner;
113         int status;
115         write_tex_preamble(out);
117         l2l_yylex_init(&scanner);
118         l2l_yyset_in(in, scanner);
119         l2l_yyset_out(out, scanner);
121         status = l2l_yylex(scanner);
123         l2l_yylex_destroy(scanner);
124         return status;
125 } /* l2l_convert_file_fh */
127 int
128 l2l_convert_file(const char *infile, const char *outfile)
130         FILE *in, *out;
132         int status;
134         in = fopen(infile, "r");
135         if (! in)
136                 return -1;
138         out = fopen(outfile, "w");
139         if (! out) {
140                 fclose(in);
141                 return -1;
142         }
144         status = l2l_convert_file_fh(in, out);
145         fclose(in);
146         fclose(out);
147         return status;
148 } /* l2l_convert_file */
150 unsigned int
151 l2l_version(void)
153         return L2L_VERSION;
154 } /* l2l_version */
156 const char *
157 l2l_version_string(void)
159         return L2L_VERSION_STRING;
160 } /* l2l_version_string */
162 const char *
163 l2l_version_extra(void)
165         return L2L_VERSION_EXTRA;
166 } /* l2l_version_extra */
168 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */