Skip to content

Commit

Permalink
Merge pull request #2 from jxmot/20220611-refactor
Browse files Browse the repository at this point in the history
Merge refactoring changes
  • Loading branch information
jxmot authored Jun 12, 2022
2 parents 69857d2 + 2cc28a5 commit d772a4e
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sh
60 changes: 27 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# minimize-prep

This repository contains a PHP script that reads a file where HTML tags (`<script>` and `<link>`) are used for including CSS and Javascript files in a web page. As each CSS or JavaScript file tag is found the file contents are read and then appended to an appropriate output file (CSS or JS).
This repository contains a PHP script that reads a file where HTML tags (`<script>` and `<link>`) are used for including CSS and Javascript files in a web page. As each CSS or JavaScript file tag is found the file contents are read and then appended to an appropriate output file (CSS or JS).

In any `<script>` and `<link>` tags where the resource is not local those will be skipped and their contents will not be appended to output files (CSS or JS).

Expand All @@ -10,6 +10,7 @@ The result are two files that are ready for *minimization*. However, minimizatio

* `minprep.json` - script configuration
* `minprep.php` - opens the specified (*in minprep.json*) HTML file, reads it and looks for `<link>` and `<script>` tags. When found the path and file name are extracted and those files will be opened and concatenated in the specified CSS and JS files.
* `minimize-prep.php` - Contains the functions used by `minprep.php`.
* `public_html/*` - this folder contains files for use in the example

## Set Up
Expand Down Expand Up @@ -58,10 +59,10 @@ Now open `minprep.json`:
"fileroot":"./public_html/",
"cssexclude": ["cssexcl"],
"jsexclude": ["jsexcl"],
"cssout": "./site.css",
"jsout": "./site.js",
"filecomment": true,
"mkbash": true
"cssout": "site.css",
"jsout": "site.js",
"mkremove": true,
"rmvscript": "rmvresources.sh"
}
```

Expand All @@ -71,8 +72,7 @@ The remaining settings are:

* `"cssexclude": ["cssexcl"]` and `"jsexclude": ["jsexcl"]` - Each is an array of strings where each is compared against the current resource found in `./public_html/example.html`. If there is a *partial* match then that resource will be excluded from the `./site.css` or `./site.js` files.
* `"cssout": "./site.css"` and `"jsout": "./site.js"` - The path + file names of the resulting output files.
* `"filecomment": true` - For convenience, when `true` a commented line with the current resource file name will precede its contents.
* `"mkbash": true` - When `true` a file named `./rmvresources.sh` will be created. It will contain Linux `rm` commands for each of the resource files found.
* `"mkremove": true` - When `true` a file named in `"rmvscript"` will be created. It will contain Linux `rm` commands for each of the resource files found.

# Run

Expand All @@ -86,29 +86,23 @@ The script will create output similar to this:
Starting preparation...
Input: ./public_html/example.html
Files Root Path: ./public_html/
./site.css and ./site.js will be overwritten.
Creating ./rmvresources.sh file
CSS found - ./assets/css/example_1.css
CSS found - ./assets/css/example_2.css
CSS found - assets/css/example_3.css
CSS exluded - <link rel="stylesheet" href="./assets/css/cssexclude_1.css" type="text/css" />
CSS exluded - <link rel="stylesheet" href="./assets/cssexcl/exclude_1.css" type="text/css" />
JS found - /assets/js/example_1.js
JS found - ./assets/js/example_2.js
JS found - ./assets/js/example_3.js
JS found - ./assets/js/example_4.js
JS exluded - <script src="/assets/js/jsexclude_1.js" type="text/javascript"></script>
JS exluded - <script src="/assets/jsexcl/exclude_1.js" type="text/javascript"></script>
./public_html/assets/css/site.css and ./public_html/assets/js/site.js will be overwritten.
Creating rmvresources.sh file
Excluded - <link rel="stylesheet" href="https://unpkg.com/[email protected]/reset.css" />
Found - ./assets/css/example_1.css
Found - ./assets/css/example_2.css
Found - assets/css/example_3.css
Excluded - <link rel="stylesheet" href="./assets/css/cssexclude_1.css" type="text/css" />
Excluded - <link rel="stylesheet" href="./assets/cssexcl/exclude_1.css" type="text/css" />
Excluded - <script src="//code.jquery.com/jquery-3.6.0.min.js" type="text/javascript"></script>
Found - /assets/js/example_1.js
Found - ./assets/js/example_2.js
Found - ./assets/js/example_3.js
Found - ./assets/js/example_4.js
Excluded - <script src="/assets/js/jsexclude_1.js" type="text/javascript"></script>
Excluded - <script src="/assets/jsexcl/exclude_1.js" type="text/javascript"></script>
Preparation Complete.
```
Expand All @@ -120,7 +114,7 @@ The current configuration will cause 3 files to be created:
* `site.css` and `site.js`
* `rmvresources.sh` - An optional bash script file that will contain Linux `rm` commands to delete the CSS and JSS *source* files that were used to create `site.css` and `site.js`. **USE THIS SCRIPT WITH CAUTION!!!**

To disable the creation of `rmvresources.sh` edit `minprep.json` and change `"mkbash"` to `false`.
To disable the creation of `rmvresources.sh` edit `minprep.json` and change `"mkremove"` to `false`.

# Important Things to Note

