mdtable

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2021 License: MIT Imports: 3 Imported by: 1

README

mdtable

godoc ci

Package mdtable generates markdown tables from string slices with formatting options for alignment and column width.

Functions

func Generate

func Generate(data [][]string, options ...Option) []byte

Generate generates a markdown table.

data -- is the table data. The top level slice contains rows and second level slices are cells. The first row is the header row. You need at least two rows (including the header row) to create a valid markdown table.

options -- are options for formatting the table. All options are in options.md.

There are three different types of alignment available, Alignment, TextAlignment and HeaderAlignment. Each of these can be set at either the table or column level.

Alignment -- controls how the markdown will appear when rendered in a browser.

TextAlignment -- controls the text alignment of fields. Default behavior is using the Alignment value of AlignLeft if that is unset.

HeaderAlignment -- is the same as TextAlignment, but for the header row only. Default behavior is using the TextAlignment value.

Examples

package main

import (
	"fmt"
	"github.com/willabides/mdtable"
)

func main() {
	data := [][]string{
		// first row is the header
		{"Name", "Favorite Animal", "Lucky Number"},

		// the rest is data
		{"Dave", "Elephant", "7"},
		{"Iris", "Gorilla", "8"},
		{"Ava Gayle", "Sloth", "972.5"},
	}

	b := mdtable.Generate(data)
	fmt.Println(string(b))

}

Output:

| Name      | Favorite Animal | Lucky Number |
|-----------|-----------------|--------------|
| Dave      | Elephant        | 7            |
| Iris      | Gorilla         | 8            |
| Ava Gayle | Sloth           | 972.5        |
Options
package main

import (
	"fmt"
	"github.com/willabides/mdtable"
)

func main() {
	// This adds options one at a time and shows what the output of
	// mdtable.Generate would be after each option is added.

	data := [][]string{
		{"Name", "Favorite Animal", "Lucky Number"},
		{"Dave", "Elephant", "7"},
		{"Iris", "Gorilla", "8"},
		{"Ava Gayle", "Sloth", "972.5"},
	}

	var options []mdtable.Option

	// Right align the whole table
	options = append(options,
		mdtable.Alignment(mdtable.AlignRight),
	)

	/*
		|      Name | Favorite Animal | Lucky Number |
		|----------:|----------------:|-------------:|
		|      Dave |        Elephant |            7 |
		|      Iris |         Gorilla |            8 |
		| Ava Gayle |           Sloth |        972.5 |
	*/

	// Left align header text
	options = append(options,
		mdtable.HeaderAlignment(mdtable.AlignLeft),
	)

	/*
		| Name      | Favorite Animal | Lucky Number |
		|----------:|----------------:|-------------:|
		|      Dave |        Elephant |            7 |
		|      Iris |         Gorilla |            8 |
		| Ava Gayle |           Sloth |        972.5 |
	*/

	// Set Favorite Animal's (offset 1) minimum width to 20 and center its text
	options = append(options,
		mdtable.ColumnMinWidth(1, 20),
		mdtable.ColumnTextAlignment(1, mdtable.AlignCenter),
	)

	/*
		| Name      | Favorite Animal      | Lucky Number |
		|----------:|---------------------:|-------------:|
		|      Dave |       Elephant       |            7 |
		|      Iris |       Gorilla        |            8 |
		| Ava Gayle |        Sloth         |        972.5 |
	*/

	options = append(options,
		mdtable.ColumnAlignment(0, mdtable.AlignLeft), // Left align Name
	)

	b := mdtable.Generate(data, options...)
	fmt.Println(string(b))

}

Output:

| Name      | Favorite Animal      | Lucky Number |
|:----------|---------------------:|-------------:|
| Dave      |       Elephant       |            7 |
| Iris      |       Gorilla        |            8 |
| Ava Gayle |        Sloth         |        972.5 |

Readme created from Go doc with goreadme

Documentation

Overview

Package mdtable generates markdown tables from string slices with formatting options for alignment and column width.

Example
package main

import (
	"fmt"

	"github.com/willabides/mdtable"
)

func main() {
	data := [][]string{
		// first row is the header
		{"Name", "Favorite Animal", "Lucky Number"},

		// the rest is data
		{"Dave", "Elephant", "7"},
		{"Iris", "Gorilla", "8"},
		{"Ava Gayle", "Sloth", "972.5"},
	}

	b := mdtable.Generate(data)
	fmt.Println(string(b))

}
Output:

| Name      | Favorite Animal | Lucky Number |
|-----------|-----------------|--------------|
| Dave      | Elephant        | 7            |
| Iris      | Gorilla         | 8            |
| Ava Gayle | Sloth           | 972.5        |
Example (Options)
package main

