mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-16 20:20:57 +01:00
Release v1.0.17 (#9)
* add shortcut -H to --no-headers, it's too cumbersome to type * added fuzzy search support * Added basic lisp plugin facilities * Lisp plugin Addidions: - added process hook facilities - added working example lisp plugin for filter hook - load-path can now be a file as well - added a couple of lisp helper functions (atoi, split), more may follow, see lisplib.go * linting fixes
This commit is contained in:
@@ -22,15 +22,33 @@ import (
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/tlinden/tablizer/cfg"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
"github.com/tlinden/tablizer/cfg"
|
||||
)
|
||||
|
||||
/*
|
||||
Parser switch
|
||||
* [!]Match a line, use fuzzy search for normal pattern strings and
|
||||
* regexp otherwise.
|
||||
*/
|
||||
func matchPattern(c cfg.Config, line string) bool {
|
||||
if len(c.Pattern) > 0 {
|
||||
if c.UseFuzzySearch {
|
||||
return fuzzy.MatchFold(c.Pattern, line)
|
||||
} else {
|
||||
return c.PatternR.MatchString(line)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/*
|
||||
Parser switch
|
||||
*/
|
||||
func Parse(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
if len(c.Separator) == 1 {
|
||||
@@ -41,7 +59,7 @@ func Parse(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
}
|
||||
|
||||
/*
|
||||
Parse CSV input.
|
||||
Parse CSV input.
|
||||
*/
|
||||
func parseCSV(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
var content io.Reader = input
|
||||
@@ -55,13 +73,25 @@ func parseCSV(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if hadFirst {
|
||||
// don't match 1st line, it's the header
|
||||
if c.PatternR.MatchString(line) == c.InvertMatch {
|
||||
if matchPattern(c, line) == c.InvertMatch {
|
||||
// by default -v is false, so if a line does NOT
|
||||
// match the pattern, we will ignore it. However,
|
||||
// if the user specified -v, the matching is inverted,
|
||||
// so we ignore all lines, which DO match.
|
||||
continue
|
||||
}
|
||||
|
||||
// apply user defined lisp filters, if any
|
||||
accept, err := RunFilterHooks(c, line)
|
||||
if err != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to apply filter hook: %w", err))
|
||||
}
|
||||
|
||||
if !accept {
|
||||
// IF there are filter hook[s] and IF one of them
|
||||
// returns false on the current line, reject it
|
||||
continue
|
||||
}
|
||||
}
|
||||
lines = append(lines, line)
|
||||
hadFirst = true
|
||||
@@ -94,11 +124,20 @@ func parseCSV(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// apply user defined lisp process hooks, if any
|
||||
userdata, changed, err := RunProcessHooks(c, data)
|
||||
if err != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to apply filter hook: %w", err))
|
||||
}
|
||||
if changed {
|
||||
data = userdata
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Parse tabular input.
|
||||
Parse tabular input.
|
||||
*/
|
||||
func parseTabular(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
data := Tabdata{}
|
||||
@@ -141,14 +180,24 @@ func parseTabular(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
}
|
||||
} else {
|
||||
// data processing
|
||||
if len(c.Pattern) > 0 {
|
||||
if c.PatternR.MatchString(line) == c.InvertMatch {
|
||||
// by default -v is false, so if a line does NOT
|
||||
// match the pattern, we will ignore it. However,
|
||||
// if the user specified -v, the matching is inverted,
|
||||
// so we ignore all lines, which DO match.
|
||||
continue
|
||||
}
|
||||
if matchPattern(c, line) == c.InvertMatch {
|
||||
// by default -v is false, so if a line does NOT
|
||||
// match the pattern, we will ignore it. However,
|
||||
// if the user specified -v, the matching is inverted,
|
||||
// so we ignore all lines, which DO match.
|
||||
continue
|
||||
}
|
||||
|
||||
// apply user defined lisp filters, if any
|
||||
accept, err := RunFilterHooks(c, line)
|
||||
if err != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to apply filter hook: %w", err))
|
||||
}
|
||||
|
||||
if !accept {
|
||||
// IF there are filter hook[s] and IF one of them
|
||||
// returns false on the current line, reject it
|
||||
continue
|
||||
}
|
||||
|
||||
idx := 0 // we cannot use the header index, because we could exclude columns
|
||||
@@ -175,6 +224,15 @@ func parseTabular(c cfg.Config, input io.Reader) (Tabdata, error) {
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to read from io.Reader: %w", scanner.Err()))
|
||||
}
|
||||
|
||||
// apply user defined lisp process hooks, if any
|
||||
userdata, changed, err := RunProcessHooks(c, data)
|
||||
if err != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to apply filter hook: %w", err))
|
||||
}
|
||||
if changed {
|
||||
data = userdata
|
||||
}
|
||||
|
||||
if c.Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user