Skip to content

Commit

Permalink
fix Issue 9155 - Ddoc: code section should strip leading spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Jan 21, 2013
1 parent bcb066f commit c47ef9e
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 5 deletions.
42 changes: 42 additions & 0 deletions src/doc.c
Expand Up @@ -108,6 +108,7 @@ Parameter *isFunctionParameter(Dsymbol *s, unsigned char *p, size_t len);

int isIdStart(unsigned char *p);
int isIdTail(unsigned char *p);
int isIndentWS(unsigned char *p);
int utfStride(unsigned char *p);

static unsigned char ddoc_default[] = "\
Expand Down Expand Up @@ -1175,6 +1176,7 @@ void DocComment::parseSections(unsigned char *comment)
p = comment;
while (*p)
{
unsigned char *pstart0 = p;
p = skipwhitespace(p);
pstart = p;
pend = p;
Expand All @@ -1190,6 +1192,11 @@ void DocComment::parseSections(unsigned char *comment)
// Check for start/end of a code section
if (*p == '-')
{
if (!inCode)
{ // restore leading indentation
while (pstart0 < pstart && isIndentWS(pstart-1)) --pstart;
}

int numdash = 0;
while (*p == '-')
{
Expand Down Expand Up @@ -1887,6 +1894,7 @@ void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, size_t offset)
int inCode = 0;
//int inComment = 0; // in <!-- ... --> comment
size_t iCodeStart; // start of code section
size_t codeIndent = 0;

size_t iLineStart = offset;

Expand Down Expand Up @@ -2053,6 +2061,30 @@ void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, size_t offset)

codebuf.write(buf->data + iCodeStart, i - iCodeStart);
codebuf.writeByte(0);

// Remove leading indentations from all lines
bool lineStart = true;
unsigned char *endp = codebuf.data + codebuf.offset;
for (unsigned char *p = codebuf.data; p < endp; )
{
if (lineStart)
{
size_t j = codeIndent;
unsigned char *q = p;
while (j-- > 0 && q < endp && isIndentWS(q))
++q;
codebuf.remove(p - codebuf.data, q - p);
assert(codebuf.data <= p);
assert(p < codebuf.data + codebuf.offset);
lineStart = false;
endp = codebuf.data + codebuf.offset; // update
continue;
}
if (*p == '\n')
lineStart = true;
++p;
}

highlightCode2(sc, s, &codebuf, 0);
buf->remove(iCodeStart, i - iCodeStart);
i = buf->insert(iCodeStart, codebuf.data, codebuf.offset);
Expand All @@ -2063,6 +2095,7 @@ void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, size_t offset)
{ static char pre[] = "$(D_CODE \n";

inCode = 1;
codeIndent = istart - iLineStart; // save indent count
i = buf->insert(i, pre, sizeof(pre) - 1);
iCodeStart = i;
i--; // place i on >
Expand Down Expand Up @@ -2325,6 +2358,15 @@ int isIdTail(unsigned char *p)
return 0;
}

/****************************************
* Determine if p points to the indentation space.
*/

int isIndentWS(unsigned char *p)
{
return (*p == ' ') || (*p == '\t');
}

/*****************************************
* Return number of bytes in UTF character.
*/
Expand Down
2 changes: 1 addition & 1 deletion test/compilable/ddoc1.d
Expand Up @@ -22,7 +22,7 @@ typedef int mytypedefint;
* #include <stdio.h>
* void main()
* {
* printf("hello\n");
* printf("hello\n");
* }
* -----
* Copyright: 1998
Expand Down
80 changes: 80 additions & 0 deletions test/compilable/ddoc9155.d
@@ -0,0 +1,80 @@
// PERMUTE_ARGS:
// REQUIRED_ARGS: -D -Ddtest_results/compilable -o-
// POST_SCRIPT: compilable/extra-files/ddocAny-postscript.sh 9155

module ddoc9155;

