From 6aa03d529794bdf048526da908eea808d7f40bd1 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sat, 18 Feb 2012 17:06:14 -0500 Subject: [PATCH 1/5] encoding output, see http://forum.dlang.org/thread/igiihgtmsaptfvfbtozz@forum.dlang.org --- src/doc.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/doc.c b/src/doc.c index 51c9d9c0912c..fa0649c809ef 100644 --- a/src/doc.c +++ b/src/doc.c @@ -288,12 +288,27 @@ void Module::gendocfile() emitMemberComments(sc); } - //printf("BODY= '%.*s'\n", buf.offset, buf.data); - Macro::define(¯otable, (unsigned char *)"BODY", 4, buf.data, buf.offset); + // if I did the character encoding right here, I think we'd be in business + OutBuffer bufEncoded; + bufEncoded.reserve(buf.offset); + for(unsigned where = 0; where < buf.offset; where++) + { + unsigned char c = buf.data[where]; + const char* replacement = escapetable->strings[c]; + if (replacement == NULL) + bufEncoded.writeByte(c); + else + bufEncoded.writestring(replacement); + } + + // printf("BODY= '%.*s'\n", bufEncoded.offset, bufEncoded.data); + // printf("BODY= '%.*s'\n", buf.offset, buf.data); + Macro::define(¯otable, (unsigned char *)"BODY", 4, bufEncoded.data, bufEncoded.offset); OutBuffer buf2; buf2.writestring("$(DDOC)\n"); unsigned end = buf2.offset; + macrotable->expand(&buf2, 0, &end, NULL, 0); #if 1 From 2dc42a7c68af7f4b3e3deb3e8951060993bcb905 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sat, 18 Feb 2012 17:29:14 -0500 Subject: [PATCH 2/5] the code highlighter html handling is now a liability; it can double encode things and serves no other purpose. I deleted it all. --- src/doc.c | 116 +----------------------------------------------------- 1 file changed, 2 insertions(+), 114 deletions(-) diff --git a/src/doc.c b/src/doc.c index fa0649c809ef..2f1fc772cc63 100644 --- a/src/doc.c +++ b/src/doc.c @@ -38,8 +38,6 @@ struct Escape { const char *strings[256]; - - static const char *escapeChar(unsigned c); }; struct Section @@ -1818,73 +1816,7 @@ void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) iLineStart = i + 1; break; - case '<': - leadingBlank = 0; - if (inCode) - break; - p = &buf->data[i]; - - // Skip over comments - if (p[1] == '!' && p[2] == '-' && p[3] == '-') - { unsigned j = i + 4; - p += 4; - while (1) - { - if (j == buf->offset) - goto L1; - if (p[0] == '-' && p[1] == '-' && p[2] == '>') - { - i = j + 2; // place on closing '>' - break; - } - j++; - p++; - } - break; - } - - // Skip over HTML tag - if (isalpha(p[1]) || (p[1] == '/' && isalpha(p[2]))) - { unsigned j = i + 2; - p += 2; - while (1) - { - if (j == buf->offset) - goto L1; - if (p[0] == '>') - { - i = j; // place on closing '>' - break; - } - j++; - p++; - } - break; - } - L1: - // Replace '<' with '<' character entity - se = Escape::escapeChar('<'); - if (se) - { size_t len = strlen(se); - buf->remove(i, 1); - i = buf->insert(i, se, len); - i--; // point to ';' - } - break; - - case '>': - leadingBlank = 0; - if (inCode) - break; - // Replace '>' with '>' character entity - se = Escape::escapeChar('>'); - if (se) - { size_t len = strlen(se); - buf->remove(i, 1); - i = buf->insert(i, se, len); - i--; // point to ';' - } break; case '&': @@ -1894,14 +1826,6 @@ void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) p = &buf->data[i]; if (p[1] == '#' || isalpha(p[1])) break; // already a character entity - // Replace '&' with '&' character entity - se = Escape::escapeChar('&'); - if (se) - { size_t len = strlen(se); - buf->remove(i, 1); - i = buf->insert(i, se, len); - i--; // point to ';' - } break; case '-': @@ -2044,15 +1968,7 @@ void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) { unsigned char c = buf->data[i]; const char *se; - se = Escape::escapeChar(c); - if (se) - { - size_t len = strlen(se); - buf->remove(i, 1); - i = buf->insert(i, se, len); - i--; // point to ';' - } - else if (isIdStart(&buf->data[i])) + if (isIdStart(&buf->data[i])) { unsigned j; j = skippastident(buf, i); @@ -2084,10 +2000,7 @@ void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) void highlightCode3(OutBuffer *buf, unsigned char *p, unsigned char *pend) { for (; p < pend; p++) - { const char *s = Escape::escapeChar(*p); - if (s) - buf->writestring(s); - else + { buf->writeByte(*p); } } @@ -2163,31 +2076,6 @@ void highlightCode2(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) global.errors = errorsave; } -/*************************************** - * Find character string to replace c with. - */ - -const char *Escape::escapeChar(unsigned c) -{ const char *s; - - switch (c) - { - case '<': - s = "<"; - break; - case '>': - s = ">"; - break; - case '&': - s = "&"; - break; - default: - s = NULL; - break; - } - return s; -} - /**************************************** * Determine if p points to the start of an identifier. */ From 763f118c1524bf08c472908eed1235590f091268 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sat, 18 Feb 2012 18:15:23 -0500 Subject: [PATCH 3/5] initialize table and check for null properly --- src/doc.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/doc.c b/src/doc.c index 2f1fc772cc63..c140fd8f0737 100644 --- a/src/doc.c +++ b/src/doc.c @@ -288,15 +288,24 @@ void Module::gendocfile() // if I did the character encoding right here, I think we'd be in business OutBuffer bufEncoded; - bufEncoded.reserve(buf.offset); - for(unsigned where = 0; where < buf.offset; where++) + if(escapetable != NULL) { - unsigned char c = buf.data[where]; - const char* replacement = escapetable->strings[c]; - if (replacement == NULL) - bufEncoded.writeByte(c); - else - bufEncoded.writestring(replacement); + bufEncoded.reserve(buf.offset); + for(unsigned where = 0; where < buf.offset; where++) + { + unsigned char c = buf.data[where]; + const char* replacement = escapetable->strings[c]; + if (replacement == NULL) + bufEncoded.writeByte(c); + else + bufEncoded.writestring(replacement); + } + } + else + { + bufEncoded.reserve(buf.offset); + for(unsigned where = 0; where < buf.offset; where++) + bufEncoded.writeByte(buf.data[where]); } // printf("BODY= '%.*s'\n", bufEncoded.offset, bufEncoded.data); @@ -1528,6 +1537,9 @@ void DocComment::parseEscapes(Escape **pescapetable, unsigned char *textstart, u unsigned char *p = textstart; unsigned char *pend = p + textlen; + // By default, set null everywhere so no character is escaped. + memset(escapetable, 0, sizeof(*escapetable)); + while (1) { while (1) From 654d39fe17397e0a80c95ce500f09ae49130bf2d Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sat, 18 Feb 2012 19:11:30 -0500 Subject: [PATCH 4/5] fully qualified names for anchors --- src/doc.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/doc.c b/src/doc.c index c140fd8f0737..c03d158d02f7 100644 --- a/src/doc.c +++ b/src/doc.c @@ -183,7 +183,7 @@ DDOC_PARAM_ID = $(TD $0)\n\ DDOC_PARAM_DESC = $(TD $0)\n\ DDOC_BLANKLINE = $(BR)$(BR)\n\ \n\ -DDOC_PSYMBOL = $(U $0)\n\ +DDOC_PSYMBOL = $(U $1)\n\ DDOC_KEYWORD = $(B $0)\n\ DDOC_PARAM = $(I $0)\n\ \n\ @@ -929,7 +929,7 @@ void AggregateDeclaration::toDocBuffer(OutBuffer *buf) #if 0 emitProtection(buf, protection); #endif - buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); buf->writestring(";\n"); } } @@ -953,7 +953,7 @@ void StructDeclaration::toDocBuffer(OutBuffer *buf) } else { - buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); } buf->writestring(";\n"); } @@ -980,7 +980,7 @@ void ClassDeclaration::toDocBuffer(OutBuffer *buf) { if (isAbstract()) buf->writestring("abstract "); - buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); } int any = 0; for (size_t i = 0; i < baseclasses->dim; i++) @@ -1017,7 +1017,7 @@ void EnumDeclaration::toDocBuffer(OutBuffer *buf) { if (ident) { - buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); buf->writestring(";\n"); } } @@ -1975,6 +1975,15 @@ void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) char *sid = s->ident->toChars(); FuncDeclaration *f = s->isFuncDeclaration(); + const char *fullyQualifiedName = s->toPrettyChars(); + int total = strlen(fullyQualifiedName); + char *terminator = (char*) malloc(total + 3); + terminator[0] = ','; // end the first argument + strncpy(terminator + 1, fullyQualifiedName, total); + ++total; // because of the comma + terminator[total] = ')'; // to close the DDOC_PSYMBOL macro + terminator[total + 1] = 0; // terminate the string + //printf("highlightCode(s = '%s', kind = %s)\n", sid, s->kind()); for (unsigned i = offset; i < buf->offset; i++) { unsigned char c = buf->data[i]; @@ -1988,7 +1997,7 @@ void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) { if (cmp(sid, buf->data + i, j - i) == 0) { - i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1; + i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, terminator) - 1; continue; } else if (f) @@ -2004,6 +2013,8 @@ void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) } } } + + free(terminator); } /**************************************** From e2aecec1fd0bbe0eb539616fddc262db674d5bab Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 11 Mar 2012 17:39:07 -0400 Subject: [PATCH 5/5] strip module name to preserve links --- src/doc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/doc.c b/src/doc.c index c03d158d02f7..efa0d7272e9b 100644 --- a/src/doc.c +++ b/src/doc.c @@ -929,7 +929,7 @@ void AggregateDeclaration::toDocBuffer(OutBuffer *buf) #if 0 emitProtection(buf, protection); #endif - buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars() + strlen(this->getModule()->toChars()) + 1); buf->writestring(";\n"); } } @@ -953,7 +953,7 @@ void StructDeclaration::toDocBuffer(OutBuffer *buf) } else { - buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars() + strlen(this->getModule()->toChars()) + 1); } buf->writestring(";\n"); } @@ -980,7 +980,7 @@ void ClassDeclaration::toDocBuffer(OutBuffer *buf) { if (isAbstract()) buf->writestring("abstract "); - buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars() + strlen(this->getModule()->toChars()) + 1); } int any = 0; for (size_t i = 0; i < baseclasses->dim; i++) @@ -1017,7 +1017,7 @@ void EnumDeclaration::toDocBuffer(OutBuffer *buf) { if (ident) { - buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars()); + buf->printf("%s $(DDOC_PSYMBOL %s, %s)", kind(), toChars(), toPrettyChars() + strlen(this->getModule()->toChars()) + 1 ); buf->writestring(";\n"); } } @@ -1975,7 +1975,7 @@ void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset) char *sid = s->ident->toChars(); FuncDeclaration *f = s->isFuncDeclaration(); - const char *fullyQualifiedName = s->toPrettyChars(); + const char *fullyQualifiedName = s->toPrettyChars() + strlen(s->getModule()->toChars()) + 1; int total = strlen(fullyQualifiedName); char *terminator = (char*) malloc(total + 3); terminator[0] = ','; // end the first argument