parser

package
v0.0.0-...-59c2813 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChildAlignMapping map[string]callisto.ChildAlign = map[string]callisto.ChildAlign{
	"mainAxisTop":         callisto.MainAxisTop,
	"mainAxisCenter":      callisto.MainAxisCenter,
	"mainAxisBottom":      callisto.MainAxisBottom,
	"crossAxisLeft":       callisto.CrossAxisLeft,
	"crossAxisCenter":     callisto.CrossAxisCenter,
	"crossAxisRight":      callisto.CrossAxisRight,
	"crossMainAxisCenter": callisto.MainCrossAxisCenter,
}
View Source
var ImageFitMapping map[string]callisto.ImageFit = map[string]callisto.ImageFit{
	"width":  callisto.FitWidth,
	"height": callisto.FitHeight,
	"both":   callisto.Both,
	"none":   callisto.NoFit,
}
View Source
var UnitSymbolMapping map[Unit]string = map[Unit]string{
	ParentDeltaRelative: "rd",
	ParentScaleRelative: "%",
	Pixel:               "px",
}

Functions

func BuildTree

func BuildTree(engine *callisto.RenderEngine, sourceFilePath string) (err error)

func GetChildAlignWithString

func GetChildAlignWithString(rawChildAlign string) callisto.ChildAlign

func GetLayoutManagerWithString

func GetLayoutManagerWithString(rawLayoutManager string) callisto.LayoutManager

func GetPaddingFromRaw

func GetPaddingFromRaw(rawString string) callisto.ElementPadding

func ParseBorder

func ParseBorder(rawString string) (callisto.ElementBorder, error)

func ParseBorderWithPosition

func ParseBorderWithPosition(rawString string) (width int, color string, err error)

func ParseBoxSizing

func ParseBoxSizing(rawString string) callisto.BoxSizing

func ParseFont

func ParseFont(rawString string) string

func ParseImageFit

func ParseImageFit(rawString string) (callisto.ImageFit, error)

Types

type Body

type Body struct {
	Children  []map[string]interface{} `json:"children"`
	Direction string                   `json:"direction"`
}

type BoxDocElement

type BoxDocElement struct {
	Width         string                   `json:"width"`
	Height        string                   `json:"height"`
	LayoutManager string                   `json:"layoutManager"`
	ChildAlign    string                   `json:"childAlign"`
	Background    string                   `json:"background"`
	Children      []map[string]interface{} `json:"children"`
	Padding       string                   `json:"padding"`
	BoxSizing     string                   `json:"boxSizing"`
	Border        string                   `json:"border"`
	BorderTop     string                   `json:"borderTop"`
	BorderRight   string                   `json:"borderRight"`
	BorderBottom  string                   `json:"borderBottom"`
	BorderLeft    string                   `json:"borderLeft"`
}

type BuildContext

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

type Doc

type Doc struct {
	Meta Meta `json:"meta"`
	Body Body `json:"body"`
}

func ReadJsonDoc

func ReadJsonDoc(docPath string) (*Doc, error)

type ElementHandler

