]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/Lines.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / min / lib / Minify / Lines.php
1 <?php
2 /**
3  * Class Minify_Lines  
4  * @package Minify
5  */
6
7 /**
8  * Add line numbers in C-style comments for easier debugging of combined content
9  *
10  * @package Minify
11  * @author Stephen Clay <steve@mrclay.org>
12  * @author Adam Pedersen (Issue 55 fix)
13  */
14 class Minify_Lines {
15
16     /**
17      * Add line numbers in C-style comments
18      *
19      * This uses a very basic parser easily fooled by comment tokens inside
20      * strings or regexes, but, otherwise, generally clean code will not be 
21      * mangled. URI rewriting can also be performed.
22      *
23      * @param string $content
24      * 
25      * @param array $options available options:
26      * 
27      * 'id': (optional) string to identify file. E.g. file name/path
28      *
29      * 'currentDir': (default null) if given, this is assumed to be the\r
30      * directory of the current CSS file. Using this, minify will rewrite\r
31      * all relative URIs in import/url declarations to correctly point to\r
32      * the desired files, and prepend a comment with debugging information about
33      * this process.
34      * 
35      * @return string 
36      */
37     public static function minify($content, $options = array()) 
38     {
39         $id = (isset($options['id']) && $options['id'])
40             ? $options['id']
41             : '';
42         $content = str_replace("\r\n", "\n", $content);
43         $lines = explode("\n", $content);
44         $numLines = count($lines);
45         // determine left padding
46         $padTo = strlen($numLines);
47         $inComment = false;
48         $i = 0;
49         $newLines = array();
50         while (null !== ($line = array_shift($lines))) {
51             if (('' !== $id) && (0 == $i % 50)) {
52                 array_push($newLines, '', "/* {$id} */", '');
53             }
54             ++$i;
55             $newLines[] = self::_addNote($line, $i, $inComment, $padTo);
56             $inComment = self::_eolInComment($line, $inComment);
57         }
58         $content = implode("\n", $newLines) . "\n";
59         
60         // check for desired URI rewriting
61         if (isset($options['currentDir'])) {
62             require_once 'Minify/CSS/UriRewriter.php';
63             Minify_CSS_UriRewriter::$debugText = '';
64             $content = Minify_CSS_UriRewriter::rewrite(
65                  $content
66                 ,$options['currentDir']
67                 ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']\r
68                 ,isset($options['symlinks']) ? $options['symlinks'] : array()
69             );
70             $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" 
71                      . Minify_CSS_UriRewriter::$debugText . "*/\n"
72                      . $content;
73         }
74         
75         return $content;
76     }
77     
78     /**
79      * Is the parser within a C-style comment at the end of this line?
80      *
81      * @param string $line current line of code
82      * 
83      * @param bool $inComment was the parser in a comment at the
84      * beginning of the line?
85      * 
86      * @return bool
87      */
88     private static function _eolInComment($line, $inComment)
89     {
90         while (strlen($line)) {
91             $search = $inComment
92                 ? '*/'
93                 : '/*';
94             $pos = strpos($line, $search);
95             if (false === $pos) {
96                 return $inComment;
97             } else {
98                 if ($pos == 0
99                     || ($inComment
100                         ? substr($line, $pos, 3)
101                         : substr($line, $pos-1, 3)) != '*/*')
102                 {
103                         $inComment = ! $inComment;
104                 }
105                 $line = substr($line, $pos + 2);
106             }
107         }
108         return $inComment;
109     }
110     
111     /**
112      * Prepend a comment (or note) to the given line
113      *
114      * @param string $line current line of code
115      *
116      * @param string $note content of note/comment
117      * 
118      * @param bool $inComment was the parser in a comment at the
119      * beginning of the line?
120      *
121      * @param int $padTo minimum width of comment
122      * 
123      * @return string
124      */
125     private static function _addNote($line, $note, $inComment, $padTo)
126     {
127         return $inComment
128             ? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line
129             : '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line;
130     }
131 }