<?php if (!defined('PmWiki')) exit();
# vim: set ts=4 sw=4 et:
##
##        File: escapedmarkup.php
##     Version: 2012-09-25
##      Status: functional
##      Author: Peter Bowers
## Create Date: 2012-09-25
##   Copyright: 2012, Peter Bowers
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License, Version 2, as
## published by the Free Software Foundation.
## http://www.gnu.org/copyleft/gpl.html
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## EscapedMarkup.php allows markup to be placed inside of escaped blocks
## or escaped character markup.
#
# Normally text inside of [=...=] and [@...@] never has any markup rules
# processed.  Thus variables remain simply as the variable names, etc.
# Sometimes, however, it is convenient to have a subset of rules processed
# over text that is otherwise escaped.  (For instance, perhaps you want to
# emphasize some portion of an escaped block of code.)  This recipe allows
# an administrator to determine which rules (if any) will be processed for
# each of the existing types of escapes ([=...=] and [@...@]) as well as for
# a new type of escape ([$...$]).  Each set of rules can be maintained
# independently of the others.

SDV($EnableEscapedMarkup, false);
# By default only run variable-substitution rule over [$...$] and no other rules
SDVA($EscapedMarkupRules, array('$' => array('{$var}', "'''")));
## If you wanted to run ''' (strong) rule and [+...+] rule (large) for [@...@]
# $EscapedMarkupRules['@'] = array("'''", '[+');
## If you wanted to run [+...+] rule for [=...=]
# $EscapedMarkupRules['='] = array("[+");

# Replace [=...=] and [@...@] rules and create the new [$...$] markup rule
Markup('[=','_begin',"/(\n[^\\S\n]*)?\\[([=@\$])(.*?)\\2\\]/se",
    "MyPreserveText(\$pagename, '$2', PSS('$3'), '$1')");

## This function copied from PreserveText() in scripts/stdmarkup.php
## Added functionality here is the ability to run certain rules over the
## $text before it gets escaped.  Also the [$...$] markup is introduced to
## allow unchanged behavior for [=...=] and [@...@] while still allowing
## markup for another escaped class.
# Each of $, @, = corresponds to an entry in $EscapedMarkupRules[X].  If an
# array exists with the relevant index then those rules will be run.
function MyPreserveText($pagename, $sigil, $text, $lead) {
  global $EnableEscapedMarkup, $EscapedMarkupRules;
  if (IsEnabled($EnableEscapedMarkup, false) && isset($EscapedMarkupRules[$sigil]) && !empty($EscapedMarkupRules[$sigil])) {
	  $text = RunMarkupRules($pagename, $EscapedMarkupRules[$sigil], $text);
  }
  if ($sigil=='=') return $lead.Keep($text);
  if (strpos($text, "\n")===false) 
    return "$lead<code class='escaped'>".Keep($text)."</code>";
  $text = preg_replace("/\n[^\\S\n]+$/", "\n", $text);
  if ($lead == "" || $lead == "\n") 
    return "$lead<pre class='escaped'>".Keep($text)."</pre>";
  return "$lead<:pre,1>".Keep($text);
}