room

package
v0.0.0-...-1d33cbc Latest Latest
Warning

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

Go to latest
Published: May 5, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

room

Room is an extensible HTML document rendering tool.

Documentation

Overview

Package room provides a tool to compile and build HTML pages out of data structures defined here.

Index

Constants

View Source
const (
	// ColumnOpen is the tag that opens a column.
	ColumnOpen = `<div class="room-col">`

	// ColumnClose is the tag that closes a column.
	ColumnClose = "</div>"
)
View Source
const RowTagName = "div"

RowTagName is the HTML tag name given to Row elements.

View Source
const SectionTagName = "section"

SectionTagName is the HTML tag name given to Section elements.

View Source
const StaticElementType = "_static"

StaticElementType is the Type given to those Section, Row, and Module elements whose final HTML form is known after compilation.

View Source
const TreeType = "room_tree"

TreeType is the Type given to the Module elements that contain a Tree in the Data.

Variables

This section is empty.

Functions

func CompileStyles

func CompileStyles(styles map[string]string, css *PageCSS) (classes []string)

CompileStyles writes CSS code out to the css buffer and gives a list of additional classes the element being processed should be given. The styles map has CSS property keys and values, though most keys are shortened, not standard CSS property names.

func ElementOpenTag

func ElementOpenTag(tagName string, idAttr string, classes []string, buf *bytes.Buffer)

ElementOpenTag writes to buf an opening HTML tag.

func WriteClassAttr

func WriteClassAttr(classes []string, b *bytes.Buffer)

WriteClassAttr checks if classes is not empty, and if it's not empty this function writes to the buffer the string ` class="classes given"` where "classes given" is the classes argument's values joined by a space.

func WriteIdAttr

func WriteIdAttr(id string, b *bytes.Buffer)

WriteIdAttr checks if id is not an empty string, and if it's not empty this function writes to the buffer the string ` id="id-given"` where "id-given" is the id argument.

Types

type Column

type Column struct {
	Modules              []*Module `protobuf:"bytes,1,rep,name=modules,proto3" json:"modules,omitempty"`
	XXX_NoUnkeyedLiteral struct{}  `json:"-"`
	XXX_unrecognized     []byte    `json:"-"`
	XXX_sizecache        int32     `json:"-"`
}

func (*Column) Descriptor

func (*Column) Descriptor() ([]byte, []int)

func (*Column) GetModules

func (m *Column) GetModules() []*Module

func (*Column) ProtoMessage

func (*Column) ProtoMessage()

func (*Column) Reset

func (m *Column) Reset()

func (*Column) String

func (m *Column) String() string

func (*Column) XXX_DiscardUnknown

func (m *Column) XXX_DiscardUnknown()

func (*Column) XXX_Marshal

func (m *Column) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Column) XXX_Merge

func (m *Column) XXX_Merge(src proto.Message)

func (*Column) XXX_Size

func (m *Column) XXX_Size() int

func (*Column) XXX_Unmarshal

func (m *Column) XXX_Unmarshal(b []byte) error

type Common

type Common struct {
	// Type indicates the broad type of the structure.
	Type string `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"`
	// IdAttr is the user's custom ID attribute for the element.
	IdAttr string `protobuf:"bytes,2,opt,name=id_attr,json=idAttr,proto3" json:"id_attr,omitempty"`
	// Classes is the user's custom classes.
	// For compiled sections and rows, this includes classes added by module compilers for styling.
	Classes []string `protobuf:"bytes,3,rep,name=classes,proto3" json:"classes,omitempty"`
	// Styles is the user's styles for the row.
	Styles map[string]string `` /* 153-byte string literal not displayed */
	// Options contains additional options.
	Options              map[string]string `` /* 155-byte string literal not displayed */
	XXX_NoUnkeyedLiteral struct{}          `json:"-"`
	XXX_unrecognized     []byte            `json:"-"`
	XXX_sizecache        int32             `json:"-"`
}

Common is a data structure commonly used in this package in various other types.

func (*Common) Descriptor

func (*Common) Descriptor() ([]byte, []int)

func (*Common) GetClasses

func (m *Common) GetClasses() []string

func (*Common) GetIdAttr

func (m *Common) GetIdAttr() string

func (*Common) GetOptions

func (m *Common) GetOptions() map[string]string

func (*Common) GetStyles

func (m *Common) GetStyles() map[string]string

func (*Common) GetType

func (m *Common) GetType() string

func (*Common) ProtoMessage

func (*Common) ProtoMessage()

func (*Common) Reset

func (m *Common) Reset()

func (*Common) String

func (m *Common) String() string

