forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement --prettify parameter (only supports HTML for now).
This is similar to --minify, but has essentially the opposite effect: instead of making the output less readable it makes it *more* readable. This uses github.com/yosssi/gohtml to perform the actual transformation. Fixes gohugoio#7190
- Loading branch information
Showing
10 changed files
with
473 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2019 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prettifiers | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"github.com/gohugoio/hugo/common/maps" | ||
"github.com/gohugoio/hugo/config" | ||
"github.com/gohugoio/hugo/docshelper" | ||
"github.com/gohugoio/hugo/parser" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/yosssi/gohtml" | ||
) | ||
|
||
type prettifyConfig struct { | ||
// Whether to prettify the published output (the HTML written to /public). | ||
PrettifyOutput bool | ||
|
||
DisableHTML bool | ||
|
||
HTML htmlConfig | ||
} | ||
|
||
type htmlConfig struct { | ||
Condense bool | ||
InlineTags []string | ||
InlineTagMaxLength int | ||
} | ||
|
||
var defaultConfig = prettifyConfig{ | ||
HTML: htmlConfig{ | ||
// Copy the defaults of gohtml | ||
Condense: gohtml.Condense, | ||
InlineTags: boolSetToSlice(gohtml.InlineTags), | ||
InlineTagMaxLength: gohtml.InlineTagMaxLength, | ||
}, | ||
} | ||
|
||
func decodeConfig(cfg config.Provider) (conf prettifyConfig, err error) { | ||
conf = defaultConfig | ||
|
||
// May be set by CLI. | ||
conf.PrettifyOutput = cfg.GetBool("prettifyOutput") | ||
|
||
v := cfg.Get("prettify") | ||
if v == nil { | ||
return | ||
} | ||
|
||
m := maps.ToStringMap(v) | ||
|
||
err = mapstructure.WeakDecode(m, &conf) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
// Set some global properties for the HTML formatter | ||
gohtml.Condense = conf.HTML.Condense | ||
gohtml.InlineTags = sliceToBoolSet(conf.HTML.InlineTags) | ||
gohtml.InlineTagMaxLength = conf.HTML.InlineTagMaxLength | ||
|
||
return | ||
} | ||
|
||
// boolSetToSlice converts a map[string]bool to a sorted list of keys. | ||
func boolSetToSlice(set map[string]bool) []string { | ||
slice := make([]string, 0, len(set)) | ||
for tag, isShort := range set { | ||
if isShort { | ||
slice = append(slice, tag) | ||
} | ||
} | ||
sort.Strings(slice) // Ensure consistent ordering | ||
return slice | ||
} | ||
|
||
// sliceToBoolSet converts a list of strings to a map[string]bool mapping the items in the list to true. | ||
func sliceToBoolSet(items []string) map[string]bool { | ||
set := make(map[string]bool) | ||
for _, tag := range items { | ||
set[strings.ToLower(tag)] = true | ||
} | ||
return set | ||
} | ||
|
||
func init() { | ||
docsProvider := func() docshelper.DocProvider { | ||
return docshelper.DocProvider{"config": map[string]interface{}{"prettify": parser.LowerCaseCamelJSONMarshaller{Value: defaultConfig}}} | ||
} | ||
docshelper.AddDocProviderFunc(docsProvider) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2019 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package prettifiers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/yosssi/gohtml" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
c := qt.New(t) | ||
v := viper.New() | ||
|
||
v.Set("prettifyOutput", true) | ||
v.Set("prettify", map[string]interface{}{ | ||
"disableHTML": true, | ||
}) | ||
|
||
conf, err := decodeConfig(v) | ||
|
||
c.Assert(err, qt.IsNil) | ||
|
||
c.Assert(conf.PrettifyOutput, qt.Equals, true) | ||
|
||
// `enable` flags | ||
c.Assert(conf.DisableHTML, qt.Equals, true) | ||
} | ||
|
||
func TestConfigCondensedHTML(t *testing.T) { testHTMLCondense(t, true) } | ||
func TestConfigUncondensedHTML(t *testing.T) { testHTMLCondense(t, false) } | ||
|
||
func testHTMLCondense(t *testing.T, condense bool) { | ||
c := qt.New(t) | ||
v := viper.New() | ||
|
||
v.Set("prettify", map[string]interface{}{ | ||
"html": map[string]interface{}{ | ||
"condense": condense, | ||
}, | ||
}) | ||
|
||
conf, err := decodeConfig(v) | ||
|
||
c.Assert(err, qt.IsNil) | ||
|
||
c.Assert(conf.HTML.Condense, qt.Equals, condense) | ||
c.Assert(gohtml.Condense, qt.Equals, condense) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright 2018 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package prettifiers contains prettifiers mapped to MIME types. This package is used | ||
// in the publishing chain. | ||
package prettifiers | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/gohugoio/hugo/config" | ||
"github.com/gohugoio/hugo/media" | ||
"github.com/gohugoio/hugo/output" | ||
"github.com/gohugoio/hugo/transform" | ||
|
||
"github.com/yosssi/gohtml" | ||
) | ||
|
||
// Client wraps a prettifier. | ||
type Client struct { | ||
// Whether output prettifying is enabled (HTML in /public) | ||
PrettifyOutput bool | ||
|
||
prettifiers map[string]prettifier | ||
} | ||
|
||
type prettifier func(input []byte, dst io.Writer) error | ||
|
||
// Transformer returns a func that can be used in the transformer publishing chain. | ||
func (m Client) Transformer(mediatype media.Type) transform.Transformer { | ||
if !m.PrettifyOutput { | ||
return nil | ||
} | ||
prettifier := m.prettifiers[mediatype.Type()] | ||
if prettifier == nil { | ||
return nil | ||
} | ||
return func(ft transform.FromTo) error { | ||
return prettifier(ft.From().Bytes(), ft.To()) | ||
} | ||
} | ||
|
||
// Prettify tries to prettify the src into dst given a MIME type. | ||
func (m Client) Prettify(mediatype media.Type, dst io.Writer, src io.Reader) error { | ||
prettifier := m.prettifiers[mediatype.Type()] | ||
if prettifier == nil { | ||
// No supported prettifier. Just pass it through. | ||
_, err := io.Copy(dst, src) | ||
return err | ||
} | ||
|
||
var w = gohtml.NewWriter(dst) | ||
_, err := io.Copy(w, src) | ||
return err | ||
} | ||
|
||
// New creates a new Client with the provided MIME types as the mapping foundation. | ||
// The HTML prettifier is also registered for additional HTML types (AMP etc.) in the | ||
// provided list of output formats. | ||
func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provider) (Client, error) { | ||
conf, err := decodeConfig(cfg) | ||
|
||
if err != nil { | ||
return Client{}, err | ||
} | ||
|
||
client := Client{ | ||
PrettifyOutput: conf.PrettifyOutput, | ||
prettifiers: make(map[string]prettifier), | ||
} | ||
|
||
// We use the Type definition of the media types defined in the site if found. | ||
|
||
// TODO: implement other media types (see ../minifiers/minifiers.go) | ||
|
||
// HTML | ||
if !conf.DisableHTML { | ||
for _, of := range outputFormats { | ||
if of.IsHTML { | ||
client.prettifiers[of.MediaType.Type()] = formatHTML | ||
} | ||
} | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
func formatHTML(input []byte, w io.Writer) error { | ||
prettified := gohtml.FormatBytes(input) | ||
|
||
n, err := w.Write(prettified) | ||
if err == nil && n != len(prettified) { | ||
err = io.ErrShortWrite | ||
} | ||
|
||
return err | ||
} |
Oops, something went wrong.