Search code examples
phpmarkuptransclusionpmwiki

Include directive with page variable doesn't work inside markup


I have some markup such as:

Markup('talk', '<include',
  '/\\(:talk:\\)/i',
  'MarkupTalk');
function MarkupTalk($m) {
  return 'Talk page: (:include {$BaseName}-talk:)'
}

but when I use it, it does nothing!

Example text (:talk:) more text

outputs

<div id="wikitext">
<p>Example text Talk page:  more text</p>

almost as if the (:include:) directive is a comment! It seems like (:include:) doesn't work when defined in other markup.

How can I make this work properly?


Solution

  • The reason that this doesn't work properly is that your markup:

    talk             <include         B>=><            
    

    is evaluated after one of its dependencies:

    {$var}           >$[phrase]       B=>>             
    

    To fix this, you could change when your markup is evaluated:

    Markup('talk', '<{$var}',
      '/\\(:talk:\\)/i',
      'MarkupTalk');
    

    but this can be undesirable if you have any markup that outputs ("depends on") yours. Instead, you can modify your function to use the PageVar() function as noted in PmWiki.PageVariables, like so:

    function MarkupTalk($m) {
      global $pagename;
      $pagename = ResolvePageName($pagename);
      return 'Talk page: (:include '. PageVar($pagename, '$BaseName') .'-talk:)'
    }
    

    This removes {$var} as a dependency and allows your markup to be safely evaluated after {$var} is.