Documentation ¶
Index ¶
- Variables
- type HTMLSpanClassCellFormatter
- type JSONCellFormatter
- type RowTemplateContext
- type TemplateContext
- type Writer
- func (w *Writer[T]) NilValue() template.HTML
- func (w *Writer[T]) TableClass() string
- func (w *Writer[T]) WithColumnFormatter(columnIndex int, formatter retable.CellFormatter) *Writer[T]
- func (w *Writer[T]) WithColumnFormatterFunc(columnIndex int, formatterFunc retable.CellFormatterFunc) *Writer[T]
- func (w *Writer[T]) WithHeaderRow(headerRow bool) *Writer[T]
- func (w *Writer[T]) WithInterfaceTypeFormatter(typ reflect.Type, fmt retable.CellFormatter) *Writer[T]
- func (w *Writer[T]) WithInterfaceTypeFormatterFunc(typ reflect.Type, fmt retable.CellFormatterFunc) *Writer[T]
- func (w *Writer[T]) WithKindFormatter(kind reflect.Kind, fmt retable.CellFormatter) *Writer[T]
- func (w *Writer[T]) WithKindFormatterFunc(kind reflect.Kind, fmt retable.CellFormatterFunc) *Writer[T]
- func (w *Writer[T]) WithNilValue(nilValue template.HTML) *Writer[T]
- func (w *Writer[T]) WithRawColumn(columnIndex int) *Writer[T]
- func (w *Writer[T]) WithTableClass(tableClass string) *Writer[T]
- func (w *Writer[T]) WithTableViewer(viewer retable.Viewer) *Writer[T]
- func (w *Writer[T]) WithTemplate(tableTemplate, rowTemplate, footerTemplate *template.Template) *Writer[T]
- func (w *Writer[T]) WithTypeFormatter(typ reflect.Type, fmt retable.CellFormatter) *Writer[T]
- func (w *Writer[T]) WithTypeFormatterFunc(typ reflect.Type, fmt retable.CellFormatterFunc) *Writer[T]
- func (w *Writer[T]) WithTypeFormatterReflectFunc(function any) *Writer[T]
- func (w *Writer[T]) WithTypeFormatterReflectRawFunc(function any) *Writer[T]
- func (w *Writer[T]) WithTypeFormatters(formatter *retable.ReflectTypeCellFormatter) *Writer[T]
- func (w *Writer[T]) Write(ctx context.Context, dest io.Writer, table T, caption ...string) error
- func (w *Writer[T]) WriteView(ctx context.Context, dest io.Writer, view retable.View) error
- func (w *Writer[T]) WriteWithViewer(ctx context.Context, dest io.Writer, viewer retable.Viewer, table T, ...) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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.Cell(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.Cell(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.Cell(row, col))) return fmt.Sprintf("<a id='%[1]s'>%[1]s</a>", value), true, nil } )
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}}", )) "</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 ¶
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 ¶
type RowTemplateContext ¶
type RowTemplateContext struct { TemplateContext IsHeaderRow bool RowIndex int RawCells []template.HTML }
type TemplateContext ¶
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 (*Writer[T]) TableClass ¶
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 (*Writer[T]) WithInterfaceTypeFormatter ¶
func (*Writer[T]) WithInterfaceTypeFormatterFunc ¶
func (*Writer[T]) WithKindFormatter ¶
func (*Writer[T]) WithKindFormatterFunc ¶
func (*Writer[T]) WithNilValue ¶
func (*Writer[T]) WithRawColumn ¶
WithRawColumn returns a new writer that interprets the collumn with columnIndex as raw HTML strings.
func (*Writer[T]) WithTableClass ¶
func (*Writer[T]) WithTableViewer ¶
func (*Writer[T]) WithTemplate ¶
func (*Writer[T]) WithTypeFormatter ¶
func (*Writer[T]) WithTypeFormatterFunc ¶
func (*Writer[T]) WithTypeFormatterReflectFunc ¶
func (*Writer[T]) WithTypeFormatterReflectRawFunc ¶
func (*Writer[T]) WithTypeFormatters ¶
func (*Writer[T]) Write ¶
Write calls WriteView with the result of Viewer.NewView(table) using the writer's viewer if not nil or else retable.DefaultViewer.