From 62bf74f8717cd65a4f5bd7a17bde0c4da7e926c1 Mon Sep 17 00:00:00 2001 From: FuXiaoHei Date: Fri, 6 May 2022 15:25:42 +0800 Subject: [PATCH] feat: move minifier to ext/ --- pkg/core/generator/context.go | 21 -------------------- pkg/core/generator/output.go | 3 ++- pkg/ext/ext.go | 6 ++++++ pkg/ext/markdown/minify.go | 37 +++++++++++++++++++++++++++++++++++ version | 2 +- 5 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 pkg/ext/markdown/minify.go diff --git a/pkg/core/generator/context.go b/pkg/core/generator/context.go index 375e82b..6b25560 100644 --- a/pkg/core/generator/context.go +++ b/pkg/core/generator/context.go @@ -11,8 +11,6 @@ import ( "sort" "sync" - "github.com/tdewolff/minify/v2" - "github.com/tdewolff/minify/v2/html" "go.uber.org/atomic" ) @@ -26,8 +24,6 @@ type Context struct { postSlugTemplate *template.Template tagLinkTemplate *template.Template - - minifier *minify.M } func NewContext(s *SiteData, opt *Option) *Context { @@ -167,20 +163,3 @@ func (ctx *Context) incrOutputCounter(delta int64) int64 { func (ctx *Context) appendCopyDir(srcDir, dstDir string) { ctx.copingDirs = append(ctx.copingDirs, &models.CopyDir{SrcDir: srcDir, DestDir: dstDir}) } - -func (ctx *Context) MinifyHTML(raw []byte) ([]byte, error) { - if ctx.minifier == nil { - m := minify.New() - m.Add("text/html", &html.Minifier{ - KeepComments: false, - KeepConditionalComments: true, - KeepDefaultAttrVals: true, - KeepDocumentTags: true, - KeepEndTags: false, - KeepQuotes: true, - KeepWhitespace: false, - }) - ctx.minifier = m - } - return ctx.minifier.Bytes("text/html", raw) -} diff --git a/pkg/core/generator/output.go b/pkg/core/generator/output.go index a0a22d3..521cd9b 100644 --- a/pkg/core/generator/output.go +++ b/pkg/core/generator/output.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "pugo/pkg/core/theme" + "pugo/pkg/ext/markdown" "pugo/pkg/utils" "pugo/pkg/utils/zlog" ) @@ -48,7 +49,7 @@ func outputFiles(s *SiteData, ctx *Context) error { data := buf.Bytes() dataLen := len(data) if s.BuildConfig.EnableMinifyHTML { - data, err = ctx.MinifyHTML(data) + data, err = markdown.MinifyHTML(data) if err != nil { zlog.Warnf("output: failed to minify: %s, %s", fpath, err) data = buf.Bytes() diff --git a/pkg/ext/ext.go b/pkg/ext/ext.go index 1607f0b..3d1d1a2 100644 --- a/pkg/ext/ext.go +++ b/pkg/ext/ext.go @@ -2,11 +2,17 @@ package ext import ( "pugo/pkg/core/configs" + "pugo/pkg/ext/markdown" "pugo/pkg/ext/sitemap" "pugo/pkg/utils/zlog" ) func Reload(cfg *configs.Config) { + markdown.InitMinfier(cfg.Build.EnableMinifyHTML) + if cfg.Build.EnableMinifyHTML { + zlog.Debugf("minify html: enabled") + } + ext := cfg.Extension if ext.Feed != nil { zlog.Debugf("feed reloaded, enabled:%v", ext.Feed.Enabled) diff --git a/pkg/ext/markdown/minify.go b/pkg/ext/markdown/minify.go new file mode 100644 index 0000000..c72d3dd --- /dev/null +++ b/pkg/ext/markdown/minify.go @@ -0,0 +1,37 @@ +package markdown + +import ( + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/minify/v2/html" +) + +var ( + globalMinifier *minify.M = nil +) + +// InitMinfier initializes minifier +func InitMinfier(flag bool) { + if flag { + m := minify.New() + m.Add("text/html", &html.Minifier{ + KeepComments: false, + KeepConditionalComments: true, + KeepDefaultAttrVals: true, + KeepDocumentTags: true, + KeepEndTags: false, + KeepQuotes: true, + KeepWhitespace: false, + }) + globalMinifier = m + } else { + globalMinifier = nil + } +} + +// MinifyHTML minifies HTML +func MinifyHTML(raw []byte) ([]byte, error) { + if globalMinifier == nil { + return raw, nil + } + return globalMinifier.Bytes("text/html", raw) +} diff --git a/version b/version index f1f1232..99a25fc 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.1.14 \ No newline at end of file +0.1.16 \ No newline at end of file