Topic on Extension talk:TemplateData

Parsing custom formats in JS

3
Samwilson (talkcontribs)

Is there an existing Javascript library (outside VisualEditor etc.) for parsing the TemplateData format syntax? Wanted for TemplateWizard (task T186653). @cscott: are you someone to ask about this?

Cscott (talkcontribs)

The implementation in Parsoid starts around here:

https://github.com/wikimedia/parsoid/blob/master/lib/html2wt/WikitextSerializer.js#L510

and the specification is

https://github.com/wikimedia/mediawiki-extensions-TemplateData/blob/master/Specification.md#37-formatstring

I think the entirety of the parser is basically this:

var FORMATSTRING_REGEXP =
  /^(\n)?(\{\{ *_+)(\n? *\|\n? *_+ *= *)(_+)(\n? *\}\})(\n)?$/;

var formatStringSubst = function(format, value, forceTrim) {
  if (forceTrim) { value = value.trim(); }
  return format.replace(/_+/, function(hole) {
    if (value === '' || hole.length <= value.length) { return value; }
    return value + (' '.repeat(hole.length - value.length));
  });
};
Samwilson (talkcontribs)

Thanks! That's brilliant info. :)

Reply to "Parsing custom formats in JS"