func (*Common) XXX_DiscardUnknown

func (m *Common) XXX_DiscardUnknown()

func (*Common) XXX_Marshal

func (m *Common) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Common) XXX_Merge

func (m *Common) XXX_Merge(src proto.Message)

func (*Common) XXX_Size

func (m *Common) XXX_Size() int

func (*Common) XXX_Unmarshal

func (m *Common) XXX_Unmarshal(b []byte) error

type DataStore

type DataStore interface {
	// Store saves the Data field of a Datum in a data store and returns the ID it was given.
	Store([]byte) (int64, error)

	// Get retrieves a single Datum. This function may be called repeatedly with a single ID throughout the
	// compiling of a Tree or building of a CompiledTree, so all data already queried should be cached for
	// efficient access.
	Get(int64) (*Datum, error)

	// GetMulti retrieves the Datum entities identified by their IDs.
	GetMulti([]int64) ([]Datum, error)
}

A DataStore represents a database: it is able to save Datum entities and retrieve them by ID. An instance of a DataStore should cache internally the Datum entities already retrieved.

type Datum

type Datum struct {
	Id   int64
	Data []byte
}

A Datum represents a generic container in which the dynamic data for a Section, Row, or Module may be saved. The Id of each Datum is a unique identifier.

type ErrMissingModuleBuilder

type ErrMissingModuleBuilder struct {
	*Module
}

ErrMissingModuleBuilder is an error type that reports that a ModuleCompiler could not be found to compile a particular Module.

func (ErrMissingModuleBuilder) Error

func (em ErrMissingModuleBuilder) Error() string

Error implements the error interface for ErrMissingModuleBuilder.

type ErrMissingModuleCompiler

type ErrMissingModuleCompiler struct {
	*Module
}

ErrMissingModuleCompiler is an error type that reports that a ModuleCompiler could not be found to compile a particular Module.

func (ErrMissingModuleCompiler) Error

func (em ErrMissingModuleCompiler) Error() string

Error implements the error interface for ErrMissingModuleCompiler.

type Module

