From b5c699b40de98fb9af89996fbd227f27e075603b Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 14 Jun 2022 13:03:32 +0100 Subject: [PATCH] Strip markup. --- cmd/zenity/build.sh | 3 +- cmd/zenity/main.go | 10 +++++- cmd/zenity/notify.go | 3 +- internal/zencmd/markup.go | 29 +++++++++++++++ internal/zencmd/markup_test.go | 36 +++++++++++++++++++ internal/zencmd/pkg.go | 2 ++ internal/{zenutil => zencmd}/unescape.go | 2 +- internal/{zenutil => zencmd}/unescape_test.go | 2 +- 8 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 internal/zencmd/markup.go create mode 100644 internal/zencmd/markup_test.go create mode 100644 internal/zencmd/pkg.go rename internal/{zenutil => zencmd}/unescape.go (99%) rename internal/{zenutil => zencmd}/unescape_test.go (98%) diff --git a/cmd/zenity/build.sh b/cmd/zenity/build.sh index 97fa695..d8d4962 100755 --- a/cmd/zenity/build.sh +++ b/cmd/zenity/build.sh @@ -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 diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index a2563c2..d61d36b 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.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 diff --git a/cmd/zenity/notify.go b/cmd/zenity/notify.go index f32b74e..b6bf629 100644 --- a/cmd/zenity/notify.go +++ b/cmd/zenity/notify.go @@ -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") } diff --git a/internal/zencmd/markup.go b/internal/zencmd/markup.go new file mode 100644 index 0000000..34919fe --- /dev/null +++ b/internal/zencmd/markup.go @@ -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) + } + } +} diff --git a/internal/zencmd/markup_test.go b/internal/zencmd/markup_test.go new file mode 100644 index 0000000..e66b821 --- /dev/null +++ b/internal/zencmd/markup_test.go @@ -0,0 +1,36 @@ +package zencmd + +import ( + "testing" +) + +var markupTests = []struct { + data string + want string +}{ + // success cases + {"", ``}, + {"abc", `abc`}, + {"<", `<`}, + {"&", `&`}, + {""", `"`}, + {"", ``}, + {"abc", `abc`}, + {""", `"`}, + {"", ``}, + + // failure cases + {"<", `<`}, + {"", ``}, + {"", ``}, + {"&", `&`}, +} + +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) + } + } +} diff --git a/internal/zencmd/pkg.go b/internal/zencmd/pkg.go new file mode 100644 index 0000000..f3540bf --- /dev/null +++ b/internal/zencmd/pkg.go @@ -0,0 +1,2 @@ +// Package zencmd is internal. DO NOT USE. +package zencmd diff --git a/internal/zenutil/unescape.go b/internal/zencmd/unescape.go similarity index 99% rename from internal/zenutil/unescape.go rename to internal/zencmd/unescape.go index eede67f..7cd6fdf 100644 --- a/internal/zenutil/unescape.go +++ b/internal/zencmd/unescape.go @@ -1,4 +1,4 @@ -package zenutil +package zencmd import "strings" diff --git a/internal/zenutil/unescape_test.go b/internal/zencmd/unescape_test.go similarity index 98% rename from internal/zenutil/unescape_test.go rename to internal/zencmd/unescape_test.go index 98c342f..13fe902 100644 --- a/internal/zenutil/unescape_test.go +++ b/internal/zencmd/unescape_test.go @@ -1,4 +1,4 @@ -package zenutil +package zencmd import ( "testing"