txt2html.h 2.8 KB

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