ennet

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 10 Imported by: 1

README

Package ennet parses Emmet-like abbreviations and expands them into XML.

Go Report Card Go Reference MIT License

go get

go get -u github.com/shu-go/ennet

Example

func Example() {
	expanded, _ := ennet.Expand("ul>li.item-${ITEM$}*3")
	fmt.Println(expanded)

	//Output:
	// <ul><li class="item-1">ITEM1</li><li class="item-2">ITEM2</li><li class="item-3">ITEM3</li></ul>
}

Documentation

Overview

Package ennet parses Emmet-like abbreviations and expands them into XML.

Limitations

  • No implicit tag names (`ul>.cls` causes an error)
  • Generates always empty-element tags (yes: <a />, no: <a></a>)
  • (internal) each TEXT {...}, QTEXT "..." is a token, unlike attr-list [, ..., ]
Example
package main

import (
	"fmt"

	"github.com/shu-go/ennet"
)

func main() {
	expanded, _ := ennet.Expand("ul>li.item-${ITEM$}*3")
	fmt.Println(expanded)

}
Output:

<ul><li class="item-1">ITEM1</li><li class="item-2">ITEM2</li><li class="item-3">ITEM3</li></ul>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(s string) (string, error)

Expand expands Emmet abbreviation in string s.

func Parse

func Parse(b []byte, builder Builder) (parseError error)

func WithPool added in v0.4.2

func WithPool(pool *sync.Pool) func(*NodeBuilder)

Types

type Builder added in v0.4.2

type Builder interface {
	Element(name string) error
	ID(name string) error
	Class(name string) error
	Attribute(name, value string) error
	Text(text string) error
	Mul(count int) error

	OpChild() error
	OpSibling() error
	OpClimbup(count int) error

	GroupBegin() error
	GroupEnd() error
}

type Lexer

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

func NewLexer

func NewLexer(b []byte) *Lexer

func (*Lexer) Back

func (l *Lexer) Back()

func (*Lexer) Close added in v0.2.0

func (l *Lexer) Close()

func (*Lexer) Dump

func (l *Lexer) Dump() string

func (*Lexer) Next

func (l *Lexer) Next() Token

func (*Lexer) Peek added in v0.4.2

func (l *Lexer) Peek() Token

type Node

type Node struct {
	Type      NodeType
	Data      string
	Attribute map[string]string

	Mul int

	Parent, FirstChild, LastChild, NextSibling, PrevSibling *Node
}

func (*Node) AppendChild

func (n *Node) AppendChild(child *Node) *Node

func (*Node) Dump

func (n *Node) Dump() string

type NodeBuilder added in v0.4.2

type NodeBuilder struct {
	Root *Node
	// contains filtered or unexported fields
}

func NewNodeBuilder added in v0.4.2

func NewNodeBuilder(opts ...NodeBuilderOption) NodeBuilder

func (*NodeBuilder) Attribute added in v0.4.2

func (nb *NodeBuilder) Attribute(name, value string) error

func (*NodeBuilder) Class added in v0.4.2

func (nb *NodeBuilder) Class(name string) error

func (*NodeBuilder) Element added in v0.4.2

func (nb *NodeBuilder) Element(name string) error

func (*NodeBuilder) GroupBegin added in v0.4.2

func (nb *NodeBuilder) GroupBegin() error

func (*NodeBuilder) GroupEnd added in v0.4.2

func (nb *NodeBuilder) GroupEnd() error

func (*NodeBuilder) ID added in v0.4.2

func (nb *NodeBuilder) ID(name string) error

func (*NodeBuilder) Mul added in v0.4.2

func (nb *NodeBuilder) Mul(count int) error

func (*NodeBuilder) NewNode added in v0.4.2

func (b *NodeBuilder) NewNode() *Node

func (*NodeBuilder) OpChild added in v0.4.2

func (nb *NodeBuilder) OpChild() error

func (*NodeBuilder) OpClimbup added in v0.4.2

func (nb *NodeBuilder) OpClimbup(count int) error

func (*NodeBuilder) OpSibling added in v0.4.2

func (nb *NodeBuilder) OpSibling() error

func (*NodeBuilder) Text added in v0.4.2

func (nb *NodeBuilder) Text(text string) error

type NodeBuilderOption added in v0.4.2

type NodeBuilderOption func(*NodeBuilder)

type NodeType

type NodeType uint8
const (
	Root NodeType = iota
	WIP

	Element
	Text
	Group
)

func (NodeType) String added in v0.4.0

func (t NodeType) String() string

type Parser

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

EBNF:

attr-list = "[", attr, { attr }, "]";
attr = STRING, ["=", (QTEXT | STRING)];
id = "#", STRING;
class = ".", STRING;

tag-element = STRING, { id | class | attr-list }, [ TEXT ];
multiplication = "*", NUMBER;

element = ( tag-element | TEXT ), [multiplication];

group = "(", abbreviation, ")", [multiplication];

operator = CHILD | SIBLING | repeatable-operator;
repeatable-operator = CLIMBUP, {CLIMBUP}

abbreviation = (group | element), [operator, abbreviation]

type Token

type Token struct {
	Type TokenType
	Text string
	Pos  int
}

func (Token) String

func (t Token) String() string

type TokenType

type TokenType uint8
const (
	EOF TokenType = iota
	ERR

	CHILD
	SIBLING
	CLIMBUP
	MULT
	GROUPBEGIN
	GROUPEND

	ID
	CLASS
	ATTRBEGIN
	ATTREND
	EQ

	STRING
	TEXT
	QTEXT
)

func (TokenType) String added in v0.4.0

func (t TokenType) String() string

Jump to

Keyboard shortcuts

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