| 1 |
// Copyright 2018 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package scss |
| 15 |
|
| 16 |
import ( |
| 17 |
"regexp" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/common/paths" |
| 20 |
"github.com/gohugoio/hugo/hugolib/filesystems" |
| 21 |
"github.com/gohugoio/hugo/resources" |
| 22 |
"github.com/spf13/afero" |
| 23 |
|
| 24 |
"github.com/mitchellh/mapstructure" |
| 25 |
) |
| 26 |
|
| 27 |
const transformationName = "tocss" |
| 28 |
|
| 29 |
type Client struct { |
| 30 |
rs *resources.Spec |
| 31 |
sfs *filesystems.SourceFilesystem |
| 32 |
workFs afero.Fs |
| 33 |
} |
| 34 |
|
| 35 |
func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) { |
| 36 |
return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs}, nil |
| 37 |
} |
| 38 |
|
| 39 |
type Options struct { |
| 40 |
// Hugo, will by default, just replace the extension of the source |
| 41 |
// to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can |
| 42 |
// control this by setting this, e.g. "styles/main.css" will create |
| 43 |
// a Resource with that as a base for RelPermalink etc. |
| 44 |
TargetPath string |
| 45 |
|
| 46 |
// Hugo automatically adds the entry directories (where the main.scss lives) |
| 47 |
// for project and themes to the list of include paths sent to LibSASS. |
| 48 |
// Any paths set in this setting will be appended. Note that these will be |
| 49 |
// treated as relative to the working dir, i.e. no include paths outside the |
| 50 |
// project/themes. |
| 51 |
IncludePaths []string |
| 52 |
|
| 53 |
// Default is nested. |
| 54 |
// One of nested, expanded, compact, compressed. |
| 55 |
OutputStyle string |
| 56 |
|
| 57 |
// Precision of floating point math. |
| 58 |
Precision int |
| 59 |
|
| 60 |
// When enabled, Hugo will generate a source map. |
| 61 |
EnableSourceMap bool |
| 62 |
|
| 63 |
// Vars will be available in 'hugo:vars', e.g: |
| 64 |
// @import "hugo:vars"; |
| 65 |
Vars map[string]any |
| 66 |
} |
| 67 |
|
| 68 |
func DecodeOptions(m map[string]any) (opts Options, err error) { |
| 69 |
if m == nil { |
| 70 |
return |
| 71 |
} |
| 72 |
err = mapstructure.WeakDecode(m, &opts) |
| 73 |
|
| 74 |
if opts.TargetPath != "" { |
| 75 |
opts.TargetPath = paths.ToSlashTrimLeading(opts.TargetPath) |
| 76 |
} |
| 77 |
|
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
var ( |
| 82 |
regularCSSImportTo = regexp.MustCompile(`.*(@import "(.*\.css)";).*`) |
| 83 |
regularCSSImportFrom = regexp.MustCompile(`.*(\/\* HUGO_IMPORT_START (.*) HUGO_IMPORT_END \*\/).*`) |
| 84 |
) |
| 85 |
|
| 86 |
func replaceRegularImportsIn(s string) (string, bool) { |
| 87 |
replaced := regularCSSImportTo.ReplaceAllString(s, "/* HUGO_IMPORT_START $2 HUGO_IMPORT_END */") |
| 88 |
return replaced, s != replaced |
| 89 |
} |
| 90 |
|
| 91 |
func replaceRegularImportsOut(s string) string { |
| 92 |
return regularCSSImportFrom.ReplaceAllString(s, "@import \"$2\";") |
| 93 |
} |