Fri, 21 Jun 2013 12:27:45 +0200
comments
src/c2html.c | file | annotate | diff | comparison | revisions | |
test/header.html | file | annotate | diff | comparison | revisions |
--- a/src/c2html.c Fri Jun 21 11:42:18 2013 +0200 +++ b/src/c2html.c Fri Jun 21 12:27:45 2013 +0200 @@ -159,67 +159,100 @@ memset(word, 0, WORDBUF_SIZE); size_t wp = 0; int isstring = 0, iscomment = 0; + static int iscommentml; int isescaping = 0; + + if (iscommentml) { + iscomment = 1; + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); + dp += 29; + } + // a fucking /* comment */ + /* again // / */ for (char c = src[sp] ; c ; c=src[++sp]) { - /* comments and strings */ - if (!isescaping && (c == '\'' || c == '\"')) { - isstring ^= 1; - if (isstring) { - memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); - dp += 28; - dp = writeescapedchar(dest, dp, c); - } else { - dp = writeescapedchar(dest, dp, c); + /* comments */ + if (c == '/') { + if (iscommentml && sp > 0 && src[sp-1] == '*') { + iscomment = 0; + iscommentml = 0; + memcpy(&(dest[dp]), "/</span>", 8); + dp += 8; + continue; + } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { + iscomment = 1; + iscommentml = (src[sp+1] == '*'); + memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); + dp += 29; + } + } + + if (iscomment) { + if (c == '\n') { memcpy(&(dest[dp]), "</span>", 7); dp += 7; } + dp = writeescapedchar(dest, dp, c); } else { - if (iscomment || isstring) { - dp = writeescapedchar(dest, dp, c); - } else if (!isalnum(c) && c != '_' && c != '#') { - /* interpret word int_t */ - if (wp > 0 && wp < WORDBUF_SIZE) { - int closespan = 1; - if (iskeyword(word)) { - memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); - dp += 29; - } else if (istype(word, wp)) { - memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); - dp += 26; - } else if (isdirective(word)) { - memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); - dp += 31; - } else { - closespan = 0; - } - for (int i = 0 ; i < wp ; i++) { - dp = writeescapedchar(dest, dp, word[i]); - } - if (closespan) { - memcpy(&(dest[dp]), "</span>", 7); - dp += 7; - } - memset(word, 0, WORDBUF_SIZE); - wp = 0; - } - dp = writeescapedchar(dest, dp, c); - } else { - /* read word */ - if (wp < WORDBUF_SIZE) { - word[wp++] = c; - } else if (wp == WORDBUF_SIZE) { - for (int i = 0 ; i < WORDBUF_SIZE ; i++) { - dp = writeescapedchar(dest, dp, word[i]); - } - wp++; + /* strings */ + if (!isescaping && (c == '\'' || c == '\"')) { + isstring ^= 1; + if (isstring) { + memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); + dp += 28; dp = writeescapedchar(dest, dp, c); } else { dp = writeescapedchar(dest, dp, c); + memcpy(&(dest[dp]), "</span>", 7); + dp += 7; + } + } else { + if (isstring) { + dp = writeescapedchar(dest, dp, c); + } else if (!isalnum(c) && c != '_' && c != '#') { + /* interpret word int_t */ + if (wp > 0 && wp < WORDBUF_SIZE) { + int closespan = 1; + if (iskeyword(word)) { + memcpy(&(dest[dp]), "<span class=\"c2html-keyword\">", 29); + dp += 29; + } else if (istype(word, wp)) { + memcpy(&(dest[dp]), "<span class=\"c2html-type\">", 26); + dp += 26; + } else if (isdirective(word)) { + memcpy(&(dest[dp]), "<span class=\"c2html-directive\">", 31); + dp += 31; + } else { + closespan = 0; + } + for (int i = 0 ; i < wp ; i++) { + dp = writeescapedchar(dest, dp, word[i]); + } + if (closespan) { + memcpy(&(dest[dp]), "</span>", 7); + dp += 7; + } + memset(word, 0, WORDBUF_SIZE); + wp = 0; + } + dp = writeescapedchar(dest, dp, c); + } else { + /* read word */ + if (wp < WORDBUF_SIZE) { + word[wp++] = c; + } else if (wp == WORDBUF_SIZE) { + for (int i = 0 ; i < WORDBUF_SIZE ; i++) { + dp = writeescapedchar(dest, dp, word[i]); + } + wp++; + dp = writeescapedchar(dest, dp, c); + } else { + dp = writeescapedchar(dest, dp, c); + } } } + + isescaping = !isescaping & (c == '\\'); } - - isescaping = !isescaping & (c == '\\'); } dest[dp] = 0; }