Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid HTML escaping in JSON files #3560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
avoid HTML escaping in JSON files
matthias314 committed Dec 11, 2024
commit e4dcb28f973273fae14f6cd5091db8f0547d6ec4
7 changes: 2 additions & 5 deletions internal/action/bindings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package action

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@@ -303,8 +302,7 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {

BindKey(k, v, Binder["buffer"])

txt, _ := json.MarshalIndent(parsed, "", " ")
return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644)
return true, config.WriteJson(filename, parsed)
}
return false, e
}
@@ -353,8 +351,7 @@ func UnbindKey(k string) error {
delete(config.Bindings["buffer"], k)
}

txt, _ := json.MarshalIndent(parsed, "", " ")
return ioutil.WriteFile(filename, append(txt, '\n'), 0644)
return config.WriteJson(filename, parsed)
}
return e
}
19 changes: 15 additions & 4 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
@@ -312,6 +312,19 @@ func InitLocalSettings(settings map[string]interface{}, path string) {
}
}

func WriteJson(filename string, v interface{}) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
file.Truncate(0)
enc := json.NewEncoder(file)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
return enc.Encode(v)
}

// WriteSettings writes the settings to the specified filename as JSON
func WriteSettings(filename string) error {
if settingsParseError {
@@ -346,8 +359,7 @@ func WriteSettings(filename string) error {
}
}

txt, _ := json.MarshalIndent(parsedSettings, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
err = WriteJson(filename, parsedSettings)
}
return err
}
@@ -368,8 +380,7 @@ func OverwriteSettings(filename string) error {
}
}

txt, _ := json.MarshalIndent(settings, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
err = WriteJson(filename, settings)
}
return err
}