github.com/pietrocarrara/hugo@v0.47.1/resource/tocss/scss/client.go (about) 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 "github.com/bep/go-tocss/scss" 18 "github.com/gohugoio/hugo/helpers" 19 "github.com/gohugoio/hugo/hugolib/filesystems" 20 "github.com/gohugoio/hugo/resource" 21 "github.com/mitchellh/mapstructure" 22 ) 23 24 type Client struct { 25 rs *resource.Spec 26 sfs *filesystems.SourceFilesystem 27 workFs *filesystems.SourceFilesystem 28 } 29 30 func New(fs *filesystems.SourceFilesystem, rs *resource.Spec) (*Client, error) { 31 return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs}, nil 32 } 33 34 type Options struct { 35 36 // Hugo, will by default, just replace the extension of the source 37 // to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can 38 // control this by setting this, e.g. "styles/main.css" will create 39 // a Resource with that as a base for RelPermalink etc. 40 TargetPath string 41 42 // Hugo automatically adds the entry directories (where the main.scss lives) 43 // for project and themes to the list of include paths sent to LibSASS. 44 // Any paths set in this setting will be appended. Note that these will be 45 // treated as relative to the working dir, i.e. no include paths outside the 46 // project/themes. 47 IncludePaths []string 48 49 // Default is nested. 50 // One of nested, expanded, compact, compressed. 51 OutputStyle string 52 53 // Precision of floating point math. 54 Precision int 55 56 // When enabled, Hugo will generate a source map. 57 EnableSourceMap bool 58 } 59 60 type options struct { 61 // The options we receive from the end user. 62 from Options 63 64 // The options we send to the SCSS library. 65 to scss.Options 66 } 67 68 func (c *Client) ToCSS(res resource.Resource, opts Options) (resource.Resource, error) { 69 internalOptions := options{ 70 from: opts, 71 } 72 73 // Transfer values from client. 74 internalOptions.to.Precision = opts.Precision 75 internalOptions.to.OutputStyle = scss.OutputStyleFromString(opts.OutputStyle) 76 77 if internalOptions.to.Precision == 0 { 78 // bootstrap-sass requires 8 digits precision. The libsass default is 5. 79 // https://github.com/twbs/bootstrap-sass/blob/master/README.md#sass-number-precision 80 internalOptions.to.Precision = 8 81 } 82 83 return c.rs.Transform( 84 res, 85 &toCSSTransformation{c: c, options: internalOptions}, 86 ) 87 } 88 89 type toCSSTransformation struct { 90 c *Client 91 options options 92 } 93 94 func (t *toCSSTransformation) Key() resource.ResourceTransformationKey { 95 return resource.NewResourceTransformationKey("tocss", t.options.from) 96 } 97 98 func DecodeOptions(m map[string]interface{}) (opts Options, err error) { 99 if m == nil { 100 return 101 } 102 err = mapstructure.WeakDecode(m, &opts) 103 104 if opts.TargetPath != "" { 105 opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath) 106 } 107 108 return 109 }