libsass

package module
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2018 License: Apache-2.0 Imports: 17 Imported by: 66

README

libsass

Circle CI Build status

The only Sass compliant Go library! go-libsass is a wrapper to the sass/libsass project.

To build, setup Go

go build

To test

go test

Basic example more examples found in examples

buf := bytes.NewBufferString("div { p { color: red; } }")
if err != nil {
	log.Fatal(err)
}
comp, err := libsass.New(os.Stdout, buf)
if err != nil {
	log.Fatal(err)
}

if err := comp.Run(); err != nil {
	log.Fatal(err)
}

Output

div p {
  color: red; }
Updating libsass
cd libsass-src; git fetch; git checkout vX.X.X
git commit -m "updated libsass to vX.X.X"
make update-libsass
# iterate on includes and code changes until tests pass
FAQ
  • Compiling go-libsass is very slow, what can be done?

    Go-libsass compiles C/C++ libsass on every build. You can install the package and speed up building go install github.com/wellington/go-libsass. Alternatively, it's possible to link against system libsass and forego C compiling with go build -tags dev.

  • How do I cross compile?

    Since this package uses C bindings, you will need gcc for the target platform. For windows see, https://github.com/wellington/go-libsass/issues/37

Documentation

Overview

Package context wraps access to libsass. Higher level abstractions are used to make the package more like Go than C. For low level access see: http://godoc.org/github.com/wellington/go-libsass/libs

For more info, see https://github.com/sass/libsass

Index

Examples

Constants

View Source
const (
	NESTED_STYLE = iota
	EXPANDED_STYLE
	COMPACT_STYLE
	COMPRESSED_STYLE
)

Constants/enums for the output style.

Variables

View Source
var (
	ErrPayloadEmpty = errors.New("empty payload")
	ErrNoCompile    = errors.New("No compile has occurred")
)
View Source
var ErrCompilerNotFound = errors.New("compiler not found")
View Source
var (
	ErrImportNotFound = errors.New("Import unreachable or not found")
)
View Source
var (
	ErrSassNumberNoUnit = errors.New("SassNumber has no units")
)
View Source
var Style map[string]int
View Source
var TestCallback = testCallback(func(_ interface{}, _ SassValue, _ *SassValue) error {
	return nil
})

TestCallback implements libs.SassCallback. TestCallback is a useful place to start when developing new handlers.

Functions

func BasePath

func BasePath(basePath string) option

BasePath sets the internal path provided to handlers requiring a base path for http calls. This is useful for hosted solutions that need to provided absolute paths to assets.

func BuildDir

func BuildDir(path string) option

BuildDir only used for spriting, how terrible!

func CacheBust

func CacheBust(t string) option

CacheBust append timestamps to static assets to prevent caching

func Comments

func Comments(b bool) option

Comments toggles whether comments should be included in the output

func FontDir

func FontDir(path string) option

FontDir specifies where to find fonts

func HTTPPath

func HTTPPath(u string) option

HTTPPath prefixes all sprites and generated images with this uri. Enabling wellington to serve images when used in HTTP mode

func Handler

func Handler(h HandlerFunc) libs.SassCallback

Handler accepts a HandlerFunc and returns SassCallback for sending to libsass. The third argument must be a pointer and the function must return an error.

func ImgBuildDir

func ImgBuildDir(path string) option

ImgBuildDir specifies the destination directory for images

func ImgDir

func ImgDir(path string) option

ImgDir specifies where to locate images for spriting

func ImportsOption

func ImportsOption(imports *Imports) option

ImportsOption specifies configuration for import resolution

func IncludePaths

func IncludePaths(includes []string) option

IncludePaths adds additional directories to search for Sass files

func LineComments

func LineComments(b bool) option

LineComments removes the line by line playby of the Sass compiler

func NewCompilerContext

func NewCompilerContext(c Compiler) context.Context

func OutputStyle

func OutputStyle(style int) option

OutputStyle controls the presentation of the CSS available option: nested, expanded, compact, compressed

func Path

func Path(path string) option

Path specifies a file to read instead of using the provided io.Reader. This activates file compiling that includes line numbers in the resulting output.

func Payload

func Payload(load context.Context) option

Payload gives access to sprite and image information for handlers to perform spriting functions.

func Precision

func Precision(prec int) option

Precision specifies the number of points beyond the decimal place is preserved during math calculations.

func RegisterHandler

func RegisterHandler(sign string, callback HandlerFunc)

RegisterHandler sets the passed signature and callback to the handlers array.

func RegisterHeader

func RegisterHeader(body string)

RegisterHeader fifo

func RegisterSassFunc

func RegisterSassFunc(sign string, fn SassFunc)

RegisterSassFunc assigns the passed Func to the specified signature sign

func SassHandler

func SassHandler(h SassFunc) libs.SassCallback

SassHandler contains callback context for running code within a libsass handler

func SourceMap

func SourceMap(b bool, path, sourceMapRoot string) option

SourceMap behaves differently depending on compiler used. For compile, it will embed sourcemap into the source. For file compile, it will include a separate file with the source map.

