gel

package module
v0.0.0-...-21bb02a Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2022 License: EPL-1.0 Imports: 7 Imported by: 0

README

* Introduction
=gel= is a lib for programmatically producing Html.

** Why?  What in the...F?
Let's just say sometimes you don't want to deal with template engines.
You don't want a build system that transpiles one language into
another, just for the sake of brevity... not to mention you have to
learn the language and nuance of that new language or templating
thing.  You have to =npm install= 42 things, and then =bower install=
something else, or =grunt= this or that, or =gem= this or that, or get
some =pip=, or wget a shell script you won't vet before running, which
install who knows what.

But why?  Why?  You just need to generate a byte stream (hell a string).

And you need to loop or inject your data into that string, and the final
look and feel of that string is html-ish.  Well, this lib can do that
from inside of Go, and without some templating engine.

Is it a silver bullet -- nope.  Never said it was.  It's just one of
a million ways of rendering text.

** Usage
#+BEGIN_SRC go
  package main

  import (
	  . "github.com/lcaballero/gel"
	  "fmt"
  )  

  func main() {
	  el := Div.Class("container").Atts("id", "id-1").Text("text")
	  html := el.String() // <div class="container", id="id-1">text</div>
	  fmt.Println(html)  
  }
#+END_SRC

** TODO:
1. Need more examples... possibly even Go examples.
1. Add a Class(s string) top-level function and Tag member.
1. Convert Tags to functions where by they return the =*Node= such that
   there's no longer a need for the Add(...) or New(...) methods.
1. Add an Atts (plural) top-level, Node member and Tag member that makes
   an attribute list, which will be a new Type much like fragment.

** License
See license file.

The use and distribution terms for this software are covered by the
[Eclipse Public License 1.0][EPL-1], which can be found in the file 'license' at the
root of this distribution. By using this software in any fashion, you are
agreeing to be bound by the terms of this license. You must not remove this
notice, or any other, from this software.

