Skip to content

Commit

Permalink
fix: escaping process
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrassov01 committed Feb 15, 2024
1 parent 4857001 commit 97d9507
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 146 deletions.
45 changes: 34 additions & 11 deletions _examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func main() {
| i-1 | server-1 | lb-domain-1 | tg-1 |
| i-2 | server-2 | lb-doamin-2<br>lb-doamin-3 | tg-2 |
| i-3 | server-3 | lb-doamin-4 | tg-3<br>tg-4 |
| i-4 | server-4 | \- | \- |
| i-5 | server-5 | lb-domain-5 | \- |
| i-5 | server-5 | \- | tg-5<br>tg-6 |
| i-4 | server-4 | &#45; | &#45; |
| i-5 | server-5 | lb-domain-5 | &#45; |
| i-5 | server-5 | &#45; | tg-5<br>tg-6 |
*/

table = mintab.NewTable(mintab.WithFormat(mintab.FormatBacklog))
Expand All @@ -78,9 +78,9 @@ func main() {
| i-1 | server-1 | lb-domain-1 | tg-1 |
| i-2 | server-2 | lb-doamin-2&br;lb-doamin-3 | tg-2 |
| i-3 | server-3 | lb-doamin-4 | tg-3&br;tg-4 |
| i-4 | server-4 | \- | \- |
| i-5 | server-5 | lb-domain-5 | \- |
| i-5 | server-5 | \- | tg-5&br;tg-6 |
| i-4 | server-4 | - | - |
| i-5 | server-5 | lb-domain-5 | - |
| i-5 | server-5 | - | tg-5&br;tg-6 |
*/

table = mintab.NewTable(mintab.WithHeader(false))
Expand Down Expand Up @@ -179,7 +179,7 @@ func main() {
*/

/*
Escaping
Escaping when select markdown format
*/

type sample2 struct {
Expand All @@ -188,18 +188,20 @@ func main() {

s2 := []sample2{
{Domain: "*.example.com"},
{Domain: "| _"},
}

table = mintab.NewTable(mintab.WithEscapeTargets([]string{"*"}), mintab.WithFormat(mintab.FormatMarkdown))
table = mintab.NewTable(mintab.WithFormat(mintab.FormatMarkdown))
if err := table.Load(s2); err != nil {
log.Fatal(err)
}
fmt.Println(table.Out())

/*
| Domain |
|----------------|
| \*.example.com |
| Domain |
|--------------------|
| &#42;.example.com |
| &#124;&nbsp;&#095; |
*/

/*
Expand Down Expand Up @@ -266,4 +268,25 @@ func main() {
| | | sg-3 | Ingress | tcp | 0 | 65535 | PrefixList | pl-id/pl-name |
| | | sg-3 | Egress | -1 | 0 | 0 | Ipv4 | 0.0.0.0/0 |
*/

table = mintab.NewTable(mintab.WithIgnoreFields([]int{1}), mintab.WithFormat(mintab.FormatMarkdown))
if err := table.Load(s1); err != nil {
log.Fatal(err)
}
fmt.Println(table.Out())

/*
| InstanceID | AttachedLB | AttachedTG |
|------------|-------------|------------|
| i-1 | lb-domain-1 | tg-1 |
| i-2 | lb-doamin-2 | tg-2 |
| | lb-doamin-3 | |
| i-3 | lb-doamin-4 | tg-3 |
| | | tg-4 |
| i-4 | - | - |
| i-5 | lb-domain-5 | - |
| i-5 | - | tg-5 |
| | | tg-6 |
*/

}
56 changes: 28 additions & 28 deletions mintab.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mintab

import (
"fmt"
"html"
"reflect"
"slices"
"strings"
Expand Down Expand Up @@ -64,8 +65,12 @@ func (t Theme) String() string {

// Dafault values
const (
DefaultEmptyFieldPlaceholder = "-"
DefaultWordDelimiter = "\n"
DefaultEmptyFieldPlaceholder = "-"
DefaultWordDelimiter = "\n"
MarkdownDefaultEmptyFieldPlaceholder = "&#45;"
MarkdownDefaultWordDelimiter = "<br>"
BacklogDefaultEmptyFieldPlaceholder = "-"
BacklogDefaultWordDelimiter = "&br;"
)

// Table represents a table in a matrix of strings.
Expand All @@ -79,7 +84,6 @@ type Table struct {
wordDelimiter string // wordDelimiter specifies the word delimiter of the field.
mergedFields []int // mergedFields holds indices of the field to be grouped.
ignoredFields []int // ignoredFields holds indices of the fields to be ignored.
escapedTargets []string // escapedTargets holds the characters to be escaped; string, not rune
}

// NewTable instantiates a table struct.
Expand Down Expand Up @@ -150,15 +154,27 @@ func WithIgnoreFields(ignoreFields []int) Option {
}
}

func WithEscapeTargets(escapeTargets []string) Option {
return func(t *Table) {
t.escapedTargets = escapeTargets
}
}

// Load validates input and converts them to table data.
// Returns error if not struct slice.
func (t *Table) Load(input any) error {
var p, d string
switch t.format {
case FormatMarkdown:
p = MarkdownDefaultEmptyFieldPlaceholder
d = MarkdownDefaultWordDelimiter
case FormatBacklog:
p = BacklogDefaultEmptyFieldPlaceholder
d = BacklogDefaultWordDelimiter
default:
p = DefaultEmptyFieldPlaceholder
d = DefaultWordDelimiter
}
if t.emptyFieldPlaceholder == DefaultEmptyFieldPlaceholder {
t.emptyFieldPlaceholder = p
}
if t.wordDelimiter == DefaultWordDelimiter {
t.wordDelimiter = d
}
if _, ok := input.([]interface{}); ok {
return fmt.Errorf("cannot parse input: elements of slice must not be empty interface")
}
Expand Down Expand Up @@ -262,9 +278,6 @@ func (t *Table) setData(v reflect.Value) error {
// Perform multi-value delimiters and whitespace handling.
// Nested fields are not processed and an error is returned.
func (t *Table) formatValue(v reflect.Value) (string, error) {
if t.format != FormatText && t.emptyFieldPlaceholder == "-" {
t.emptyFieldPlaceholder = "\\-"
}
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return t.emptyFieldPlaceholder, nil
Expand All @@ -277,23 +290,10 @@ func (t *Table) formatValue(v reflect.Value) (string, error) {
if s == "" {
return t.emptyFieldPlaceholder, nil
}
if t.format != FormatText {
s = strings.ReplaceAll(s, " ", "&nbsp;")
}
if t.format == FormatMarkdown {
s = strings.ReplaceAll(s, "\n", "<br>")
if t.wordDelimiter == "\n" {
t.wordDelimiter = "<br>"
}
}
if t.format == FormatBacklog {
s = strings.ReplaceAll(s, "\n", "&br;")
if t.wordDelimiter == "\n" {
t.wordDelimiter = "&br;"
}
}
for _, escapeTarget := range t.escapedTargets {
s = strings.ReplaceAll(s, escapeTarget, `\`+escapeTarget)
s = html.EscapeString(s)
r := strings.NewReplacer(" ", "&nbsp;", "|", "&#124;", "*", "&#42;", "\\", "&#92;", "_", "&#095;")
s = r.Replace(s)
}
return strings.TrimSpace(s), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand Down
Loading

0 comments on commit 97d9507

Please sign in to comment.