Expand All @@ -138,7 +132,7 @@ This will **not** work:
-->
```

All `<link>` and `<script>` tags need to be contained on a single line, for example this will not work:
All `<link>` and `<script>` tags need to be contained on a single line, for example this will **not** work:

```
<script
Expand All @@ -148,7 +142,7 @@ src="path/to/some.js"

The paths in the `<link>` and `<script>` can be *relative* or *absolute*. The script will create a path using `"fileroot"`in the `minprep.json` file.

Each time you run `minprep.php` it will **overwrite** the `site.css` and `site.js` files. And if `"mkbash"` is `true` in `minprep.json` then a *bash script* named `rmvresources.sh` will be created. Its purpose is to make removing the original CSS and JS easier. Although removing them is not required, it is your descision for your project.
Each time you run `minprep.php` it will **overwrite** the `site.css` and `site.js` files. And if `"mkremove"` is `true` in `minprep.json` then a *bash script* named in `"rmvscript"` will be created. Its purpose is to make removing the original CSS and JS easier. Although removing them is not required, it is your descision for your project.

---
<img src="http://webexperiment.info/extcounter/mdcount.php?id=minimize-prep">
162 changes: 162 additions & 0 deletions minimize-prep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
/*
Does the line contain a <link>?
$hline - a line of text read from the input file
Returns:
> 0 - it contains a <link>, return value is position of "href="
false - it does not contain a <link>, or if it does then it's commented out
-1 - it does not contain a <link>
*/
function isLink($hline) {
$ret = -1;
// is the line commented out?
// get positions of markers...
$cbeg = strpos($hline, '<!--');
$cend = strpos($hline, '-->');
// first check...
if(strpos($hline, '<link') !== false) {
// second check...
if(strpos($hline, 'rel="stylesheet"') !== false) {
// get the href
$href = strpos($hline, 'href=');
// see if the "href=" is between comment markers
if($href === false || ($href > $cbeg) && ($href < $cend)) $ret = false;
else $ret = $href; // return position, > 0
}
}
return $ret;
}

/*
Does the line contain a <script>?
$hline - a line of text read from the input file
Returns:
> 0 - it contains a <script>, return value is position of "src="
false - it does not contain a <script>, or if it does then it's commented out
-1 - it does not contain a <script>
*/
function isScript($hline) {
$ret = -1;
// is the line commented out?
// get positions of markers...
$cbeg = strpos($hline, '<!--');
$cend = strpos($hline, '-->');

// first check...
// script?
if(strpos($hline, '<script') !== false) {
// get the src
$src = strpos($hline, 'src=');
// see if the "src=" is between comment markers
if($src === false || ($src > $cbeg) && ($src < $cend)) $ret = false;
else $ret = $src; // return position, > 0
}
return $ret;
}

/*
Determines if a line should be excluded
from further processing.
$hline - a line of text read from the input file
$exclude - an array of strings, if found in $hline then exclude
Returns:
true - exclude(skip) this line
false - process this line
*/
function isExcluded($hline, $exclude) {
$excl = false;
// <link> or <script> containing the following
// will be excluded
if((strpos($hline, 'http:') === false) &&
(strpos($hline, 'https:') === false) &&
(strpos($hline, 'site.css') === false) &&
(strpos($hline, 'site.js') === false) &&
(strpos($hline, 'site.min.css') === false) &&
(strpos($hline, 'site.min.js') === false) &&
(strpos($hline, 'jquery') === false) &&
(strpos($hline, '//') === false)) {
// check the exclusion list
if(count($exclude) > 0) {
for($ix = 0;$ix < count($exclude);$ix++) {
if(strpos($hline, $exclude[$ix]) !== false) {
// exclude this one
$excl = true;
break;
}
}
}
} else {
$excl = true;
}
return $excl;
}

/*
Get the URL out of the line, starts at an
offset and ends with a double-quote
$hline - a line of text read from the input file
$offset - starting poistion where the URL begins
Returns:
A string containing the URL of the resource
*/
function getURL($hline, $offset) {
$url = substr($hline, $offset);
$url = substr($url, 0, strpos($url, '"'));
return $url;
}

/*
Get the full path to the resource
$url - string returned from getURL()
$fileroot - path to root of resource file tree
Returns:
Full tile path to the resource
*/
function getFilePath($url, $fileroot) {
if(strpos($url, './') === 0) {
$fpath = $fileroot . substr($url, 2);
} else {
if(strpos($url, '/') === 0) {
$fpath = $fileroot . substr($url, 1);
} else {
$fpath = $fileroot . $url;
}
}
return $fpath;
}

/*
Put the contents of a resource into the
output file.
$fpath - the full path returned from getFilePath()
$fout - file handle to the output file
Returns:
true - resource content was written to the output file
false - the resource file could not be found
*/
function putContents($fpath, $fout) {
if(file_exists($fpath) === false) {
return false;
} else {
$content = file_get_contents($fpath);
fwrite($fout, $content);
fwrite($fout, "\n");
fflush($fout);
return true;
}
}
?>
8 changes: 4 additions & 4 deletions minprep.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"fileroot":"./public_html/",
"cssexclude": ["cssexcl"],
"jsexclude": ["jsexcl"],
"cssout": "site.css",
"jsout": "site.js",
"filecomment": true,
"mkbash": true
"cssout": "assets/css/site.css",
"jsout": "assets/js/site.js",
"mkremove": true,
"rmvscript": "rmvresources.sh"
}
Loading

0 comments on commit d772a4e

Please sign in to comment.