Skip to content

Commit

Permalink
Add HTML encoding escape and unescape (#36)
Browse files Browse the repository at this point in the history
* Add HTML encoding escape and unescape

* Update README.md
  • Loading branch information
denandz authored Jan 20, 2024
1 parent 8e38d31 commit 842263a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ OPTIONS:
ENCODERS
b64encode - Base64 encoder
hexencode - Hex string encoder
htmlescape - HTML escape
jsonescape - JSON escape
unicodeencodeall - Unicode escape string encode (all characters)
urlencode - URL encode reserved characters
Expand All @@ -37,6 +38,7 @@ ENCODERS
DECODERS
b64decode - Base64 decoder
hexdecode - Hex string decoder
htmlunescape - HTML unescape
jsonunescape - JSON unescape
unicodedecode - Unicode escape string decode
urldecode - URL decode
Expand Down
10 changes: 6 additions & 4 deletions pkg/pencode/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var availableEncoders = map[string]Encoder{
"filename.tmpl": Template{},
"hexencode": HexEncoder{},
"hexdecode": HexDecoder{},
"htmlescape": HTMLEscaper{},
"htmlunescape": HTMLUnescaper{},
"jsonescape": JSONEscaper{},
"jsonunescape": JSONUnescaper{},
"lower": StrToLower{},
Expand Down Expand Up @@ -44,8 +46,8 @@ func NewChain() *Chain {
return &c
}

//Initialize loops through requested names for encoders and sets up the encoder chain. If an unknown encoder is
//requested, error will be returned.
// Initialize loops through requested names for encoders and sets up the encoder chain. If an unknown encoder is
// requested, error will be returned.
func (c *Chain) Initialize(actions []string) error {
c.actions = actions
c.Encoders = make([]Encoder, 0)
Expand Down Expand Up @@ -85,7 +87,7 @@ func (c *Chain) Encode(input []byte) ([]byte, error) {
return input, nil
}

//HasEncoder returns true if encoder with a specified name is configured
// HasEncoder returns true if encoder with a specified name is configured
func (c *Chain) HasEncoder(name string) (bool, error) {
if _, ok := availableEncoders[name]; ok {
return true, nil
Expand All @@ -107,7 +109,7 @@ func (c *Chain) GetEncoders() []string {
return names
}

//Usage prints the help string for each configured encoder
// Usage prints the help string for each configured encoder
func (c *Chain) Usage() {
// Calculate maximum keyword length for nice help formatting
max_length := 0
Expand Down
19 changes: 19 additions & 0 deletions pkg/pencode/htmldecode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pencode

import "html"

type HTMLUnescaper struct{}

func (u HTMLUnescaper) Encode(input []byte) ([]byte, error) {
out := html.UnescapeString(string(input))

return []byte(out), nil
}

func (u HTMLUnescaper) HelpText() string {
return "HTML unescape"
}

func (u HTMLUnescaper) Type() string {
return "decoders"
}
19 changes: 19 additions & 0 deletions pkg/pencode/htmlencode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pencode

import "html"

type HTMLEscaper struct{}

func (u HTMLEscaper) Encode(input []byte) ([]byte, error) {
out := html.EscapeString(string(input))

return []byte(out), nil
}

func (u HTMLEscaper) HelpText() string {
return "HTML escape"
}

func (u HTMLEscaper) Type() string {
return "encoders"
}

0 comments on commit 842263a

Please sign in to comment.