Skip to content

Commit

Permalink
Merge pull request #7043 from WalterBright/fix17697
Browse files Browse the repository at this point in the history
fix Issue 17697 - Ddoc: automatically highlight URLs outside of macro…
merged-on-behalf-of: Walter Bright <WalterBright@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Jul 31, 2017
2 parents 154aa1b + f100182 commit d15072f
Show file tree
Hide file tree
Showing 6 changed files with 582 additions and 6 deletions.
32 changes: 32 additions & 0 deletions changelog/fix17697.dd
@@ -0,0 +1,32 @@
Fix Issue 17697 - Ddoc: automatically highlight URLs outside of macro arguments

URLs which appear in Ddoc text:

---
/****
* http://www.fooa.com/test1
*/
---

will be treated as if they were written:

---
/****
* $(LINK http://www.fooa.com/test1)
*/
---

and a clickable hyperlink will be emitted to the resulting .html file.

This detection does not happen if the URL is part of a macro argument:

---
/****
* $(DOLLAR)$(LPAREN)ABC http://www.fooa.com/test1$(RPAREN)
*/
---

The behavior of that is unchanged. URLs can start with `http://` or `https://`

`http:` and `https:` at the beginning of the line are no longer treated
as section headings.
48 changes: 46 additions & 2 deletions src/ddmd/doc.d
Expand Up @@ -1643,7 +1643,13 @@ struct DocComment
const(char)* q = p + utfStride(p);
while (isIdTail(q))
q += utfStride(q);
if (*q == ':') // identifier: ends it
if (*q == ':' && // 'identifier:' ends it
// but not 'http://' or 'https://'
!(q[1] == '/' && q[2] == '/' &&
(q - p == 4 && Port.memicmp(p, "http".ptr, 4) == 0 ||
q - p == 5 && Port.memicmp(p, "https".ptr, 5) == 0)
)
)
{
idlen = q - p;
idstart = p;
Expand Down Expand Up @@ -2151,6 +2157,7 @@ extern (C++) void highlightText(Scope* sc, Dsymbols* a, OutBuffer* buf, size_t o
int inCode = 0;
int inBacktick = 0;
//int inComment = 0; // in <!-- ... --> comment
int inMacro = 0;
size_t iCodeStart = 0; // start of code section
size_t codeIndent = 0;
size_t iLineStart = offset;
Expand Down Expand Up @@ -2405,6 +2412,32 @@ extern (C++) void highlightText(Scope* sc, Dsymbols* a, OutBuffer* buf, size_t o
}
}
break;

case '$':
{
/* Look for the start of a macro, '$(Identifier'
*/
leadingBlank = 0;
if (inCode || inBacktick)
break;
const slice = buf.peekSlice();
auto p = &slice[i];
if (p[1] == '(' && isIdStart(&p[2]))
++inMacro;
break;
}

case ')':
{ /* End of macro
*/
leadingBlank = 0;
if (inCode || inBacktick)
break;
if (inMacro)
--inMacro;
break;
}

default:
leadingBlank = 0;
if (sc._module.isDocFile || inCode)
Expand All @@ -2418,7 +2451,18 @@ extern (C++) void highlightText(Scope* sc, Dsymbols* a, OutBuffer* buf, size_t o
size_t k = skippastURL(buf, i);
if (i < k)
{
i = k - 1;
/* The URL is buf[i..k]
*/
if (inMacro)
/* Leave alone if already in a macro
*/
i = k - 1;
else
{
/* Replace URL with '$(LINK URL)'
*/
i = buf.bracket(i, "$(LINK ", k, ")") - 1;
}
break;
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/compilable/ddoc17697.d
@@ -0,0 +1,16 @@
// PERMUTE_ARGS:
// REQUIRED_ARGS: -D -Dd${RESULTS_DIR}/compilable -o-
// POST_SCRIPT: compilable/extra-files/ddocAny-postscript.sh 17697

/***
* See:
* http://www.fooa.com/test1
* http://www.fooa.com/_test1
* https://www.foob.com/test1
* $(LINK http://www.fooc.com/test1)
* $(LINK2 http://www.food.com/test1, test1)
*/

module test1;

int a;
2 changes: 1 addition & 1 deletion test/compilable/ddoc4899.d
Expand Up @@ -11,7 +11,7 @@ compilable/ddoc4899.d(16): Warning: Ddoc: Stray ')'. This may cause incorrect Dd

/++
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
foo:)
+/
module d;

Expand Down
6 changes: 3 additions & 3 deletions test/compilable/extra-files/ddoc10.html
Expand Up @@ -731,7 +731,7 @@ <h4>Declaration</h4>
<section class="section ddoc_sections">
<div class="ddoc_summary">
<p class="para">
blah http://www.map3.com <code class="code">map3</code>
blah <a href="http://www.map3.com">http://www.map3.com</a> <code class="code">map3</code>
</p>
</div>

Expand Down Expand Up @@ -763,7 +763,7 @@ <h4>Declaration</h4>
<section class="section ddoc_sections">
<div class="ddoc_summary">
<p class="para">
blah http://www.map.com map
blah <a href="http://www.map.com">http://www.map.com</a> map
</p>
</div>

Expand Down Expand Up @@ -794,7 +794,7 @@ <h4>Declaration</h4>
<section class="section ddoc_sections">
<div class="ddoc_summary">
<p class="para">
blah http://www.map.com map
blah <a href="http://www.map.com">http://www.map.com</a> map
</p>
</div>

Expand Down

0 comments on commit d15072f

Please sign in to comment.