func ToScss

func ToScss(r io.Reader, w io.Writer) error

ToScss converts Sass to Scss with libsass sass2scss.h

func Unmarshal

func Unmarshal(arg SassValue, v ...interface{}) error

Decode converts Sass Value to Go compatible data types.

func Version

func Version() string

Version reports libsass version information

func WithSyntax

func WithSyntax(mode Syntax) option

Types

type Compiler

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
}

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.

Example (Stdin)
src := bytes.NewBufferString(`div { p { color: red; } }`)

comp, err := New(os.Stdout, src)
if err != nil {
	log.Fatal(err)
}
err = comp.Run()
if err != nil {
	log.Fatal(err)
}
Output:

div p {
  color: red; }

func CompFromCtx

func CompFromCtx(ctx context.Context) (Compiler, error)

CompFromCtx retrieves a compiler from a passed context

func New

func New(dst io.Writer, src io.Reader, opts ...option) (Compiler, error)

type Func

type Func struct {
	Sign string
	Fn   libs.SassCallback
	Ctx  interface{}
}

Cookie is used for passing context information to libsass. Cookie is passed to custom handlers when libsass executes them through the go bridge.

type Funcs

type Funcs struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewFuncs

func NewFuncs(ctx *compctx) *Funcs

func (*Funcs) Add

func (fs *Funcs) Add(f Func)

func (*Funcs) Bind

func (fs *Funcs) Bind(goopts libs.SassOptions)

SetFunc assigns the registered methods to SassOptions. Functions are called when the compiler encounters the registered signature.

func (*Funcs) Close

func (fs *Funcs) Close()

type HandlerFunc

type HandlerFunc func(v interface{}, req SassValue, res *SassValue) error

HandlerFunc describes the method signature for registering a Go function to be called by libsass.

type Header struct {
	Content string
	// contains filtered or unexported fields
}

type Headers

type Headers struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewHeaders

func NewHeaders() *Headers

NewHeaders instantiates a Headers for prefixing Sass to input See: https://github.com/sass/libsass/wiki/API-Sass-Importer

func (*Headers) Add

func (h *Headers) Add(s string)

func (*Headers) Bind

func (hdrs *Headers) Bind(opts libs.SassOptions)

func (*Headers) Close

func (hdrs *Headers) Close()

func (*Headers) Has

func (h *Headers) Has(s string) bool

func (*Headers) Len

func (h *Headers) Len() int

type Import

type Import struct {
	Body io.ReadCloser

	Prev string
	Path string
	// contains filtered or unexported fields
}

Import contains Rel and Abs path and a string of the contents representing an import.

func (Import) ModTime

func (i Import) ModTime() time.Time

ModTime returns modification time

type Imports

type Imports struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Imports is a map with key of "path/to/file"

func NewImports

func NewImports() *Imports

func (*Imports) Add

func (p *Imports) Add(prev string, path string, bs []byte) error

Add registers an import in the context.Imports

func (*Imports) Bind

func (p *Imports) Bind(opts libs.SassOptions)

Bind accepts a SassOptions and adds the registered importers in the context.

func (*Imports) Close

func (i *Imports) Close()

func (*Imports) Del

func (p *Imports) Del(path string)

Del removes the import from the context.Imports

func (*Imports) Get

func (p *Imports) Get(prev, path string) ([]byte, error)

Get retrieves import bytes by path

func (*Imports) Init

func (p *Imports) Init()

Init sets up a new Imports map

func (*Imports) Len

func (p *Imports) Len() int

Len counts the number of entries in context.Imports

func (*Imports) Update

func (p *Imports) Update(name string)

Update attempts to create a fresh Body from the given path Files last modified stamps are compared against import timestamp

type Pather

type Pather interface {
	ImgDir() string
	BuildDir() string
	HTTPPath() string
	ImgBuildDir() string
	FontDir() string
}

Pather describes the file system paths necessary for a project

type SassError

type SassError struct {
	Status, Line, Column int
	File, Message        string
}

SassError represents an error object returned from Sass. SassError stores useful information for bubbling up libsass errors.

type SassFunc

type SassFunc func(ctx context.Context, in SassValue) (*SassValue, error)

SassFunc describes func for handling Sass Values

type SassValue

type SassValue struct {
	// contains filtered or unexported fields
}

func Error

func Error(err error) SassValue

Error takes a Go error and returns a libsass Error

func Marshal

func Marshal(v interface{}) (SassValue, error)

func NewSassValue

func NewSassValue() SassValue

func Warn

func Warn(s string) SassValue

Warn takes a string and causes a warning in libsass

func (SassValue) Val

func (sv SassValue) Val() libs.UnionSassValue

type Syntax

type Syntax int

Syntax lists that available syntaxes for the compiler

const (
	SCSSSyntax Syntax = iota
	SassSyntax
)

Directories

Path Synopsis
examples
custompreamble
Create a preamble for every CSS file
Create a preamble for every CSS file
external
libs is a direct mapping to libsass C API.
libs is a direct mapping to libsass C API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL