Code

l2l_scanner: Print "Abbildung|Listing" in bold font in figure captions.
[lm2latex.git] / src / l2l_scanner.l
1 /*
2  * lm2latex - src/l2l_scanner.l
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 -- LM markup scanner.
30  */
32 %{
33 #if HAVE_CONFIG_H
34 #       include "config.h"
35 #endif
37 #include <assert.h>
39 #include <ctype.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 typedef struct {
47         char  *data;
48         size_t size;
49         size_t len;
50 } l2l_strbuf_t;
52 #define L2L_STRBUF_INIT { NULL, 0, 0 }
53 #define L2L_STRBUF_FREE(buf) ((buf)->size - (buf)->len)
54 #define L2L_STRBUF_END(buf) ((buf)->data + (buf)->len)
55 #define L2L_STRBUF_CLEAR(buf) ((buf)->len = 0)
57 static int
58 l2l_strbuf_append(l2l_strbuf_t *buf, const char *fmt, ...);
60 static void
61 l2l_strbuf_chomp(l2l_strbuf_t *buf);
63 static void
64 l2l_strbuf_destroy(l2l_strbuf_t *buf);
66 #define L2L_YY_APPEND_OR_ECHO(strbuf, ...) \
67         do { \
68                 if ((strbuf)) { \
69                         if (l2l_strbuf_append((strbuf), __VA_ARGS__)) \
70                                 YY_FATAL_ERROR("ERROR: internal error"); \
71                 } \
72                 else \
73                         fprintf(yyout, __VA_ARGS__); \
74         } while (0)
76 #define L2L_STATE_HEADER_OR_ABORT \
77         do { \
78                 if (l2l_scanner_state != L2L_HEADER) \
79                         YY_FATAL_ERROR("ERROR: unexpected header definition"); \
80         } while (0)
82 #define L2L_PATTERN_IS(str) (! strncmp(yytext, (str), strlen(str)))
83 #define L2L_PATTERN_IS_HEADER \
84         (L2L_PATTERN_IS("@R:") || L2L_PATTERN_IS("@SW:") \
85                 || L2L_PATTERN_IS("@D:") || L2L_PATTERN_IS("@T:") \
86                 || L2L_PATTERN_IS("@A:") || L2L_PATTERN_IS("@V:"))
87 %}
89 %option yylineno
90 %option noyywrap
92 /* optimize for "fast scanning"; create an 8-bit scanner */
93 %option fast 8bit
95 /* generate a reentrant scanner in its own namespace */
96 %option reentrant
97 %option prefix="l2l_yy" outfile="lex.yy.c"
99 %option verbose warn
101 %s box
103 %x caption box_caption
104 %x command
105 %x headertxt
106 %x itemize
107 %x listing
108 %x title
109 %x url
111 COMMENT @#:.*
112 WHITESPACE [ \t]
114 ANY_COMMAND @(R|SW|D|T|V|A|L|ZT|LI|B|Bi|IT|IL|IE|KT|KL|KE|TT|TH|TL|TE):
116 %%
118 %{
119         enum {
120                 L2L_HEADER,
121                 L2L_BODY,
122                 L2L_FIGURE,
124                 L2L_UNQUOTED_TEXT,
125                 L2L_QUOTED_TEXT,
126         };
128         int l2l_scanner_state = L2L_HEADER;
129         int l2l_quote_state   = L2L_UNQUOTED_TEXT;
131         /* text formatting */
132         _Bool l2l_italic      = 0;
133         _Bool l2l_bold        = 0;
134         _Bool l2l_code        = 0;
135         _Bool l2l_sup         = 0;
136         _Bool l2l_sub         = 0;
137         _Bool l2l_li_italic   = 0;
139         /* title page information */
140         l2l_strbuf_t l2l_category = L2L_STRBUF_INIT;
141         l2l_strbuf_t l2l_keyword  = L2L_STRBUF_INIT;
142         l2l_strbuf_t l2l_title    = L2L_STRBUF_INIT;
143         l2l_strbuf_t l2l_headline = L2L_STRBUF_INIT;
144         l2l_strbuf_t l2l_author   = L2L_STRBUF_INIT;
145         l2l_strbuf_t l2l_abstract = L2L_STRBUF_INIT;
147         /* temp. buffers */
148         l2l_strbuf_t l2l_caption  = L2L_STRBUF_INIT;
149         l2l_strbuf_t l2l_ititle   = L2L_STRBUF_INIT;
151         l2l_strbuf_t *l2l_buf_ptr = NULL;
153         int l2l_last_yystart = INITIAL;
154 %}
156 {COMMENT} { fprintf(yyout, "%%%s", yytext + strlen("@#:")); }
158 <INITIAL,box,caption,box_caption,command,headertxt,itemize,title>{
159         /* LaTeX reserved characters */
160         [#$%&_{}] { L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\%s", yytext); }
161         [\^~]     { L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\%s{}", yytext); }
162         [\\]      { L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "$\\backslash$"); }
164         /* special characters */
165         /* '---' and '--' are handled by LaTeX */
166         \*\* L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "~");
168         :\* L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\,");
170         ["] {
171                 if (l2l_quote_state == L2L_UNQUOTED_TEXT) {
172                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\"`");
173                         l2l_quote_state = L2L_QUOTED_TEXT;
174                 }
175                 else {
176                         assert(l2l_quote_state == L2L_QUOTED_TEXT);
177                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\"'");
178                         l2l_quote_state = L2L_UNQUOTED_TEXT;
179                 }
180         }
182         /* text formatting */
183         \<[iI]> {
184                 if (l2l_italic) {
185                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
186                         l2l_italic = 0;
187                 }
188                 else {
189                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "{\\itshape ");
190                         l2l_italic = 1;
191                 }
192         }
194         \<[bB]> {
195                 if (l2l_bold) {
196                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
197                         l2l_bold = 0;
198                 }
199                 else {
200                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "{\\bfseries ");
201                         l2l_bold = 1;
202                 }
203         }
205         \<[cC]> {
206                 if (l2l_code) {
207                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
208                         l2l_code = 0;
209                 }
210                 else {
211                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "{\\ttfamily ");
212                         l2l_code = 1;
213                 }
214         }
216         \<[\+]> {
217                 if (l2l_sup) {
218                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}}");
219                         l2l_sup = 0;
220                 }
221                 else {
222                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr,
223                                         "\\ensuremath{^{\rmfamily ");
224                         l2l_sup = 1;
225                 }
226         }
228         \<[\-]> {
229                 if (l2l_sub) {
230                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}}");
231                         l2l_sub = 0;
232                 }
233                 else {
234                         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr,
235                                         "\\ensuremath{_{\rmfamily ");
236                         l2l_sub = 1;
237                 }
238         }
240         \[[0-9]+\] {
241                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr,
242                                 "{\\bfseries\\textcolor{DarkBlue}{%s}}", yytext);
243         }
246 \<[uU]> {
247         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\url{");
248         BEGIN(url);
251 <url>\<[uU]> {
252         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
253         BEGIN(l2l_last_yystart);
256 <url>.|\n {
257         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "%s", yytext);
260 (Abbildung|Listing)({WHITESPACE}|\n)+[0-9]+ {
261         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\textcolor{DarkBlue}{%s}", yytext);
264         /*
265          * header
266          */
268 @R:{WHITESPACE}* {
269         L2L_STATE_HEADER_OR_ABORT;
270         l2l_buf_ptr = &l2l_category;
271         BEGIN(headertxt);
274 @SW:{WHITESPACE}* {
275         L2L_STATE_HEADER_OR_ABORT;
276         l2l_buf_ptr = &l2l_keyword;
277         BEGIN(headertxt);
280 @D:{WHITESPACE}* {
281         L2L_STATE_HEADER_OR_ABORT;
282         l2l_buf_ptr = &l2l_title;
283         BEGIN(headertxt);
286 @T:{WHITESPACE}* {
287         L2L_STATE_HEADER_OR_ABORT;
288         l2l_buf_ptr = &l2l_headline;
289         BEGIN(headertxt);
292 @A:{WHITESPACE}* {
293         L2L_STATE_HEADER_OR_ABORT;
294         l2l_buf_ptr = &l2l_author;
295         BEGIN(headertxt);
298 @V:{WHITESPACE}* {
299         L2L_STATE_HEADER_OR_ABORT;
300         l2l_buf_ptr = &l2l_abstract;
301         BEGIN(headertxt);
304 <headertxt>{ANY_COMMAND} {
305         if (l2l_buf_ptr) {
306                 l2l_strbuf_chomp(l2l_buf_ptr);
307                 l2l_buf_ptr = NULL;
308         }
310         if (! L2L_PATTERN_IS_HEADER) {
311                 assert(l2l_scanner_state == L2L_HEADER);
313                 if (l2l_category.len || l2l_keyword.len
314                                 || l2l_title.len || l2l_headline.len) {
315                         fprintf(yyout, "\\title{");
317                         if (l2l_category.len || l2l_keyword.len) {
318                                 fprintf(yyout, "{\\scriptsize [");
319                                 if (l2l_category.len) {
320                                         fprintf(yyout, "%s", l2l_category.data);
321                                         if (l2l_keyword.len)
322                                                 fprintf(yyout, " $\\rightarrow$ ");
323                                 }
324                                 if (l2l_keyword.len)
325                                         fprintf(yyout, "%s", l2l_keyword.data);
326                                 fprintf(yyout, "]}\\\\\n\\vspace{5mm}\n");
327                         }
329                         if (l2l_headline.len) {
330                                 if (l2l_title.len)
331                                         fprintf(yyout, "\\normalsize %s\\\\\n\\LARGE ",
332                                                         l2l_title.data);
333                                 fprintf(yyout, "%s", l2l_headline.data);
334                         }
335                         else if (l2l_title.len)
336                                 fprintf(yyout, "%s", l2l_title.data);
338                         fprintf(yyout, "}\n");
339                 }
341                 if (l2l_author.len)
342                         fprintf(yyout, "\\author{%s}\n", l2l_author.data);
343                 fprintf(yyout, "\\date{\\today}\n\n");
345                 fprintf(yyout, "\\begin{document}\n\n");
346                 l2l_scanner_state = L2L_BODY;
348                 fprintf(yyout, "\\maketitle\n\n");
350                 if (l2l_abstract.len)
351                         fprintf(yyout, "\\begin{abstract}\n%s\n"
352                                         "\\end{abstract}\n\n", l2l_abstract.data);
354                 fprintf(yyout, "\\twocolumn\n\n");
355         }
357         yyless(0);
358         BEGIN(l2l_last_yystart);
361 <headertxt>{COMMENT} { /* ignore */ }
363 <headertxt>.|\n {
364         if (l2l_buf_ptr)
365                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
366                         YY_FATAL_ERROR("ERROR: internal error");
367         /* else ignore */
370         /*
371          * body
372          */
374 \n\n{WHITESPACE}*\*{WHITESPACE}* {
375         fprintf(yyout, "\n\n\\begin{itemize}\n\\item ");
376         BEGIN(itemize);
379 <itemize>({WHITESPACE}|\n)*\n\n |
380 <itemize>({WHITESPACE}|\n)*{ANY_COMMAND} {
381         fprintf(yyout, "\n\\end{itemize}");
382         yyless(0);
383         BEGIN(l2l_last_yystart);
386         /* this will produce a longer match and, thus, take precedence over
387          * the pattern above matching an empty line after the itemize */
388 <itemize>({WHITESPACE}|\n)*\n{WHITESPACE}*\*{WHITESPACE}* {
389         fprintf(yyout, "\n\\item ");
391 <itemize>({WHITESPACE}|\n)*\n\n({WHITESPACE}{2,}|\t) ECHO;
393 <itemize>. ECHO;
395 @ZT:{WHITESPACE}* {
396         fprintf(yyout, "\\subsection*{");
397         BEGIN(command);
400 <command>({WHITESPACE}|\n)+{ANY_COMMAND} {
401         fprintf(yyout, "}");
402         yyless(0);
403         BEGIN(l2l_last_yystart);
406 <command>. ECHO;
408 @LI:{WHITESPACE}*\n? {
409         /* \xb7 = MIDDLE DOT (iso-8859-1 and iso-8859-15) */
410         fprintf(yyout, "\\begin{lstlisting}[escapechar=\xb7]\n");
411         BEGIN(listing);
414 <listing>\n*{ANY_COMMAND} {
415         fprintf(yyout, "\n\\end{lstlisting}");
416         yyless(0);
417         BEGIN(l2l_last_yystart);
420 <listing>\<§§I> {
421         if (l2l_li_italic) {
422                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7");
423                 l2l_li_italic = 0;
424         }
425         else {
426                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7\\itshape ");
427                 l2l_li_italic = 1;
428         }
431 <listing>§§\n /* ignore => lstlisting will insert appropriate arrows */;
433 <listing>. ECHO;
435         /*
436          * images/boxes
437          */
439 @(B|KT):{WHITESPACE}*((Abbildung|Listing)({WHITESPACE}|\n)+[0-9]+:)? {
440         _Bool is_image = 0;
441         _Bool is_box   = 0;
443         char *label    = NULL;
445         if (! strncmp(yytext, "@B:", 3))
446                 is_image = 1;
447         else if (! strncmp(yytext, "@KT:", 4))
448                 is_box = 1;
450         if (l2l_scanner_state != L2L_FIGURE) {
451                 if (is_box)
452                         fprintf(yyout, "\\begin{l2lbox}\n");
453                 else
454                         fprintf(yyout, "\\begin{figure}\n");
455                 l2l_scanner_state = L2L_FIGURE;
456         }
457         else {
458                 l2l_scanner_state = L2L_BODY;
459         }
461         label = strstr(yytext, "Abbildung");
462         if (! label)
463                 label = strstr(yytext, "Listing");
465         if (label)
466                 if (l2l_strbuf_append(&l2l_caption, "{\\bfseries %s}", label))
467                         YY_FATAL_ERROR("ERROR: internal error");
469         l2l_buf_ptr = &l2l_caption;
470         if (is_image)
471                 BEGIN(caption);
472         else if (is_box)
473                 BEGIN(box_caption);
474         else
475                 assert(0);
478 @Bi:.*\n {
479         char *filename;
481         yytext[yyleng - 1] = '\0';
483         filename = yytext + strlen("@Bi:");
484         while (isspace((int)*filename))
485                 ++filename;
487         if (l2l_scanner_state != L2L_FIGURE) {
488                 fprintf(yyout, "\\begin{figure}\n");
489                 l2l_scanner_state = L2L_FIGURE;
490         }
491         else {
492                 l2l_scanner_state = L2L_BODY;
493         }
495         fprintf(yyout, "\\includegraphics[width=\\columnwidth]{%s}\n", filename);
497         if (l2l_scanner_state == L2L_BODY)
498                 fprintf(yyout, "\\end{figure}\n");
501 @KL: {
502         if (l2l_scanner_state != L2L_FIGURE) {
503                 fprintf(yyout, "\\begin{l2lbox}\n");
504                 l2l_scanner_state = L2L_FIGURE;
505         }
506         else {
507                 l2l_scanner_state = L2L_BODY;
508         }
510         l2l_last_yystart = box;
511         BEGIN(box);
514 <box>@(KE|L):{WHITESPACE}* {
515         fprintf(yyout, "\\end{l2lbox}");
516         l2l_scanner_state = L2L_BODY;
517         l2l_last_yystart = INITIAL;
518         BEGIN(l2l_last_yystart);
521 <caption,box_caption>{ANY_COMMAND} {
522         if (l2l_buf_ptr) {
523                 l2l_strbuf_chomp(l2l_buf_ptr);
524                 l2l_buf_ptr = NULL;
525         }
527         if (l2l_caption.len) {
528                 fprintf(yyout, "\\caption*{%s}\n", l2l_caption.data);
529                 L2L_STRBUF_CLEAR(&l2l_caption);
530         }
532         if ((l2l_scanner_state == L2L_BODY) && (YY_START != box_caption))
533                 fprintf(yyout, "\\end{figure}\n\n");
535         yyless(0);
536         if (YY_START == caption)
537                 BEGIN(l2l_last_yystart);
538         else if (YY_START == box_caption) {
539                 l2l_last_yystart = box;
540                 BEGIN(box);
541         }
542         else
543                 assert(0);
546 <caption,box_caption>{COMMENT} { /* ignore */ }
548 <caption,box_caption>.|\n {
549         if (l2l_buf_ptr)
550                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
551                         YY_FATAL_ERROR("ERROR: internal error");
552         /* else ignore */
555         /*
556          * infos
557          */
559 @IT:{WHITESPACE}* {
560         l2l_buf_ptr = &l2l_ititle;
561         BEGIN(title);
564 <title>{ANY_COMMAND} {
565         if (l2l_buf_ptr) {
566                 l2l_strbuf_chomp(l2l_buf_ptr);
567                 l2l_buf_ptr = NULL;
568         }
570         if (l2l_ititle.len) {
571                 fprintf(yyout, "\\subsection*{\\rule{\\columnwidth}{1pt}"
572                                 "\\newline %s}\n", l2l_ititle.data);
573                 L2L_STRBUF_CLEAR(&l2l_ititle);
574         }
576         yyless(0);
577         BEGIN(l2l_last_yystart);
580 <title>{COMMENT} { /* ignore */ }
582 <title>.|\n {
583         if (l2l_buf_ptr)
584                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
585                         YY_FATAL_ERROR("ERROR: internal error");
586         /* else ignore */
589 @IL: { /* ignore */ }
591 @IE: { /* ignore */ }
593         /*
594          * tables
595          */
597 @TT: /* TODO */;
599 @TH: /* TODO */;
601 @TL: /* TODO */;
603 @TE: /* TODO */;
605         /* let all other rules that match "@L:" have a higher preceedence */
606 @L:{WHITESPACE}* { /* nothing to do */ }
608 <<EOF>> {
609         if (l2l_scanner_state == L2L_BODY) {
610                 fprintf(yyout, "\\end{document}\n");
611         }
612         else
613                 YY_FATAL_ERROR("ERROR: incomplete document");
615         l2l_strbuf_destroy(&l2l_category);
616         l2l_strbuf_destroy(&l2l_keyword);
617         l2l_strbuf_destroy(&l2l_title);
618         l2l_strbuf_destroy(&l2l_headline);
619         l2l_strbuf_destroy(&l2l_author);
620         l2l_strbuf_destroy(&l2l_abstract);
622         l2l_strbuf_destroy(&l2l_caption);
623         l2l_strbuf_destroy(&l2l_title);
625         yyterminate();
628 %%
630 static int
631 l2l_strbuf_append(l2l_strbuf_t *buf, const char *fmt, ...)
633         size_t  str_len;
634         va_list ap;
636         int status;
638         if ((! buf) || (! fmt))
639                 return 0;
641         va_start(ap, fmt);
642         status = vsnprintf(NULL, 0, fmt, ap);
643         va_end(ap);
645         if (status <= 0)
646                 return -1;
648         str_len = (size_t)status;
650         if (str_len > L2L_STRBUF_FREE(buf)) {
651                 char  *new_data;
652                 size_t new_size = 2 * buf->size;
654                 if (str_len > buf->size)
655                         new_size += str_len;
657                 new_data = realloc(buf->data, new_size + 1);
658                 if (! new_data) {
659                         l2l_strbuf_destroy(buf);
660                         return -1;
661                 }
663                 buf->data = new_data;
664                 buf->size = new_size;
665         }
666         assert(buf->len + str_len <= buf->size);
668         va_start(ap, fmt);
669         status = vsnprintf(L2L_STRBUF_END(buf), L2L_STRBUF_FREE(buf) + 1, fmt, ap);
670         va_end(ap);
672         if (status <= 0)
673                 return -1;
675         assert((size_t)status == str_len);
676         buf->len += str_len;
677         buf->data[buf->len] = '\0';
678         return 0;
679 } /* l2l_strbuf_append */
681 static void
682 l2l_strbuf_chomp(l2l_strbuf_t *buf)
684         if (! buf)
685                 return;
687         while (buf->len && isspace((int)buf->data[buf->len - 1])) {
688                 buf->data[buf->len - 1] = '\0';
689                 --buf->len;
690         }
691 } /* l2l_strbuf_chomp */
693 static void
694 l2l_strbuf_destroy(l2l_strbuf_t *buf)
696         if (! buf)
697                 return;
699         if (buf->data)
700                 free(buf->data);
701         memset(buf, 0, sizeof(*buf));
702 } /* l2l_strbuf_destroy */
704 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */