mirror of
https://github.com/mrclay/minify.git
synced 2025-08-10 08:04:14 +02:00
builder app improvements
Minify.php : + setDocRoot() for IIS (min only checks if on Windows) Controller/Page.php : + 'file' option for simpler usage
This commit is contained in:
39
min/builder/_index.html
Normal file
39
min/builder/_index.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||||
"http://www.w3.org/TR/html4/strict.dtd">
|
||||
<head>
|
||||
<title>Minify URI Builder</title>
|
||||
<style type="text/css">
|
||||
#sources {margin:0; padding:0;}
|
||||
#sources li {margin:0 0 0 40px}
|
||||
#sources li input {margin-left:2px}
|
||||
#add {margin:5px 0 1em 40px}
|
||||
#update, #uriTable {display:none}
|
||||
b {color:#c00}
|
||||
</style>
|
||||
</head>
|
||||
<h1>Minify URI Builder</h1>
|
||||
|
||||
<p>Create a list of Javascript or CSS files and click [Update] to generate
|
||||
a URI to serve them as one file through Minify.</p>
|
||||
|
||||
<ol id=sources><li></li></ol>
|
||||
<div id=add><button>Add file +</button></div>
|
||||
|
||||
<p><button id=update>Update</button></p>
|
||||
|
||||
<table id=uriTable>
|
||||
<tr><th>URI</th><td><a id=uriA onclick="this.target='_blank'">/min</a> <small>(opens in new window)</small></td></tr>
|
||||
<tr><th>HTML</th><td><input id=uriHtml type=text size=80 readonly></td></tr>
|
||||
</table>
|
||||
|
||||
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
// workaround required to test when /min isn't child of web root
|
||||
var src = location.pathname.replace(/\/[^\/]*$/, '/index.js').substr(1);
|
||||
document.write('<\script type="text/javascript" src="../?f=' + src + '"><\/script>');
|
||||
</script>
|
||||
|
||||
<!--[
|
||||
This comment remains because the "[" makes it look like an IE conditional comment
|
||||
-->
|
@@ -1,24 +0,0 @@
|
||||
<!doctype html>
|
||||
<head>
|
||||
<title>Minify URI Builder</title>
|
||||
<style type="text/css">
|
||||
#sources {margin:0; padding:0;}
|
||||
#sources li {margin:0 0 0 40px}
|
||||
#add {margin:5px 0 1em 40px}
|
||||
li span {margin:0 3px}
|
||||
</style>
|
||||
</head>
|
||||
<h1>Minify URI Builder</h1>
|
||||
|
||||
<ol id=sources></ol>
|
||||
<div id=add><a href='#'>Add file +</a></div>
|
||||
|
||||
<p><button id=update>Update</button></p>
|
||||
|
||||
<table style="display:none">
|
||||
<tr><th>URI</th><td><a id=uriA target="_blank">/min</a> <small>(opens in new window)</small></td></tr>
|
||||
<tr><th>HTML</th><td><input id=uriHtml type=text size=80 readonly></td></tr>
|
||||
</table>
|
||||
|
||||
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
|
||||
<script type="text/javascript" src="index.js"></script>
|
@@ -2,16 +2,17 @@
|
||||
var MUB = {
|
||||
_uid : 0
|
||||
,newLi : function () {
|
||||
return '<li id="li' + MUB._uid + '">http://' + location.host + '<span>/</span><input type=text size=20>'
|
||||
return '<li id="li' + MUB._uid + '">http://' + location.host + '/<input type=text size=20>'
|
||||
+ ' <button title="Remove">x</button> <button title="Include Earlier">↑</button>'
|
||||
+ ' <button title="Include Later">↓</button> <a href=# target="_blank" '
|
||||
+ 'title="Open this URL in a new window">test link</a></li>';
|
||||
+ ' <button title="Include Later">↓</button> <span></span></li>';
|
||||
}
|
||||
,addLi : function () {
|
||||
$('#sources').append(MUB.newLi());
|
||||
var li = $('#li' + MUB._uid)[0];
|
||||
$('button[title=Remove]', li).click(function () {
|
||||
var hadValue = !!$('input', li)[0].value;
|
||||
$(li).remove();
|
||||
hadValue && MUB.update();
|
||||
});
|
||||
$('button[title$=Earlier]', li).click(function () {
|
||||
$(li).prev('li').find('input').each(function () {
|
||||
@@ -19,6 +20,8 @@ var MUB = {
|
||||
var tmp = this.value;
|
||||
this.value = $('input', li).val();
|
||||
$('input', li).val(tmp);
|
||||
MUB.updateAllTestLinks();
|
||||
MUB.update();
|
||||
});
|
||||
});
|
||||
$('button[title$=Later]', li).click(function () {
|
||||
@@ -27,18 +30,83 @@ var MUB = {
|
||||
var tmp = this.value;
|
||||
this.value = $('input', li).val();
|
||||
$('input', li).val(tmp);
|
||||
MUB.updateAllTestLinks();
|
||||
MUB.update();
|
||||
});
|
||||
});
|
||||
$('input', li).keyup(function () {
|
||||
$('a', li)[0].href = '/' + this.value;
|
||||
});
|
||||
++MUB._uid;
|
||||
}
|
||||
,liUpdateTestLink : function () { // call in context of li element
|
||||
if (! $('input', this)[0].value)
|
||||
return;
|
||||
var li = this;
|
||||
$('span', this).html('');
|
||||
var url = 'http://' + location.host + '/'
|
||||
+ $('input', this)[0].value.replace(/^\//, '');
|
||||
$.ajax({
|
||||
url : url
|
||||
,complete : function (xhr, stat) {
|
||||
$('span', li).html(
|
||||
'success' == stat
|
||||
? '✓'
|
||||
: '<b>file not found!</b>'
|
||||
);
|
||||
}
|
||||
,dataType : 'text'
|
||||
});
|
||||
}
|
||||
,updateAllTestLinks : function () {
|
||||
$('#sources li').each(MUB.liUpdateTestLink);
|
||||
}
|
||||
,getCommonCharAtPos : function (arr, pos) {
|
||||
var i
|
||||
,l = arr.length
|
||||
,c = arr[0].charAt(pos);
|
||||
if (c === '' || l === 1)
|
||||
return c;
|
||||
for (i = 1; i < l; ++i)
|
||||
if (arr[i].charAt(pos) !== c)
|
||||
return '';
|
||||
return c;
|
||||
}
|
||||
,getBestUri : function (sources) {
|
||||
var pos = 0
|
||||
,base = ''
|
||||
,c;
|
||||
while (true) {
|
||||
c = MUB.getCommonCharAtPos(sources, pos);
|
||||
if (c === '')
|
||||
break;
|
||||
else
|
||||
base += c;
|
||||
++pos;
|
||||
}
|
||||
base = base.replace(/[^\/]+$/, '');
|
||||
var uri = '/min/?f=' + sources.join(',');
|
||||
if (base.charAt(base.length - 1) === '/') {
|
||||
// we have a base dir!
|
||||
var basedSources = sources
|
||||
,i
|
||||
,l = sources.length;
|
||||
for (i = 0; i < l; ++i) {
|
||||
basedSources[i] = sources[i].substr(base.length);
|
||||
}
|
||||
base = base.substr(0, base.length - 1);
|
||||
var bUri = '/min/?b=' + base + '&f=' + basedSources.join(',');
|
||||
//window.console && console.log([uri, bUri]);
|
||||
uri = uri.length < bUri.length
|
||||
? uri
|
||||
: bUri;
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
,update : function () {
|
||||
MUB.updateAllTestLinks();
|
||||
var sources = []
|
||||
,ext = false
|
||||
,fail = false;
|
||||
$('#sources input').each(function () {
|
||||
var m, val;
|
||||
if (! fail && this.value && (m = this.value.match(/\.(css|js)$/))) {
|
||||
var thisExt = m[1];
|
||||
if (ext === false)
|
||||
@@ -47,6 +115,7 @@ var MUB = {
|
||||
fail = true;
|
||||
return alert('extensions must match!');
|
||||
}
|
||||
this.value = this.value.replace(/^\//, '');
|
||||
if (-1 != $.inArray(this.value, sources)) {
|
||||
fail = true;
|
||||
return alert('duplicate file!');
|
||||
@@ -56,24 +125,28 @@ var MUB = {
|
||||
});
|
||||
if (fail || ! sources.length)
|
||||
return;
|
||||
var uri = '/min/?f=' + sources.join(',')
|
||||
,uriH = uri.replace(/</, '<').replace(/>/, '>').replace(/&/, '&');
|
||||
var uri = MUB.getBestUri(sources)
|
||||
,uriH = uri.replace(/</, '<').replace(/>/, '>').replace(/&/, '&');
|
||||
$('#uriA').html(uriH)[0].href = uri;
|
||||
$('#uriHtml').val(
|
||||
ext === 'js'
|
||||
? '<script type="text/javascript" src="' + uriH + '"></script>'
|
||||
: '<link type="text/css" rel="stylesheet" href="' + uriH + '" />'
|
||||
);
|
||||
$('table').show();
|
||||
$('#uriTable').show();
|
||||
}
|
||||
,init : function () {
|
||||
$('#add a').click(MUB.addLi);
|
||||
$('#sources').html('');
|
||||
$('#add button').click(function () {
|
||||
MUB.addLi();
|
||||
MUB.updateAllTestLinks();
|
||||
$('#update').show().click(MUB.update);
|
||||
});
|
||||
$('#uriHtml').click(function () {
|
||||
this.select();
|
||||
}).focus(function () {
|
||||
this.select();
|
||||
});
|
||||
$('#update').click(MUB.update);
|
||||
}
|
||||
};
|
||||
window.onload = MUB.init;
|
12
min/builder/index.php
Normal file
12
min/builder/index.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
set_include_path(dirname(__FILE__) . '/../lib' . PATH_SEPARATOR . get_include_path());
|
||||
|
||||
require 'Minify.php';
|
||||
|
||||
Minify::setCache();
|
||||
|
||||
Minify::serve('Page', array(
|
||||
'file' => dirname(__FILE__) . '/_index.html'
|
||||
,'minifyAll' => true
|
||||
));
|
@@ -27,6 +27,10 @@ if (! (include 'Minify.php')) {
|
||||
);
|
||||
}
|
||||
|
||||
if (0 === stripos(PHP_OS, 'win')) {
|
||||
Minify::setDocRoot(); // we may be on IIS
|
||||
}
|
||||
|
||||
Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
|
||||
|
||||
if (isset($_GET['g'])) {
|
||||
|
@@ -323,6 +323,30 @@ class Minify {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* On IIS, create $_SERVER['DOCUMENT_ROOT']
|
||||
*
|
||||
* @param bool $unsetPathInfo (default false) if true, $_SERVER['PATH_INFO']
|
||||
* will be unset (it is inconsistent with Apache's setting)
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public static function setDocRoot($unsetPathInfo = false)
|
||||
{
|
||||
if (isset($_SERVER['SERVER_SOFTWARE'])
|
||||
&& 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/')
|
||||
) {
|
||||
$_SERVER['DOCUMENT_ROOT'] = substr(
|
||||
$_SERVER['PATH_TRANSLATED']
|
||||
,0
|
||||
,strlen($_SERVER['PATH_TRANSLATED']) - strlen($_SERVER['SCRIPT_NAME'])
|
||||
);
|
||||
if ($unsetPathInfo) {
|
||||
unset($_SERVER['PATH_INFO']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Minify_Controller active controller for current request
|
||||
*/
|
||||
|
@@ -36,13 +36,18 @@ class Minify_Controller_Page extends Minify_Controller_Base {
|
||||
* @todo Add 'file' option to read HTML file.
|
||||
*/
|
||||
public function setupSources($options) {
|
||||
// strip controller options
|
||||
$sourceSpec = array(
|
||||
'content' => $options['content']
|
||||
,'id' => $options['id']
|
||||
);
|
||||
unset($options['content'], $options['id']);
|
||||
|
||||
if (isset($options['file'])) {
|
||||
$sourceSpec = array(
|
||||
'filepath' => $options['file']
|
||||
);
|
||||
} else {
|
||||
// strip controller options
|
||||
$sourceSpec = array(
|
||||
'content' => $options['content']
|
||||
,'id' => $options['id']
|
||||
);
|
||||
unset($options['content'], $options['id']);
|
||||
}
|
||||
if (isset($options['minifyAll'])) {
|
||||
// this will be the 2nd argument passed to Minify_HTML::minify()
|
||||
$sourceSpec['minifyOptions'] = array(
|
||||
|
Reference in New Issue
Block a user