1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
package libsass
import (
"errors"
"io"
"golang.org/x/net/context"
)
var (
ErrPayloadEmpty = errors.New("empty payload")
ErrNoCompile = errors.New("No compile has occurred")
_ Pather = &sass{}
_ Compiler = &sass{}
)
// Pather describes the file system paths necessary for a project
type Pather interface {
ImgDir() string
BuildDir() string
HTTPPath() string
ImgBuildDir() string
FontDir() string
}
// Compiler interface is used to translate input Sass network, filepath,
// or otherwise and transforms it to CSS. The interface includes methods
// for adding imports and specifying build options necessary to do the
// transformation.
//
type Compiler interface {
// Run does a synchronous build via cgo. It is thread safe, but there is
// no guarantee that the cgo calls will always be that way.
Run() error
// Imports returns the imports used for a compile. This is built
// at parser time in libsass
Imports() []string
// Option allows the configuration of the compiler. The option is
// unexported to encourage use of preconfigured option functions.
Option(...option) error
// CacheBust specifies the cache bust method used by the compiler
// Available options: ts, sum
CacheBust() string
// LineComments specifies whether line comments were inserted into
// output CSS
LineComments() bool
// Payload returns the attached spritewell information attached
// to the compiler context
Payload() context.Context
// Syntax represents the style of code Sass or SCSS
Syntax() Syntax
}
func New(dst io.Writer, src io.Reader, opts ...option) (Compiler, error) {
c := &sass{
dst: dst,
src: src,
ctx: newContext(),
}
c.ctx.in = src
c.ctx.out = dst
c.ctx.compiler = c
err := c.Option(opts...)
return c, err
}
// sass implements compiler interface for Sass and Scss stylesheets. To
// configure the compiler, use the option method.
type sass struct {
// FIXME: old context for storing state, use compiler instead
ctx *compctx
dst io.Writer
mappath string
// src is the input stream to compile
src io.Reader
// path to the input file
srcFile string
// cachebust instructs the compiler to generate new paths
// preventing browser caching
cachebust string
httpPath string
includePaths []string
imports []string
cmt bool
sourceMapRoot string
// payload is passed around for handlers to have context
payload context.Context
// current syntax of the compiler, Sass or SCSS
syntax Syntax
}
var _ Compiler = &sass{}
func (c *sass) run() error {
defer func() {
c.imports = c.ctx.ResolvedImports
}()
if len(c.srcFile) > 0 {
return c.ctx.fileCompile(c.srcFile, c.dst, c.mappath, c.sourceMapRoot)
}
return c.ctx.compile(c.dst, c.src)
}
// BuildDir is where CSS is written to disk
func (s *sass) BuildDir() string {
return s.ctx.BuildDir
}
// CacheBust reveals the current cache busting state
func (c *sass) CacheBust() string {
return c.cachebust
}
func (s *sass) HTTPPath() string {
return s.ctx.HTTPPath
}
// ImgBuildDir fetch the image build directory
func (s *sass) ImgBuildDir() string {
return s.ctx.GenImgDir
}
// ImgDir returns the Image Directory used for locating images
func (c *sass) ImgDir() string {
return c.ctx.ImageDir
}
// Imports returns the full list of partials used to build the
// output stylesheet.
func (c *sass) Imports() []string {
return c.imports
}
// FontDir returns the font directory option
func (c *sass) FontDir() string {
return c.ctx.FontDir
}
// LineComments returns the source comment status
func (c *sass) LineComments() bool {
return c.cmt
}
func (c *sass) Payload() context.Context {
return c.ctx.Payload
}
// Run starts transforming S[c|a]ss to CSS
func (c *sass) Run() error {
return c.run()
}
func (c *sass) Syntax() Syntax {
return c.syntax
}
|