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 struct marker
*marker_char(char c
)
56 for (i
= 0; i
< LENGTH(markers
); i
++)
57 if (markers
[i
].beg
== c
)
62 static char *possible_inline(char *s
)
65 struct marker
*m
= NULL
;
69 if ((s
== home
|| isspace(*(s
- 1))) && !isspace(*(s
+ 1)))
70 if ((m
= marker_char(*s
)))
77 static void fillbuf(char *buf
, int len
, char *beg
, char *end
)
79 int n
= len
- 1 < end
- beg
? len
- 1 : end
- beg
;
84 static char *fmt_put_inline(struct fmt
*fmt
, char *s
)
88 struct marker
*marker
;
90 if (marker_char(*(s
+ 1))) {
91 fillbuf(buf
, LENGTH(buf
), s
+ 1, s
+ 2);
92 fmt
->ops
->put(fmt
->doc
, buf
);
97 marker
= marker_char(*s
);
98 if (marker
&& marker
->end
)
99 r
= strchr(s
+ 1, marker
->end
);
100 if (marker
&& !marker
->end
)
101 for (r
= s
+ 1; isalnum(*r
); r
++);
103 fillbuf(buf
, LENGTH(buf
), s
+ 1, r
);
104 fmt
->ops
->put_txt(fmt
->doc
, buf
, marker
->id
);
111 static void put_text(struct fmt
*fmt
, char *s
)
116 char *r
= possible_inline(s
);
119 fillbuf(buf
, LENGTH(buf
), done
, r
);
120 fmt
->ops
->put(fmt
->doc
, buf
);
122 if ((r
= fmt_put_inline(fmt
, r
)))
127 fmt
->ops
->put(fmt
->doc
, done
);
130 static void raw_line(struct fmt
*fmt
, char *s
)
132 fmt
->ops
->put(fmt
->doc
, s
);
135 static void put_line(struct fmt
*fmt
, int n
)
137 put_text(fmt
, fmt_line(fmt
, n
));
141 static void put_lines(struct fmt
*fmt
, int beg
, int end
)
144 for (i
= beg
; i
< end
; i
++)
148 static void raw_lines(struct fmt
*fmt
, int beg
, int end
)
151 for (i
= beg
; i
< end
; i
++) {
152 raw_line(fmt
, fmt_line(fmt
, i
));
157 static int indents(char *s
)
165 static int islist(char *first
, char *line
)
169 if (!first
|| strlen(first
) < 2)
171 if (first
[1] != ' ' || !strchr(signs
, first
[0]))
175 if (strlen(line
) < 2)
177 return line
[0] == first
[0] && line
[1] == first
[1];
180 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
);
182 static int fmt_head(struct fmt
*fmt
, int beg
, int end
)
184 char *line
= fmt_line(fmt
, beg
);
185 char *next
= fmt_line(fmt
, beg
+ 1);
189 if (!next
|| !*line
|| beg
== end
|| fmt
->level
)
192 if (!c
|| !strchr(signs
, *next
))
197 fmt
->ops
->head_beg(fmt
->doc
, strchr(signs
, *next
) - signs
);
199 fmt
->ops
->head_end(fmt
->doc
, strchr(signs
, *next
) - signs
);
203 static int par_end(struct fmt
*fmt
, int beg
, int end
)
207 while (i
< end
&& (line
= fmt_line(fmt
, i
))) {
208 if (!*line
|| indents(line
) || islist(line
, NULL
))
215 static int fmt_par(struct fmt
*fmt
, int beg
, int end
)
218 if (indents(fmt_line(fmt
, beg
)))
220 fmt
->ops
->par_beg(fmt
->doc
);
221 i
= par_end(fmt
, beg
, end
);
222 put_lines(fmt
, beg
, i
);
223 fmt
->ops
->par_end(fmt
->doc
);
227 static int min(int a
, int b
)
229 return a
< b
? a
: b
;
232 static int fmt_deindent(struct fmt
*fmt
, int n
, int indent
)
236 while ((line
= fmt_line(fmt
, n
))) {
237 if (*line
&& indents(line
) < indent
)
246 static int fmt_block(struct fmt
*fmt
, int beg
, int end
)
248 int level
= indents(fmt_line(fmt
, beg
));
252 next
= min(end
, fmt_deindent(fmt
, beg
+ 1, level
));
253 fmt_handle(fmt
, beg
, next
, level
);
257 static int fmt_list(struct fmt
*fmt
, int beg
, int end
)
260 char *first
= fmt_line(fmt
, i
);
262 if (!islist(first
, NULL
))
264 fmt
->ops
->list_beg(fmt
->doc
);
265 while ((line
= fmt_line(fmt
, i
)) && islist(first
, line
)) {
266 int next
= min(end
, fmt_deindent(fmt
, i
+ 1, 2));
268 fmt
->ops
->item_beg(fmt
->doc
);
271 i
= par_end(fmt
, i
, next
);
272 put_lines(fmt
, head
, i
);
275 fmt_handle(fmt
, i
, next
, 2);
277 fmt
->ops
->item_end(fmt
->doc
);
281 fmt
->ops
->list_end(fmt
->doc
);
285 static int fmt_pre(struct fmt
*fmt
, int beg
, int end
)
289 fmt
->ops
->pre_beg(fmt
->doc
);
290 raw_lines(fmt
, beg
, end
);
291 fmt
->ops
->pre_end(fmt
->doc
);
295 static int fmt_formula(struct fmt
*fmt
, int beg
, int end
)
298 if (!fmt
->level
|| *fmt_line(fmt
, beg
) != '$')
300 fmt
->ops
->formula_beg(fmt
->doc
);
301 for (i
= beg
; i
< end
; i
++) {
302 char *line
= fmt_line(fmt
, i
);
309 fmt
->ops
->formula_end(fmt
->doc
);
313 static int fmt_raw(struct fmt
*fmt
, int beg
, int end
)
315 if (!fmt
->level
|| *fmt_line(fmt
, beg
) != '%')
317 raw_line(fmt
, fmt_line(fmt
, beg
) + 1);
319 raw_lines(fmt
, beg
+ 1, end
);
323 static int table_columns(char *line
)
326 for (n
= 0; *line
; n
++) {
327 while (*line
== '\t')
329 while (*line
&& *line
!= '\t')
335 static void table_row(struct fmt
*fmt
, char *s
)
340 fmt
->ops
->row_beg(fmt
->doc
);
343 while (*s
&& *s
!= '\t')
345 fmt
->ops
->entry_beg(fmt
->doc
);
346 fillbuf(buf
, LENGTH(buf
), r
, s
);
348 fmt
->ops
->entry_end(fmt
->doc
);
352 fmt
->ops
->row_end(fmt
->doc
);
355 static int fmt_table(struct fmt
*fmt
, int beg
, int end
)
359 if (*fmt_line(fmt
, beg
) != '=')
361 n
= table_columns(fmt_line(fmt
, beg
+ 1));
362 fmt
->ops
->table_beg(fmt
->doc
, n
);
363 for (i
= beg
+ 1; i
< end
; i
++) {
364 if (!fmt
->level
&& !*fmt_line(fmt
, i
))
366 table_row(fmt
, fmt_line(fmt
, i
));
368 fmt
->ops
->table_end(fmt
->doc
);
372 static int (*parts
[])(struct fmt
*fmt
, int beg
, int end
) =
373 {fmt_head
, fmt_list
, fmt_formula
, fmt_raw
,
374 fmt_table
, fmt_pre
, fmt_par
, fmt_block
};
376 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
)
383 if (!*fmt_line(fmt
, line
)) {
384 put_line(fmt
, line
++);
387 for (i
= 0; i
< LENGTH(parts
); i
++)
388 if ((c
= parts
[i
](fmt
, line
, end
)))
392 put_line(fmt
, line
++);
397 void format(struct doc
*doc
, struct txt
*txt
, struct fmt_ops
*ops
)
399 struct fmt
*fmt
= fmt_alloc(doc
, txt
, ops
);
400 fmt
->ops
->doc_beg(doc
);
401 fmt_handle(fmt
, 0, txt
->n
, 0);
402 fmt
->ops
->doc_end(doc
);