From 98742b2ce23cd34f3987d3413aa6037b6dac9ab5 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 7 Sep 2014 18:13:55 +0800 Subject: [PATCH] =?UTF-8?q?JSON=20=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- editor/editors.go | 61 --------------------- editor/formatter.go | 127 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + static/js/wide.js | 12 +++++ 4 files changed, 140 insertions(+), 61 deletions(-) diff --git a/editor/editors.go b/editor/editors.go index 8a28aa4..ab02a0f 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -12,7 +12,6 @@ import ( "github.com/b3log/wide/conf" "github.com/b3log/wide/user" - "github.com/b3log/wide/util" "github.com/golang/glog" "github.com/gorilla/websocket" ) @@ -75,66 +74,6 @@ func WSHandler(w http.ResponseWriter, r *http.Request) { } } -func GoFmtHandler(w http.ResponseWriter, r *http.Request) { - data := map[string]interface{}{"succ": true} - defer util.RetJSON(w, r, data) - - decoder := json.NewDecoder(r.Body) - - var args map[string]interface{} - - if err := decoder.Decode(&args); err != nil { - glog.Error(err) - data["succ"] = false - - return - } - - filePath := args["file"].(string) - - fout, err := os.Create(filePath) - - if nil != err { - glog.Error(err) - data["succ"] = false - - return - } - - code := args["code"].(string) - - fout.WriteString(code) - if err := fout.Close(); nil != err { - glog.Error(err) - data["succ"] = false - - return - } - - argv := []string{filePath} - cmd := exec.Command("gofmt", argv...) - - bytes, _ := cmd.Output() - output := string(bytes) - if "" == output { - data["succ"] = false - - return - } - - code = string(output) - data["code"] = code - - fout, err = os.Create(filePath) - fout.WriteString(code) - if err := fout.Close(); nil != err { - glog.Error(err) - data["succ"] = false - - return - } -} - func AutocompleteHandler(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) diff --git a/editor/formatter.go b/editor/formatter.go index 0ac4775..d42c48b 100644 --- a/editor/formatter.go +++ b/editor/formatter.go @@ -4,12 +4,73 @@ import ( "encoding/json" "net/http" "os" + "os/exec" "github.com/88250/gohtml" "github.com/b3log/wide/util" "github.com/golang/glog" ) +func GoFmtHandler(w http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{"succ": true} + defer util.RetJSON(w, r, data) + + decoder := json.NewDecoder(r.Body) + + var args map[string]interface{} + + if err := decoder.Decode(&args); err != nil { + glog.Error(err) + data["succ"] = false + + return + } + + filePath := args["file"].(string) + + fout, err := os.Create(filePath) + + if nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + code := args["code"].(string) + + fout.WriteString(code) + if err := fout.Close(); nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + argv := []string{filePath} + cmd := exec.Command("gofmt", argv...) + + bytes, _ := cmd.Output() + output := string(bytes) + if "" == output { + data["succ"] = false + + return + } + + code = string(output) + data["code"] = code + + fout, err = os.Create(filePath) + fout.WriteString(code) + if err := fout.Close(); nil != err { + glog.Error(err) + data["succ"] = false + + return + } +} + func HTMLFmtHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} defer util.RetJSON(w, r, data) @@ -65,3 +126,69 @@ func HTMLFmtHandler(w http.ResponseWriter, r *http.Request) { return } } + +func JSONFmtHandler(w http.ResponseWriter, r *http.Request) { + data := map[string]interface{}{"succ": true} + defer util.RetJSON(w, r, data) + + decoder := json.NewDecoder(r.Body) + + var args map[string]interface{} + + if err := decoder.Decode(&args); err != nil { + glog.Error(err) + data["succ"] = false + + return + } + + filePath := args["file"].(string) + + fout, err := os.Create(filePath) + + if nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + code := args["code"].(string) + + fout.WriteString(code) + if err := fout.Close(); nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + obj := new(interface{}) + if err := json.Unmarshal([]byte(code), &obj); nil != err { + glog.Error(err) + data["succ"] = false + + return + } + + glog.Info(obj) + + bytes, err := json.MarshalIndent(obj, "", " ") + if nil != err { + data["succ"] = false + + return + } + + code = string(bytes) + data["code"] = code + + fout, err = os.Create(filePath) + fout.WriteString(code) + if err := fout.Close(); nil != err { + glog.Error(err) + data["succ"] = false + + return + } +} diff --git a/main.go b/main.go index cda866b..e54a56f 100644 --- a/main.go +++ b/main.go @@ -92,6 +92,7 @@ func main() { http.HandleFunc("/go/fmt", editor.GoFmtHandler) http.HandleFunc("/autocomplete", editor.AutocompleteHandler) http.HandleFunc("/html/fmt", editor.HTMLFmtHandler) + http.HandleFunc("/json/fmt", editor.JSONFmtHandler) // Shell http.HandleFunc("/shell/ws", shell.WSHandler) diff --git a/static/js/wide.js b/static/js/wide.js index 2f452e6..0765b84 100644 --- a/static/js/wide.js +++ b/static/js/wide.js @@ -159,6 +159,18 @@ var wide = { } }); + break; + case "application/json": + try { + // 在客户端浏览器中进行 JSON 格式化 + var json = JSON.parse(wide.curEditor.getValue()); + wide.curEditor.setValue(JSON.stringify(json, "", " ")); + + this.save(); + } catch (e) { + delete e; + } + break; default : // TODO: XML/JSON 格式化处理