|
|
tttml.c - tttml - converters for a simpler syntax than markdown |
|
|
 |
git clone git://bitreich.org/tttml git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/tttml (git://bitreich.org) |
|
|
 |
Log |
|
|
 |
Files |
|
|
 |
Refs |
|
|
 |
Tags |
|
|
 |
README |
|
|
|
--- |
|
|
|
tttml.c (1298B) |
|
|
|
--- |
|
|
|
1 #include <string.h> |
|
|
|
2 #include <stdio.h> |
|
|
|
3 #include <ctype.h> |
|
|
|
4 |
|
|
|
5 #define WORD_MAX 1024 |
|
|
|
6 #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
|
|
|
7 |
|
|
|
8 /* |
|
|
|
9 * Put the character c to stdout, and if the line is too long, insert |
|
|
|
10 * a newline. If c == EOF, flush the last word out of fmtc's static |
|
|
|
11 * buffer. |
|
|
|
12 * |
|
|
|
13 * It preserve the double space after a punctuation mark [.!?] if any. |
|
|
|
14 */ |
|
|
|
15 int |
|
|
|
16 fmtc(char c, char *prefix, int max) |
|
|
|
17 { |
|
|
|
18 static int pos = 0, nspaces = 0, wlen = 0, was_space = 1, was_punct = 0, first = 1; |
|
|
|
19 static char word[WORD_MAX], *w = word; |
|
|
|
20 |
|
|
|
21 if (c != ' ' && c != '\n' && c != EOF) { |
|
|
|
22 if (wlen + 1 < WORD_MAX) { |
|
|
|
23 *w++ = c; |
|
|
|
24 wlen++; |
|
|
|
25 } |
|
|
|
26 was_space = 0; |
|
|
|
27 was_punct = !!strchr(".!?", c); |
|
|
|
28 |
|
|
|
29 } else { |
|
|
|
30 if (!was_space) { |
|
|
|
31 was_space = 1; |
|
|
|
32 |
|
|
|
33 if (pos + nspaces + wlen > max) { |
|
|
|
34 pos = strlen(prefix); |
|
|
|
35 fputc('\n', stdout); |
|
|
|
36 fputs(prefix, stdout); |
|
|
|
37 } else if (!first) { |
|
|
|
38 pos += nspaces; |
|
|
|
39 while (nspaces-- > 0) |
|
|
|
40 fputc(' ', stdout); |
|
|
|
41 } |
|
|
|
42 |
|
|
|
43 pos += wlen; |
|
|
|
44 word[wlen] = '\0'; |
|
|
|
45 fputs(word, stdout); |
|
|
|
46 |
|
|
|
47 w = word; |
|
|
|
48 wlen = 0; |
|
|
|
49 nspaces = 1; |
|
|
|
50 } else { |
|
|
|
51 nspaces = was_punct ? 2 : 1; |
|
|
|
52 } |
|
|
|
53 |
|
|
|
54 if (c == EOF) |
|
|
|
55 fputc('\n', stdout); |
|
|
|
56 |
|
|
|
57 first = 0; |
|
|
|
58 } |
|
|
|
59 |
|
|
|
60 return c; |
|
|
|
61 } |
|
|
|
62 |
|
|
|
63 int |
|
|
|
64 main(void) |
|
|
|
65 { |
|
|
|
66 int c; |
|
|
|
67 char *prefix; |
|
|
|
68 |
|
|
|
69 prefix = " |"; |
|
|
|
70 |
|
|
|
71 fputs(prefix, stdout); |
|
|
|
72 while (fmtc(fgetc(stdin), prefix, 80) != EOF); |
|
|
|
73 } |
|