Skip to content

Commit 81ebddd

Browse files
authored
Merge pull request #41 from WebFreak001/fix-unknown-2
Add option to leave unknown macros 2 merged-on-behalf-of: Jan Jurzitza <gh@webfreak.org>
2 parents beff9b8 + 097b796 commit 81ebddd

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/ddoc/comments.d

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module ddoc.comments;
77
import ddoc.sections;
88
import ddoc.lexer;
99

10-
Comment parseComment(string text, string[string] macros)
10+
Comment parseComment(string text, string[string] macros, bool removeUnknown = true)
1111
out(retVal)
1212
{
1313
assert(retVal.sections.length >= 2);
@@ -39,13 +39,13 @@ body
3939
if (!doMapping(p[0]))
4040
throw new DdocParseException("Unable to parse Key/Value pairs", p[0].content);
4141
foreach (ref kv; p[0].mapping)
42-
kv[1] = expand(Lexer(highlight(kv[1])), sMacros);
42+
kv[1] = expand(Lexer(highlight(kv[1])), sMacros, removeUnknown);
4343
}
4444

4545
foreach (ref Section sec; sections)
4646
{
4747
if (sec.name != "Macros" && sec.name != "Escapes" && sec.name != "Params")
48-
sec.content = expand(Lexer(highlight(sec.content)), sMacros);
48+
sec.content = expand(Lexer(highlight(sec.content)), sMacros, removeUnknown);
4949
}
5050
return Comment(sections);
5151
}
@@ -104,6 +104,30 @@ Returns:
104104
assert(c.sections[3].name == "Returns");
105105
}
106106

107+
unittest
108+
{
109+
auto macros = ["A" : "<a href=\"$0\">"];
110+
auto comment = `Best $(Unknown comment) ever`;
111+
112+
Comment c = parseComment(comment, macros, true);
113+
114+
assert(c.sections.length >= 1);
115+
assert(c.sections[0].name is null);
116+
assert(c.sections[0].content == "Best ever", c.sections[0].content);
117+
}
118+
119+
unittest
120+
{
121+
auto macros = ["A" : "<a href=\"$0\">"];
122+
auto comment = `Best $(Unknown comment) ever`;
123+
124+
Comment c = parseComment(comment, macros, false);
125+
126+
assert(c.sections.length >= 1);
127+
assert(c.sections[0].name is null);
128+
assert(c.sections[0].content == "Best $(Unknown comment) ever", c.sections[0].content);
129+
}
130+
107131
unittest
108132
{
109133
auto comment = `---

src/ddoc/macros.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ shared static this()
167167
* DDOC. Which means if an user provides a macro such as $(D macros["B"] = "<h1>$0</h1>";),
168168
* it will be used, otherwise the default $(D macros["B"] = "<b>$0</b>";) will be used.
169169
* To undefine hardwired macros, just set them to an empty string: $(D macros["B"] = "";).
170+
* removeUnknown = Set to true to make unknown macros disappear from the output or false to make them output unprocessed.
170171
* output = An object satisfying $(D std.range.isOutputRange), usually a $(D std.array.Appender).
171172
*/
172173
void expand(O)(Lexer input, in string[string] macros, O output, bool removeUnknown = true) if (isOutputRange!(O,

src/ddoc/standalone.d

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ string parseFile(string path, in string[string] context) {
4343
return parseDdocBody(macros);
4444
}
4545

46-
string parseDDString(string text, string[string] macros)
46+
string parseDDString(string text, string[string] macros, bool removeUnknown = true)
4747
{
4848
import ddoc.highlight;
4949
import std.string : strip;
@@ -64,29 +64,33 @@ string parseDDString(string text, string[string] macros)
6464
// macros["COPYRIGHT"] = copyright;
6565
text = highlight(text);
6666
auto lexer = Lexer(text, true);
67-
return expand(lexer, macros);
67+
return expand(lexer, macros, removeUnknown);
6868
}
6969

7070
///
7171
unittest {
7272
import std.stdio, std.string;
7373

7474
auto text = `Ddoc
75-
This file is a standalone Ddoc file. It can contain any kind of
75+
This file is a $(unknown standalone) Ddoc file. It can contain any kind of
7676
$(MAC macros), defined in the $(MAC 'Macros:' section).
7777
7878
Macros:
7979
MAC=$0
8080
_=
8181
`;
8282

83-
auto expected = `This file is a standalone Ddoc file. It can contain any kind of
83+
auto expected1 = `This file is a Ddoc file. It can contain any kind of
84+
macros, defined in the 'Macros:' section.`;
85+
auto expected2 = `This file is a $(unknown standalone) Ddoc file. It can contain any kind of
8486
macros, defined in the 'Macros:' section.`;
8587

8688
auto lex = Lexer(text, true);
8789
// Whitespace and newline before / after not taken into account.
8890
auto res = parseDDString(text, null).strip;
89-
assert(res == expected, res);
91+
assert(res == expected1, res);
92+
res = parseDDString(text, null, false).strip;
93+
assert(res == expected2, res);
9094
}
9195

9296
// Warning: Does not support embedded code / inlining.

0 commit comments

Comments
 (0)