Skip to content

Commit

Permalink
fix: Comment strip in Quote
Browse files Browse the repository at this point in the history
  • Loading branch information
pluveto committed Feb 5, 2022
1 parent 22c5b7f commit 63079dc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 41 deletions.
37 changes: 37 additions & 0 deletions extensions/imgurl.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"meta": {
"id": "imgurl",
"name": "ImgUrl Uploader",
"type": "simple-http-uploader",
"version": "0.0.1",
"repository": ""
},
"http": {
"request": {
// See https://api.imgur.com/
"url": "https://sm.ms/api/v2/upload",
"method": "POST",
"headers": {
"Authorization": "Client-ID #ext_config.client_id#",
"Content-Type": "multipart/form-data",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36"
},
"body": {
"format": {
"type": "string",
"value": "json"
},
"smfile": {
"type": "file",
"value": "#task.local_path#"
}
}
}
},
"upload": {
"rawUrl": {
"from": "json_response",
"path": "data.url"
}
}
}
36 changes: 2 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -296,7 +295,7 @@ func upload() {
// load file to json
jsonBytes, err := ioutil.ReadFile(filepath.Join(extDir, fname))
abortErr(err)
jsonBytes = removeJsoncComments(jsonBytes)
jsonBytes = RemoveJsoncComments(jsonBytes)
GVerbose.Trace("file content: %s", string(jsonBytes))
var uploaderDef map[string]interface{}
err = json.Unmarshal(jsonBytes, &uploaderDef)
Expand All @@ -314,7 +313,7 @@ func upload() {
uploader.Config = extConfig
GVerbose.Trace("uploader config:")
GVerbose.TraceStruct(uploader.Config)
}else{
} else {
GVerbose.Trace("no uploader config found")
}
break
Expand All @@ -327,37 +326,6 @@ func upload() {

}

func removeJsoncComments(data []byte) []byte {
var buf bytes.Buffer
var inBlockComment bool
var inLineComment bool
for _, b := range data {
if inBlockComment {
if b == '*' && data[len(data)-1] == '/' {
inBlockComment = false
}
continue
}
if inLineComment {
if b == '\n' {
inLineComment = false
}
continue
}
if b == '/' {
if data[len(data)-1] == '/' {
inLineComment = true
continue
}
if data[len(data)-1] == '*' {
inBlockComment = true
continue
}
}
buf.WriteByte(b)
}
return buf.Bytes()
}

func loadClipboard() {
if len(opt.LocalPaths) == 1 && strings.ToLower(opt.LocalPaths[0]) == kClipboardPlaceholder {
Expand Down
20 changes: 14 additions & 6 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ func RemoveFmtUnderscore(in string) (out string) {

func RemoveJsoncComments(data []byte) []byte {
var buf bytes.Buffer
var inLineComment bool = false
for i, b := range data {
if b == '/' && i+1 < len(data) && data[i+1] == '/' {
inLineComment = true
var inQuote bool
var inComment bool
for _, b := range data {
if b == '"' {
inQuote = !inQuote
}
if inQuote {
buf.WriteByte(b)
continue
}
if b == '/' {
inComment = true
}
if b == '\n' {
inLineComment = false
inComment = false
}
if inLineComment {
if inComment {
continue
}
buf.WriteByte(b)
Expand Down
19 changes: 18 additions & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,31 @@ a
a
`,
),
},
{"1", args{
[]byte(
`
{
url: "http:https://www.example.com" // url
}
`,
),
},
[]byte(
`
{
url: "http:https://www.example.com"
}
`,
),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveJsoncComments(tt.args.data); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveJsoncComments() = %v, want %v", got, tt.want)
t.Errorf("RemoveJsoncComments() = %v, want %v", string(got), string(tt.want))
}
})
}
Expand Down

0 comments on commit 63079dc

Please sign in to comment.