Skip to content

Commit

Permalink
Support HTML out of PHP tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien committed Mar 29, 2014
1 parent 5251c5a commit e5b947d
Showing 1 changed file with 70 additions and 29 deletions.
99 changes: 70 additions & 29 deletions src/sbp/sbp.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ static public function replaceString($match)
{
$GLOBALS['commentStrings'][] = $id;
}
elseif(strpos($match[0], '?') === 0)
{
$GLOBALS['htmlCodes'][] = $id;
}
else
{
$GLOBALS['quotedStrings'][] = $id;
Expand Down Expand Up @@ -321,36 +325,65 @@ static protected function replace($content, $replace)
static public function parse($content)
{
$GLOBALS['replaceStrings'] = array();
$GLOBALS['htmlCodes'] = array();
$GLOBALS['quotedStrings'] = array();
$GLOBALS['commentStrings'] = array();
$content = static::replace($content, array(
self::SUBST => self::SUBST.self::SUBST,
'#<\?(?!php)#' => '<?php',
));

$content = preg_replace('#^(\s*<\?php)(\s)#', '$1 '.self::COMMENT.(is_null(static::$lastParsedFile) ? '' : '/*:'.static::$lastParsedFile.':*/').'$2', $content, 1, $count);
$content = static::replace(

$content = static::replace($content, array(
/*****************************************/
/* Mark the compiled file with a comment */
/*****************************************/
'<?php '.self::COMMENT.(is_null(static::$lastParsedFile) ? '' : '/*:'.static::$lastParsedFile.':*/').' ?>'.
$content, array(

/***********/
/* PHPUnit */
/***********/
'#^(\s*)(.+\s)?should\s+not\s(.+);\s*$#m'

/***************************/
/* Complete PHP shrot-tags */
/***************************/
'#<\?(?!php)#'
=> '<?php',


/***************************/
/* Remove useless PHP tags */
/***************************/
'#\?><\?php#'
=> '',


/*******************************/
/* Escape the escape-character */
/*******************************/
self::SUBST
=> self::SUBST.self::SUBST,


/*************************************************************/
/* Save the comments, quoted string and HTML out of PHP tags */
/*************************************************************/
'#'.self::COMMENTS.'|'.self::stringRegex().'|\?>.+<\?php#sU'
=> array(get_class(), 'replaceString'),


/*************************************/
/* should key-word fo PHPUnit assert */
/*************************************/
'#^(\s*)(\S.*\s)?should\s+not\s(.*[^;]);*\s*$#mU'
=> function ($match)
{
list($all, $spaces, $before, $after) = $match;
return $spaces . '>assertFalse(' . $before . preg_replace('#(?<![a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff$])be(?![a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff])#', 'is', $after) . ', ' . var_export(trim($all), true) . ');';
},

'#^(\s*)(.+\s)?should\s(.+);\s*$#m'
'#^(\s*)(\S.*\s)?should\s(.*[^;]);*\s*$#mU'
=> function ($match)
{
list($all, $spaces, $before, $after) = $match;
return $spaces . '>assertTrue(' . $before . preg_replace('#(?<![a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff$])be(?![a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff])#', 'is', $after) . ', ' . var_export(trim($all), true) . ');';
},
));

$content = preg_replace_callback('#'.self::COMMENTS.'|'.self::stringRegex().'|\?'.'>.*<\?php#sU', array(get_class(), 'replaceString'), $content);
$validSubst = self::validSubst('(?:'.implode('|', $GLOBALS['quotedStrings']).')');
$validComments = self::validSubst('(?:'.implode('|', $GLOBALS['commentStrings']).')');

Expand All @@ -363,12 +396,8 @@ static public function parse($content)
$__file = var_export($__file, true);
$__dir = var_export($__dir, true);

if(!$count)
{
$content = '<?php '.self::COMMENT.' ?>'.$content;
}

$content = static::replace($content, array(

/*********/
/* Class */
/*********/
Expand Down Expand Up @@ -427,9 +456,9 @@ static public function parse($content)
=> $__dir,


/**************/
/* Constantes */
/**************/
/*************/
/* Constants */
/*************/
'#'.self::START.'('.self::CONSTNAME.')\s*=#'
=> '$1const $2 =',

Expand All @@ -442,6 +471,10 @@ static public function parse($content)
'#([\(;\s\.+/*=]):('.self::CONSTNAME.')#'
=> '$1static::$2',


/*************/
/* Functions */
/*************/
'#'.self::START.'<(?![\?=])#'
=> '$1return ',

Expand All @@ -454,13 +487,17 @@ static public function parse($content)
'#(?<![a-zA-Z0-9_])f°\s*(\$|use|\{|\n|$)#'
=> 'function $1',


/****************/
/* > to $this-> */
/****************/
'#([\(;\s\.+/*:+\/\*\?\&\|\!\^\~]\s*|return(?:\(\s*|\s+)|[=-]\s+)>(\$?'.self::VALIDNAME.')#'
=> '$1$this->$2',


/*************/
/* Attributs */
/*************/
/**************/
/* Attributes */
/**************/
'#'.self::START.'-\s*(('.$validComments.'\s*)*\$'.self::VALIDNAME.')#U'
=> '$1private $2',

Expand All @@ -480,9 +517,9 @@ static public function parse($content)
=> '$1static protected $2',


/************/
/* Méthodes */
/************/
/***********/
/* Methods */
/***********/
'#'.self::START.'\*\s*(('.$validComments.'\s*)*'.self::VALIDNAME.')#U'
=> '$1protected function $2',

Expand Down Expand Up @@ -518,9 +555,9 @@ static public function parse($content)
=> "break;",


/***************/
/* Assignation */
/***************/
/***********/
/* Summons */
/***********/
'#(\$.*\S)\s*\*\*=\s*('.self::VALIDNAME.')\s*\(\s*\)#U'
=> "$1 = $2($1)",

Expand All @@ -542,6 +579,10 @@ static public function parse($content)
'#(\$.*\S)(\!\!|\!|~);#U'
=> "$1 = $2$1;",


/***************/
/* Comparisons */
/***************/
'#\seq\s#'
=> " == ",

Expand Down

0 comments on commit e5b947d

Please sign in to comment.