[[http://opensource.org/licenses/eclipse-1.0.txt][EPL-1]]

Documentation

Index

Constants

View Source
const DefaultIncrement = 1
View Source
const DefaultLevel = 0
View Source
const DefaultTab = "  "

Variables

This section is empty.

Functions

This section is empty.

Types

type FileReader

type FileReader func(string) ([]byte, error)

type Fragment

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

Fragment is the concrete type of the Views interface.

func NewFragment

func NewFragment() *Fragment

NewViews allocates an empty views list to which move views can be added.

func (*Fragment) Add

func (v *Fragment) Add(views ...View) Views

Add appends the given views to the currently established list of views.

func (Fragment) Len

func (v Fragment) Len() int

Len returns the number of views held by the Fragment.

func (Fragment) ToNode

func (v Fragment) ToNode() *Node

ToNode implements the View interface, producing the final transformation to a *Node which can be rendered to stream.

func (*Fragment) ToView

func (v *Fragment) ToView() View

ToViews implements to the Viewable interface returning a concrete Node fragment instance.

type Indent

type Indent struct {
	Level int
	Inc   int
	Tab   string
}

Indent represents indention at a given level.

func NewIndent

func NewIndent() Indent

Returns an Indent value starting at level 0, with an increment of 0, and a tab of 2 spaces.

func (Indent) Decr

func (n Indent) Decr() Indent

Decr reduces Indent by one level.

func (Indent) HasIndent

func (n Indent) HasIndent() bool

HasIndent returns true if the Inc is > 0 and Tab != ”.

func (Indent) Incr

func (n Indent) Incr() Indent

Incr adds one level to the Indent.

func (Indent) String

func (n Indent) String() string

String produces the indention for the given level of the Indent.

func (Indent) WriteTo

func (n Indent) WriteTo(w io.Writer)

WriteTo outputs the Indent to the Writer.

type Inserter

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

func NewInserter

func NewInserter(res Resolver, reader FileReader) Inserter

func (Inserter) Include

func (r Inserter) Include(file string) View

type Node

type Node struct {
	Tag        string
	Children   []*Node
	Attributes []*Node
	Type       Type
	Key        string
	Value      string
	CData      string
	IsVoid     bool
}

Nodes represent the different parts of of Html as one type. A single Node can be one and only one of the following types: Textual, Element, or Attribute. In general a Textual node will not have children, but will have CData, Elements can have both children of type Text, Element, Attributes or Fragment, but cannot directly hold CData. Attributes have only Key and Value strings, and all other fields are empty or nil. Lastly, Fragments can have children of type Text and Element, while all other fields are empty or nil.

func (*Node) Add

func (v *Node) Add(nodes ...View) View

Add will collect and bucket the nodes into atts and children. Nodes of type Text or Element are added to children and Attribute type are added to the Atts slice. If the Node is not an Element then attributes will silently be ignored.

func (*Node) String

func (e *Node) String() string

String renders the Node as html (text, element, or attribute).

func (*Node) Text

func (n *Node) Text(ts ...string) View

Text adds the given strings as text nodes.

func (*Node) ToNode

func (e *Node) ToNode() *Node

ToNode is implemented to conform to a component pattern of Nodes within Nodes, but additionally some other instance can pose as a View if they implement ToNode.

func (*Node) WriteTo

func (e *Node) WriteTo(w io.Writer)

WriteTo will output the Node to the writer correctly nesting children and attributes.

func (*Node) WriteToIndented

func (e *Node) WriteToIndented(in Indent, w io.Writer)

WriteTo writes the Node to the given writer with the given indention.

type Resolver

type Resolver func(string) string

type Tag

type Tag func(...View) View

Tag is the starting point of an element.

var (
	// Normal tags requiring closing tag.
	A          Tag = El("a", false)
	Abbr       Tag = El("abbr", false)
	Address    Tag = El("address", false)
	Article    Tag = El("article", false)
	Aside      Tag = El("aside", false)
	Audio      Tag = El("audio", false)
	B          Tag = El("b", false)
	Bdi        Tag = El("bdi", false)
	Bdo        Tag = El("bdo", false)
	Blockquote Tag = El("blockquote", false)
	Body       Tag = El("body", false)
	Button     Tag = El("button", false)
	Canvas     Tag = El("canvas", false)
	Caption    Tag = El("caption", false)
	Cite       Tag = El("cite", false)
	Code       Tag = El("code", false)
	Colgroup   Tag = El("colgroup", false)
	Data       Tag = El("data", false)
	Datalist   Tag = El("datalist", false)
	Dd         Tag = El("dd", false)
	Del        Tag = El("del", false)
	Dfn        Tag = El("dfn", false)
	Div        Tag = El("div", false)
	Dl         Tag = El("dl", false)
	Dt         Tag = El("dt", false)
	Em         Tag = El("em", false)
	Fieldset   Tag = El("fieldset", false)
	Figcaption Tag = El("figcaption", false)
	Figure     Tag = El("figure", false)
	Footer     Tag = El("footer", false)
	Form       Tag = El("form", false)
	H1         Tag = El("h1", false)
	H2         Tag = El("h2", false)
	H3         Tag = El("h3", false)
	H4         Tag = El("h4", false)
	H5         Tag = El("h5", false)
	H6         Tag = El("h6", false)
	Head       Tag = El("head", false)
	Header     Tag = El("header", false)
	Html       Tag = El("html", false)
	I          Tag = El("i", false)
	Iframe     Tag = El("iframe", false)
	Ins        Tag = El("ins", false)
	Kbd        Tag = El("kbd", false)
	Label      Tag = El("label", false)
	Legend     Tag = El("legend", false)
	Li         Tag = El("li", false)
	Main       Tag = El("main", false)
	Map        Tag = El("map", false)
	Mark       Tag = El("mark", false)
	Meter      Tag = El("meter", false)
	Nav        Tag = El("nav", false)
	Noscript   Tag = El("noscript", false)
	Object     Tag = El("object", false)
	Ol         Tag = El("ol", false)
	Optgroup   Tag = El("optgroup", false)
	Option     Tag = El("option", false)
	Output     Tag = El("output", false)
	P          Tag = El("p", false)
	Pre        Tag = El("pre", false)
	Progress   Tag = El("progress", false)
	Q          Tag = El("q", false)
	Rb         Tag = El("rb", false)
	Rp         Tag = El("rp", false)
	Rt         Tag = El("rt", false)
	Rtc        Tag = El("rtc", false)
	Ruby       Tag = El("ruby", false)
	S          Tag = El("s", false)
	Samp       Tag = El("samp", false)
	Script     Tag = El("script", false)
	Section    Tag = El("section", false)
	Select     Tag = El("select", false)
	Small      Tag = El("small", false)
	Span       Tag = El("span", false)
	Strong     Tag = El("strong", false)
	Style      Tag = El("style", false)
	Sub        Tag = El("sub", false)
	Sup        Tag = El("sup", false)
	Table      Tag = El("table", false)
	Tbody      Tag = El("tbody", false)
	Td         Tag = El("td", false)
	Template   Tag = El("template", false)
	Textarea   Tag = El("textarea", false)
	Tfoot      Tag = El("tfoot", false)
	Th         Tag = El("th", false)
	Thead      Tag = El("thead", false)
	Time       Tag = El("time", false)
	Title      Tag = El("title", false)
	Tr         Tag = El("tr", false)
	U          Tag = El("u", false)
	Ul         Tag = El("ul", false)
	Var        Tag = El("var", false)
	Video      Tag = El("video", false)

	// Void elements that must be self closed.
	Area   Tag = El("area", true)
	Base   Tag = El("base", true)
	Br     Tag = El("br", true)
	Col    Tag = El("col", true)
	Embed  Tag = El("embed", true)
	Hr     Tag = El("hr", true)
	Img    Tag = El("img", true)
	Input  Tag = El("input", true)
	Keygen Tag = El("keygen", true)
	Link   Tag = El("link", true)
	Meta   Tag = El("meta", true)
	Param  Tag = El("param", true)
	Source Tag = El("source", true)
	Track  Tag = El("track", true)
	Wbr    Tag = El("wbr", true)
)

func E

func E(tag string) Tag

E creates an element that can hold children.

func El

func El(tag string, isVoid bool) Tag

El creates a Tag func for the given tag name and isVoid flag.

func (Tag) Add

func (t Tag) Add(children ...View) View

Add appends the children provided as views to the current tag.

func (Tag) Atts

func (t Tag) Atts(pairs ...string) Tag

Atts creates a tag with the given pairs of Attributes.

func (Tag) Class

func (t Tag) Class(class string) Tag

Adds the class attribute with the given value

func (Tag) Fmt

func (t Tag) Fmt(format string, args ...interface{}) View

Fmt creates a Text tag using the given format and args like Sprintf.

func (Tag) Text

func (t Tag) Text(c ...string) View

Text will create an Element Node from the Tag and then immediately add the given strings as Text nodes.

func (Tag) ToNode

func (t Tag) ToNode() *Node

ToNode renders the Tag as a Node.

type ToNode

type ToNode func() *Node

ToNode is an empty function that returns a rendered/completed Node.

func (ToNode) ToNode

func (t ToNode) ToNode() *Node

ToNode here implements the View interface, meaning that a ToNode function can now be used like a View.

type ToView

type ToView func() View

ToView is the functional equivalent of Viewable, which allows an empty function returning a View to be a Viewable.

func (ToView) ToView

func (v ToView) ToView() View

ToView method implments the Viewable interface for the ToView func.

type Type

type Type int
const (
	Textual       Type = 1
	Element       Type = 2
	Attribute     Type = 3
	NodeList      Type = 4
	AttributeList Type = 5
)

The types of Node(s)

func (Type) String

func (i Type) String() string

type View

type View interface {
	ToNode() *Node
}

View interface represents anything that can be turned into a Node via the ToNode() function.

func Att

func Att(key, value string) View

Att creates a new Node with Attribute type and the given key, value pair.

func Atts

func Atts(pairs ...string) View

Atts attempts to pair up parameters and make an AttributeList.

func Def

func Def(val interface{}, def interface{}) View

Def takes two values, should the first be unconvertible to a Viewable or a view it will then attempt to convert the second. Nil is not convertible so in cases where the first is nil, the second will be used if possible. Should they both be nil the View will be of an empty Text node.

func Fmt

func Fmt(format string, args ...interface{}) View

Fmt creates a Text node using Sprintf.

func Frag

func Frag(children ...View) View

Frag creates a view that is a list of children views without a containing element.

func Maybe

func Maybe(val interface{}) View

Maybe check if val is nil, a view or a viewable, and if so returns a View based on the val, but if it's none of these then it returns an empty Text node.

func None

func None() View

None produces an empty Text Node.

func Text

func Text(c string) View

Text creates a new Node with Textual type and CData from the provided string.

type Viewable

type Viewable interface {
	ToView() View
}

Viewable is the interface for anything that can be rendered into a View.

type Views

type Views interface {
	Viewable
	View
	Add(views ...View) Views
}

Views represent a list of views that can be rendered as a one View.

Jump to

Keyboard shortcuts

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