Some code (wrote very quickly, very much unfinished and untested)...
function init__template_export()
{
define('TPE_BORING',0);
define('TPE_INSIDE_TAG_DEFINITION',1);
define('TPE_INSIDE_TAG_ATTRIBUTE',2);
define('TPE_INSIDE_TAG_CSS',3);
define('TPE_INSIDE_TEMPCODE_TAG_DEFINITION',4);
define('TPE_INSIDE_CSS',5);
}
// NB: $parameters is an array of strings, no compiled Tempcode allowed
function do_exportable_template($template_name,$raw_tempcode,$parameters,$context=NULL)
{
if (!is_null($context)) $context=TPE_BORING;
$stream='';
_inject_into_context($context,$stream,'[TEMPLATE-START:'.$template_name.']');
$brace_depth=NULL;
$len=strlen($raw_tempcode);
for ($i=0;$i<$len;$i++)
{
$next=$raw_tempcode[$i];
$after=($i!=$len-1)?$raw_tempcode[$i+1]:'';
switch ($next)
{
case '\\': // Escaping
if ((!$escaping) && (($after=='\\') || ($after=='{') || ($after=='}')))
{
$stream.=$after;
{
$stream.=$next;
}
break;
case '<':
TODO
case '>':
TODO
case '{':
if ((strtoupper($after)==$after) && (!is_numeric($after))) // Check it's really going to be treated as Tempcode
{
$balance=0;
$to_inject='';
while ($i<$len)
{
$next=$raw_tempcode[$i];
$to_inject.=$next;
if ($next=='{') $balance++;
elseif ($next=='}') $balance--;
$i++;
if ($balance==0) break;
}
$with=NULL;
$matches=array();
if (preg_match('#^\{(\w+)[^\{\}]*\}$#',$to_inject,$matches)!=0)
{
$with=$paramters[$matches[1]];
}
_inject_into_context($context,$stream,$to_inject,$with);
break;
}
default:
$stream.=$next;
break;
}
}
function _inject_into_context($context,&$stream,$to_inject,$with=NULL)
{
if (!is_null($with)) $stream.=$to_inject.$with;
switch ($context)
{
case 'TPE_BORING':
$stream.=$to_inject;
break;
case 'TPE_INSIDE_TAG_DEFINITION':
TODO
break;
case 'TPE_INSIDE_TAG_ATTRIBUTE':
$matches=array();
preg_match('#(\w+)="([^"]*)$#',$stream,$matches);
$attribute=$matches[1];
$prior=$matches[2];
$tag_start=strrpos($stream,'<');
$stream=substr($stream,0,$tag_start).'[TEMPCODE-PLACEHOLDER:tags-attribute:'.$attribute.':after:'._bracket_escape($prior).'::'._bracket_escape($to_inject).']'.substr($stream,$tag_start);
break;
case 'TPE_INSIDE_TAG_CSS':
case 'TPE_INSIDE_CSS':
$stream.='/*[TEMPCODE-PLACEHOLDER:un_css_comment::'._bracket_escape($to_inject).']*/';
if (!is_null($with)) $stream.=$with;
break;
case 'TPE_INSIDE_TEMPCODE_TAG_DEFINITION':
TODO
break;
}
TODO
}
function _bracket_escape($text)
{
return str_replace(array('[',']'),array('\[','\]'),$text);
}
function init__template_export()
{
define('TPE_BORING',0);
define('TPE_INSIDE_TAG_DEFINITION',1);
define('TPE_INSIDE_TAG_ATTRIBUTE',2);
define('TPE_INSIDE_TAG_CSS',3);
define('TPE_INSIDE_TEMPCODE_TAG_DEFINITION',4);
define('TPE_INSIDE_CSS',5);
}
// NB: $parameters is an array of strings, no compiled Tempcode allowed
function do_exportable_template($template_name,$raw_tempcode,$parameters,$context=NULL)
{
if (!is_null($context)) $context=TPE_BORING;
$stream='';
_inject_into_context($context,$stream,'[TEMPLATE-START:'.$template_name.']');
$brace_depth=NULL;
$len=strlen($raw_tempcode);
for ($i=0;$i<$len;$i++)
{
$next=$raw_tempcode[$i];
$after=($i!=$len-1)?$raw_tempcode[$i+1]:'';
switch ($next)
{
case '\\': // Escaping
if ((!$escaping) && (($after=='\\') || ($after=='{') || ($after=='}')))
{
$stream.=$after;
{
$stream.=$next;
}
break;
case '<':
TODO
case '>':
TODO
case '{':
if ((strtoupper($after)==$after) && (!is_numeric($after))) // Check it's really going to be treated as Tempcode
{
$balance=0;
$to_inject='';
while ($i<$len)
{
$next=$raw_tempcode[$i];
$to_inject.=$next;
if ($next=='{') $balance++;
elseif ($next=='}') $balance--;
$i++;
if ($balance==0) break;
}
$with=NULL;
$matches=array();
if (preg_match('#^\{(\w+)[^\{\}]*\}$#',$to_inject,$matches)!=0)
{
$with=$paramters[$matches[1]];
}
_inject_into_context($context,$stream,$to_inject,$with);
break;
}
default:
$stream.=$next;
break;
}
}
_inject_into_context($context,$stream,'[TEMPLATE-END:'.$template_name.']');
return $stream;
}
function _inject_into_context($context,&$stream,$to_inject,$with=NULL)
{
if (!is_null($with)) $stream.=$to_inject.$with;
switch ($context)
{
case 'TPE_BORING':
$stream.=$to_inject;
break;
case 'TPE_INSIDE_TAG_DEFINITION':
TODO
break;
case 'TPE_INSIDE_TAG_ATTRIBUTE':
$matches=array();
preg_match('#(\w+)="([^"]*)$#',$stream,$matches);
$attribute=$matches[1];
$prior=$matches[2];
$tag_start=strrpos($stream,'<');
$stream=substr($stream,0,$tag_start).'[TEMPCODE-PLACEHOLDER:tags-attribute:'.$attribute.':after:'._bracket_escape($prior).'::'._bracket_escape($to_inject).']'.substr($stream,$tag_start);
break;
case 'TPE_INSIDE_TAG_CSS':
case 'TPE_INSIDE_CSS':
$stream.='/*[TEMPCODE-PLACEHOLDER:un_css_comment::'._bracket_escape($to_inject).']*/';
if (!is_null($with)) $stream.=$with;
break;
case 'TPE_INSIDE_TEMPCODE_TAG_DEFINITION':
TODO
break;
}
TODO
}
function _bracket_escape($text)
{
return str_replace(array('[',']'),array('\[','\]'),$text);
}