highlight

Parses a string and replace embedded code (code between at least 3 '-') with the relevant macros.

string
highlight
(
string str
)

Parameters

str string

A string that might contain embedded code. Only code will be modified. If the string doesn't contain any embedded code, it will be returned as is.

Return Value

Type: string

A (possibly new) string containing the embedded code put in the proper macros.

Examples

	import ddoc.lexer;

	auto s1 = `Here is some embedded D code I'd like to show you:
$(MY_D_CODE
------
// Entry point...
void main() {
  import std.stdio : writeln;
  writeln("Hello,", " ", "world", "!");
}
------
)
Isn't it pretty ?`;
	// Embedded code is surrounded by D_CODE macro, and tokens have their own
	// macros (see: D_KEYWORD for example).
	auto r1 = highlight(s1);
	auto e1 = `Here is some embedded D code I'd like to show you:
$(MY_D_CODE
$(D_CODE $(D_COMMENT // Entry point...)
$(D_KEYWORD void) main() {
  $(D_KEYWORD import) std.stdio : writeln;
  writeln($(D_STRING "Hello,"), $(D_STRING " "), $(D_STRING "world"), $(D_STRING "!"));
})
)
Isn't it pretty ?`;
	assert(r1 == e1, r1);

	// No allocation is performed if the string doesn't contain inline code.
	auto s2 = `This is some simple string
--
It doesn't do much
--
Hope you won't allocate`;
	auto r2 = highlight(s2);
	assert(r2 is s2, r2);

Meta