rule.c (1378B)
1 #include "txt2html.h" 2 3 size_t rule_len(NodeType type) 4 { 5 if (type & CLOSE) return 2; 6 7 int len = 0; 8 switch (type) { 9 case OPEN+OL+LI: 10 return 3; 11 case OPEN+UL+LI: 12 return 2; 13 case OPEN+PRE: 14 return 1; 15 } 16 return len; 17 } 18 19 bool rule_match(const char *str, NodeType type) 20 { 21 assert(str); 22 23 if ((type & CLOSE) && strlen(str) >= 2) 24 return (str[0] == '\n' && str[1] == '\n'); 25 26 bool match = false; 27 switch (type) { 28 case H1: 29 match = (rule_match_heading(str) == H1); 30 break; 31 case H2: 32 match = (rule_match_heading(str) == H2); 33 break; 34 case OPEN+OL+LI: 35 if (strlen(str) >= rule_len(OPEN+OL+LI)) 36 match = (isalnum(str[0]) && str[1] == '.' && str[2] == ' '); 37 break; 38 case OPEN+UL+LI: 39 if (strlen(str) >= rule_len(OPEN+UL+LI)) 40 match = ((str[0] == '-' || str[0] == '*') && str[1] == ' '); 41 break; 42 case OPEN+PRE: 43 // +1 to peek and make sure next char is print 44 if (strlen(str) >= rule_len(OPEN+PRE)+1) 45 match = (str[0] == '\t' && isprint(str[1])); 46 break; 47 default: 48 match = false; 49 } 50 return match; 51 } 52 53 NodeType rule_match_heading(const char *str) 54 { 55 assert(str); 56 57 NodeType heading = 0; 58 while (*str && *str++ != '\n'); // skip line 59 if (strlen(str) >= 3) { 60 if (*str == '=' && *(str+1) == '=' && *(str+2) == '=') 61 heading = H1; 62 else if (*str == '-' && *(str+1) == '-' && *(str+2) == '-') 63 heading = H2; 64 } 65 return heading; 66 }