Created
August 4, 2016 15:14
-
-
Save suyash/a8f395962df1ad3f397a575da60e864f to your computer and use it in GitHub Desktop.
convert atom snippets to vscode snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"flag" | |
"io/ioutil" | |
"os" | |
"regexp" | |
"strings" | |
) | |
type Snippet struct { | |
Prefix string `json:"prefix"` | |
Body interface{} `json:"body"` | |
} | |
var nl = regexp.MustCompile("[^\n]*") | |
func splitNewlines(body string) []string { | |
return nl.FindAllString(body, -1) | |
} | |
func main() { | |
flag.Parse() | |
file := flag.Arg(0) | |
d, err := ioutil.ReadFile(file) | |
if err != nil { | |
panic(err) | |
} | |
atom := make(map[string]map[string]Snippet) | |
vscode := make(map[string]map[string]Snippet) | |
err = json.Unmarshal(d, &atom) | |
if err != nil { | |
panic(err) | |
} | |
for atomscope, atomsnippets := range atom { | |
vscodescopename := atomscope[strings.LastIndexByte(atomscope, '.')+1:] | |
vscodescope := make(map[string]Snippet) | |
for title, snippet := range atomsnippets { | |
lines := splitNewlines(snippet.Body.(string)) | |
sn := Snippet{} | |
sn.Prefix = snippet.Prefix | |
if len(lines) == 1 { | |
sn.Body = lines[0] | |
} else { | |
sn.Body = lines | |
} | |
vscodescope[title] = sn | |
} | |
vscode[vscodescopename] = vscodescope | |
} | |
for scope, data := range vscode { | |
if scope == "*" { | |
continue | |
} | |
for k, v := range vscode["*"] { | |
data[k] = v | |
} | |
w, err := os.Create(scope + ".json") | |
if err != nil { | |
panic(err) | |
} | |
defer w.Close() | |
vscodedata, err := json.MarshalIndent(data, "", "\t") | |
if err != nil { | |
panic(err) | |
} | |
vscodedata = bytes.Replace(vscodedata, []byte("\\u003c"), []byte("<"), -1) | |
vscodedata = bytes.Replace(vscodedata, []byte("\\u003e"), []byte(">"), -1) | |
vscodedata = bytes.Replace(vscodedata, []byte("\\u0026"), []byte("&"), -1) | |
vscodedata = bytes.Replace(vscodedata, []byte("\\t"), []byte("\t"), -1) | |
w.Write(vscodedata) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment