Page MenuHomePhabricator

Proposal: Allow lua to access unparsed arguments.
Open, Needs TriagePublicFeature

Description

Feature summary

I would like to propose allowing lua to access the unparsed version of template arguments.

Specificly, right now you can do things like frame:getArgument( arg ) which returns an object with one method expand. I would like to add a second method called unexpand (Name could probably be bikesheded. getText() might be another choice). What frame:getArgument( "foo" ):unexpand() would do, is essentially call ->expand() but with PPFrame::RECOVER_ORIG as the second argument. i.e.

$oldOT = $parser->getOutputType();
$parser->setOutputType( Parser::OT_PLAIN );
$result = is_int( $name ) ? $frame->expand( $frame->numberedArgs[$name], PPFrame::RECOVER_ORIG ) : $frame->expand( $frame->namedArgs[$name], PPFrame::RECOVER_ORIG ) ;
$parser->setOutputType( $oldOT );
return $result;

[Changing the output type is only required for headers, as it is not included in RECOVER_ORIG. If we actually did this we might just want to add an new flag for that to expand instead of messing with output type. I don't understand why its not like that currently. It seems like a bug that {{msgnw::foo}} replaces headers with strip markers]

Use case(s) (list the steps that you performed to discover that problem, and describe the actual underlying problem which you want to solve. Do not describe only a solution):

  • Allow lua to operate on raw arguments. Example you could make something like {{#invoke:String|rawlen|{{:Jurassic Park}}}} to get the length pre-expansion, which might be useful in some situations.
  • Allow lua to better support parsing DSL in cases where they conflict with wikitext. For example, we already have cases where LUA is parsing some DSL, for example {{Chess from pgn}}. This works ok for cases where there isn't a conflict with wikitext syntax, but could be much expanded if lua could reliably get the raw arguments. Taken to extremes you could for example, implement a markdown parser in lua. A more likely example, is if making a lua template that does something similar to easytimeline.
  • Allow custom macro expansion. Whether or not this is a good thing is debatable, but one could have something like `{{#invoke:macros|A bunch of wikitext here with CONSTANT1 repeated multiple times.|CONSTANT1=foo}}

Benefits (why should this be implemented?):
It would increase the power of lua templates, possibly both for good and for bad.

At first glance, this seems a bit like something that is a little internal-ish to expose, but it is essentially already exposed via {{msgnw:...}}. An alternative might be to expose the $flags second argument to expand, which would allow choosing which parts to expand, but that seems really internalish.

This bug is based on a discussion in the MWStake matrix channel.

Related Objects