Strip markup.
This commit is contained in:
parent
b94ee718ba
commit
b5c699b40d
8 changed files with 82 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
|||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
TAG=$(git tag --points-at HEAD)
|
||||
echo 'package main; const tag = "'$TAG'"' > tag.go
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
|
||||
"github.com/ncruces/go-strftime"
|
||||
"github.com/ncruces/zenity"
|
||||
"github.com/ncruces/zenity/internal/zencmd"
|
||||
"github.com/ncruces/zenity/internal/zenutil"
|
||||
)
|
||||
|
||||
|
@ -58,6 +59,7 @@ var (
|
|||
|
||||
// Message options
|
||||
noWrap bool
|
||||
noMarkup bool
|
||||
ellipsize bool
|
||||
|
||||
// Entry options
|
||||
|
@ -219,8 +221,8 @@ func setupFlags() {
|
|||
// Message options
|
||||
flag.StringVar(&icon, "icon-name", "", "Set the dialog `icon` (dialog-error, dialog-information, dialog-question, dialog-warning)")
|
||||
flag.BoolVar(&noWrap, "no-wrap", false, "Do not enable text wrapping")
|
||||
flag.BoolVar(&noMarkup, "no-markup", false, "Do not enable Pango markup")
|
||||
flag.BoolVar(&ellipsize, "ellipsize", false, "Enable ellipsizing in the dialog text")
|
||||
flag.Bool("no-markup", true, "Do not enable Pango markup")
|
||||
|
||||
// Entry options
|
||||
flag.StringVar(&entryText, "entry-text", "", "Set the entry `text`")
|
||||
|
@ -463,6 +465,12 @@ func loadFlags() []zenity.Option {
|
|||
if ellipsize {
|
||||
opts = append(opts, zenity.Ellipsize())
|
||||
}
|
||||
if noMarkup == false {
|
||||
switch {
|
||||
case errorDlg, infoDlg, warningDlg, questionDlg:
|
||||
text = zencmd.StripMarkup(text)
|
||||
}
|
||||
}
|
||||
|
||||
// Entry options
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/ncruces/zenity"
|
||||
"github.com/ncruces/zenity/internal/zencmd"
|
||||
"github.com/ncruces/zenity/internal/zenutil"
|
||||
)
|
||||
|
||||
|
@ -27,7 +28,7 @@ func notify(opts ...zenity.Option) error {
|
|||
var cmd, msg string
|
||||
if n := strings.IndexByte(line, ':'); n >= 0 {
|
||||
cmd = strings.TrimSpace(line[:n])
|
||||
msg = strings.TrimSpace(zenutil.Unescape(line[n+1:]))
|
||||
msg = strings.TrimSpace(zencmd.Unescape(line[n+1:]))
|
||||
} else {
|
||||
fmt.Fprint(os.Stderr, "Could not parse command from stdin")
|
||||
}
|
||||
|
|
29
internal/zencmd/markup.go
Normal file
29
internal/zencmd/markup.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package zencmd
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StripMarkup is internal.
|
||||
func StripMarkup(s string) string {
|
||||
// Strips XML markup described in:
|
||||
// https://docs.gtk.org/Pango/pango_markup.html
|
||||
|
||||
dec := xml.NewDecoder(strings.NewReader(s))
|
||||
var buf strings.Builder
|
||||
|
||||
for {
|
||||
t, err := dec.Token()
|
||||
if err == io.EOF {
|
||||
return buf.String()
|
||||
}
|
||||
if err != nil {
|
||||
return s
|
||||
}
|
||||
if t, ok := t.(xml.CharData); ok {
|
||||
buf.Write(t)
|
||||
}
|
||||
}
|
||||
}
|
36
internal/zencmd/markup_test.go
Normal file
36
internal/zencmd/markup_test.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package zencmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var markupTests = []struct {
|
||||
data string
|
||||
want string
|
||||
}{
|
||||
// success cases
|
||||
{"", ``},
|
||||
{"abc", `abc`},
|
||||
{"<", `<`},
|
||||
{"&", `&`},
|
||||
{""", `"`},
|
||||
{"<i></i>", ``},
|
||||
{"<i>abc</i>", `abc`},
|
||||
{"<i>"</i>", `"`},
|
||||
{"<!--abc-->", ``},
|
||||
|
||||
// failure cases
|
||||
{"<", `<`},
|
||||
{"<i", `<i`},
|
||||
{"<i>", `<i>`},
|
||||
{"<i></b>", `<i></b>`},
|
||||
{"<i>&</i>", `<i>&</i>`},
|
||||
}
|
||||
|
||||
func TestStripMarkup(t *testing.T) {
|
||||
for _, test := range markupTests {
|
||||
if got := StripMarkup(test.data); got != test.want {
|
||||
t.Errorf("StripMarkup(%q) = %q; want %q", test.data, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
2
internal/zencmd/pkg.go
Normal file
2
internal/zencmd/pkg.go
Normal file
|
@ -0,0 +1,2 @@
|
|||
// Package zencmd is internal. DO NOT USE.
|
||||
package zencmd
|
|
@ -1,4 +1,4 @@
|
|||
package zenutil
|
||||
package zencmd
|
||||
|
||||
import "strings"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package zenutil
|
||||
package zencmd
|
||||
|
||||
import (
|
||||
"testing"
|
Loading…
Reference in a new issue