/++
+ Note:
+ test document note
+ 2nd line
+ Example:
+ ---
+ import std.stdio; //&
+ writeln("Hello world!");
+ if (test) {
+ writefln("D programming language");
+ }
+
+ algorithm;
+
+ xxx; //comment
+ yyy;
+ /* test
+ * comment
+ */
+
+ // Create MIME Base64 with CRLF, per line 76.
+File f = File("./text.txt", "r");
+uint line = 0;
+ // The ElementType of data is not aggregation type
+foreach (encoded; Base64.encoder(data))
+ ---
+/

/**
--------------------------------------------------------
wstring ws;
transcode("hello world",ws);
// transcode from UTF-8 to UTF-16
--------------------------------------------------------
*/

/**
* Example:
* ---
* import std.stdio; //&
* writeln("Hello world!");
* if (test) {
* writefln("D programming language");
* }
*
* algorithm;
*
* xxx; //comment
* yyy;
* /+ test
* + comment
* +/
* ---
*/

/**
----
#!/usr/bin/env rdmd
// Computes average line length for standard input.
import std.stdio;
----
*/
/**
---
writefln(q"EOS
This
is a multi-line
heredoc string
EOS"
);
---
*/

void foo(){}
8 changes: 4 additions & 4 deletions test/compilable/extra-files/ddoc1.html
Expand Up @@ -16,11 +16,11 @@ <h1>abc</h1>
city
<br><br>
paragraph 2 about of F
<pre class="d_code"> #include &lt;stdio.h&gt;
<font color=blue>void</font> main()
{
<pre class="d_code">#include &lt;stdio.h&gt;
<font color=blue>void</font> main()
{
printf(<font color=red>"hello\n"</font>);
}
}
</pre>
<br><br>
<b>Copyright:</b><br>
Expand Down
81 changes: 81 additions & 0 deletions test/compilable/extra-files/ddoc9155.html
@@ -0,0 +1,81 @@
<html><head>
<META http-equiv="content-type" content="text/html; charset=utf-8">
<title>ddoc9155</title>
</head><body>
<h1>ddoc9155</h1>
<br><br>
<dl><dt><big><a name="foo"></a>void <u>foo</u>();
</big></dt>
<dd><b>Note:</b><br>
test document note
2nd line
<br><br>
<b>Example:</b><br>
<pre class="d_code"><font color=blue>import</font> std.stdio; <font color=green>//&amp;
</font>writeln(<font color=red>"Hello world!"</font>);
<font color=blue>if</font> (test) {
writefln(<font color=red>"D programming language"</font>);
}

algorithm;

xxx; <font color=green>//comment
</font> yyy;
<font color=green>/* test
* comment
*/</font>

<font color=green>// Create MIME Base64 with CRLF, per line 76.
</font>File f = File(<font color=red>"./text.txt"</font>, <font color=red>"r"</font>);
<font color=blue>uint</font> line = 0;
<font color=green>// The ElementType of data is not aggregation type
</font><font color=blue>foreach</font> (encoded; Base64.encoder(data))
</pre>

<br><br>

<pre class="d_code">wstring ws;
transcode(<font color=red>"hello world"</font>,ws);
<font color=green>// transcode from UTF-8 to UTF-16
</font></pre>


<br><br>
<b>Example:</b><br>
<pre class="d_code"><font color=blue>import</font> std.stdio; <font color=green>//&amp;
</font>writeln(<font color=red>"Hello world!"</font>);
<font color=blue>if</font> (test) {
writefln(<font color=red>"D programming language"</font>);
}

algorithm;

xxx; <font color=green>//comment
</font> yyy;
<font color=green>/+ test
+ comment
+/</font>
</pre>

<br><br>

<pre class="d_code">#!/usr/bin/env rdmd
<font color=green>// Computes average line length for standard input.
</font><font color=blue>import</font> std.stdio;
</pre>
<br><br>

<pre class="d_code">writefln(<font color=red>q"EOS
This
is a multi-line
heredoc string
EOS"</font>
);
</pre>
<br><br>

</dd>
</dl>

<hr><small>Page generated by <a href="http://dlang.org/ddoc.html">Ddoc</a>. </small>
</body></html>

0 comments on commit c47ef9e

Please sign in to comment.