instead of
to separate lines.
if ($options & self::TEXT_NEWLINE_AS_P)
{
$lines = array_map('trim', explode("\n", escape(trim($text))));
$result = '';
foreach ($lines as $line)
{
$result .= "
$line
\n"; } return $result; } // This option uses to separate paragraphs.
if ($options & self::TEXT_DOUBLE_NEWLINE_AS_P)
{
$lines = preg_replace('!(
)+\s*$!', '', nl2br(escape(trim($text))));
$lines = preg_split('!(
\s*)+
!', $lines);
$result = '';
foreach ($lines as $line)
{
$result .= "
\n" . trim($line) . "\n
\n"; } return $result; } // The default is to use tags.
$html = preg_replace('!
]*>\s*!i', "\n", $html);
$html = preg_replace('!
]*>\s*!i', '', $html); $html = preg_replace('!
]*>\s*!i', "\n\n", $html); // Encode links and images to preserve essential information. $html = preg_replace_callback('!]*href="([^>"]+)"[^>]*>([^<]*)!i', function($matches) { return trim($matches[2] . ' <' . $matches[1] . '>'); }, $html); $html = preg_replace_callback('!{param}'); $parser->addCodeDefinition($builder->build()); $builder = new \JBBCode\CodeDefinitionBuilder('code', '
{param}');
$builder->setParseContent(false);
$parser->addCodeDefinition($builder->build());
$parser->parse($bbcode);
$html = $parser->getAsHtml();
return Filters\HTMLFilter::clean($html);
}
/**
* Apply smart quotes and other stylistic enhancements to HTML.
*
* @param string $html
* @return string
*/
public static function applySmartQuotes($html)
{
return \Michelf\SmartyPants::defaultTransform($html, 'qbBdDiew');
}
/**
* Compile LESS into CSS.
*
* @param string|array $source_filename
* @param string $target_filename
* @param array $variables (optional)
* @parsm bool $minify (optional)
* @return bool
*/
public static function compileLESS($source_filename, $target_filename, $variables = array(), $minify = false)
{
// Get the cleaned and concatenated content.
$content = self::concatCSS($source_filename, $target_filename);
// Compile!
try
{
$less_compiler = new \lessc;
$less_compiler->setFormatter($minify ? 'compressed' : 'lessjs');
$less_compiler->setImportDir(array(dirname(is_array($source_filename) ? array_first($source_filename) : $source_filename)));
if ($variables)
{
$less_compiler->setVariables($variables);
}
$content = $less_compiler->compile($content) . "\n";
$content = strpos($content, '@charset') === false ? ('@charset "UTF-8";' . "\n" . $content) : $content;
$result = true;
}
catch (\Exception $e)
{
$content = '/*' . "\n" . 'Error while compiling LESS:' . "\n" . $e->getMessage() . "\n" . '*/' . "\n";
$result = false;
}
// Save the result to the target file.
Storage::write($target_filename, $content);
return $result;
}
/**
* Compile SCSS into CSS.
*
* @param string|array $source_filename
* @param string $target_filename
* @param array $variables (optional)
* @parsm bool $minify (optional)
* @return bool
*/
public static function compileSCSS($source_filename, $target_filename, $variables = array(), $minify = false)
{
// Get the cleaned and concatenated content.
$content = self::concatCSS($source_filename, $target_filename);
// Compile!
try
{
$scss_compiler = new \ScssPhp\ScssPhp\Compiler;
$scss_compiler->setOutputStyle($minify ? \ScssPhp\ScssPhp\OutputStyle::COMPRESSED : \ScssPhp\ScssPhp\OutputStyle::EXPANDED);
$scss_compiler->setImportPaths(array(dirname(is_array($source_filename) ? array_first($source_filename) : $source_filename)));
if ($variables)
{
$scss_compiler->setVariables($variables);
}
$content = $scss_compiler->compile($content) . "\n";
$content = strpos($content, '@charset') === false ? ('@charset "UTF-8";' . "\n" . $content) : $content;
$result = true;
}
catch (\Exception $e)
{
$content = '/*' . "\n" . 'Error while compiling SCSS:' . "\n" . $e->getMessage() . "\n" . '*/' . "\n";
$result = false;
}
// Save the result to the target file.
Storage::write($target_filename, $content);
return $result;
}
/**
* Minify CSS.
*
* @param string|array $source_filename
* @param string $target_filename
* @return bool
*/
public static function minifyCSS($source_filename, $target_filename)
{
$minifier = new \MatthiasMullie\Minify\CSS();
$minifier->setMaxImportSize(5);
$minifier->setImportExtensions(['svg' => 'data:image/svg+xml']);
if (is_array($source_filename))
{
foreach ($source_filename as $filename)
{
$minifier->add($filename);
}
}
else
{
$minifier->add($source_filename);
}
$content = $minifier->execute($target_filename);
Storage::write($target_filename, $content);
return strlen($content) ? true : false;
}
/**
* Minify JS.
*
* @param string|array $source_filename
* @param string $target_filename
* @return bool
*/
public static function minifyJS($source_filename, $target_filename)
{
$minifier = new \MatthiasMullie\Minify\JS();
if (is_array($source_filename))
{
foreach ($source_filename as $filename)
{
$minifier->add($filename);
}
}
else
{
$minifier->add($source_filename);
}
$content = $minifier->execute($target_filename);
Storage::write($target_filename, $content);
return strlen($content) ? true : false;
}
/**
* CSS concatenation subroutine for compileLESS() and compileSCSS().
*
* @param string|array $source_filename
* @param string $target_filename
* @param bool $add_comment
* @return string
*/
public static function concatCSS($source_filename, $target_filename, $add_comment = true)
{
$result = '';
if (!is_array($source_filename))
{
$source_filename = array($source_filename);
}
foreach ($source_filename as $filename)
{
// Get the media query.
if (is_array($filename) && count($filename) >= 2)
{
list($filename, $media) = $filename;
}
else
{
$media = null;
}
// Clean the content.
$content = utf8_clean(file_get_contents($filename));
// Convert all paths in LESS and SCSS imports, too.
$import_type = ends_with('.scss', $filename) ? 'scss' : 'normal';
$content = preg_replace_callback('/@import\s+(?:\\([^()]+\\))?([^;]+);/', function($matches) use($filename, $target_filename, $import_type) {
$import_content = '';
$import_files = array_map(function($str) use($filename, $import_type) {
$str = trim(trim(trim(preg_replace('/^url\\(([^()]+)\\)$/', '$1', trim($str))), '"\''));
if ($import_type === 'scss')
{
if (($dirpos = strrpos($str, '/')) !== false)
{
$basename = substr($str, $dirpos + 1);
if (!ends_with('.scss', $basename))
{
$basename = '_' . $basename . '.scss';
}
return dirname($filename) . '/' . substr($str, 0, $dirpos) . '/' . $basename;
}
else
{
$basename = $str;
if (!ends_with('.scss', $basename))
{
$basename = '_' . $basename . '.scss';
}
return dirname($filename) . '/' . $basename;
}
}
else
{
return dirname($filename) . '/' . $str;
}
}, explode(',', $matches[1]));
foreach ($import_files as $import_filename)
{
if (file_exists($import_filename))
{
$import_content .= self::concatCSS($import_filename, $target_filename, false);
}
}
return trim($import_content);
}, $content);
// Convert all paths to be relative to the new filename.
$path_converter = new \MatthiasMullie\PathConverter\Converter($filename, $target_filename);
$content = preg_replace_callback('/\burl\\(([^)]+)\\)/iU', function($matches) use ($path_converter) {
$url = trim($matches[1], '\'"');
if (!strlen($url) || $url[0] === '/' || preg_match('#^(?:https?|data):#', $url))
{
return $matches[0];
}
else
{
return 'url("' . str_replace('\\$', '$', escape_dqstr($path_converter->convert($url))) . '")';
}
}, $content);
unset($path_converter);
// Wrap the content in a media query if there is one.
if ($media !== null)
{
$content = "@media $media {\n\n" . trim($content) . "\n\n}";
}
// Append to the result string.
$original_filename = starts_with(\RX_BASEDIR, $filename) ? substr($filename, strlen(\RX_BASEDIR)) : $filename;
if ($add_comment)
{
$result .= '/* Original file: ' . $original_filename . ' */' . "\n\n" . trim($content) . "\n\n";
}
else
{
$result .= trim($content) . "\n\n";
}
}
return $result;
}
/**
* JS concatenation subroutine.
*
* @param string|array $source_filename
* @param string $target_filename
* @return string
*/
public static function concatJS($source_filename, $target_filename)
{
$result = '';
if (!is_array($source_filename))
{
$source_filename = array($source_filename);
}
foreach ($source_filename as $filename)
{
// Get the IE condition.
if (is_array($filename) && count($filename) >= 2)
{
list($filename, $targetie) = $filename;
}
else
{
$targetie = null;
}
// Clean the content.
$content = utf8_clean(file_get_contents($filename));
// Wrap the content in an IE condition if there is one.
if ($targetie !== null)
{
$content = 'if (' . self::convertIECondition($targetie) . ') {' . "\n\n" . trim($content) . ";\n\n" . '}';
}
// Append to the result string.
$original_filename = starts_with(\RX_BASEDIR, $filename) ? substr($filename, strlen(\RX_BASEDIR)) : $filename;
$result .= '/* Original file: ' . $original_filename . ' */' . "\n\n" . trim($content) . ";\n\n";
}
return $result;
}
/**
* Convert IE conditional comments to JS conditions.
*
* @param string $condition
* @return string
*/
public static function convertIECondition($condition)
{
$conversions = array(
'/^true$/i' => 'true',
'/^false$/i' => 'false',
'/^IE$/i' => 'window.navigator.userAgent.match(/MSIE\s/)',
'/^IE\s*(\d+)$/i' => '(/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] == %d)',
'/^gt IE\s*(\d+)$/i' => '(/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] > %d)',
'/^gte IE\s*(\d+)$/i' => '(/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] >= %d)',
'/^lt IE\s*(\d+)$/i' => '(/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] < %d)',
'/^lte IE\s*(\d+)$/i' => '(/MSIE (\d+)/.exec(window.navigator.userAgent) && /MSIE (\d+)/.exec(window.navigator.userAgent)[1] <= %d)',
);
$result = array();
$conditions = preg_split('/([\&\|])/', $condition, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
foreach ($conditions as $condition)
{
$condition = trim(preg_replace('/[\(\)]/', '', $condition));
if ($condition === '')
{
continue;
}
if ($condition === '&' || $condition === '|')
{
$result[] = $condition . $condition;
continue;
}
$negation = $condition[0] === '!';
if ($negation)
{
$condition = trim(substr($condition, 1));
}
foreach ($conversions as $regexp => $replacement)
{
if (preg_match($regexp, $condition, $matches))
{
if (count($matches) > 1)
{
array_shift($matches);
$result[] = ($negation ? '!' : '') . vsprintf($replacement, $matches);
}
else
{
$result[] = ($negation ? '!' : '') . $replacement;
}
break;
}
}
}
return count($result) ? implode(' ', $result) : 'false';
}
}