import (
	"fmt"

	"github.com/willabides/mdtable"
)

func main() {
	// This adds options one at a time and shows what the output of
	// mdtable.Generate would be after each option is added.

	data := [][]string{
		{"Name", "Favorite Animal", "Lucky Number"},
		{"Dave", "Elephant", "7"},
		{"Iris", "Gorilla", "8"},
		{"Ava Gayle", "Sloth", "972.5"},
	}

	var options []mdtable.Option

	// Right align the whole table
	options = append(options,
		mdtable.Alignment(mdtable.AlignRight),
	)

	/*
		|      Name | Favorite Animal | Lucky Number |
		|----------:|----------------:|-------------:|
		|      Dave |        Elephant |            7 |
		|      Iris |         Gorilla |            8 |
		| Ava Gayle |           Sloth |        972.5 |
	*/

	// Left align header text
	options = append(options,
		mdtable.HeaderAlignment(mdtable.AlignLeft),
	)

	/*
		| Name      | Favorite Animal | Lucky Number |
		|----------:|----------------:|-------------:|
		|      Dave |        Elephant |            7 |
		|      Iris |         Gorilla |            8 |
		| Ava Gayle |           Sloth |        972.5 |
	*/

	// Set Favorite Animal's (offset 1) minimum width to 20 and center its text
	options = append(options,
		mdtable.ColumnMinWidth(1, 20),
		mdtable.ColumnTextAlignment(1, mdtable.AlignCenter),
	)

	/*
		| Name      | Favorite Animal      | Lucky Number |
		|----------:|---------------------:|-------------:|
		|      Dave |       Elephant       |            7 |
		|      Iris |       Gorilla        |            8 |
		| Ava Gayle |        Sloth         |        972.5 |
	*/

	options = append(options,
		mdtable.ColumnAlignment(0, mdtable.AlignLeft), // Left align Name
	)

	b := mdtable.Generate(data, options...)
	fmt.Println(string(b))

}
Output:

| Name      | Favorite Animal      | Lucky Number |
|:----------|---------------------:|-------------:|
| Dave      |       Elephant       |            7 |
| Iris      |       Gorilla        |            8 |
| Ava Gayle |        Sloth         |        972.5 |

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate added in v0.3.0

func Generate(data [][]string, options ...Option) []byte

Generate generates a markdown table.

data -- is the table data. The top level slice contains rows and second level slices are cells. The first row is the header row. You need at least two rows (including the header row) to create a valid markdown table.

options -- are options for formatting the table. All options are in options.md.

There are three different types of alignment available, Alignment, TextAlignment and HeaderAlignment. Each of these can be set at either the table or column level.

Alignment -- controls how the markdown will appear when rendered in a browser.

TextAlignment -- controls the text alignment of fields. Default behavior is using the Alignment value of AlignLeft if that is unset.

HeaderAlignment -- is the same as TextAlignment, but for the header row only. Default behavior is using the TextAlignment value.

Types

type Align

type Align uint8

Align is a value for markdown and text alignment

const (
	AlignDefault Align = iota // markdown: |--------|  text: | foo     |
	AlignLeft                 // markdown: |:-------|  text: | foo     |
	AlignRight                // markdown: |-------:|  text: |     foo |
	AlignCenter               // markdown: |:------:|  text: |   foo   |
)

Align values

type Option added in v0.3.0

type Option func(*table)

Option is an option to control a table's formatting

func Alignment added in v0.3.0

func Alignment(val Align) Option

Alignment sets alignment for columns.

func ColumnAlignment added in v0.3.0

func ColumnAlignment(column int, alignment Align) Option

ColumnAlignment sets the markdown alignment for a column

func ColumnHeaderAlignment added in v0.3.0

func ColumnHeaderAlignment(column int, alignment Align) Option

ColumnHeaderAlignment sets the text alignment for a column header

func ColumnMinWidth added in v0.3.0

func ColumnMinWidth(column, width int) Option

ColumnMinWidth sets the minimum width for a column

func ColumnTextAlignment added in v0.3.0

func ColumnTextAlignment(column int, alignment Align) Option

ColumnTextAlignment sets the text alignment for a column

func HeaderAlignment added in v0.3.0

func HeaderAlignment(val Align) Option

HeaderAlignment sets the text alignment for headers

func TextAlignment added in v0.3.0

func TextAlignment(val Align) Option

TextAlignment sets the default text alignment for non-header cells

Jump to

Keyboard shortcuts

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