8 #define LENGTH(vars) (sizeof(vars) / sizeof(vars[0]))
10 static char *markers
[] = {"**", "$$", "%%", "[]", "||", "//", "''", "``"};
14 int txt
; /* process inline markups in the block */
16 {".#1", ".#2", 1}, {".!1", ".!2"},
17 {".TS", ".TE", 1}, {".DS", ".DE", 1}, {".G1", ".G2"},
18 {".EQ", ".EN"}, {".PS", ".PE"}, {".[", ".]"}, {".de", ".."},
28 static struct fmt
*fmt_alloc(struct doc
*doc
, struct txt
*txt
,
31 struct fmt
*fmt
= xmalloc(sizeof(struct fmt
));
39 static void fmt_free(struct fmt
*fmt
)
44 static char *fmt_line(struct fmt
*fmt
, int line
)
46 char *s
= txt_line(fmt
->txt
, line
);
48 for (i
= 0; i
< fmt
->level
&& s
&& *s
; i
++)
53 static char *marker_char(char c
, char p
, char n
)
56 if (p
== '\\' || (p
&& !isspace(p
)) || isspace(n
))
58 for (i
= 0; i
< LENGTH(markers
); i
++)
59 if (markers
[i
][0] == c
)
64 static char *fillbuf(char *beg
, char *end
)
66 static char buf
[MAXLINE
];
67 memcpy(buf
, beg
, end
- beg
);
68 buf
[end
- beg
] = '\0';
72 static void put_text(struct fmt
*fmt
, char *s
)
78 while (*s
&& !marker_char(*s
, s
== _s
? 0 : s
[-1], s
[1]))
82 m
= marker_char(*s
, s
== _s
? 0 : s
[-1], s
[1]);
83 r
= strchr(s
+ 1, m
[1]);
85 fmt
->ops
->put(fmt
->doc
, fillbuf(o
, s
));
86 fmt
->ops
->put_txt(fmt
->doc
, fillbuf(s
+ 1, r
), m
);
93 fmt
->ops
->put(fmt
->doc
, o
);
96 static void raw_line(struct fmt
*fmt
, char *s
)
98 fmt
->ops
->put(fmt
->doc
, s
);
101 static void put_line(struct fmt
*fmt
, int n
)
103 put_text(fmt
, fmt_line(fmt
, n
));
107 static void put_lines(struct fmt
*fmt
, int beg
, int end
)
110 for (i
= beg
; i
< end
; i
++)
114 static void raw_lines(struct fmt
*fmt
, int beg
, int end
)
117 for (i
= beg
; i
< end
; i
++) {
118 raw_line(fmt
, fmt_line(fmt
, i
));
123 static int indents(char *s
)
131 static int islist(char *first
, char *line
)
135 if (!first
|| strlen(first
) < 2)
137 if (first
[1] != ' ' || !strchr(signs
, first
[0]))
141 if (strlen(line
) < 2)
143 return line
[0] == first
[0] && line
[1] == first
[1];
146 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
);
148 static int fmt_head(struct fmt
*fmt
, int beg
, int end
)
150 char *line
= fmt_line(fmt
, beg
);
151 char *next
= fmt_line(fmt
, beg
+ 1);
155 if (!next
|| !*line
|| beg
== end
|| fmt
->level
)
158 if (!c
|| !strchr(signs
, *next
))
163 fmt
->ops
->head_beg(fmt
->doc
, strchr(signs
, *next
) - signs
);
165 fmt
->ops
->head_end(fmt
->doc
, strchr(signs
, *next
) - signs
);
169 static int cmd_line(struct fmt
*fmt
, int i
)
171 return !fmt
->level
&& *fmt_line(fmt
, i
) == '.';
174 static int par_end(struct fmt
*fmt
, int beg
, int end
)
178 while (i
< end
&& (line
= fmt_line(fmt
, i
))) {
179 if (!*line
|| indents(line
) || islist(line
, NULL
) || cmd_line(fmt
, i
))
186 static int fmt_par(struct fmt
*fmt
, int beg
, int end
)
189 if (indents(fmt_line(fmt
, beg
)) || cmd_line(fmt
, beg
))
191 fmt
->ops
->par_beg(fmt
->doc
);
192 i
= par_end(fmt
, beg
, end
);
193 put_lines(fmt
, beg
, i
);
194 fmt
->ops
->par_end(fmt
->doc
);
198 static int fmt_rawline(struct fmt
*fmt
, int beg
, int end
)
200 if (*fmt_line(fmt
, beg
) != '.')
202 put_lines(fmt
, beg
, beg
+ 1);
206 static int min(int a
, int b
)
208 return a
< b
? a
: b
;
211 static int fmt_deindent(struct fmt
*fmt
, int n
, int indent
)
215 while ((line
= fmt_line(fmt
, n
))) {
216 if (*line
&& indents(line
) < indent
)
225 static int fmt_pre(struct fmt
*fmt
, int beg
, int end
)
227 int level
= indents(fmt_line(fmt
, beg
));
228 int next
= min(end
, fmt_deindent(fmt
, beg
+ 1, level
));
230 fmt
->ops
->block_beg(fmt
->doc
, NULL
);
231 raw_lines(fmt
, beg
, next
);
232 fmt
->ops
->block_end(fmt
->doc
, NULL
);
237 static int fmt_block(struct fmt
*fmt
, int beg
, int end
)
239 struct block
*blk
= NULL
;
244 line
= fmt_line(fmt
, beg
);
246 return fmt_pre(fmt
, beg
, end
);
247 for (i
= 0; i
< LENGTH(blocks
); i
++)
248 if (!strncmp(blocks
[i
].beg
, line
, strlen(blocks
[i
].beg
)))
253 while (next
< end
&& depth
> 0) {
254 line
= fmt_line(fmt
, next
++);
255 if (!strncmp(blk
->beg
, line
, strlen(blk
->beg
)))
257 if (!strncmp(blk
->end
, line
, strlen(blk
->end
)))
260 fmt
->ops
->block_beg(fmt
->doc
, fmt_line(fmt
, beg
));
261 if (beg
+ 1 < next
) {
263 put_lines(fmt
, beg
+ 1, next
- 1);
265 raw_lines(fmt
, beg
+ 1, next
- 1);
267 fmt
->ops
->block_end(fmt
->doc
, fmt_line(fmt
, next
- 1));
271 static char *listhead(char *s
, char *head
)
276 while (*s
&& *s
!= ':')
286 static int fmt_list(struct fmt
*fmt
, int beg
, int end
)
289 char *first
= fmt_line(fmt
, i
);
291 if (!islist(first
, NULL
))
293 fmt
->ops
->list_beg(fmt
->doc
, line
[0]);
294 while ((line
= fmt_line(fmt
, i
)) && islist(first
, line
)) {
295 int next
= min(end
, fmt_deindent(fmt
, i
+ 1, 2));
298 if (line
[0] == '+') {
299 line
= listhead(line
, lhead
);
300 fmt
->ops
->item_beg(fmt
->doc
, lhead
);
305 fmt
->ops
->item_beg(fmt
->doc
, NULL
);
309 i
= par_end(fmt
, i
, next
);
310 put_lines(fmt
, head
, i
);
313 fmt_handle(fmt
, i
, next
, 2);
315 fmt
->ops
->item_end(fmt
->doc
);
319 fmt
->ops
->list_end(fmt
->doc
);
324 static int fmt_table(struct fmt
*fmt
, int beg
, int end
)
326 char *hdr
= fmt_line(fmt
, beg
);
328 int row
= 0, col
= 0;
329 if (hdr
[0] != '.' || hdr
[1] != 'T' || hdr
[2] != '1')
331 fmt
->ops
->table_beg(fmt
->doc
, 0);
332 for (i
= beg
+ 1; i
< end
; i
++) {
333 char *cur
= fmt_line(fmt
, i
);
334 if (cur
[0] == '.' && cur
[1] == 'T' && cur
[2] == '2')
337 raw_line(fmt
, cur
+ 2);
341 if (strchr("_=\n", cur
[0])) {
343 fmt
->ops
->entry_end(fmt
->doc
);
345 fmt
->ops
->row_end(fmt
->doc
);
346 if (cur
[0] != '\n') {
353 if (strchr("#*-+", cur
[0])) {
355 fmt
->ops
->row_beg(fmt
->doc
);
358 fmt
->ops
->entry_end(fmt
->doc
);
360 fmt
->ops
->entry_beg(fmt
->doc
);
368 fmt
->ops
->entry_end(fmt
->doc
);
370 fmt
->ops
->row_end(fmt
->doc
);
371 fmt
->ops
->table_end(fmt
->doc
);
377 static int table_columns(char *line
)
380 for (n
= 0; *line
; n
++) {
381 while (*line
== '\t')
383 while (*line
&& *line
!= '\t')
389 static void table_row(struct fmt
*fmt
, char *s
)
391 if (*s
== '=' || *s
== '-')
393 fmt
->ops
->row_beg(fmt
->doc
);
396 while (*s
&& *s
!= '\t')
398 fmt
->ops
->entry_beg(fmt
->doc
);
399 put_text(fmt
, fillbuf(r
, s
));
400 fmt
->ops
->entry_end(fmt
->doc
);
404 fmt
->ops
->row_end(fmt
->doc
);
407 static int fmt_tableascii(struct fmt
*fmt
, int beg
, int end
)
411 if (*fmt_line(fmt
, beg
) != '=')
413 n
= table_columns(fmt_line(fmt
, beg
+ 1));
414 fmt
->ops
->table_beg(fmt
->doc
, n
);
415 for (i
= beg
+ 1; i
< end
; i
++) {
416 if (!fmt
->level
&& !*fmt_line(fmt
, i
))
418 table_row(fmt
, fmt_line(fmt
, i
));
420 fmt
->ops
->table_end(fmt
->doc
);
426 static int (*parts
[])(struct fmt
*fmt
, int beg
, int end
) = {
427 fmt_head
, fmt_list
, fmt_table
, fmt_tableascii
,
428 fmt_block
, fmt_rawline
431 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
)
438 if (!*fmt_line(fmt
, line
)) {
439 put_line(fmt
, line
++);
442 for (i
= 0; i
< LENGTH(parts
); i
++)
443 if ((c
= parts
[i
](fmt
, line
, end
)))
445 /* start a paragraph only after a blank line */
446 if (!c
&& (line
== beg
|| !*fmt_line(fmt
, line
- 1)))
447 c
= fmt_par(fmt
, line
, end
);
450 put_line(fmt
, line
++);
455 void format(struct doc
*doc
, struct txt
*txt
, struct fmt_ops
*ops
)
457 struct fmt
*fmt
= fmt_alloc(doc
, txt
, ops
);
458 fmt
->ops
->doc_beg(doc
);
459 fmt_handle(fmt
, 0, txt
->n
, 0);
460 fmt
->ops
->doc_end(doc
);