txt2html.h (2825B)
1 #ifndef _TXT2HTML_H_ 2 #define _TXT2HTML_H_ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <stdarg.h> 7 #include <stdint.h> 8 #include <string.h> 9 #include <stdbool.h> 10 #include <ctype.h> // TODO replace with utf8 support 11 #include <assert.h> 12 13 #define OPT_V 0x10 // print verbose logs 14 #define OPT_NM 0x20 // no memory limit 15 #define OPT_BR 0x01 // newlines as <br/> nodes within <p> (not ' ') 16 17 typedef uint8_t NodeType; 18 19 enum { 20 NONE = 0x00, 21 OPEN = 0x10, 22 CLOSE = 0x20, 23 24 H1 = 0x01, 25 H2 = 0x02, 26 P = 0x03, 27 PRE = 0x04, 28 BR = 0x05, 29 LI = 0x06, 30 OL = 0x07, 31 UL = 0x08 32 }; 33 34 struct node { 35 struct node *prev, *next; 36 NodeType type; 37 char *buf; 38 }; 39 40 /*-------- 41 rule.c 42 --------*/ 43 // get the length of a rule for `NodeType t`. 44 size_t rule_len(NodeType t); 45 46 // check if `str` matches the rule for `NodeType t`. 47 bool rule_match(const char *str, NodeType t); 48 49 // return H1 or H2 if `str` matches said NodeType. 50 // If it matches neither 0 will be returned.y 51 NodeType rule_match_heading(const char *str); 52 53 /*-------- 54 node.c 55 --------*/ 56 struct node *node_create(struct node *prev, NodeType t); 57 58 // write a character to `n->buf`. 59 // Has an internal static buffer (`buf`) that `c` is written to. 60 // If `c == EOF` or `buf` reaches `BUFSIZ` or `n` does not match 61 // `n` from the previous call, then `buf` is written to the previous 62 // `n` and reset for a new set of data. 63 void node_writec(struct node **n, int c); 64 65 // rule `str` against a set of rules and determine the next node type. 66 // `n` will be updated to a newly created node of the determined type. 67 size_t node_next(const char *str, struct node **n); 68 69 /*--------- 70 parse.c 71 ---------*/ 72 // main parsing function 73 struct node *parse_buf(const char *buf, struct node **n, uint8_t opts); 74 75 // parse `str` into `n` until *\0* or *\n\n* is found. 76 // If `opts & OPT_BR` then `\n` will be parsed as a `<br/>` node. 77 // If `n->type` is *PRE*, then parsing will also stop after the first 78 // `\n` that is not followed by a `\t`. 79 // The number of parsed bytes is returned 80 size_t parse_textblock(const char *str, struct node **n, bool softbreaks); 81 82 // parse a line of text from `str` into `n` and skip the line after 83 // aslong as it contains *=* or *-*. 84 // The number of parsed bytes is returned. 85 size_t parse_heading(const char *str, struct node **n); 86 87 // parse `str` into `n` for *OL+LI* until *CLOSE+OL*. 88 // The number of parsed bytes is returned. 89 size_t parse_oli(const char *str, struct node **n, uint8_t opts); 90 91 // parse `str` into`n` until *\0* or *\n\n*. After this, assign 92 // a new node to `n` of CLOSE+P. 93 // The number of parsed bytes is returned. 94 size_t parse_p(const char *str, struct node **n, uint8_t opts); 95 96 // parse `str` into `n` for *UL+LI* until *CLOSE+UL*. 97 // The number of parsed bytes is returned. 98 size_t parse_uli(const char *str, struct node **n, uint8_t opts); 99 100 #endif