type Module struct {
	// Type indicates broadly the kind of module that this is. It is used to identify what ModuleCompiler or
	// ModuleBuilder to use.
	Type string `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"`
	// Dyn is the ID of the record holding the module's dynamic settings. Using the Dyn field is optional for
	// each module type, but if it is non-zero then it must be the ID of the Datum entity which holds the
	// dynamic settings. (This requirement is intended to make it possible to get all of the data for the
	// entire page tree with a single query before the page is built.)
	Dyn int64 `protobuf:"varint,2,opt,name=dyn,proto3" json:"dyn,omitempty"`
	// Data contains all of the configuration data needed for the module. The data can be encoded in any
	// format, and it's up to the corresponding ModuleCompiler and ModuleBuilder to be able to decode it.
	//
	// When a Module is compiled, if Compile returns true then Data must already contain all the pre-built
	// HTML.
	Data                 []byte   `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

A Module represents the smallest unit of composition that appears as an element in the HTML of a page. We know how to deal with the Data field when we know the Type and have a matching ModuleCompiler to compile it or a matching ModuleBuilder to build its final HTML.

A compiled module may be fully static and ready to be written out to the page without building, in which case the data contains the final HTML and the type is "_static".

func (*Module) Descriptor

func (*Module) Descriptor() ([]byte, []int)

func (*Module) GetData

func (m *Module) GetData() []byte

func (*Module) GetDyn

func (m *Module) GetDyn() int64

func (*Module) GetType

func (m *Module) GetType() string

func (*Module) ProtoMessage

func (*Module) ProtoMessage()

func (*Module) Reset

func (m *Module) Reset()

func (*Module) String

func (m *Module) String() string

func (*Module) XXX_DiscardUnknown

func (m *Module) XXX_DiscardUnknown()

func (*Module) XXX_Marshal

func (m *Module) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Module) XXX_Merge

func (m *Module) XXX_Merge(src proto.Message)

func (*Module) XXX_Size

func (m *Module) XXX_Size() int

func (*Module) XXX_Unmarshal

func (m *Module) XXX_Unmarshal(b []byte) error

type ModuleBuilder

type ModuleBuilder interface {
	// BuildHTML writes to the Buffer the HTML output of the Module. The CSS for the module that should be used
	// with the page is written to the PageCSS buffer.
	BuildHTML(DataStore, ModuleCustomizer, *Module, *bytes.Buffer, *PageCSS) error
}

A ModuleBuilder is able to build out the HTML of a Module. Each Module will be built dynamically upon each request to display if it is not built out into its final static form at compile time.

type ModuleCompiler

type ModuleCompiler interface {
	// Compile compiles a Module. The Type field of the returned *Module must be StaticElementType if the compiled module
	// can be saved as is, using the Data field as the pre-built HTML not needing any building.
	//
	// A ModuleCustomizer is passed in to make recursive Tree compilation possible.
	//
	// The *Module passed in points to the same Module that the ModuleCustomizer got at first. Compile can decode the
	// Data field as needed.
	//
	// As much of the CSS as possible should be written out to PageCSS at compile time.
	Compile(DataStore, ModuleCustomizer, *Module, *PageCSS) (*Module, error)
}

A ModuleCompiler compiles a Module as much as possible to minimize building HTML upon requests to display the module.

type ModuleCustomizer

type ModuleCustomizer interface {
	// GetModuleCompiler returns a ModuleCompiler to customize the way a Module is compiled.
	// This function should not unmarshall the Data field in the Module because the same Module will be passed in
	// to the ModuleCompiler's Compile method.
	GetModuleCompiler(*Module) ModuleCompiler

	// GetModuleBuilder returns a ModuleBuilder to customize the HTML output of a Module.
	// This function should not unmarshall the Data field in the Module because the same Module will be passed in
	// to the ModuleBuilder's BuildHTML method.
	GetModuleBuilder(*Module) ModuleBuilder
}

A ModuleCustomizer is able to customize what ModuleCompiler is used to compile a Module and what ModuleBuilder is used to build its HTML.

type PageCSS

type PageCSS struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

PageCSS embeds the bytes.Buffer type and contains the state of the CSS on the page, which is a record of the modules that have written their CSS to the page. Before a module writes anything to a PageCSSState, it should first check if another module of the same type in the Tree has already written the same CSS.

After the compilation of a Tree, if PageCSS turns out to be not empty then it should be stored and passed in to the Tree upon building.

func (*PageCSS) IsWritten

func (css *PageCSS) IsWritten(moduleType string) bool

IsWritten says if a module with the given type has already written its CSS to the page.

func (*PageCSS) ProtoMessage

func (css *PageCSS) ProtoMessage() proto.Message

ProtoMessage returns a proto.Message that can be used to The concrete type of the message is special.PageCSS.

func (*PageCSS) RecordWriting

func (css *PageCSS) RecordWriting(moduleType string)

RecordWriting records that a module with the given type just now wrote its CSS to the page.

func (*PageCSS) Reset

func (css *PageCSS) Reset()

func (*PageCSS) UnmarshalProto

func (css *PageCSS) UnmarshalProto(msg []byte) error

UnmarshalProto reads the Protocol Buffers encoded message into the internal data structure. The struct is first reset.

type Row

type Row struct {
	// Common includes common fields. The Type is the column layout type of the row.
	Common *Common `protobuf:"bytes,1,opt,name=common,proto3" json:"common,omitempty"`
	// Dyn is the ID of the row's dynamic settings, if not zero.
	Dyn int64 `protobuf:"varint,2,opt,name=dyn,proto3" json:"dyn,omitempty"`
	// Columns contains the row's columns. For compiled Rows, this field is not empty only if there is content
	// within the row that is dynamic.
	Columns []*Column `protobuf:"bytes,3,rep,name=columns,proto3" json:"columns,omitempty"`
	// Html is used only for compiled rows when the entire row is static HTML.
	Html                 []byte   `protobuf:"bytes,4,opt,name=html,proto3" json:"html,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

A Row holds the settings of both static and dynamic page rows. Directly within a Row are columns. A compiled Row only needs to be built, along with everything it contains, to be displayed.

func (*Row) Descriptor

func (*Row) Descriptor() ([]byte, []int)

func (*Row) GetColumns

func (m *Row) GetColumns() []*Column

func (*Row) GetCommon

func (m *Row) GetCommon() *Common

func (*Row) GetDyn

func (m *Row) GetDyn() int64

func (*Row) GetHtml

func (m *Row) GetHtml() []byte

func (*Row) ProtoMessage

func (*Row) ProtoMessage()

func (*Row) Reset

func (m *Row) Reset()

func (*Row) String

func (m *Row) String() string

func (*Row) XXX_DiscardUnknown

func (m *Row) XXX_DiscardUnknown()

func (*Row) XXX_Marshal

func (m *Row) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Row) XXX_Merge

func (m *Row) XXX_Merge(src proto.Message)

func (*Row) XXX_Size

func (m *Row) XXX_Size() int

func (*Row) XXX_Unmarshal

func (m *Row) XXX_Unmarshal(b []byte) error

type Section

