htmltable

package
v0.0.0-...-508fc3c Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	HTMLPreCellFormatter retable.CellFormatterFunc = func(ctx context.Context, view retable.View, row, col int) (str string, raw bool, err error) {
		value := template.HTMLEscapeString(fmt.Sprint(view.AnyValue(row, col)))
		return "<pre>" + value + "</pre>", true, nil
	}

	HTMLPreCodeCellFormatter retable.CellFormatterFunc = func(ctx context.Context, view retable.View, row, col int) (str string, raw bool, err error) {
		value := template.HTMLEscapeString(fmt.Sprint(view.AnyValue(row, col)))
		return "<pre><code>" + value + "</code></pre>", true, nil
	}

	// ValueAsHTMLAnchorCellFormatter formats the cell value using fmt.Sprint,
	// escapes it for HTML and returns an HTML anchor element with the
	// value as id and inner text.
	ValueAsHTMLAnchorCellFormatter retable.CellFormatterFunc = func(ctx context.Context, view retable.View, row, col int) (str string, raw bool, err error) {
		value := template.HTMLEscapeString(fmt.Sprint(view.AnyValue(row, col)))
		return fmt.Sprintf("<a id='%[1]s'>%[1]s</a>", value), true, nil
	}
)
View Source
var (
	HeaderTemplate = template.Must(template.New("header").Parse(
		"<table{{if .TableClass}} class='{{.TableClass}}'{{end}}>\n" +
			"{{if .Caption}}  <caption>{{.Caption}}</caption>\n{{end}}",
	))

	RowTemplate = template.Must(template.New("row").Parse("" +
		"{{if .IsHeaderRow}}" +
		"  <tr>{{range $cell := .RawCells}}<th>{{$cell}}</th>{{end}}</tr>\n" +
		"{{else}}" +
		"  <tr>{{range $cell := .RawCells}}<td>{{$cell}}</td>{{end}}</tr>\n" +
		"{{end}}",
	))

	FooterTemplate = template.Must(template.New("footer").Parse(
		"</table>",
	))
)

Functions

This section is empty.

Types

type HTMLSpanClassCellFormatter

type HTMLSpanClassCellFormatter string

HTMLSpanClassCellFormatter formats the cell value within an HTML span element with the class of the underlying string value.

func (HTMLSpanClassCellFormatter) FormatCell

func (class HTMLSpanClassCellFormatter) FormatCell(ctx context.Context, view retable.View, row, col int) (str string, raw bool, err error)

type JSONCellFormatter

type JSONCellFormatter string

JSONCellFormatter formats the cell value as JSON in a <pre> HTML element. The string value of the formatter is used as indentation for the JSON or if empty, then the JSON is compacted. Any value that formats to an empty string (like nil) will not be interpreted as JSON and "", false, nil will be returned.

func (JSONCellFormatter) FormatCell

func (indent JSONCellFormatter) FormatCell(ctx context.Context, view retable.View, row, col int) (str string, raw bool, err error)

type RowTemplateContext

type RowTemplateContext struct {
	TemplateContext

	IsHeaderRow bool
	RowIndex    int
	RawCells    []template.HTML
}

type TemplateContext

type TemplateContext struct {
	TableClass string
	Caption    string
}

type Writer

type Writer[T any] struct {
	// contains filtered or unexported fields
}
Example
type Row struct {
	Status        json.RawMessage `db:"-"              col:"Status"`
	CompanyName   string          `db:"company_name"   col:"Company"`
	InternalNames []string        `db:"internal_names" col:"-"`
	CompanyID     uint64          `db:"company_id"     col:"Company ID"`
}
table := []Row{
	{Status: nil, CompanyName: "Company 1", InternalNames: []string{"Company 1a"}, CompanyID: 1},
	{Status: json.RawMessage(`{"ok":true}`), CompanyName: "Company 2", InternalNames: nil, CompanyID: 2},
}

NewWriter[[]Row]().
	WithHeaderRow(true).
	WithTypeFormatter(reflect.TypeOf(json.RawMessage(nil)), JSONCellFormatter("")).
	Write(context.Background(), os.Stdout, table, "Table Title")
Output:

<table>
  <caption>Table Title</caption>
  <tr><th>Status</th><th>Company</th><th>Company ID</th></tr>
  <tr><td></td><td>Company 1</td><td>1</td></tr>
  <tr><td><pre>{"ok":true}</pre></td><td>Company 2</td><td>2</td></tr>
