Advertisement
Guest User

Untitled

a guest
Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. int postword(char p)
  2. {
  3.    return stb_ischar(p, " \r\n\t.!?,;:-)]}'\"");
  4. }
  5.  
  6. int preword(char p)
  7. {
  8.    return stb_ischar(p, " \r\n\t([{'\"");
  9. }
  10.  
  11. int nbsp_len = 2;
  12. static int nbsp(char *p)
  13. {
  14.    return p[0] == '`' && p[1] == '`';
  15. }
  16.  
  17. int justescaped;
  18.  
  19. int canmodechange(char *p, int in_mode)
  20. {
  21.    if (!in_mode) {
  22.       // conditions for starting a mode:
  23.       //    preceding character must be whitespace, or a mode start
  24.       //    following character must be non-white
  25.       //
  26.       // Note that we can force a following character to be non-white
  27.       // by using an escaped space. We can also force the preceding
  28.       // character to _be_ white in the same way (escaped spaces are dropped)
  29.       if (postword(p[1])) return 0;
  30.       if (nbsp(p+1)) return 0;
  31.       --p;
  32.       for(;;) {
  33.          if (preword(*p)) return 1;
  34.          if (nbsp(p-1)) return 1;
  35.          if (!stb_ischar(*p, modechars)) return 0;
  36.          --p;
  37.       }
  38.    } else {
  39.       // conditions for ending a mode:
  40.       //    preceding character must be non-white
  41.       //    following character must be whitespace, or a mode end
  42.       //
  43.       // Note that escaping these requires extra smarts. In the above
  44.       // case, an escaped space will produce the visually correct results.
  45.       // In this case, an escaped space will join up in appropriately,
  46.       // and we have to parse it clearly.
  47.       //
  48.       // E.g., let's start a mode in the middle of a word:
  49.       //       foo\ *bar*
  50.       //    -> foo<i>bar</i>
  51.       // but end a mode in the middle:
  52.       //       *foo*\ bar
  53.      
  54.       if (preword(p[-1]) && (p[-1] != ' ' || p[-2] != '\\' || !justescaped)) return 0;
  55.       if (nbsp(p-2)) return 0;
  56.       ++p;
  57.       for(;;) {
  58.          if (postword(*p) || (*p == '\\' && p[1] == ' ')) return 1;
  59.          if (nbsp(p)) return 1;
  60.          if (!stb_ischar(*p, modechars)) return 0;
  61.          ++p;
  62.       }
  63.    }
  64. }
  65.  
  66. int count_newline(char *p)
  67. {
  68.    int n = 0;
  69.    while (*p == '\r' || *p == '\n') {
  70.       p += (p[0] + p[1] == '\r' + '\n' ? 2 : 1);
  71.       ++n;
  72.    }
  73.    return n;
  74. }
  75.  
  76. [...]
  77.  
  78.          while (*p) {
  79.             if (*p == '\r' || *p == '\n') {
  80.                if (in_header) {
  81.                   newline = prev;
  82.                   fprintf(g, "</h%d>" LINE, in_header);
  83.                   in_header = 0;
  84.                }
  85.                ++newline;
  86.                // lookahead for getting out of pre
  87.                if (pre && newline == 1 && count_newline(p) > 1) {
  88.                   fputs("</pre>", g);
  89.                   pre = 0;
  90.                }
  91.                if (newline == 2 && !pre) {
  92.                   if (italic) { fputs("</i>", g); italic = 0; }
  93.                   if (bold  ) { fputs("</b>", g); bold = 0; }
  94.                   if (tt    ) { fputs("</tt>",g); tt = 0; }
  95.                   fputs("<p>", g);
  96.                }
  97.                fputc(p[0], g);
  98.                if (p[0] + p[1] == '\r' + '\n') { fputc(p[1], g); ++p; }
  99.                ++p;
  100.                continue;
  101.             }
  102.  
  103.             if (newline) {
  104.                prev = newline;
  105.                newline = 0;
  106.  
  107.                if (prev >= 2 && *p == '#') {
  108.                   int z=0;
  109.  
  110.                   if (pre  ) fputs("</pre>", g);
  111.                   if (block) fputs("</blockquote>", g);
  112.                   pre = block = 0;
  113.  
  114.                   while (p[z] == '#') ++z;
  115.                   p += z;
  116.                   if (z > 6) z = 6;
  117.                   while (*p == ' ') ++p;
  118.                   fprintf(g, "<h%d>", z);
  119.                   in_header = z;
  120.                   continue;
  121.                }
  122.  
  123.                if (*p == '>') {
  124.                   ++p;
  125.                   if (!block) {
  126.                      if (pre) { fputs("</pre>", g); pre=0; }
  127.                      fputs("<blockquote>", g);
  128.                      block = 1;
  129.                   }
  130.                   if (*p == ' ') ++p;
  131.                } else {
  132.                   if (block) {
  133.                      fputs("</blockquote>", g);
  134.                      block = 0;
  135.                   }
  136.                }
  137.                if (*p == ' ') {
  138.                   ++p;
  139.                   if (!pre) {
  140.                      fputs("<pre>", g);
  141.                      pre = 1;
  142.                   }
  143.                } else {
  144.                   if (pre) {
  145.                      fputs("</pre>", g);
  146.                      pre = 0;
  147.                   }
  148.                }
  149.                continue;
  150.             }
  151.  
  152.             if (!pre && *p == '\\') {
  153.                if (p[1] == '<')
  154.                   fputs("&lt;", g);
  155.                else if (p[1] == '\r' || p[1] == '\n') {
  156.                   fputs("<br>", g);
  157.                   ++p;
  158.                   continue;
  159.                } else
  160.                   fputc(p[1], g);
  161.                p += 2;
  162.                justescaped = 1;
  163.                continue;
  164.             } else if (!pre && nbsp(p)) {
  165.                fputs("&nbsp;", g);
  166.                p += nbsp_len;
  167.                justescaped = 0;
  168.                continue;
  169.             } else if (!pre && *p == '_' && canmodechange(p,italic)) {
  170.                italic = !italic;
  171.                fputs(italic ? "<i>" : "</i>", g);
  172.             } else if (!pre && *p == '*' && canmodechange(p, bold)) {
  173.                bold = !bold;
  174.                fputs(bold ? "<b>" : "</b>", g);
  175.             } else if (!pre && *p == '`' && canmodechange(p, tt)) {
  176.                tt = !tt;
  177.                fputs(tt ? "<tt>" : "</tt>", g);
  178.             } else if (*p == '<' && (tt||pre)) {
  179.                fputs("&lt;", g);
  180.             } else if (*p == '>' && (tt||pre)) {
  181.                fputs("&gt;", g);
  182.             } else if (*p == '&') {
  183.                fputs("&amp;", g);
  184.             } else
  185.                fputc(*p, g);
  186.             ++p;
  187.             justescaped = 0;
  188.          }
  189.  
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement