1 /* Monolith smart text. -*- C -*-
2 * - by Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: ml_smarttext.l,v 1.1 2002/12/08 17:29:17 rich Exp $
34 #include "ml_smarttext.h"
36 /* This removes a warning message. */
39 static void emit_str (const char *s);
40 static void emit_strn (const char *str, const char *end);
41 static void emit_char (char c);
46 /* Characters in URLs. */
49 /* Characters in bold/italic/monospace text. */
50 ORDCHARS [[:alnum:] \t,\']
55 [[:lower:]]+"://"{URLCHAR}+ {
56 emit_str ("<a href=\"");
58 emit_str ("\" target=\"_new\">");
63 /* Special mailto: URLs. */
64 /* XXX Email addresses. */
66 emit_str ("<a href=\"");
68 emit_str ("\" target=\"_new\"><img src=\"/ml-icons/envelope.gif\"> ");
73 /* Fractions - must appear before italics. */
74 "1/4" emit_str ("¼");
75 "1/2" emit_str ("½");
76 "3/4" emit_str ("¾");
78 /* Special characters. */
79 "(C)" emit_str ("©");
80 "(R)" emit_str ("®");
81 "(TM)" emit_str ("<sup>TM</sup>");
83 /* Bold, italic, monospace text. */
86 emit_strn (yytext+1, &yytext[strlen (yytext)-1]);
91 emit_strn (yytext+1, &yytext[strlen (yytext)-1]);
96 emit_strn (yytext+1, &yytext[strlen (yytext)-1]);
102 emit_str ("<img src=\"/ml-icons/smiley.gif\">");
105 emit_str ("<img src=\"/ml-icons/sad.gif\">");
108 emit_str ("<img src=\"/ml-icons/tongue.gif\">");
111 /* HTML entities which need to be escaped. */
112 "&" emit_str ("&");
113 "<" emit_str ("<");
114 ">" emit_str (">");
115 \" emit_str (""");
116 \n emit_str ("<br>");
118 /* Anything else just gets emitted as a normal character. */
119 . emit_char (yytext[0]);
123 /* These global variables control where the output gets sent.
124 * Note that because none of this code blocks, we don't need to
125 * worry about these globals getting corrupted.
127 static pool out_pool; /* Pool for allocations. */
128 static char *out_str; /* Output string. */
129 static int used, allocated; /* Space used and allocated in the string. */
132 ml_smarttext_to_html (pool pool, const char *text)
136 /* Set up the global variables so that output goes to our string. */
139 used = allocated = 0;
141 /* We're going to scan from this buffer (instead of stdin). */
142 buf = yy_scan_string (text);
144 /* Run the scanner. */
148 yy_delete_buffer (buf);
150 /* Tack an ASCII NUL on the end of the string to terminate it. */
157 emit_str (const char *s)
159 while (*s) emit_char (*s++);
163 emit_strn (const char *str, const char *end)
165 while (str < end) emit_char (*str++);
171 if (used >= allocated)
174 out_str = prealloc (out_pool, out_str, allocated);