type ElementHandler func(context BuildContext) (callisto.Element, error)
var BoxHandler ElementHandler = func(context BuildContext) (callisto.Element, error) {
	var docElm BoxDocElement
	err := mapstructure.Decode(context.content, &docElm)
	if err != nil {
		return nil, err
	}
	node := &callisto.Box{

		Color: docElm.Background,

		LayoutManager: GetLayoutManagerWithString(docElm.LayoutManager),

		ChildrenAlign: GetChildAlignWithString(docElm.ChildAlign),
		ElementPosition: callisto.ElementPosition{
			UseParentDelta: true,
		},
	}

	dimension := callisto.ElementDimension{}

	width, widthUnit, err := ParseValueAndUnit(docElm.Width)
	switch widthUnit {
	case ParentScaleRelative:
		dimension.UserParentRelative = true
		dimension.ParentRelativeScaleWidth = width / 100
	case Pixel:
		dimension.Width = width
	case ParentDeltaRelative:
		dimension.UserParentRelative = true
		dimension.ParentRelativeDeltaWidth = width
	}

	height, heightUnit, err := ParseValueAndUnit(docElm.Height)
	switch heightUnit {
	case ParentScaleRelative:
		dimension.UserParentRelative = true
		dimension.ParentRelativeScaleHeight = height / 100
	case Pixel:
		dimension.Height = height
	case ParentDeltaRelative:
		dimension.UserParentRelative = true
		dimension.ParentRelativeDeltaWidth = height
	}
	node.ElementDimension = dimension

	node.Padding = GetPaddingFromRaw(docElm.Padding)

	node.BoxSizing = ParseBoxSizing(docElm.BoxSizing)

	border, err := ParseBorder(docElm.Border)
	if err != nil {
		return nil, err
	}
	node.Border = border

	topWidth, topColor, err := ParseBorderWithPosition(docElm.BorderTop)
	if err != nil {
		return nil, err
	}
	node.Border.Top = callisto.Border{
		Width: topWidth,
		Color: topColor,
	}

	rightWidth, rightColor, err := ParseBorderWithPosition(docElm.BorderRight)
	if err != nil {
		return nil, err
	}
	node.Border.Right = callisto.Border{
		Width: rightWidth,
		Color: rightColor,
	}

	bottomWidth, bottomColor, err := ParseBorderWithPosition(docElm.BorderBottom)
	if err != nil {
		return nil, err
	}
	node.Border.Bottom = callisto.Border{
		Width: bottomWidth,
		Color: bottomColor,
	}

	leftWidth, leftColor, err := ParseBorderWithPosition(docElm.BorderLeft)
	if err != nil {
		return nil, err
	}
	node.Border.Left = callisto.Border{
		Width: leftWidth,
		Color: leftColor,
	}

	return node, nil
}
var ImageElementHandler ElementHandler = func(context BuildContext) (callisto.Element, error) {
	boxElement, err := BoxHandler(context)
	if err != nil {
		return nil, err
	}
	box := boxElement.(*callisto.Box)

	var docElm ImageDocElement
	err = mapstructure.Decode(&context.content, &docElm)
	if err != nil {
		return nil, err
	}

	node := callisto.ImageBox{
		Box:             *box,
		Src:             docElm.Src,
		OriginDimension: docElm.OriginDimension,
		Fit:             callisto.FitHeight,
	}

	imageFit, err := ParseImageFit(docElm.Fit)
	if err != nil {
		return nil, err
	}
	node.Fit = imageFit
	return &node, nil

}
var ParagraphHandler ElementHandler = func(context BuildContext) (callisto.Element, error) {
	boxElement, err := BoxHandler(context)
	if err != nil {
		return nil, err
	}
	box := boxElement.(*callisto.Box)
	var docElm ParagraphDocElement
	err = mapstructure.Decode(&context.content, &docElm)
	if err != nil {
		return nil, err
	}

	node := callisto.ParagraphBox{
		Box: *box,
	}
	err = copier.Copy(&node, &docElm)
	if err != nil {
		return nil, err
	}
	node.FontPath = ParseFont(docElm.Font)
	return &node, nil
}
var TextHandler ElementHandler = func(context BuildContext) (callisto.Element, error) {
	boxElement, err := BoxHandler(context)
	if err != nil {
		return nil, err
	}
	box := boxElement.(*callisto.Box)
	var docElm TextDocElement
	err = mapstructure.Decode(&context.content, &docElm)
	if err != nil {
		return nil, err
	}

	node := callisto.TextBox{
		Box: *box,
	}
	err = copier.Copy(&node, &docElm)
	if err != nil {
		return nil, err
	}
	node.FontPath = ParseFont(docElm.Font)
	return &node, nil
}

func Dispatch

func Dispatch(elementType string) ElementHandler

type ImageDocElement

type ImageDocElement struct {
	Src             string `json:"src"`
	OriginDimension bool   `json:"originDimension"`
	Fit             string `json:"fit"`
}

type Meta

type Meta struct {
	From         string  `json:"from"`
	TemplatePath string  `json:"templatePath"`
	Width        float64 `json:"width"`
	Height       float64 `json:"height"`
}

type ParagraphDocElement

type ParagraphDocElement struct {
	Text  string  `json:"text"`
	Font  string  `json:"font"`
	Size  float64 `json:"size"`
	Color string  `json:"color"`
	Wrap  bool    `json:"wrap"`
}

type TextDocElement

type TextDocElement struct {
	Font  string  `json:"font"`
	Size  float64 `json:"size"`
	Text  string  `json:"text"`
	Color string  `json:"color"`
}

type Unit

type Unit int
const (
	ParentScaleRelative Unit = iota + 1
	ParentDeltaRelative
	Pixel
)

func ParseValueAndUnit

func ParseValueAndUnit(raw string) (float64, Unit, error)

Jump to

Keyboard shortcuts

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