downtable

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 6 Imported by: 0

README

Downtable

package downtable generates markdown tables from csv and json files. there is methods for modifying the table data before generating the markdown table.

Installation

go get -u github.com/dextor19/downtable

Usage

Parsing CSV File

Providing an CSV file as a input for the markdown table you need to use the WithCSVFile(*os.File, lazyQuotes: bool, trimLeadingQuotes: bool) function inside of the AddTable() method on the MarkdownTable interface. When using providing a CSV file you need to enable formatting options based on the type of CSV file, options lazyQuotes " double quotes are allowed in csv fields and trimLeadingQuotes leading white spaces in the csv field is ignored.

Parsing JSON Files

JSON files are also able to provided as input for markdown tables, using the MarkdownTable method AddJSONFileTable(*os.File).

Parse a JSON file it requires a specific format:

{
    "Headers": [
        "header1",
        "header2",
        "header3",
    ],
    "Rows": [
        [
            "row1item1",
            "row1item2",
            "row1item3",
        ],
        [
            "row2item1",
            "row2item2",
            "row3item3",
        ]
    ]
}
Output Markdown Table String

there are three way of exporting a Markdown table, with the MarkdownTable interface you are able to output the markdown table in os.Stdout, string or []byte type.

Stdout

PrintMarkdownTable() prints a single string of the markdown table via the standard output.

String

GetMarkdownTableString() outputs a single string of the markdown table.

Byte

GetMarkdownTable() outputs a byte array containing a single string of the markdown table.

Examples

CSV File
package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"

    "github.com/dextor19/downtable"
)

func main() {
    input := []byte("header1, header2, header3, header4, header5\nrow1item1, row1item2, row1item3, row1item4, row1item5\nrow2item1, row2item2, row2item3, row2item4, row2item5\nrow3item1, row3item2, row3item3, row3item4, row3item5\n")
    mdt := downtable.NewMarkdownTable()
    tmpFile, err := ioutil.TempFile(os.TempDir(), "temp_example.csv")
    if err != nil {
        log.Fatal(err)
    }
    if _, err := tmpFile.Write(input); err != nil {
        log.Fatal(err)
    }
    if _, err := tmpFile.Seek(0, 0); err != nil {
        log.Fatal(err)
    }
    mdt.AddTable(downtable.WithCSVFile(tmpFile, true, true))
    _, err = mdt.PrintMarkdownTable()
    if err != nil {
        log.Fatal(err)
    }
}
JSON File
package main

import (
    "encoding/json"
    "io/ioutil"
    "log"
    "os"

    "github.com/dextor19/downtable"
)

type Table struct {
    Headers []string
    Rows    [][]string
}

func main() {
    input := Table{
        Headers: []string{"header1", "header2", "header3", "header4", "header5"},
        Rows: [][]string{{"row1item1", "row1item2", "row1item3", "row1item4", "row1item5"},
            {"row2item1", "row2item2", "row2item3", "row2item4", "row2item5"},
            {"row3item1", "row3item2", "row3item3", "row3item4", "row3item5"}},
    }
    jsonInput, err := json.Marshal(input)
    if err != nil {
        log.Fatal(err)
    }
    mdt := downtable.NewMarkdownTable()
    tmpFile, err := ioutil.TempFile(os.TempDir(), "temp_example.json")
    if err != nil {
        log.Fatal(err)
    }
    if _, err := tmpFile.Write(jsonInput); err != nil {
        log.Fatal(err)
    }
    if _, err := tmpFile.Seek(0, 0); err != nil {
        log.Fatal(err)
    }
    mdt.AddJSONFileTable(tmpFile)
    _, err = mdt.PrintMarkdownTable()
    if err != nil {
        log.Fatal(err)
    }
}
Output
| header1  | header2  | header3  | header4  | header5    |
|-------|-------|-------|-------|-------|
| row1item1    | row1item2    | row1item3    | row1item4    | row1item5    |
| row2item1    | row2item2    | row2item3    | row2item4    | row2item5    |
| row3item1    | row3item2    | row3item3    | row3item4    | row3item5    |
header1 header2 header3 header4 header5
row1item1 row1item2 row1item3 row1item4 row1item5
row2item1 row2item2 row2item3 row2item4 row2item5
row3item1 row3item2 row3item3 row3item4 row3item5

