Skip to content

Commit

Permalink
Add option not to remove unknown macros (#39)
Browse files Browse the repository at this point in the history
Add option not to remove unknown macros
merged-on-behalf-of: BBasile <[email protected]>
  • Loading branch information
WebFreak001 authored and dlang-bot committed Dec 31, 2018
1 parent bf04727 commit e5d98b8
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/ddoc/macros.d
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ shared static this()
* To undefine hardwired macros, just set them to an empty string: $(D macros["B"] = "";).
* output = An object satisfying $(D std.range.isOutputRange), usually a $(D std.array.Appender).
*/
void expand(O)(Lexer input, in string[string] macros, O output) if (isOutputRange!(O,
void expand(O)(Lexer input, in string[string] macros, O output, bool removeUnknown = true) if (isOutputRange!(O,
string))
{
// First, we need to turn every embedded code into a $(D_CODE)
Expand All @@ -183,7 +183,16 @@ void expand(O)(Lexer input, in string[string] macros, O output) if (isOutputRang
{
auto mac = Lexer(matchParenthesis(input), true);
if (!mac.empty)
expandMacroImpl(mac, macros, output);
{
if (!expandMacroImpl(mac, macros, output) && !removeUnknown)
{
output.put("$");
output.put("(");
foreach (val; mac)
output.put(val.text);
output.put(")");
}
}
}
else
output.put("$");
Expand All @@ -197,12 +206,12 @@ void expand(O)(Lexer input, in string[string] macros, O output) if (isOutputRang
}

/// Ditto
string expand(Lexer input, string[string] macros)
string expand(Lexer input, string[string] macros, bool removeUnknown = true)
{
import std.array : appender;

auto app = appender!string();
expand(input, macros, app);
expand(input, macros, app, removeUnknown);
return app.data;
}

Expand All @@ -222,6 +231,14 @@ unittest
assert(r == exp, r);
}

unittest
{
auto lex = Lexer(`$(B this) $(UNKN $(B is)) unknown!`);
immutable r = expand(lex, [`B` : `<b>$0</b>`], false);
immutable exp = `<b>this</b> $(UNKN $(B is)) unknown!`;
assert(r == exp, r);
}

/**
* Expand a macro, and write the result to an $(D OutputRange).
*
Expand Down Expand Up @@ -439,28 +456,28 @@ bool parseKeyValuePair(ref Lexer lexer, ref KeyValuePair[] pairs)

private:
// upperArgs is a string[11] actually, or null.
void expandMacroImpl(O)(Lexer input, in string[string] macros, O output)
bool expandMacroImpl(O)(Lexer input, in string[string] macros, O output)
{
import std.conv : text;

//debug writeln("Expanding: ", input.text);
// Check if the macro exist and get it's value.
if (input.front.type != Type.word)
return;
return false;
string macroName = input.front.text;
//debug writeln("[EXPAND] Macro name: ", input.front.text);
string macroValue = lookup(macroName, macros);
// No point loosing time if the macro is undefined.
if (macroValue is null)
return;
return false;
//debug writeln("[EXPAND] Macro value: ", macroValue);
input.popFront();

// Special case for $(DDOC). It's ugly, but it gets the job done.
if (input.empty && macroName == "BODY")
{
output.put(lookup("BODY", macros));
return;
return true;
}

// Collect the arguments
Expand All @@ -472,10 +489,11 @@ void expandMacroImpl(O)(Lexer input, in string[string] macros, O output)
// First pass
auto argOutput = appender!string();
if (!replaceArgs(macroValue, arguments, argOutput))
return;
return true;

// Second pass
replaceMacs(argOutput.data, macros, output);
return true;
}

unittest
Expand Down

0 comments on commit e5d98b8

Please sign in to comment.