github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/resource_transformers/tocss/dartsass/client.go (about)

     1  // Copyright 2020 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 godartsass integrates with the Dass Sass Embedded protocol to transpile
    15  // SCSS/SASS.
    16  package dartsass
    17  
    18  import (
    19  	"io"
    20  
    21  	"github.com/gohugoio/hugo/helpers"
    22  	"github.com/gohugoio/hugo/hugolib/filesystems"
    23  	"github.com/gohugoio/hugo/resources"
    24  	"github.com/gohugoio/hugo/resources/resource"
    25  	"github.com/spf13/afero"
    26  
    27  	"github.com/bep/godartsass"
    28  	"github.com/mitchellh/mapstructure"
    29  )
    30  
    31  // used as part of the cache key.
    32  const transformationName = "tocss-dart"
    33  
    34  func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) {
    35  	if !Supports() {
    36  		return &Client{dartSassNotAvailable: true}, nil
    37  	}
    38  
    39  	if err := rs.ExecHelper.Sec().CheckAllowedExec(dartSassEmbeddedBinaryName); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	transpiler, err := godartsass.Start(godartsass.Options{})
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs, transpiler: transpiler}, nil
    48  }
    49  
    50  type Client struct {
    51  	dartSassNotAvailable bool
    52  	rs                   *resources.Spec
    53  	sfs                  *filesystems.SourceFilesystem
    54  	workFs               afero.Fs
    55  	transpiler           *godartsass.Transpiler
    56  }
    57  
    58  func (c *Client) ToCSS(res resources.ResourceTransformer, args map[string]interface{}) (resource.Resource, error) {
    59  	if c.dartSassNotAvailable {
    60  		return res.Transform(resources.NewFeatureNotAvailableTransformer(transformationName, args))
    61  	}
    62  	return res.Transform(&transform{c: c, optsm: args})
    63  }
    64  
    65  func (c *Client) Close() error {
    66  	if c.transpiler == nil {
    67  		return nil
    68  	}
    69  	return c.transpiler.Close()
    70  }
    71  
    72  func (c *Client) toCSS(args godartsass.Args, src io.Reader) (godartsass.Result, error) {
    73  	var res godartsass.Result
    74  
    75  	in := helpers.ReaderToString(src)
    76  	args.Source = in
    77  
    78  	res, err := c.transpiler.Execute(args)
    79  	if err != nil {
    80  		return res, err
    81  	}
    82  
    83  	return res, err
    84  }
    85  
    86  type Options struct {
    87  
    88  	// Hugo, will by default, just replace the extension of the source
    89  	// to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can
    90  	// control this by setting this, e.g. "styles/main.css" will create
    91  	// a Resource with that as a base for RelPermalink etc.
    92  	TargetPath string
    93  
    94  	// Hugo automatically adds the entry directories (where the main.scss lives)
    95  	// for project and themes to the list of include paths sent to LibSASS.
    96  	// Any paths set in this setting will be appended. Note that these will be
    97  	// treated as relative to the working dir, i.e. no include paths outside the
    98  	// project/themes.
    99  	IncludePaths []string
   100  
   101  	// Default is nested.
   102  	// One of nested, expanded, compact, compressed.
   103  	OutputStyle string
   104  
   105  	// When enabled, Hugo will generate a source map.
   106  	EnableSourceMap bool
   107  }
   108  
   109  func decodeOptions(m map[string]interface{}) (opts Options, err error) {
   110  	if m == nil {
   111  		return
   112  	}
   113  	err = mapstructure.WeakDecode(m, &opts)
   114  
   115  	if opts.TargetPath != "" {
   116  		opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
   117  	}
   118  
   119  	return
   120  }