Documentation

Overview

package downtable generates markdown tables from csv and json files. there is methods for modifying the table data before generating the markdown table.

to parse an csv file as a input for the markdown table you need to use the WithCSVFile function inside of the [AddTable] method on the MarkdownTable interface. csv data needs to have the first row be the headers and the subsequent rows will be general rows within the table.

"header1, header2,header3,\nrow1item1, row1item2, row1item3\n"

when using providing a csv file you need to enable formatting options based on the type of csv file, options `lazyQuotes` " double quotes are allowed in csv fields and `trimLeadingQuotes` leading white spaces in the csv field is ignored.

json files are also able to provided as input for markdown tables, using the MarkdownTable method [AddJSONFileTable].

to parse json files the package requires the following format:

{
  "Headers": ["header1"],
  "Rows": [["row1item1"], ["row2item1"]]
}

main idea is to use the MarkdownTable interface to parse array strings and produce a string of markdown that represents a table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCSVFile

func WithCSVFile(file *os.File, lazyQuotes bool, trimLeadingSpace bool) ([][]string, error)

WithCSVFile takes a file pointer with csv reader options as parameters to the function and outputs a matrix of strings which the first row in the matrix is the headers and the other rows are row items for the markdown table.

the matrix of strings output is meant for the MarkdownTable.AddTable() method which populates [table] struct instances using matrix of strings.

Types

type MarkdownTable

type MarkdownTable interface {
	// AddHeader adds a single string to the end of the headers string array
	AddHeader(string)

	// AddHeaders replaces the array of strings in table instance with new array of strings
	// that will be used as headers for the markdown table.
	AddHeaders([]string)

	// DeleteHeaders removes all items inside the array of strings for table.headers
	DeleteHeaders()

	// AddRowItem adds a single items to one row in the matrix of arrays for table.rows.
	// requires an input specifying which row in the matrix this string will be appended too.
	AddRowItem(string, int)

	// AddRow will append an addition row to the matrix of strings for table.rows.
	AddRow([]string) error

	// AddRows will replace all matrix of strings with a new matrix for table.rows.
	AddRows([][]string)

	// DeleteRows will delete the matrix of strings in table.rows.
	DeleteRows()

	// AddTable takes a matrix of strings as input and will use the first row in the matrix
	// as the headers list of strings and replace existing headers. all other rows will be
	// appended to the matrix of strings for table.rows.
	//
	// AddTable requires that the headers and rows have the same number of items in the string array
	// otherwise error will occur saying that header and row arrays need to have the same length.
	AddTable([][]string, error) error

	// AddJSONFileTable takes a file pointer as input requires the file associated with the
	// pointer contains a json formatted object and has a specific Headers and Rows structure.
	// this method will replace existing headers in the table instance.
	AddJSONFileTable(*os.File) error

	// GetMarkdownTableString a single string of the markdown table via the standard output.
	GetMarkdownTableString() (string, error)

	// GetMarkdownTableString outputs a single string of the markdown table.
	GetMarkdownTable() ([]byte, error)

	// GetMarkdownTable outputs a byte array containing a single string of the markdown table.
	PrintMarkdownTable() (int, error)
}

MarkdownTable interface defines all the methods that consumers of this package can use to generate a markdown table string.

func NewMarkdownTable

func NewMarkdownTable() MarkdownTable

NewMarkdownTable initiates a empty instance of a [table] struct with a MarkdownTable interface that allows you to modify the data within the [table] instance using getter and setter methods or formatted data files.

Jump to

Keyboard shortcuts

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