github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/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{dartSassNoAvailable: true}, nil
    37  	}
    38  	transpiler, err := godartsass.Start(godartsass.Options{})
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs, transpiler: transpiler}, nil
    43  }
    44  
    45  type Client struct {
    46  	dartSassNoAvailable bool
    47  	rs                  *resources.Spec
    48  	sfs                 *filesystems.SourceFilesystem
    49  	workFs              afero.Fs
    50  	transpiler          *godartsass.Transpiler
    51  }
    52  
    53  func (c *Client) ToCSS(res resources.ResourceTransformer, args map[string]interface{}) (resource.Resource, error) {
    54  	if c.dartSassNoAvailable {
    55  		return res.Transform(resources.NewFeatureNotAvailableTransformer(transformationName, args))
    56  	}
    57  	return res.Transform(&transform{c: c, optsm: args})
    58  }
    59  
    60  func (c *Client) Close() error {
    61  	if c.transpiler == nil {
    62  		return nil
    63  	}
    64  	return c.transpiler.Close()
    65  }
    66  
    67  func (c *Client) toCSS(args godartsass.Args, src io.Reader) (godartsass.Result, error) {
    68  	var res godartsass.Result
    69  
    70  	in := helpers.ReaderToString(src)
    71  	args.Source = in
    72  
    73  	res, err := c.transpiler.Execute(args)
    74  	if err != nil {
    75  		return res, err
    76  	}
    77  
    78  	return res, err
    79  }
    80  
    81  type Options struct {
    82  
    83  	// Hugo, will by default, just replace the extension of the source
    84  	// to .css, e.g. "scss/main.scss" becomes "scss/main.css". You can
    85  	// control this by setting this, e.g. "styles/main.css" will create
    86  	// a Resource with that as a base for RelPermalink etc.
    87  	TargetPath string
    88  
    89  	// Hugo automatically adds the entry directories (where the main.scss lives)
    90  	// for project and themes to the list of include paths sent to LibSASS.
    91  	// Any paths set in this setting will be appended. Note that these will be
    92  	// treated as relative to the working dir, i.e. no include paths outside the
    93  	// project/themes.
    94  	IncludePaths []string
    95  
    96  	// Default is nested.
    97  	// One of nested, expanded, compact, compressed.
    98  	OutputStyle string
    99  
   100  	// When enabled, Hugo will generate a source map.
   101  	EnableSourceMap bool
   102  }
   103  
   104  func decodeOptions(m map[string]interface{}) (opts Options, err error) {
   105  	if m == nil {
   106  		return
   107  	}
   108  	err = mapstructure.WeakDecode(m, &opts)
   109  
   110  	if opts.TargetPath != "" {
   111  		opts.TargetPath = helpers.ToSlashTrimLeading(opts.TargetPath)
   112  	}
   113  
   114  	return
   115  }