JSON 格式化

This commit is contained in:
Liang Ding 2014-09-07 18:13:55 +08:00
parent 530837f96a
commit 98742b2ce2
4 changed files with 140 additions and 61 deletions

View File

@ -12,7 +12,6 @@ import (
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/user" "github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gorilla/websocket" "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) { func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)

View File

@ -4,12 +4,73 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"os" "os"
"os/exec"
"github.com/88250/gohtml" "github.com/88250/gohtml"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
"github.com/golang/glog" "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) { func HTMLFmtHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true} data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data) defer util.RetJSON(w, r, data)
@ -65,3 +126,69 @@ func HTMLFmtHandler(w http.ResponseWriter, r *http.Request) {
return 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
}
}

View File

@ -92,6 +92,7 @@ func main() {
http.HandleFunc("/go/fmt", editor.GoFmtHandler) http.HandleFunc("/go/fmt", editor.GoFmtHandler)
http.HandleFunc("/autocomplete", editor.AutocompleteHandler) http.HandleFunc("/autocomplete", editor.AutocompleteHandler)
http.HandleFunc("/html/fmt", editor.HTMLFmtHandler) http.HandleFunc("/html/fmt", editor.HTMLFmtHandler)
http.HandleFunc("/json/fmt", editor.JSONFmtHandler)
// Shell // Shell
http.HandleFunc("/shell/ws", shell.WSHandler) http.HandleFunc("/shell/ws", shell.WSHandler)

View File

@ -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; break;
default : default :
// TODO: XML/JSON 格式化处理 // TODO: XML/JSON 格式化处理