Open FaFre opened 3 years ago
To begin with, try something like this (I'm running in CSharpRepl):
> using MwParserFromScratch;
using MwParserFromScratch.Nodes;
var parser = new WikitextParser();
> var ast = parser.Parse("{{marker|type=go|name=Soekarno-Hatta International Airport|lat=-6.1252|long=106.6597}} ({{IATA|CGK}}) is 20 km northwest of the city in the neighboring city of Tangerang");
> ast
[{{marker|type=go|name=Soekarno-Hatta International Airport|lat=-6.1252|long=106.6597}} ({{IATA|CGK}}) is 20 km northwest of the city in the neighboring city of Tangerang]
> void MyPlainTextFormatter(Node node, StringBuilder sb) {
if (node is Template t && MwParserUtility.NormalizeTitle(t.Name) == "Marker") {
sb.Append("[marker:");
t.Arguments["name"]?.Value.ToPlainText(sb);
sb.Append("]");
return;
}
// Use default ToPlainText implementation. Remember to pass down custom NodePlainTextFormatter.
node.ToPlainText(sb, MyPlainTextFormatter);
}
> ast.ToPlainText(MyPlainTextFormatter)
"[marker:Soekarno-Hatta International Airport] () is 20 km northwest of the city in the neighboring city of Tangerang"
I looked at the implementation of
ToPlainText()
now for while, but I don't grasp how to implement custom template formatters.For example, I have the following Wikitext with two different Templates (marker, IATA):
{{marker|type=go|name=Soekarno-Hatta International Airport|lat=-6.1252|long=106.6597}} ({{IATA|CGK}}) is 20 km northwest of the city in the neighboring city of Tangerang
With
ToPlainText()
I want to to get something like this:[marker:Soekarno-Hatta International Airport] ([IATA:CGK]) is 20 km northwest of the city in the neighboring city of Tangerang
How does the
NodePlainTextFormatter
does have to look like when appying directly on aInlineContainerLineNode
, containing the templates asInlineNode
's?