Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int postword(char p)
- {
- return stb_ischar(p, " \r\n\t.!?,;:-)]}'\"");
- }
- int preword(char p)
- {
- return stb_ischar(p, " \r\n\t([{'\"");
- }
- int nbsp_len = 2;
- static int nbsp(char *p)
- {
- return p[0] == '`' && p[1] == '`';
- }
- int justescaped;
- int canmodechange(char *p, int in_mode)
- {
- if (!in_mode) {
- // conditions for starting a mode:
- // preceding character must be whitespace, or a mode start
- // following character must be non-white
- //
- // Note that we can force a following character to be non-white
- // by using an escaped space. We can also force the preceding
- // character to _be_ white in the same way (escaped spaces are dropped)
- if (postword(p[1])) return 0;
- if (nbsp(p+1)) return 0;
- --p;
- for(;;) {
- if (preword(*p)) return 1;
- if (nbsp(p-1)) return 1;
- if (!stb_ischar(*p, modechars)) return 0;
- --p;
- }
- } else {
- // conditions for ending a mode:
- // preceding character must be non-white
- // following character must be whitespace, or a mode end
- //
- // Note that escaping these requires extra smarts. In the above
- // case, an escaped space will produce the visually correct results.
- // In this case, an escaped space will join up in appropriately,
- // and we have to parse it clearly.
- //
- // E.g., let's start a mode in the middle of a word:
- // foo\ *bar*
- // -> foo<i>bar</i>
- // but end a mode in the middle:
- // *foo*\ bar
- if (preword(p[-1]) && (p[-1] != ' ' || p[-2] != '\\' || !justescaped)) return 0;
- if (nbsp(p-2)) return 0;
- ++p;
- for(;;) {
- if (postword(*p) || (*p == '\\' && p[1] == ' ')) return 1;
- if (nbsp(p)) return 1;
- if (!stb_ischar(*p, modechars)) return 0;
- ++p;
- }
- }
- }
- int count_newline(char *p)
- {
- int n = 0;
- while (*p == '\r' || *p == '\n') {
- p += (p[0] + p[1] == '\r' + '\n' ? 2 : 1);
- ++n;
- }
- return n;
- }
- [...]
- while (*p) {
- if (*p == '\r' || *p == '\n') {
- if (in_header) {
- newline = prev;
- fprintf(g, "</h%d>" LINE, in_header);
- in_header = 0;
- }
- ++newline;
- // lookahead for getting out of pre
- if (pre && newline == 1 && count_newline(p) > 1) {
- fputs("</pre>", g);
- pre = 0;
- }
- if (newline == 2 && !pre) {
- if (italic) { fputs("</i>", g); italic = 0; }
- if (bold ) { fputs("</b>", g); bold = 0; }
- if (tt ) { fputs("</tt>",g); tt = 0; }
- fputs("<p>", g);
- }
- fputc(p[0], g);
- if (p[0] + p[1] == '\r' + '\n') { fputc(p[1], g); ++p; }
- ++p;
- continue;
- }
- if (newline) {
- prev = newline;
- newline = 0;
- if (prev >= 2 && *p == '#') {
- int z=0;
- if (pre ) fputs("</pre>", g);
- if (block) fputs("</blockquote>", g);
- pre = block = 0;
- while (p[z] == '#') ++z;
- p += z;
- if (z > 6) z = 6;
- while (*p == ' ') ++p;
- fprintf(g, "<h%d>", z);
- in_header = z;
- continue;
- }
- if (*p == '>') {
- ++p;
- if (!block) {
- if (pre) { fputs("</pre>", g); pre=0; }
- fputs("<blockquote>", g);
- block = 1;
- }
- if (*p == ' ') ++p;
- } else {
- if (block) {
- fputs("</blockquote>", g);
- block = 0;
- }
- }
- if (*p == ' ') {
- ++p;
- if (!pre) {
- fputs("<pre>", g);
- pre = 1;
- }
- } else {
- if (pre) {
- fputs("</pre>", g);
- pre = 0;
- }
- }
- continue;
- }
- if (!pre && *p == '\\') {
- if (p[1] == '<')
- fputs("<", g);
- else if (p[1] == '\r' || p[1] == '\n') {
- fputs("<br>", g);
- ++p;
- continue;
- } else
- fputc(p[1], g);
- p += 2;
- justescaped = 1;
- continue;
- } else if (!pre && nbsp(p)) {
- fputs(" ", g);
- p += nbsp_len;
- justescaped = 0;
- continue;
- } else if (!pre && *p == '_' && canmodechange(p,italic)) {
- italic = !italic;
- fputs(italic ? "<i>" : "</i>", g);
- } else if (!pre && *p == '*' && canmodechange(p, bold)) {
- bold = !bold;
- fputs(bold ? "<b>" : "</b>", g);
- } else if (!pre && *p == '`' && canmodechange(p, tt)) {
- tt = !tt;
- fputs(tt ? "<tt>" : "</tt>", g);
- } else if (*p == '<' && (tt||pre)) {
- fputs("<", g);
- } else if (*p == '>' && (tt||pre)) {
- fputs(">", g);
- } else if (*p == '&') {
- fputs("&", g);
- } else
- fputc(*p, g);
- ++p;
- justescaped = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement