Skip to content

Commit

Permalink
[Console] Implemented '<' escaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Jul 10, 2012
1 parent 8cf82b7 commit aaf4950
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions src/Symfony/Component/Console/Formatter/OutputFormatter.php
Expand Up @@ -23,12 +23,29 @@ class OutputFormatter implements OutputFormatterInterface
/**
* The pattern to phrase the format.
*/
const FORMAT_PATTERN = '#<(/?)([a-z][a-z0-9_=;-]+)?>([^<]*)#is';
const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>([^\\\\<]*)#is';

/**
* The escape sequence for LG char.
*/
const LG_CHAR_ESCAPING = '\\';

private $decorated;
private $styles = array();
private $styleStack;

/**
* Escapes "<" special char in given text.
*
* @param string $text Text to escape
*
* @return string Escaped text
*/
public static function escape($text)
{
return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
}

/**
* Initializes console output formatter.
*
Expand Down Expand Up @@ -135,7 +152,9 @@ public function getStyle($name)
*/
public function format($message)
{
return preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
$message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);

return str_replace(self::LG_CHAR_ESCAPING.'<', '<', $message);
}

/**
Expand All @@ -147,35 +166,40 @@ public function format($message)
*/
private function replaceStyle($match)
{
if ('' === $match[2]) {
if ('/' === $match[1]) {
// we got "\<" escaped char
if (self::LG_CHAR_ESCAPING === $match[1]) {
return $match[0];
}

if ('' === $match[3]) {
if ('/' === $match[2]) {
// we got "</>" tag
$this->styleStack->pop();

return $this->applyStyle($this->styleStack->getCurrent(), $match[3]);
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]);
}

// we got "<>" tag
return '<>'.$match[3];
return '<>'.$match[4];
}

if (isset($this->styles[strtolower($match[2])])) {
$style = $this->styles[strtolower($match[2])];
if (isset($this->styles[strtolower($match[3])])) {
$style = $this->styles[strtolower($match[3])];
} else {
$style = $this->createStyleFromString($match[2]);
$style = $this->createStyleFromString($match[3]);

if (false === $style) {
return $match[0];
}
}

if ('/' === $match[1]) {
if ('/' === $match[2]) {
$this->styleStack->pop($style);
} else {
$this->styleStack->push($style);
}

return $this->applyStyle($this->styleStack->getCurrent(), $match[3]);
return $this->applyStyle($this->styleStack->getCurrent(), $match[4]);
}

/**
Expand Down

0 comments on commit aaf4950

Please sign in to comment.