</table>

func NewWriter

func NewWriter[T any]() *Writer[T]

func (*Writer[T]) NilValue

func (w *Writer[T]) NilValue() template.HTML

func (*Writer[T]) TableClass

func (w *Writer[T]) TableClass() string

func (*Writer[T]) WithColumnFormatter

func (w *Writer[T]) WithColumnFormatter(columnIndex int, formatter retable.CellFormatter) *Writer[T]

WithColumnFormatter returns a new writer with the passed formatter registered for columnIndex. If nil is passed as formatter, then a previous registered column formatter is removed.

func (*Writer[T]) WithColumnFormatterFunc

func (w *Writer[T]) WithColumnFormatterFunc(columnIndex int, formatterFunc retable.CellFormatterFunc) *Writer[T]

WithColumnFormatterFunc returns a new writer with the passed formatterFunc registered for columnIndex. If nil is passed as formatterFunc, then a previous registered column formatter is removed.

func (*Writer[T]) WithHeaderRow

func (w *Writer[T]) WithHeaderRow(headerRow bool) *Writer[T]

func (*Writer[T]) WithInterfaceTypeFormatter

func (w *Writer[T]) WithInterfaceTypeFormatter(typ reflect.Type, fmt retable.CellFormatter) *Writer[T]

func (*Writer[T]) WithInterfaceTypeFormatterFunc

func (w *Writer[T]) WithInterfaceTypeFormatterFunc(typ reflect.Type, fmt retable.CellFormatterFunc) *Writer[T]

func (*Writer[T]) WithKindFormatter

func (w *Writer[T]) WithKindFormatter(kind reflect.Kind, fmt retable.CellFormatter) *Writer[T]

func (*Writer[T]) WithKindFormatterFunc

func (w *Writer[T]) WithKindFormatterFunc(kind reflect.Kind, fmt retable.CellFormatterFunc) *Writer[T]

func (*Writer[T]) WithNilValue

func (w *Writer[T]) WithNilValue(nilValue template.HTML) *Writer[T]

func (*Writer[T]) WithRawColumn

func (w *Writer[T]) WithRawColumn(columnIndex int) *Writer[T]

WithRawColumn returns a new writer that interprets the collumn with columnIndex as raw HTML strings.

func (*Writer[T]) WithTableClass

func (w *Writer[T]) WithTableClass(tableClass string) *Writer[T]

func (*Writer[T]) WithTableViewer

func (w *Writer[T]) WithTableViewer(viewer retable.Viewer) *Writer[T]

func (*Writer[T]) WithTemplate

func (w *Writer[T]) WithTemplate(tableTemplate, rowTemplate, footerTemplate *template.Template) *Writer[T]

func (*Writer[T]) WithTypeFormatter

func (w *Writer[T]) WithTypeFormatter(typ reflect.Type, fmt retable.CellFormatter) *Writer[T]

func (*Writer[T]) WithTypeFormatterFunc

func (w *Writer[T]) WithTypeFormatterFunc(typ reflect.Type, fmt retable.CellFormatterFunc) *Writer[T]

func (*Writer[T]) WithTypeFormatterReflectFunc

func (w *Writer[T]) WithTypeFormatterReflectFunc(function any) *Writer[T]

func (*Writer[T]) WithTypeFormatterReflectRawFunc

func (w *Writer[T]) WithTypeFormatterReflectRawFunc(function any) *Writer[T]

func (*Writer[T]) WithTypeFormatters

func (w *Writer[T]) WithTypeFormatters(formatter *retable.ReflectTypeCellFormatter) *Writer[T]

func (*Writer[T]) Write

func (w *Writer[T]) Write(ctx context.Context, dest io.Writer, table T, caption ...string) error

Write calls WriteView with the result of Viewer.NewView(table) using the writer's viewer if not nil or else retable.DefaultViewer.

func (*Writer[T]) WriteView

func (w *Writer[T]) WriteView(ctx context.Context, dest io.Writer, view retable.View) error

func (*Writer[T]) WriteWithViewer

func (w *Writer[T]) WriteWithViewer(ctx context.Context, dest io.Writer, viewer retable.Viewer, table T, caption ...string) error

Jump to

Keyboard shortcuts

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