Code

l2l, l2l_scanner: Surround boxes with rules.
[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         }
241 \<[uU]> {
242         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\\url{");
243         BEGIN(url);
246 <url>\<[uU]> {
247         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "}");
248         BEGIN(l2l_last_yystart);
251 <url>.|\n {
252         L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "%s", yytext);
255         /*
256          * header
257          */
259 @R:{WHITESPACE}* {
260         L2L_STATE_HEADER_OR_ABORT;
261         l2l_buf_ptr = &l2l_category;
262         BEGIN(headertxt);
265 @SW:{WHITESPACE}* {
266         L2L_STATE_HEADER_OR_ABORT;
267         l2l_buf_ptr = &l2l_keyword;
268         BEGIN(headertxt);
271 @D:{WHITESPACE}* {
272         L2L_STATE_HEADER_OR_ABORT;
273         l2l_buf_ptr = &l2l_title;
274         BEGIN(headertxt);
277 @T:{WHITESPACE}* {
278         L2L_STATE_HEADER_OR_ABORT;
279         l2l_buf_ptr = &l2l_headline;
280         BEGIN(headertxt);
283 @A:{WHITESPACE}* {
284         L2L_STATE_HEADER_OR_ABORT;
285         l2l_buf_ptr = &l2l_author;
286         BEGIN(headertxt);
289 @V:{WHITESPACE}* {
290         L2L_STATE_HEADER_OR_ABORT;
291         l2l_buf_ptr = &l2l_abstract;
292         BEGIN(headertxt);
295 <headertxt>{ANY_COMMAND} {
296         if (l2l_buf_ptr) {
297                 l2l_strbuf_chomp(l2l_buf_ptr);
298                 l2l_buf_ptr = NULL;
299         }
301         if (! L2L_PATTERN_IS_HEADER) {
302                 assert(l2l_scanner_state == L2L_HEADER);
304                 if (l2l_category.len || l2l_keyword.len
305                                 || l2l_title.len || l2l_headline.len) {
306                         fprintf(yyout, "\\title{");
308                         if (l2l_category.len || l2l_keyword.len) {
309                                 fprintf(yyout, "{\\scriptsize [");
310                                 if (l2l_category.len) {
311                                         fprintf(yyout, "%s", l2l_category.data);
312                                         if (l2l_keyword.len)
313                                                 fprintf(yyout, " $\\rightarrow$ ");
314                                 }
315                                 if (l2l_keyword.len)
316                                         fprintf(yyout, "%s", l2l_keyword.data);
317                                 fprintf(yyout, "]}\\\\\n\\vspace{5mm}\n");
318                         }
320                         if (l2l_headline.len) {
321                                 if (l2l_title.len)
322                                         fprintf(yyout, "\\normalsize %s\\\\\n\\LARGE ",
323                                                         l2l_title.data);
324                                 fprintf(yyout, "%s", l2l_headline.data);
325                         }
326                         else if (l2l_title.len)
327                                 fprintf(yyout, "%s", l2l_title.data);
329                         fprintf(yyout, "}\n");
330                 }
332                 if (l2l_author.len)
333                         fprintf(yyout, "\\author{%s}\n", l2l_author.data);
334                 fprintf(yyout, "\\date{\\today}\n\n");
336                 fprintf(yyout, "\\begin{document}\n\n");
337                 l2l_scanner_state = L2L_BODY;
339                 fprintf(yyout, "\\maketitle\n\n");
341                 if (l2l_abstract.len)
342                         fprintf(yyout, "\\begin{abstract}\n%s\n"
343                                         "\\end{abstract}\n\n", l2l_abstract.data);
345                 fprintf(yyout, "\\twocolumn\n\n");
346         }
348         yyless(0);
349         BEGIN(l2l_last_yystart);
352 <headertxt>{COMMENT} { /* ignore */ }
354 <headertxt>.|\n {
355         if (l2l_buf_ptr)
356                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
357                         YY_FATAL_ERROR("ERROR: internal error");
358         /* else ignore */
361         /*
362          * body
363          */
365 \n\n{WHITESPACE}*\*{WHITESPACE}* {
366         fprintf(yyout, "\n\n\\begin{itemize}\n\\item ");
367         BEGIN(itemize);
370 <itemize>({WHITESPACE}|\n)*\n\n |
371 <itemize>({WHITESPACE}|\n)*{ANY_COMMAND} {
372         fprintf(yyout, "\n\\end{itemize}");
373         yyless(0);
374         BEGIN(l2l_last_yystart);
377         /* this will produce a longer match and, thus, take precedence over
378          * the pattern above matching an empty line after the itemize */
379 <itemize>({WHITESPACE}|\n)*\n{WHITESPACE}*\*{WHITESPACE}* {
380         fprintf(yyout, "\n\\item ");
382 <itemize>({WHITESPACE}|\n)*\n\n({WHITESPACE}{2,}|\t) ECHO;
384 <itemize>. ECHO;
386 @ZT:{WHITESPACE}* {
387         fprintf(yyout, "\\subsection*{");
388         BEGIN(command);
391 <command>({WHITESPACE}|\n)+{ANY_COMMAND} {
392         fprintf(yyout, "}");
393         yyless(0);
394         BEGIN(l2l_last_yystart);
397 <command>. ECHO;
399 @LI:{WHITESPACE}*\n? {
400         /* \xb7 = MIDDLE DOT (iso-8859-1 and iso-8859-15) */
401         fprintf(yyout, "\\begin{lstlisting}[escapechar=\xb7]\n");
402         BEGIN(listing);
405 <listing>\n*{ANY_COMMAND} {
406         fprintf(yyout, "\n\\end{lstlisting}");
407         yyless(0);
408         BEGIN(l2l_last_yystart);
411 <listing>\<§§I> {
412         if (l2l_li_italic) {
413                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7");
414                 l2l_li_italic = 0;
415         }
416         else {
417                 L2L_YY_APPEND_OR_ECHO(l2l_buf_ptr, "\xb7\\itshape ");
418                 l2l_li_italic = 1;
419         }
422 <listing>§§\n /* ignore => lstlisting will insert appropriate arrows */;
424 <listing>. ECHO;
426         /*
427          * images/boxes
428          */
430 @(B|KT):{WHITESPACE}* {
431         _Bool is_image = 0;
432         _Bool is_box   = 0;
434         if (! strncmp(yytext, "@B:", 3))
435                 is_image = 1;
436         else if (! strncmp(yytext, "@KT:", 4))
437                 is_box = 1;
439         if (l2l_scanner_state != L2L_FIGURE) {
440                 if (is_box)
441                         fprintf(yyout, "\\begin{l2lbox}\n");
442                 else
443                         fprintf(yyout, "\\begin{figure}\n");
444                 l2l_scanner_state = L2L_FIGURE;
445         }
446         else {
447                 l2l_scanner_state = L2L_BODY;
448         }
450         l2l_buf_ptr = &l2l_caption;
451         if (is_image)
452                 BEGIN(caption);
453         else if (is_box)
454                 BEGIN(box_caption);
455         else
456                 assert(0);
459 @Bi:.*\n {
460         char *filename;
462         yytext[yyleng - 1] = '\0';
464         filename = yytext + strlen("@Bi:");
465         while (isspace((int)*filename))
466                 ++filename;
468         if (l2l_scanner_state != L2L_FIGURE) {
469                 fprintf(yyout, "\\begin{figure}\n");
470                 l2l_scanner_state = L2L_FIGURE;
471         }
472         else {
473                 l2l_scanner_state = L2L_BODY;
474         }
476         fprintf(yyout, "\\includegraphics[width=\\columnwidth]{%s}\n", filename);
478         if (l2l_scanner_state == L2L_BODY)
479                 fprintf(yyout, "\\end{figure}\n");
482 @KL: {
483         if (l2l_scanner_state != L2L_FIGURE) {
484                 fprintf(yyout, "\\begin{l2lbox}\n");
485                 l2l_scanner_state = L2L_FIGURE;
486         }
487         else {
488                 l2l_scanner_state = L2L_BODY;
489         }
491         l2l_last_yystart = box;
492         BEGIN(box);
495 <box>@(KE|L):{WHITESPACE}* {
496         fprintf(yyout, "\\end{l2lbox}");
497         l2l_scanner_state = L2L_BODY;
498         l2l_last_yystart = INITIAL;
499         BEGIN(l2l_last_yystart);
502 <caption,box_caption>{ANY_COMMAND} {
503         if (l2l_buf_ptr) {
504                 l2l_strbuf_chomp(l2l_buf_ptr);
505                 l2l_buf_ptr = NULL;
506         }
508         if (l2l_caption.len) {
509                 fprintf(yyout, "\\caption*{%s}\n", l2l_caption.data);
510                 L2L_STRBUF_CLEAR(&l2l_caption);
511         }
513         if ((l2l_scanner_state == L2L_BODY) && (YY_START != box_caption))
514                 fprintf(yyout, "\\end{figure}\n\n");
516         yyless(0);
517         if (YY_START == caption)
518                 BEGIN(l2l_last_yystart);
519         else if (YY_START == box_caption) {
520                 l2l_last_yystart = box;
521                 BEGIN(box);
522         }
523         else
524                 assert(0);
527 <caption,box_caption>{COMMENT} { /* ignore */ }
529 <caption,box_caption>.|\n {
530         if (l2l_buf_ptr)
531                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
532                         YY_FATAL_ERROR("ERROR: internal error");
533         /* else ignore */
536         /*
537          * infos
538          */
540 @IT:{WHITESPACE}* {
541         l2l_buf_ptr = &l2l_ititle;
542         BEGIN(title);
545 <title>{ANY_COMMAND} {
546         if (l2l_buf_ptr) {
547                 l2l_strbuf_chomp(l2l_buf_ptr);
548                 l2l_buf_ptr = NULL;
549         }
551         if (l2l_ititle.len) {
552                 fprintf(yyout, "\\subsection*{\\rule{\\columnwidth}{1pt}"
553                                 "\\newline %s}\n", l2l_ititle.data);
554                 L2L_STRBUF_CLEAR(&l2l_ititle);
555         }
557         yyless(0);
558         BEGIN(l2l_last_yystart);
561 <title>{COMMENT} { /* ignore */ }
563 <title>.|\n {
564         if (l2l_buf_ptr)
565                 if (l2l_strbuf_append(l2l_buf_ptr, "%s", yytext))
566                         YY_FATAL_ERROR("ERROR: internal error");
567         /* else ignore */
570 @IL: { /* ignore */ }
572 @IE: { /* ignore */ }
574         /*
575          * tables
576          */
578 @TT: /* TODO */;
580 @TH: /* TODO */;
582 @TL: /* TODO */;
584 @TE: /* TODO */;
586         /* let all other rules that match "@L:" have a higher preceedence */
587 @L:{WHITESPACE}* { /* nothing to do */ }
589 <<EOF>> {
590         if (l2l_scanner_state == L2L_BODY) {
591                 fprintf(yyout, "\\end{document}\n");
592         }
593         else
594                 YY_FATAL_ERROR("ERROR: incomplete document");
596         l2l_strbuf_destroy(&l2l_category);
597         l2l_strbuf_destroy(&l2l_keyword);
598         l2l_strbuf_destroy(&l2l_title);
599         l2l_strbuf_destroy(&l2l_headline);
600         l2l_strbuf_destroy(&l2l_author);
601         l2l_strbuf_destroy(&l2l_abstract);
603         l2l_strbuf_destroy(&l2l_caption);
604         l2l_strbuf_destroy(&l2l_title);
606         yyterminate();
609 %%
611 static int
612 l2l_strbuf_append(l2l_strbuf_t *buf, const char *fmt, ...)
614         size_t  str_len;
615         va_list ap;
617         int status;
619         if ((! buf) || (! fmt))
620                 return 0;
622         va_start(ap, fmt);
623         status = vsnprintf(NULL, 0, fmt, ap);
624         va_end(ap);
626         if (status <= 0)
627                 return -1;
629         str_len = (size_t)status;
631         if (str_len > L2L_STRBUF_FREE(buf)) {
632                 char  *new_data;
633                 size_t new_size = 2 * buf->size;
635                 if (str_len > buf->size)
636                         new_size += str_len;
638                 new_data = realloc(buf->data, new_size + 1);
639                 if (! new_data) {
640                         l2l_strbuf_destroy(buf);
641                         return -1;
642                 }
644                 buf->data = new_data;
645                 buf->size = new_size;
646         }
647         assert(buf->len + str_len <= buf->size);
649         va_start(ap, fmt);
650         status = vsnprintf(L2L_STRBUF_END(buf), L2L_STRBUF_FREE(buf) + 1, fmt, ap);
651         va_end(ap);
653         if (status <= 0)
654                 return -1;
656         assert((size_t)status == str_len);
657         buf->len += str_len;
658         buf->data[buf->len] = '\0';
659         return 0;
660 } /* l2l_strbuf_append */
662 static void
663 l2l_strbuf_chomp(l2l_strbuf_t *buf)
665         if (! buf)
666                 return;
668         while (buf->len && isspace((int)buf->data[buf->len - 1])) {
669                 buf->data[buf->len - 1] = '\0';
670                 --buf->len;
671         }
672 } /* l2l_strbuf_chomp */
674 static void
675 l2l_strbuf_destroy(l2l_strbuf_t *buf)
677         if (! buf)
678                 return;
680         if (buf->data)
681                 free(buf->data);
682         memset(buf, 0, sizeof(*buf));
683 } /* l2l_strbuf_destroy */
685 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */