Skip to content

Commit

Permalink
Fix parsing of HTML in PHP/ASP/EJS file; support ASP, PHP, Mustache, …
Browse files Browse the repository at this point in the history
…Handlebars, EJS and Go templates for command line
  • Loading branch information
tdewolff committed Nov 29, 2023
1 parent 05a6789 commit 5b4030c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
76 changes: 54 additions & 22 deletions cmd/minify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ import (
var Version = "built from source"

var extMap = map[string]string{
"asp": "text/asp",
"css": "text/css",
"ejs": "text/x-ejs-template",
"gohtml": "text/x-go-template",
"handlebars": "text/x-handlebars-template",
"htm": "text/html",
"html": "text/html",
"js": "application/javascript",
"mjs": "application/javascript",
"json": "application/json",
"mjs": "application/javascript",
"mustache": "text/x-mustache-template",
"php": "application/x-httpd-php",
"rss": "application/rss+xml",
"svg": "image/svg+xml",
"tmpl": "text/x-go-template",
"webmanifest": "application/manifest+json",
"xhtml": "application/xhtml-xml",
"xml": "text/xml",
Expand Down Expand Up @@ -170,12 +177,12 @@ func run() int {
var output string
var siteurl string

cssMinifier := &css.Minifier{}
htmlMinifier := &html.Minifier{}
jsMinifier := &js.Minifier{}
jsonMinifier := &json.Minifier{}
svgMinifier := &svg.Minifier{}
xmlMinifier := &xml.Minifier{}
cssMinifier := css.Minifier{}
htmlMinifier := html.Minifier{}
jsMinifier := js.Minifier{}
jsonMinifier := json.Minifier{}
svgMinifier := svg.Minifier{}
xmlMinifier := xml.Minifier{}

f := argp.New("minify")
f.AddRest(&inputs, "inputs", "Input files or directories, leave blank to use stdin")
Expand Down Expand Up @@ -248,16 +255,10 @@ func run() int {
return 0
}

if len(inputs) == 0 && mimetype == "" && oldmimetype == "" {
if !quiet {
fmt.Printf("minify: must specify --type in order to use stdin and stdout\n")
fmt.Printf("Try 'minify --help' for more information\n")
}
return 1
} else if len(inputs) == 1 && inputs[0] == "-" {
inputs = inputs[:0]
if len(inputs) == 1 && inputs[0] == "-" {
inputs = inputs[:0] // stdin
} else if output == "-" {
output = ""
output = "" // stdout
}
useStdin := len(inputs) == 0

Expand Down Expand Up @@ -336,6 +337,22 @@ func run() int {
return 1
}
if mimetype == "" {
if !recursive {
okAll := true
for _, input := range inputs {
ext := filepath.Ext(input)
if 0 < len(ext) {
ext = ext[1:]
}
if _, ok := extMap[ext]; !ok {
Error.Println("cannot infer mimetype from extension in", input, ", set --type explicitly")
okAll = false
}
}
if !okAll {
return 1
}
}
Info.Println("infer mimetype from file extensions")
} else {
Info.Println("use mimetype", mimetype)
Expand Down Expand Up @@ -459,12 +476,27 @@ func run() int {
////////////////

m = min.New()
m.Add("text/css", cssMinifier)
m.Add("text/html", htmlMinifier)
m.Add("image/svg+xml", svgMinifier)
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma|j|live)script(1\\.[0-5])?$|^module$"), jsMinifier)
m.AddRegexp(regexp.MustCompile("[/+]json$"), jsonMinifier)
m.AddRegexp(regexp.MustCompile("[/+]xml$"), xmlMinifier)
m.Add("text/css", &cssMinifier)
m.Add("text/html", &htmlMinifier)
m.Add("image/svg+xml", &svgMinifier)
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma|j|live)script(1\\.[0-5])?$|^module$"), &jsMinifier)
m.AddRegexp(regexp.MustCompile("[/+]json$"), &jsonMinifier)
m.AddRegexp(regexp.MustCompile("[/+]xml$"), &xmlMinifier)

aspMinifier := htmlMinifier
aspMinifier.TemplateDelims = [2]string{"<%", "%>"}
m.Add("text/asp", &aspMinifier)
m.Add("text/x-ejs-template", &aspMinifier)

phpMinifier := htmlMinifier
phpMinifier.TemplateDelims = [2]string{"<?", "?>"} // also handles <?php
m.Add("application/x-httpd-php", &phpMinifier)

tmplMinifier := htmlMinifier
tmplMinifier.TemplateDelims = [2]string{"{{", "}}"}
m.Add("text/x-go-template", &tmplMinifier)
m.Add("text/x-mustache-template", &tmplMinifier)
m.Add("text/x-handlebars-template", &tmplMinifier)

if m.URL, err = url.Parse(siteurl); err != nil {
Error.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/fsnotify/fsnotify v1.7.0
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/tdewolff/argp v0.0.0-20231127201036-c09ac479d39f
github.com/tdewolff/argp v0.0.0-20231129210956-bb03d6873d97
github.com/tdewolff/parse/v2 v2.7.6
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/tdewolff/argp v0.0.0-20231030173501-fa6c54897951 h1:QiakK8TMHgZfKZ2en
github.com/tdewolff/argp v0.0.0-20231030173501-fa6c54897951/go.mod h1:fF+gnKbmf3iMG+ErLiF+orMU/InyZIEnKVVigUjfriw=
github.com/tdewolff/argp v0.0.0-20231127201036-c09ac479d39f h1:GsLzRgO6VYhzXBuUPFF1wfav4mB06rfuj3R5IH/DBy0=
github.com/tdewolff/argp v0.0.0-20231127201036-c09ac479d39f/go.mod h1:fF+gnKbmf3iMG+ErLiF+orMU/InyZIEnKVVigUjfriw=
github.com/tdewolff/argp v0.0.0-20231129210956-bb03d6873d97 h1:guej/op7QIuHIlsX8wrWimsTzyc3CKAgXvJsUvsoMXc=
github.com/tdewolff/argp v0.0.0-20231129210956-bb03d6873d97/go.mod h1:fF+gnKbmf3iMG+ErLiF+orMU/InyZIEnKVVigUjfriw=
github.com/tdewolff/parse/v2 v2.7.6 h1:PGZH2b/itDSye9RatReRn4GBhsT+KFEMtAMjHRuY1h8=
github.com/tdewolff/parse/v2 v2.7.6/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
Expand Down
15 changes: 15 additions & 0 deletions minify/minify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ func init() {
Default.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma|j|live)script(1\\.[0-5])?$|^module$"), js.Minify)
Default.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
Default.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

aspMinifier := &html.Minifier{}
aspMinifier.TemplateDelims = [2]string{"<%", "%>"}
Default.Add("text/asp", &aspMinifier)

Check failure on line 29 in minify/minify.go

View workflow job for this annotation

GitHub Actions / build

cannot use &aspMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 29 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &aspMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 29 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &aspMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)
Default.Add("text/x-ejs-template", &aspMinifier)

Check failure on line 30 in minify/minify.go

View workflow job for this annotation

GitHub Actions / build

cannot use &aspMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 30 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &aspMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 30 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &aspMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

phpMinifier := &html.Minifier{}
phpMinifier.TemplateDelims = [2]string{"<?", "?>"} // also handles <?php
Default.Add("application/x-httpd-php", &phpMinifier)

Check failure on line 34 in minify/minify.go

View workflow job for this annotation

GitHub Actions / build

cannot use &phpMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 34 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &phpMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

tmplMinifier := &html.Minifier{}
tmplMinifier.TemplateDelims = [2]string{"{{", "}}"}
Default.Add("text/x-go-template", &tmplMinifier)

Check failure on line 38 in minify/minify.go

View workflow job for this annotation

GitHub Actions / build

cannot use &tmplMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 38 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &tmplMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)
Default.Add("text/x-mustache-template", &tmplMinifier)

Check failure on line 39 in minify/minify.go

View workflow job for this annotation

GitHub Actions / build

cannot use &tmplMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 39 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &tmplMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)
Default.Add("text/x-handlebars-template", &tmplMinifier)

Check failure on line 40 in minify/minify.go

View workflow job for this annotation

GitHub Actions / build

cannot use &tmplMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)

Check failure on line 40 in minify/minify.go

View workflow job for this annotation

GitHub Actions / lint

cannot use &tmplMinifier (value of type **"github.com/tdewolff/minify/v2/html".Minifier) as "github.com/tdewolff/minify/v2".Minifier value in argument to Default.Add: **"github.com/tdewolff/minify/v2/html".Minifier does not implement "github.com/tdewolff/minify/v2".Minifier (missing method Minify)) (typecheck)
}

// CSS string minifier using all default minifiers
Expand Down

0 comments on commit 5b4030c

Please sign in to comment.