/resources/resource_transformers/tocss/scss/client.go

https://gitlab.com/gohugo/hugo · Go · 90 lines · 46 code · 19 blank · 25 comment · 5 complexity · 6c43232202eac5d19ba9940ccadcf35e MD5 · raw file

  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. package scss
  14. import (
  15. "regexp"
  16. "github.com/gohugoio/hugo/helpers"
  17. "github.com/gohugoio/hugo/hugolib/filesystems"
  18. "github.com/gohugoio/hugo/resources"
  19. "github.com/spf13/afero"
  20. "github.com/mitchellh/mapstructure"
  21. )
  22. const transformationName = "tocss"
  23. type Client struct {
  24. rs *resources.Spec
  25. sfs *filesystems.SourceFilesystem
  26. workFs afero.Fs
  27. }
  28. func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) {
  29. return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs}, nil
  30. }
  31. type Options struct {
  32. // Hugo, will by default, just replace the extension of the source
  33. // to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can
  34. // control this by setting this, e.g. "styles/main.css" will create
  35. // a Resource with that as a base for RelPermalink etc.
  36. TargetPath string
  37. // Hugo automatically adds the entry directories (where the main.scss lives)
  38. // for project and themes to the list of include paths sent to LibSASS.
  39. // Any paths set in this setting will be appended. Note that these will be
  40. // treated as relative to the working dir, i.e. no include paths outside the
  41. // project/themes.
  42. IncludePaths []string
  43. // Default is nested.
  44. // One of nested, expanded, compact, compressed.
  45. OutputStyle string
  46. // Precision of floating point math.
  47. Precision int
  48. // When enabled, Hugo will generate a source map.
  49. EnableSourceMap bool
  50. }
  51. func DecodeOptions(m map[string]any) (opts Options, err error) {
  52. if m == nil {
  53. return
  54. }
  55. err = mapstructure.WeakDecode(m, &opts)
  56. if opts.TargetPath != "" {
  57. opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
  58. }
  59. return
  60. }
  61. var (
  62. regularCSSImportTo = regexp.MustCompile(`.*(@import "(.*\.css)";).*`)
  63. regularCSSImportFrom = regexp.MustCompile(`.*(\/\* HUGO_IMPORT_START (.*) HUGO_IMPORT_END \*\/).*`)
  64. )
  65. func replaceRegularImportsIn(s string) (string, bool) {
  66. replaced := regularCSSImportTo.ReplaceAllString(s, "/* HUGO_IMPORT_START $2 HUGO_IMPORT_END */")
  67. return replaced, s != replaced
  68. }
  69. func replaceRegularImportsOut(s string) string {
  70. return regularCSSImportFrom.ReplaceAllString(s, "@import \"$2\";")
  71. }