type Section struct {
	// Common includes common fields. The Type must be either "standard" or "full".
	Common *Common `protobuf:"bytes,1,opt,name=common,proto3" json:"common,omitempty"`
	// Dyn is the ID of the section's dynamic settings, if not zero.
	Dyn int64 `protobuf:"varint,2,opt,name=dyn,proto3" json:"dyn,omitempty"`
	// Rows contains the section's rows.
	Rows []*Row `protobuf:"bytes,3,rep,name=rows,proto3" json:"rows,omitempty"`
	// Html is used only for compiled rows when the entire row is static HTML.
	Html                 []byte   `protobuf:"bytes,4,opt,name=html,proto3" json:"html,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

A Section holds the settings of a section in a Tree. Within a Section are Row elements. A compiled Section only needs to be built, along with everything it contains, to be displayed.

func (*Section) Descriptor

func (*Section) Descriptor() ([]byte, []int)

func (*Section) GetCommon

func (m *Section) GetCommon() *Common

func (*Section) GetDyn

func (m *Section) GetDyn() int64

func (*Section) GetHtml

func (m *Section) GetHtml() []byte

func (*Section) GetRows

func (m *Section) GetRows() []*Row

func (*Section) ProtoMessage

func (*Section) ProtoMessage()

func (*Section) Reset

func (m *Section) Reset()

func (*Section) String

func (m *Section) String() string

func (*Section) XXX_DiscardUnknown

func (m *Section) XXX_DiscardUnknown()

func (*Section) XXX_Marshal

func (m *Section) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Section) XXX_Merge

func (m *Section) XXX_Merge(src proto.Message)

func (*Section) XXX_Size

func (m *Section) XXX_Size() int

func (*Section) XXX_Unmarshal

func (m *Section) XXX_Unmarshal(b []byte) error

type Tree

type Tree struct {
	Sections             []*Section `protobuf:"bytes,1,rep,name=sections,proto3" json:"sections,omitempty"`
	XXX_NoUnkeyedLiteral struct{}   `json:"-"`
	XXX_unrecognized     []byte     `json:"-"`
	XXX_sizecache        int32      `json:"-"`
}

A Tree represents the structure of a page. Each Section within a Tree may contain Row elements, which contain Column elements, which contain Module elements.

Tree implements the ModuleCompiler and ModuleBuilder interfaces, so a Tree can be nested as a Module.

A compiled Tree represents the structure of a compiled page, though some elements may need to be compiled dynamically. Each compiled Section within a Tree contains compiled Row elements, each of which may in turn contain Column elements with compiled Module elements.

func (*Tree) BuildHTML

func (t *Tree) BuildHTML(ds DataStore, mc ModuleCustomizer, compiled *Module, page *bytes.Buffer, css *PageCSS) error

BuildHTML implements ModuleBuilder for Tree. This function writes the built HTML to the page buffer. The Data field of the compiled Module is unmarshalled and used to construct the Tree.

func (*Tree) Compile

func (t *Tree) Compile(ds DataStore, mc ModuleCustomizer, m *Module, css *PageCSS) (*Module, error)

Compile takes the contents of the Tree and compiles all sections, rows, and modules as much as possible, the remaining elements left to being built dynamically at the time a page is requested.

The m.Data field is unmarshalled and used to construct the Tree.

func (*Tree) Descriptor

func (*Tree) Descriptor() ([]byte, []int)

func (*Tree) DynamicDataIDs

func (t *Tree) DynamicDataIDs() []int64

DynamicDataIDs returns the IDs of the Datum entities that contain the dynamic settings for this Tree.

func (*Tree) GetSections

func (m *Tree) GetSections() []*Section

func (*Tree) ProtoMessage

func (*Tree) ProtoMessage()

func (*Tree) Reset

func (m *Tree) Reset()

func (*Tree) String

func (m *Tree) String() string

func (*Tree) XXX_DiscardUnknown

func (m *Tree) XXX_DiscardUnknown()

func (*Tree) XXX_Marshal

func (m *Tree) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Tree) XXX_Merge

func (m *Tree) XXX_Merge(src proto.Message)

func (*Tree) XXX_Size

func (m *Tree) XXX_Size() int

func (*Tree) XXX_Unmarshal

func (m *Tree) XXX_Unmarshal(b []byte) error

Directories

Path Synopsis
Package roomutil provides a few utility functions and types for changing a room.Tree structure.
Package roomutil provides a few utility functions and types for changing a room.Tree structure.
sample
Package sample has sample Tree structures useful for testing.
Package sample has sample Tree structures useful for testing.

Jump to

Keyboard shortcuts

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