Remove trailing whitespace

This commit is contained in:
Kijin Sung 2023-01-17 20:57:44 +09:00
parent 3b0030e82b
commit a9f72a5cd2
81 changed files with 2455 additions and 2455 deletions

View file

@ -12,16 +12,16 @@ class Formatter
*/
const TEXT_NEWLINE_AS_P = 1;
const TEXT_DOUBLE_NEWLINE_AS_P = 2;
/**
* Options for Markdown to HTML conversion.
*/
const MD_NEWLINE_AS_BR = 16;
const MD_ENABLE_EXTRA = 128;
/**
* Convert plain text to HTML.
*
*
* @param string $text
* @param int $options (optional)
* @return string
@ -39,7 +39,7 @@ class Formatter
}
return $result;
}
// This option uses <br> to separate lines and <p> to separate paragraphs.
if ($options & self::TEXT_DOUBLE_NEWLINE_AS_P)
{
@ -52,14 +52,14 @@ class Formatter
}
return $result;
}
// The default is to use <br> always.
return nl2br(escape(trim($text))) . "<br />\n";
}
/**
* Convert HTML to plain text.
*
*
* @param string $html
* @return string
*/
@ -69,7 +69,7 @@ class Formatter
$html = preg_replace('!<br[^>]*>\s*!i', "\n", $html);
$html = preg_replace('!<p\b[^>]*>\s*!i', '', $html);
$html = preg_replace('!</p[^>]*>\s*!i', "\n\n", $html);
// Encode links and images to preserve essential information.
$html = preg_replace_callback('!<a\b[^>]*href="([^>"]+)"[^>]*>([^<]*)</a>!i', function($matches) {
return trim($matches[2] . ' &lt;' . $matches[1] . '&gt;');
@ -79,20 +79,20 @@ class Formatter
$title = $title ?: (preg_match('!alt="([^>"]+)"!i', $matches[0], $m) ? $m[1] : 'IMAGE');
return trim('[' . $title . '] &lt;' . $matches[1] . '&gt;');
}, $html);
// Strip all other HTML.
$text = html_entity_decode(strip_tags($html));
unset($html);
// Normalize whitespace and return.
$text = str_replace("\r\n", "\n", $text);
$text = preg_replace('/\n(?:\s*\n)+/', "\n\n", $text);
return trim($text) . "\n";
}
/**
* Convert Markdown to HTML.
*
*
* @param string $markdown
* @param int $options (optional)
* @return string
@ -110,19 +110,19 @@ class Formatter
$classes = false;
$parser = new \Michelf\Markdown;
}
if ($options & self::MD_NEWLINE_AS_BR)
{
$parser->hard_wrap = true;
}
$html = $parser->transform($markdown);
return Filters\HTMLFilter::clean($html, $classes);
}
/**
* Convert HTML to Markdown.
*
*
* @param string $html
* @return string
*/
@ -133,10 +133,10 @@ class Formatter
$converter->getConfig()->setOption('strip_tags', true);
return trim($converter->convert($html)) . "\n";
}
/**
* Convert BBCode to HTML.
*
*
* @param string $bbcode
* @return string
*/
@ -144,21 +144,21 @@ class Formatter
{
$parser = new \JBBCode\Parser;
$parser->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet());
$builder = new \JBBCode\CodeDefinitionBuilder('quote', '<blockquote>{param}</blockquote>');
$parser->addCodeDefinition($builder->build());
$builder = new \JBBCode\CodeDefinitionBuilder('code', '<pre><code>{param}</code></pre>');
$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
*/
@ -166,10 +166,10 @@ class Formatter
{
return \Michelf\SmartyPants::defaultTransform($html, 'qbBdDiew');
}
/**
* Compile LESS into CSS.
*
*
* @param string|array $source_filename
* @param string $target_filename
* @param array $variables (optional)
@ -181,7 +181,7 @@ class Formatter
// Get the cleaned and concatenated content.
$imported_list = [];
$content = self::concatCSS($source_filename, $target_filename, true, $imported_list);
// Compile!
try
{
@ -192,7 +192,7 @@ class Formatter
{
$less_compiler->setVariables($variables);
}
$content = $less_compiler->compile($content) . "\n";
$content = strpos($content, '@charset') === false ? ('@charset "UTF-8";' . "\n" . $content) : $content;
$result = true;
@ -204,20 +204,20 @@ class Formatter
$content = sprintf("/*\n Error while compiling %s\n\n %s\n*/\n", $filename, $message);
$result = false;
}
// Save the result to the target file.
Storage::write($target_filename, $content);
// Save the list of imported files.
Storage::writePHPData(preg_replace('/\.css$/', '.imports.php', $target_filename), $imported_list, null, false);
// Also return the compiled CSS content.
return $result;
}
/**
* Compile SCSS into CSS.
*
*
* @param string|array $source_filename
* @param string $target_filename
* @param array $variables (optional)
@ -229,7 +229,7 @@ class Formatter
// Get the cleaned and concatenated content.
$imported_list = [];
$content = self::concatCSS($source_filename, $target_filename, true, $imported_list);
// Compile!
try
{
@ -240,7 +240,7 @@ class Formatter
{
$scss_compiler->addVariables(array_map('\ScssPhp\ScssPhp\ValueConverter::parseValue', $variables));
}
$content = $scss_compiler->compileString($content)->getCss() . "\n";
$content = strpos($content, '@charset') === false ? ('@charset "UTF-8";' . "\n" . $content) : $content;
$result = true;
@ -252,20 +252,20 @@ class Formatter
$content = sprintf("/*\n Error while compiling %s\n\n %s\n*/\n", $filename, $message);
$result = false;
}
// Save the result to the target file.
Storage::write($target_filename, $content);
// Save the list of imported files.
Storage::writePHPData(preg_replace('/\.css$/', '.imports.php', $target_filename), $imported_list, null, false);
// Also return the compiled CSS content.
return $result;
}
/**
* Minify CSS.
*
*
* @param string|array $source_filename
* @param string $target_filename
* @return bool
@ -290,10 +290,10 @@ class Formatter
Storage::write($target_filename, $content);
return strlen($content) ? true : false;
}
/**
* Minify JS.
*
*
* @param string|array $source_filename
* @param string $target_filename
* @return bool
@ -316,10 +316,10 @@ class Formatter
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
@ -331,12 +331,12 @@ class Formatter
$charsets = [];
$imported_urls = [];
$result = '';
if (!is_array($source_filename))
{
$source_filename = array($source_filename);
}
foreach ($source_filename as $filename)
{
// Get the media query.
@ -348,10 +348,10 @@ class Formatter
{
$media = null;
}
// Clean the content.
$content = utf8_clean(file_get_contents($filename));
// Convert all paths in LESS and SCSS imports, too.
$dirname = dirname($filename);
$import_type = ends_with('.scss', $filename) ? 'scss' : 'normal';
@ -421,7 +421,7 @@ class Formatter
}
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) {
@ -436,19 +436,19 @@ class Formatter
}
}, $content);
unset($path_converter);
// Extract all @charset declarations.
$content = preg_replace_callback('/@charset\s+(["\'a-z0-9_-]+);[\r\n]*/i', function($matches) use (&$charsets) {
$charsets[] = trim($matches[1], '"\'');
return '';
}, $content);
// 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)
@ -460,7 +460,7 @@ class Formatter
$result .= trim($content) . "\n\n";
}
}
// Place all @charset and @import statements at the beginning.
if (count($imported_urls))
{
@ -474,13 +474,13 @@ class Formatter
$charset = '@charset "' . escape_dqstr(array_first($charsets)) . '";';
$result = $charset . "\n" . $result;
}
return $result;
}
/**
* JS concatenation subroutine.
*
*
* @param string|array $source_filename
* @param string $target_filename
* @return string
@ -488,12 +488,12 @@ class Formatter
public static function concatJS($source_filename, $target_filename)
{
$result = '';
if (!is_array($source_filename))
{
$source_filename = array($source_filename);
}
foreach ($source_filename as $filename)
{
// Handle the array format, previously used for the targetIE attribute.
@ -501,21 +501,21 @@ class Formatter
{
$filename = reset($filename);
}
// Clean the content.
$content = utf8_clean(file_get_contents($filename));
// 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.
*
*
* @deprecated
* @param string $condition
* @return string