8 #define LENGTH(vars) (sizeof(vars) / sizeof(vars[0]))
10 static char *markers
[] = {"**", "$$", "%%", "[]", "||", "//", "''", "``"};
11 static char *blocks
[][2] = {
12 {".EQ", ".EN"}, {".PS", ".PE"}, {".TS", ".TE"},
13 {".DS", ".DE"}, {".G1", ".G2"}, {".[", ".]"},
23 static struct fmt
*fmt_alloc(struct doc
*doc
, struct txt
*txt
,
26 struct fmt
*fmt
= xmalloc(sizeof(struct fmt
));
34 static void fmt_free(struct fmt
*fmt
)
39 static char *fmt_line(struct fmt
*fmt
, int line
)
41 char *s
= txt_line(fmt
->txt
, line
);
43 for (i
= 0; i
< fmt
->level
&& s
&& *s
; i
++)
48 static char *marker_char(char c
, char p
, char n
)
51 if (p
== '\\' || (p
&& !isspace(p
)) || isspace(n
))
53 for (i
= 0; i
< LENGTH(markers
); i
++)
54 if (markers
[i
][0] == c
)
59 static char *fillbuf(char *beg
, char *end
)
61 static char buf
[MAXLINE
];
62 memcpy(buf
, beg
, end
- beg
);
63 buf
[end
- beg
] = '\0';
67 static void put_text(struct fmt
*fmt
, char *s
)
73 while (*s
&& !marker_char(*s
, s
== _s
? 0 : s
[-1], s
[1]))
77 m
= marker_char(*s
, s
== _s
? 0 : s
[-1], s
[1]);
78 r
= strchr(s
+ 1, m
[1]);
80 fmt
->ops
->put(fmt
->doc
, fillbuf(o
, s
));
81 fmt
->ops
->put_txt(fmt
->doc
, fillbuf(s
+ 1, r
), m
);
88 fmt
->ops
->put(fmt
->doc
, o
);
91 static void raw_line(struct fmt
*fmt
, char *s
)
93 fmt
->ops
->put(fmt
->doc
, s
);
96 static void put_line(struct fmt
*fmt
, int n
)
98 put_text(fmt
, fmt_line(fmt
, n
));
102 static void put_lines(struct fmt
*fmt
, int beg
, int end
)
105 for (i
= beg
; i
< end
; i
++)
109 static void raw_lines(struct fmt
*fmt
, int beg
, int end
)
112 for (i
= beg
; i
< end
; i
++) {
113 raw_line(fmt
, fmt_line(fmt
, i
));
118 static int indents(char *s
)
126 static int islist(char *first
, char *line
)
130 if (!first
|| strlen(first
) < 2)
132 if (first
[1] != ' ' || !strchr(signs
, first
[0]))
136 if (strlen(line
) < 2)
138 return line
[0] == first
[0] && line
[1] == first
[1];
141 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
);
143 static int fmt_head(struct fmt
*fmt
, int beg
, int end
)
145 char *line
= fmt_line(fmt
, beg
);
146 char *next
= fmt_line(fmt
, beg
+ 1);
150 if (!next
|| !*line
|| beg
== end
|| fmt
->level
)
153 if (!c
|| !strchr(signs
, *next
))
158 fmt
->ops
->head_beg(fmt
->doc
, strchr(signs
, *next
) - signs
);
160 fmt
->ops
->head_end(fmt
->doc
, strchr(signs
, *next
) - signs
);
164 static int cmd_line(struct fmt
*fmt
, int i
)
166 return !fmt
->level
&& *fmt_line(fmt
, i
) == '.';
169 static int par_end(struct fmt
*fmt
, int beg
, int end
)
173 while (i
< end
&& (line
= fmt_line(fmt
, i
))) {
174 if (!*line
|| indents(line
) || islist(line
, NULL
) || cmd_line(fmt
, i
))
181 static int fmt_par(struct fmt
*fmt
, int beg
, int end
)
184 if (indents(fmt_line(fmt
, beg
)) || cmd_line(fmt
, beg
))
186 fmt
->ops
->par_beg(fmt
->doc
);
187 i
= par_end(fmt
, beg
, end
);
188 put_lines(fmt
, beg
, i
);
189 fmt
->ops
->par_end(fmt
->doc
);
193 static int fmt_rawline(struct fmt
*fmt
, int beg
, int end
)
195 if (*fmt_line(fmt
, beg
) != '.')
197 raw_lines(fmt
, beg
, beg
+ 1);
201 static int min(int a
, int b
)
203 return a
< b
? a
: b
;
206 static int fmt_deindent(struct fmt
*fmt
, int n
, int indent
)
210 while ((line
= fmt_line(fmt
, n
))) {
211 if (*line
&& indents(line
) < indent
)
220 static int fmt_pre(struct fmt
*fmt
, int beg
, int end
)
222 int level
= indents(fmt_line(fmt
, beg
));
223 int next
= min(end
, fmt_deindent(fmt
, beg
+ 1, level
));
225 fmt
->ops
->block_beg(fmt
->doc
, NULL
);
226 raw_lines(fmt
, beg
, next
);
227 fmt
->ops
->block_end(fmt
->doc
, NULL
);
232 static int fmt_block(struct fmt
*fmt
, int beg
, int end
)
234 char *block_beg
= NULL
;
235 char *block_end
= NULL
;
240 line
= fmt_line(fmt
, beg
);
242 return fmt_pre(fmt
, beg
, end
);
245 for (i
= 0; i
< LENGTH(blocks
); i
++) {
246 block_beg
= blocks
[i
][0];
247 block_end
= blocks
[i
][1];
248 if (!strncmp(block_beg
, line
, strlen(block_beg
)))
251 if (i
== LENGTH(blocks
))
254 while (next
< end
&& depth
> 0) {
255 line
= fmt_line(fmt
, next
++);
256 if (!strncmp(block_beg
, line
, strlen(block_beg
)))
258 if (!strncmp(block_end
, line
, strlen(block_end
)))
261 fmt
->ops
->block_beg(fmt
->doc
, fmt_line(fmt
, beg
));
263 raw_lines(fmt
, beg
+ 1, next
- 1);
264 fmt
->ops
->block_end(fmt
->doc
, fmt_line(fmt
, next
- 1));
268 static int fmt_list(struct fmt
*fmt
, int beg
, int end
)
271 char *first
= fmt_line(fmt
, i
);
273 if (!islist(first
, NULL
))
275 fmt
->ops
->list_beg(fmt
->doc
);
276 while ((line
= fmt_line(fmt
, i
)) && islist(first
, line
)) {
277 int next
= min(end
, fmt_deindent(fmt
, i
+ 1, 2));
279 fmt
->ops
->item_beg(fmt
->doc
);
282 i
= par_end(fmt
, i
, next
);
283 put_lines(fmt
, head
, i
);
286 fmt_handle(fmt
, i
, next
, 2);
288 fmt
->ops
->item_end(fmt
->doc
);
292 fmt
->ops
->list_end(fmt
->doc
);
297 static int fmt_table(struct fmt
*fmt
, int beg
, int end
)
299 char *hdr
= fmt_line(fmt
, beg
);
301 int row
= 0, col
= 0;
302 if (hdr
[0] != '.' || hdr
[1] != 'T' || hdr
[2] != '1')
304 fmt
->ops
->table_beg(fmt
->doc
, 0);
305 for (i
= beg
+ 1; i
< end
; i
++) {
306 char *cur
= fmt_line(fmt
, i
);
307 if (cur
[0] == '.' && cur
[1] == 'T' && cur
[2] == '2')
310 raw_line(fmt
, cur
+ 2);
314 if (strchr("_=\n", cur
[0])) {
316 fmt
->ops
->entry_end(fmt
->doc
);
318 fmt
->ops
->row_end(fmt
->doc
);
319 if (cur
[0] != '\n') {
326 if (strchr("#*-+", cur
[0])) {
328 fmt
->ops
->row_beg(fmt
->doc
);
331 fmt
->ops
->entry_end(fmt
->doc
);
333 fmt
->ops
->entry_beg(fmt
->doc
);
341 fmt
->ops
->entry_end(fmt
->doc
);
343 fmt
->ops
->row_end(fmt
->doc
);
344 fmt
->ops
->table_end(fmt
->doc
);
350 static int table_columns(char *line
)
353 for (n
= 0; *line
; n
++) {
354 while (*line
== '\t')
356 while (*line
&& *line
!= '\t')
362 static void table_row(struct fmt
*fmt
, char *s
)
364 if (*s
== '=' || *s
== '-')
366 fmt
->ops
->row_beg(fmt
->doc
);
369 while (*s
&& *s
!= '\t')
371 fmt
->ops
->entry_beg(fmt
->doc
);
372 put_text(fmt
, fillbuf(r
, s
));
373 fmt
->ops
->entry_end(fmt
->doc
);
377 fmt
->ops
->row_end(fmt
->doc
);
380 static int fmt_tableascii(struct fmt
*fmt
, int beg
, int end
)
384 if (*fmt_line(fmt
, beg
) != '=')
386 n
= table_columns(fmt_line(fmt
, beg
+ 1));
387 fmt
->ops
->table_beg(fmt
->doc
, n
);
388 for (i
= beg
+ 1; i
< end
; i
++) {
389 if (!fmt
->level
&& !*fmt_line(fmt
, i
))
391 table_row(fmt
, fmt_line(fmt
, i
));
393 fmt
->ops
->table_end(fmt
->doc
);
399 static int (*parts
[])(struct fmt
*fmt
, int beg
, int end
) = {
400 fmt_head
, fmt_list
, fmt_table
, fmt_tableascii
,
401 fmt_block
, fmt_rawline
, fmt_par
404 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
)
411 if (!*fmt_line(fmt
, line
)) {
412 put_line(fmt
, line
++);
415 for (i
= 0; i
< LENGTH(parts
); i
++)
416 if ((c
= parts
[i
](fmt
, line
, end
)))
420 put_line(fmt
, line
++);
425 void format(struct doc
*doc
, struct txt
*txt
, struct fmt_ops
*ops
)
427 struct fmt
*fmt
= fmt_alloc(doc
, txt
, ops
);
428 fmt
->ops
->doc_beg(doc
);
429 fmt_handle(fmt
, 0, txt
->n
, 0);
430 fmt
->ops
->doc_end(doc
);