summaryrefslogtreecommitdiff
path: root/inc/lib/minify/Minify/Lines.php
diff options
context:
space:
mode:
authorMichael Save <savetheinternet@tinyboard.org>2012-04-09 20:52:26 +1000
committerMichael Save <savetheinternet@tinyboard.org>2012-04-09 20:52:26 +1000
commit874b8cbf002e9d8bf0ff39671e1501165016320a (patch)
tree1428acec23fef6c607e10fd355d88af31c2029ed /inc/lib/minify/Minify/Lines.php
parent69c9a9dd452299e0e8f7335966a1d0b4036df583 (diff)
inc/contrib -> inc/lib
Diffstat (limited to 'inc/lib/minify/Minify/Lines.php')
-rwxr-xr-xinc/lib/minify/Minify/Lines.php137
1 files changed, 137 insertions, 0 deletions
diff --git a/inc/lib/minify/Minify/Lines.php b/inc/lib/minify/Minify/Lines.php
new file mode 100755
index 00000000..ca8afa1b
--- /dev/null
+++ b/inc/lib/minify/Minify/Lines.php
@@ -0,0 +1,137 @@
+<?php
+/**
+ * Class Minify_Lines
+ * @package Minify
+ */
+
+/**
+ * Add line numbers in C-style comments for easier debugging of combined content
+ *
+ * @package Minify
+ * @author Stephen Clay <steve@mrclay.org>
+ * @author Adam Pedersen (Issue 55 fix)
+ */
+class Minify_Lines {
+
+ /**
+ * Add line numbers in C-style comments
+ *
+ * This uses a very basic parser easily fooled by comment tokens inside
+ * strings or regexes, but, otherwise, generally clean code will not be
+ * mangled. URI rewriting can also be performed.
+ *
+ * @param string $content
+ *
+ * @param array $options available options:
+ *
+ * 'id': (optional) string to identify file. E.g. file name/path
+ *
+ * 'currentDir': (default null) if given, this is assumed to be the
+ * directory of the current CSS file. Using this, minify will rewrite
+ * all relative URIs in import/url declarations to correctly point to
+ * the desired files, and prepend a comment with debugging information about
+ * this process.
+ *
+ * @return string
+ */
+ public static function minify($content, $options = array())
+ {
+ $id = (isset($options['id']) && $options['id'])
+ ? $options['id']
+ : '';
+ $content = str_replace("\r\n", "\n", $content);
+
+ // Hackily rewrite strings with XPath expressions that are
+ // likely to throw off our dumb parser (for Prototype 1.6.1).
+ $content = str_replace('"/*"', '"/"+"*"', $content);
+ $content = preg_replace('@([\'"])(\\.?//?)\\*@', '$1$2$1+$1*', $content);
+
+ $lines = explode("\n", $content);
+ $numLines = count($lines);
+ // determine left padding
+ $padTo = strlen((string) $numLines); // e.g. 103 lines = 3 digits
+ $inComment = false;
+ $i = 0;
+ $newLines = array();
+ while (null !== ($line = array_shift($lines))) {
+ if (('' !== $id) && (0 == $i % 50)) {
+ array_push($newLines, '', "/* {$id} */", '');
+ }
+ ++$i;
+ $newLines[] = self::_addNote($line, $i, $inComment, $padTo);
+ $inComment = self::_eolInComment($line, $inComment);
+ }
+ $content = implode("\n", $newLines) . "\n";
+
+ // check for desired URI rewriting
+ if (isset($options['currentDir'])) {
+ require_once 'Minify/CSS/UriRewriter.php';
+ Minify_CSS_UriRewriter::$debugText = '';
+ $content = Minify_CSS_UriRewriter::rewrite(
+ $content
+ ,$options['currentDir']
+ ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
+ ,isset($options['symlinks']) ? $options['symlinks'] : array()
+ );
+ $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n"
+ . Minify_CSS_UriRewriter::$debugText . "*/\n"
+ . $content;
+ }
+
+ return $content;
+ }
+
+ /**
+ * Is the parser within a C-style comment at the end of this line?
+ *
+ * @param string $line current line of code
+ *
+ * @param bool $inComment was the parser in a comment at the
+ * beginning of the line?
+ *
+ * @return bool
+ */
+ private static function _eolInComment($line, $inComment)
+ {
+ while (strlen($line)) {
+ $search = $inComment
+ ? '*/'
+ : '/*';
+ $pos = strpos($line, $search);
+ if (false === $pos) {
+ return $inComment;
+ } else {
+ if ($pos == 0
+ || ($inComment
+ ? substr($line, $pos, 3)
+ : substr($line, $pos-1, 3)) != '*/*')
+ {
+ $inComment = ! $inComment;
+ }
+ $line = substr($line, $pos + 2);
+ }
+ }
+ return $inComment;
+ }
+
+ /**
+ * Prepend a comment (or note) to the given line
+ *
+ * @param string $line current line of code
+ *
+ * @param string $note content of note/comment
+ *
+ * @param bool $inComment was the parser in a comment at the
+ * beginning of the line?
+ *
+ * @param int $padTo minimum width of comment
+ *
+ * @return string
+ */
+ private static function _addNote($line, $note, $inComment, $padTo)
+ {
+ return $inComment
+ ? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line
+ : '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line;
+ }
+}