CXuesong / MwParserFromScratch

A basic .NET Library for parsing wikitext into AST.
Apache License 2.0
18 stars 5 forks source link

Implement NodePlainTextFormatter for multiple Templates #20

Open FaFre opened 3 years ago

FaFre commented 3 years ago

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 a InlineContainerLineNode, containing the templates as InlineNode's?

CXuesong commented 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"