Compare commits
15 Commits
d7ffa94db5
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
4327127200
|
|||
| 08b4ec19d7 | |||
|
6996e4634f
|
|||
|
5dcccb9880
|
|||
| cf2b11fe1a | |||
|
a01edbac7d
|
|||
| 60b2f676bd | |||
|
13dedbac3b
|
|||
| c7fd26e7b4 | |||
|
e7f8f3e3ad
|
|||
|
f1aa5d3eff
|
|||
|
bcb0a39d82
|
|||
|
bdbc7bf16b
|
|||
|
1ff1ae7cd0
|
|||
|
1dbd735d93
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,6 @@
|
||||
/bin
|
||||
# /.vscode
|
||||
*.csv
|
||||
*.tsv
|
||||
*.md
|
||||
!example.csv
|
||||
|
||||
87
.vscode/launch.json
vendored
87
.vscode/launch.json
vendored
@@ -2,19 +2,74 @@
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${fileBasename}",
|
||||
"args": [
|
||||
"example.csv",
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "csv",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "main.go",
|
||||
"args": [
|
||||
"-f=example.csv",
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "csv aligned",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "main.go",
|
||||
"args": [
|
||||
"-f=example.csv",
|
||||
"-a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "csv header",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "main.go",
|
||||
"args": [
|
||||
"-f=example.csv",
|
||||
"-h=\"My Header\"",
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "tsv",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "main.go",
|
||||
"args": [
|
||||
"-f=example.tsv",
|
||||
"-t"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "tsv aligned",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "main.go",
|
||||
"args": [
|
||||
"-f=example.tsv",
|
||||
"-t",
|
||||
"-a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "tsv header",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "main.go",
|
||||
"args": [
|
||||
"-f=example.tsv",
|
||||
"-h=\"My Header\"",
|
||||
"-t"
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
21
Makefile
21
Makefile
@@ -1,6 +1,3 @@
|
||||
# https://habr.com/ru/post/461467/
|
||||
# https://tutorialedge.net/golang/makefiles-for-go-developers/
|
||||
# https://earthly.dev/blog/golang-makefile/
|
||||
BINARY_NAME=csv2md
|
||||
ARCH=amd64
|
||||
|
||||
@@ -12,30 +9,36 @@ LINUX_FILE="${LINUX_PATH}/${BINARY_NAME}"
|
||||
WINDOWS_FILE="${WINDOWS_PATH}/${BINARY_NAME}.exe"
|
||||
DARWIN_FILE="${DARWIN_PATH}/${BINARY_NAME}"
|
||||
|
||||
## clean: Removes all compiled binaries
|
||||
## clean: Remove all compiled binaries
|
||||
clean:
|
||||
@go clean
|
||||
@rm -rf bin/
|
||||
|
||||
## linux: Builds new binaries for linux (x64)
|
||||
## linux: Build new binaries for linux (x64)
|
||||
linux:
|
||||
@rm -rf ${LINUX_PATH}
|
||||
@GOARCH=${ARCH} GOOS=linux go build -o ${LINUX_FILE} . && echo "Compiled: ${LINUX_FILE}"
|
||||
|
||||
## win: Builds new binaries for windows (x64)
|
||||
## win: Build new binaries for windows (x64)
|
||||
win:
|
||||
@rm -rf ${WINDOWS_PATH}
|
||||
@GOARCH=${ARCH} GOOS=windows go build -o ${WINDOWS_FILE} . && echo "Compiled: ${WINDOWS_FILE}"
|
||||
|
||||
## darwin: Builds new binaries for darwin (x64)
|
||||
## darwin: Build new binaries for darwin (x64)
|
||||
darwin:
|
||||
@rm -rf ${DARWIN_PATH}
|
||||
@GOARCH=${ARCH} GOOS=darwin go build -o ${DARWIN_FILE} . && echo "Compiled: ${DARWIN_FILE}"
|
||||
|
||||
## build: Builds new binaries for linux, windows and darwin (x64)
|
||||
## all: Build new binaries for linux, windows and darwin (x64)
|
||||
all: clean linux win darwin
|
||||
|
||||
## compile: This message
|
||||
## release: Build all binaries and zip them
|
||||
release: clean darwin linux win
|
||||
@zip -j ${LINUX_PATH}.zip ${LINUX_FILE}
|
||||
@zip -j ${DARWIN_PATH}.zip ${DARWIN_FILE}
|
||||
@zip -j ${WINDOWS_PATH}.zip ${WINDOWS_FILE}
|
||||
|
||||
## help: This message
|
||||
help: Makefile
|
||||
@echo "Choose a command run:"
|
||||
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
|
||||
|
||||
68
README.md
68
README.md
@@ -1,23 +1,65 @@
|
||||
# csv2md
|
||||
|
||||
Stupidly simple tool to convert csv-file to [markdown](https://spec-md.com/) table.
|
||||
Stupidly simple tool to convert csv/tsv to [markdown](https://spec-md.com/) table.
|
||||
|
||||
Outputs result in stdout.
|
||||
Prints result in stdout.
|
||||
|
||||
Building:
|
||||
|
||||
```shell
|
||||
make help
|
||||
```
|
||||
|
||||
Usage:
|
||||
csv2md [-help|--help] [-f=<FILE>] [-h=<HEADER>] [-t] [-a]
|
||||
|
||||
```shell
|
||||
csv2md example.csv > example.md # makes new file
|
||||
csv2md example.csv | less # view result using *pager*
|
||||
...anything is possible with redirection and piping
|
||||
Available arguments:
|
||||
-help|--help - show this help
|
||||
-f=<FILE> - convert specified FILE
|
||||
-h=<HEADER> - add main header (h1) to result
|
||||
-t - convert input as tsv
|
||||
-a - align columns width
|
||||
|
||||
FILE formats supported:
|
||||
- csv (default)
|
||||
- tsv (with -t flag)
|
||||
|
||||
Path to FILE may be presented as:
|
||||
- absolute
|
||||
- relative to current working directory
|
||||
- relative to user home directory (~)
|
||||
|
||||
Both HEADER and FILE path with whitespaces must be double-quoted.
|
||||
To save result as separate file you should use redirection operators (> or >>).
|
||||
```
|
||||
|
||||
> **IMPORTANT:** input must be valid csv and whitespaces are allowed only between double-quotes.
|
||||
**IMPORTANT:**
|
||||
1. Input data must be valid csv/tsv
|
||||
2. Whitespaces allowed only between double-quotes
|
||||
3. Due to markdown spec first line of result table will always be presented as header.
|
||||
So if your raw data hasn't one you'll should add it before conversion or edit later in ready md.
|
||||
|
||||
Examples can be found here: https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html
|
||||
## Examples
|
||||
|
||||
```
|
||||
csv2md - paste or type csv to stdin and then
|
||||
press Ctrl+D to view result in stdout
|
||||
csv2md -t > example.md - paste or type tsv to stdin and then
|
||||
press Ctrl+D to write result in new file
|
||||
csv2md -f example.csv - convert csv from file and view result in stdout
|
||||
csv2md -t < example.tsv - convert tsv from stdin and view result in stdout
|
||||
csv2md -t < example.tsv > example.md - convert tsv from stdin and write result in new file
|
||||
cat example.csv | csv2md - convert csv from stdin and view result in stdout
|
||||
csv2md -t -f=example.tsv > example.md - convert tsv from file and write result in new file
|
||||
csv2md -f example.csv | less - convert csv from file and view result in stdout using pager
|
||||
csv2md -f example.csv | code -n - - convert csv from file and open result in vscode
|
||||
|
||||
...anything is possible with redirection and piping, inc. grep, sed, awk, etc.
|
||||
```
|
||||
|
||||
You can generate some examples here: [csv](https://onlinerandomtools.com/generate-random-csv), [tsv](https://onlinerandomtools.com/generate-random-tsv)
|
||||
|
||||
## Compilation
|
||||
|
||||
1) [Install go](https://go.dev/learn/).
|
||||
2) Download this repo via zip or `git clone`.
|
||||
3) Run `make help` to get help about compilation or `go run . [ARGS...]` to build and run temporary binary.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE)
|
||||
|
||||
64
example.csv
64
example.csv
@@ -1,14 +1,50 @@
|
||||
"Month","1958","1959","1960"
|
||||
"JAN",340,360,417
|
||||
"FEB",318,342,391
|
||||
"MAR",362,406,419
|
||||
"APR",348,396,461
|
||||
"MAY",363,420,472
|
||||
"JUN",435,472,535
|
||||
"JUL",491,548,622
|
||||
"AUG",505,559,606
|
||||
"SEP",404,463,508
|
||||
"OCT",359,407,461
|
||||
"NOV",310,362,390
|
||||
"DEC",337,405,432
|
||||
|
||||
"Name","Team","Position","Height(inches)","Weight(lbs)","Age"
|
||||
"Adam Donachie","BAL","Catcher",74,180,22.99
|
||||
"Paul Bako","BAL","Catcher",74,215,34.69
|
||||
"Ramon Hernandez","BAL","Catcher",72,210,30.78
|
||||
"Kevin Millar","BAL","First Baseman",72,210,35.43
|
||||
"Chris Gomez","BAL","First Baseman",73,188,35.71
|
||||
"Brian Roberts","BAL","Second Baseman",69,176,29.39
|
||||
"Miguel Tejada","BAL","Shortstop",69,209,30.77
|
||||
"Melvin Mora","BAL","Third Baseman",71,200,35.07
|
||||
"Aubrey Huff","BAL","Third Baseman",76,231,30.19
|
||||
"Adam Stern","BAL","Outfielder",71,180,27.05
|
||||
"Jeff Fiorentino","BAL","Outfielder",73,188,23.88
|
||||
"Freddie Bynum","BAL","Outfielder",73,180,26.96
|
||||
"Nick Markakis","BAL","Outfielder",74,185,23.29
|
||||
"Brandon Fahey","BAL","Outfielder",74,160,26.11
|
||||
"Corey Patterson","BAL","Outfielder",69,180,27.55
|
||||
"Jay Payton","BAL","Outfielder",70,185,34.27
|
||||
"Jay Gibbons","BAL","Designated Hitter",72,197,30
|
||||
"Erik Bedard","BAL","Starting Pitcher",73,189,27.99
|
||||
"Hayden Penn","BAL","Starting Pitcher",75,185,22.38
|
||||
"Adam Loewen","BAL","Starting Pitcher",78,219,22.89
|
||||
"Daniel Cabrera","BAL","Starting Pitcher",79,230,25.76
|
||||
"Steve Trachsel","BAL","Starting Pitcher",76,205,36.33
|
||||
"Jaret Wright","BAL","Starting Pitcher",74,230,31.17
|
||||
"Kris Benson","BAL","Starting Pitcher",76,195,32.31
|
||||
"Scott Williamson","BAL","Relief Pitcher",72,180,31.03
|
||||
"John Parrish","BAL","Relief Pitcher",71,192,29.26
|
||||
"Danys Baez","BAL","Relief Pitcher",75,225,29.47
|
||||
"Chad Bradford","BAL","Relief Pitcher",77,203,32.46
|
||||
"Jamie Walker","BAL","Relief Pitcher",74,195,35.67
|
||||
"Brian Burres","BAL","Relief Pitcher",73,182,25.89
|
||||
"Kurt Birkins","BAL","Relief Pitcher",74,188,26.55
|
||||
"James Hoey","BAL","Relief Pitcher",78,200,24.17
|
||||
"Sendy Rleal","BAL","Relief Pitcher",73,180,26.69
|
||||
"Chris Ray","BAL","Relief Pitcher",75,200,25.13
|
||||
"Jeremy Guthrie","BAL","Relief Pitcher",73,200,27.9
|
||||
"A.J. Pierzynski","CWS","Catcher",75,245,30.17
|
||||
"Toby Hall","CWS","Catcher",75,240,31.36
|
||||
"Paul Konerko","CWS","First Baseman",74,215,30.99
|
||||
"Tadahito Iguchi","CWS","Second Baseman",69,185,32.24
|
||||
"Juan Uribe","CWS","Shortstop",71,175,27.61
|
||||
"Alex Cintron","CWS","Shortstop",74,199,28.2
|
||||
"Joe Crede","CWS","Third Baseman",73,200,28.85
|
||||
"Josh Fields","CWS","Third Baseman",73,215,24.21
|
||||
"Ryan Sweeney","CWS","Outfielder",76,200,22.02
|
||||
"Brian N. Anderson","CWS","Outfielder",74,205,24.97
|
||||
"Luis Terrero","CWS","Outfielder",74,206,26.78
|
||||
"Pablo Ozuna","CWS","Outfielder",70,186,32.51
|
||||
"Scott Podsednik","CWS","Outfielder",72,188,30.95
|
||||
"Jermaine Dye","CWS","Outfielder",77,220,33.09
|
||||
|
||||
|
6
example.tsv
Normal file
6
example.tsv
Normal file
@@ -0,0 +1,6 @@
|
||||
impossible political hand larger upward during
|
||||
shade tip opinion star keep aside
|
||||
wrong heat line pool song just
|
||||
she slowly gain snow ourselves six
|
||||
race thrown get yard nearest swam
|
||||
though than teacher away dirt escape
|
||||
|
172
main.go
172
main.go
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@@ -9,33 +11,59 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const VERSION = "1.0.0"
|
||||
const VERSION = "1.3.0"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
os.Exit(1)
|
||||
log.SetFlags(0)
|
||||
is_help := flag.Bool("help", false, "Get help")
|
||||
filepath := flag.String("f", "", "File path")
|
||||
header := flag.String("h", "", "Add main header (h1)")
|
||||
as_tsv := flag.Bool("t", false, "Parse input as tsv")
|
||||
aligned := flag.Bool("a", false, "Align columns width")
|
||||
flag.Parse()
|
||||
|
||||
// show help
|
||||
if *is_help {
|
||||
usage(os.Stdout)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
src, err := ExpandPath(os.Args[1])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
// if filepath is not provided then convert data from stdin
|
||||
if len(*filepath) == 0 {
|
||||
data, err := ReadStdin(*as_tsv)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
Print(Convert(*header, data, *aligned))
|
||||
} else { // otherwise convert data from file
|
||||
src, err := ExpandPath(*filepath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(src); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
data, err := ReadFile(src, *as_tsv)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
Print(Convert(*header, data, *aligned))
|
||||
}
|
||||
if _, err := os.Stat(src); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Print writes converted data to stdout
|
||||
func Print(data []string) {
|
||||
if len(data) == 0 {
|
||||
usage(os.Stderr)
|
||||
}
|
||||
|
||||
result := convert(readCsv(src))
|
||||
for _, row := range result {
|
||||
for _, row := range data {
|
||||
fmt.Println(row)
|
||||
}
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintln(os.Stderr, "csv2md v" + VERSION)
|
||||
fmt.Fprintln(os.Stderr, "Usage: csv2md data.csv > data.md")
|
||||
}
|
||||
|
||||
// ExpandPath returns absolute path to file replacing ~ to user's home folder
|
||||
func ExpandPath(path string) (string, error) {
|
||||
homepath, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
@@ -45,37 +73,127 @@ func ExpandPath(path string) (string, error) {
|
||||
return newpath, err
|
||||
}
|
||||
|
||||
func readCsv(filePath string) [][]string {
|
||||
// ReadFile reads data from file
|
||||
func ReadFile(filePath string, as_tsv bool) ([][]string, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to read input file " + filePath, err)
|
||||
return nil, errors.New("Failed to open file '" + filePath + "': " + err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
csvReader := csv.NewReader(f)
|
||||
if as_tsv {
|
||||
csvReader.Comma = '\t'
|
||||
}
|
||||
|
||||
records, err := csvReader.ReadAll()
|
||||
if err != nil {
|
||||
log.Fatal("Unable to parse file as CSV for " + filePath, err)
|
||||
return nil, errors.New("Failed to parse file '" + filePath + "': " + err.Error())
|
||||
}
|
||||
|
||||
return records
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func convert(records [][]string) []string {
|
||||
// ReadStdin reads data from stdin
|
||||
func ReadStdin(as_tsv bool) ([][]string, error) {
|
||||
csvReader := csv.NewReader(os.Stdin)
|
||||
if as_tsv {
|
||||
csvReader.Comma = '\t'
|
||||
}
|
||||
|
||||
records, err := csvReader.ReadAll()
|
||||
if err != nil {
|
||||
return nil, errors.New("Failed to parse input from stdin: " + err.Error())
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// Convert formats data from file or stdin as markdown
|
||||
func Convert(header string, records [][]string, aligned bool) []string {
|
||||
var result []string
|
||||
for idx, row := range records {
|
||||
|
||||
// add h1 if passed
|
||||
header = strings.Trim(header, "\t\r\n ")
|
||||
if len(header) != 0 {
|
||||
result = append(result, "# " + header)
|
||||
result = append(result, "")
|
||||
}
|
||||
|
||||
// if user wants aligned columns width then we
|
||||
// count max length of every value in every column
|
||||
widths := make(map[int]int)
|
||||
if aligned {
|
||||
for _, row := range records {
|
||||
for col_idx, col := range row {
|
||||
length := len(col)
|
||||
if len(widths) == 0 || widths[col_idx] < length {
|
||||
widths[col_idx] = length
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// build markdown table
|
||||
for row_idx, row := range records {
|
||||
|
||||
// table content
|
||||
str := "| "
|
||||
for _, col := range row {
|
||||
str += col + " | "
|
||||
for col_idx, col := range row {
|
||||
if aligned {
|
||||
str += fmt.Sprintf("%-*s | ", widths[col_idx], col)
|
||||
} else {
|
||||
str += col + " | "
|
||||
}
|
||||
}
|
||||
result = append(result, str)
|
||||
if idx == 0 {
|
||||
|
||||
// content separator only after first row (header)
|
||||
if row_idx == 0 {
|
||||
str := "| "
|
||||
for range row {
|
||||
str += "--- | "
|
||||
for col_idx := range row {
|
||||
if !aligned || widths[col_idx] < 3 {
|
||||
str += "--- | "
|
||||
} else {
|
||||
str += strings.Repeat("-", widths[col_idx]) + " | "
|
||||
}
|
||||
}
|
||||
result = append(result, str)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// usage Print help into writer
|
||||
func usage(writer *os.File) {
|
||||
usage := []string{
|
||||
"csv2md v" + VERSION,
|
||||
"Anthony Axenov (c) 2022, MIT License",
|
||||
"https://github.com/anthonyaxenov/csv2md",
|
||||
"",
|
||||
"Usage:",
|
||||
"\tcsv2md [-help|--help] [-f=<FILE>] [-h=<HEADER>] [-t] [-a]",
|
||||
"",
|
||||
"Available arguments:",
|
||||
"\t-help|--help - show this help",
|
||||
"\t-f=<FILE> - Convert specified FILE",
|
||||
"\t-h=<HEADER> - add main header (h1) to result",
|
||||
"\t-t - Convert input as tsv",
|
||||
"\t-a - align columns width",
|
||||
"",
|
||||
"FILE formats supported:",
|
||||
"\t- csv (default)",
|
||||
"\t- tsv (with -t flag)",
|
||||
"",
|
||||
"Path to FILE may be presented as:",
|
||||
"\t- absolute",
|
||||
"\t- relative to current working directory",
|
||||
"\t- relative to user home directory (~)",
|
||||
"",
|
||||
"Both HEADER and FILE path with whitespaces must be double-quoted.",
|
||||
"To save result as separate file you should use redirection operators (> or >>).",
|
||||
}
|
||||
for _, str := range usage {
|
||||
fmt.Fprintln(writer, str)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user