37 static struct fmt
*fmt_alloc(struct doc
*doc
, struct txt
*txt
,
40 struct fmt
*fmt
= xmalloc(sizeof(struct fmt
));
48 static void fmt_free(struct fmt
*fmt
)
53 static char *fmt_line(struct fmt
*fmt
, int line
)
55 char *s
= txt_line(fmt
->txt
, line
);
57 for (i
= 0; i
< fmt
->level
&& s
&& *s
; i
++)
62 static struct marker
*marker_char(char c
)
65 for (i
= 0; i
< LENGTH(markers
); i
++)
66 if (markers
[i
].beg
== c
)
71 static char *possible_inline(char *s
)
74 struct marker
*m
= NULL
;
78 if ((s
== home
|| isspace(*(s
- 1))) && !isspace(*(s
+ 1)))
79 if ((m
= marker_char(*s
)))
86 static void fillbuf(char *buf
, int len
, char *beg
, char *end
)
88 int n
= len
- 1 < end
- beg
? len
- 1 : end
- beg
;
93 static char *fmt_put_inline(struct fmt
*fmt
, char *s
)
97 struct marker
*marker
;
99 if (marker_char(*(s
+ 1))) {
100 fillbuf(buf
, LENGTH(buf
), s
+ 1, s
+ 2);
101 fmt
->ops
->put(fmt
->doc
, buf
);
106 marker
= marker_char(*s
);
107 if (marker
&& marker
->end
)
108 r
= strchr(s
+ 1, marker
->end
);
109 if (marker
&& !marker
->end
)
110 for (r
= s
+ 1; isalnum(*r
); r
++);
112 fillbuf(buf
, LENGTH(buf
), s
+ 1, r
);
113 fmt
->ops
->put_txt(fmt
->doc
, buf
, marker
->id
);
120 static void put_text(struct fmt
*fmt
, char *s
)
125 char *r
= possible_inline(s
);
128 fillbuf(buf
, LENGTH(buf
), done
, r
);
129 fmt
->ops
->put(fmt
->doc
, buf
);
131 if ((r
= fmt_put_inline(fmt
, r
)))
136 fmt
->ops
->put(fmt
->doc
, done
);
139 static void raw_line(struct fmt
*fmt
, char *s
)
141 fmt
->ops
->put(fmt
->doc
, s
);
144 static void put_line(struct fmt
*fmt
, int n
)
146 put_text(fmt
, fmt_line(fmt
, n
));
150 static void put_lines(struct fmt
*fmt
, int beg
, int end
)
153 for (i
= beg
; i
< end
; i
++)
157 static void raw_lines(struct fmt
*fmt
, int beg
, int end
)
160 for (i
= beg
; i
< end
; i
++) {
161 raw_line(fmt
, fmt_line(fmt
, i
));
166 static int indents(char *s
)
174 static int islist(char *first
, char *line
)
178 if (!first
|| strlen(first
) < 2)
180 if (first
[1] != ' ' || !strchr(signs
, first
[0]))
184 if (strlen(line
) < 2)
186 return line
[0] == first
[0] && line
[1] == first
[1];
189 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
);
191 static int fmt_head(struct fmt
*fmt
, int beg
, int end
)
193 char *line
= fmt_line(fmt
, beg
);
194 char *next
= fmt_line(fmt
, beg
+ 1);
198 if (!next
|| !*line
|| beg
== end
|| fmt
->level
)
201 if (!c
|| !strchr(signs
, *next
))
206 fmt
->ops
->head_beg(fmt
->doc
, strchr(signs
, *next
) - signs
);
208 fmt
->ops
->head_end(fmt
->doc
, strchr(signs
, *next
) - signs
);
212 static int par_end(struct fmt
*fmt
, int beg
, int end
)
216 while (i
< end
&& (line
= fmt_line(fmt
, i
))) {
217 if (!*line
|| indents(line
) || islist(line
, NULL
))
224 static int fmt_par(struct fmt
*fmt
, int beg
, int end
)
227 if (indents(fmt_line(fmt
, beg
)))
229 fmt
->ops
->par_beg(fmt
->doc
);
230 i
= par_end(fmt
, beg
, end
);
231 put_lines(fmt
, beg
, i
);
232 fmt
->ops
->par_end(fmt
->doc
);
236 static int min(int a
, int b
)
238 return a
< b
? a
: b
;
241 static int fmt_deindent(struct fmt
*fmt
, int n
, int indent
)
245 while ((line
= fmt_line(fmt
, n
))) {
246 if (*line
&& indents(line
) < indent
)
255 static int fmt_block(struct fmt
*fmt
, int beg
, int end
)
257 int level
= indents(fmt_line(fmt
, beg
));
258 struct block
*block
= NULL
;
264 next
= min(end
, fmt_deindent(fmt
, beg
+ 1, level
));
265 line
= fmt_line(fmt
, beg
);
266 for (i
= 0; i
< LENGTH(blocks
); i
++) {
267 if (blocks
[i
].sign
== line
[level
] && isspace(line
[level
+ 1])) {
272 if (!blocks
[i
].sign
) {
278 fmt
->ops
->block_beg(fmt
->doc
, block
->id
);
279 raw_lines(fmt
, beg
, next
);
280 fmt
->ops
->block_end(fmt
->doc
, block
->id
);
285 static int fmt_list(struct fmt
*fmt
, int beg
, int end
)
288 char *first
= fmt_line(fmt
, i
);
290 if (!islist(first
, NULL
))
292 fmt
->ops
->list_beg(fmt
->doc
);
293 while ((line
= fmt_line(fmt
, i
)) && islist(first
, line
)) {
294 int next
= min(end
, fmt_deindent(fmt
, i
+ 1, 2));
296 fmt
->ops
->item_beg(fmt
->doc
);
299 i
= par_end(fmt
, i
, next
);
300 put_lines(fmt
, head
, i
);
303 fmt_handle(fmt
, i
, next
, 2);
305 fmt
->ops
->item_end(fmt
->doc
);
309 fmt
->ops
->list_end(fmt
->doc
);
313 static int table_columns(char *line
)
316 for (n
= 0; *line
; n
++) {
317 while (*line
== '\t')
319 while (*line
&& *line
!= '\t')
325 static void table_row(struct fmt
*fmt
, char *s
)
330 fmt
->ops
->row_beg(fmt
->doc
);
333 while (*s
&& *s
!= '\t')
335 fmt
->ops
->entry_beg(fmt
->doc
);
336 fillbuf(buf
, LENGTH(buf
), r
, s
);
338 fmt
->ops
->entry_end(fmt
->doc
);
342 fmt
->ops
->row_end(fmt
->doc
);
345 static int fmt_table(struct fmt
*fmt
, int beg
, int end
)
349 if (*fmt_line(fmt
, beg
) != '=')
351 n
= table_columns(fmt_line(fmt
, beg
+ 1));
352 fmt
->ops
->table_beg(fmt
->doc
, n
);
353 for (i
= beg
+ 1; i
< end
; i
++) {
354 if (!fmt
->level
&& !*fmt_line(fmt
, i
))
356 table_row(fmt
, fmt_line(fmt
, i
));
358 fmt
->ops
->table_end(fmt
->doc
);
362 static int (*parts
[])(struct fmt
*fmt
, int beg
, int end
) =
363 {fmt_head
, fmt_list
, fmt_table
, fmt_block
, fmt_par
};
365 static void fmt_handle(struct fmt
*fmt
, int beg
, int end
, int level
)
372 if (!*fmt_line(fmt
, line
)) {
373 put_line(fmt
, line
++);
376 for (i
= 0; i
< LENGTH(parts
); i
++)
377 if ((c
= parts
[i
](fmt
, line
, end
)))
381 put_line(fmt
, line
++);
386 void format(struct doc
*doc
, struct txt
*txt
, struct fmt_ops
*ops
)
388 struct fmt
*fmt
= fmt_alloc(doc
, txt
, ops
);
389 fmt
->ops
->doc_beg(doc
);
390 fmt_handle(fmt
, 0, txt
->n
, 0);
391 fmt
->ops
->doc_end(doc
);