diff --git a/editor/editors.go b/editor/editors.go index cb991b8..8a28aa4 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -3,17 +3,18 @@ package editor import ( "bytes" "encoding/json" - "github.com/b3log/wide/conf" - "github.com/b3log/wide/user" - "github.com/b3log/wide/util" - "github.com/golang/glog" - "github.com/gorilla/websocket" "net/http" "os" "os/exec" "runtime" "strconv" "strings" + + "github.com/b3log/wide/conf" + "github.com/b3log/wide/user" + "github.com/b3log/wide/util" + "github.com/golang/glog" + "github.com/gorilla/websocket" ) var editorWS = map[string]*websocket.Conn{} @@ -103,7 +104,6 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) { code := args["code"].(string) fout.WriteString(code) - if err := fout.Close(); nil != err { glog.Error(err) data["succ"] = false @@ -112,19 +112,27 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) { } argv := []string{filePath} - cmd := exec.Command("gofmt", argv...) bytes, _ := cmd.Output() output := string(bytes) - if "" == output { data["succ"] = false return } - data["code"] = string(output) + 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) { diff --git a/editor/formatter.go b/editor/formatter.go new file mode 100644 index 0000000..55d5a73 --- /dev/null +++ b/editor/formatter.go @@ -0,0 +1,59 @@ +package editor + +import ( + "encoding/json" + "net/http" + "os" + + "github.com/88250/gohtml" + "github.com/b3log/wide/util" + "github.com/golang/glog" +) + +func HTMLFmtHandler(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 + } + + output := gohtml.Format(code) + + if "" == output { + data["succ"] = false + + return + } + + data["code"] = string(output) +} diff --git a/main.go b/main.go index 5d1ed22..cda866b 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,12 @@ package main import ( "flag" + "html/template" + "math/rand" + "net/http" + "runtime" + "strconv" + "github.com/b3log/wide/conf" "github.com/b3log/wide/editor" "github.com/b3log/wide/file" @@ -10,11 +16,6 @@ import ( "github.com/b3log/wide/shell" "github.com/b3log/wide/user" "github.com/golang/glog" - "html/template" - "math/rand" - "net/http" - "runtime" - "strconv" ) // Wide 中唯一一个 init 函数. @@ -90,6 +91,7 @@ func main() { http.HandleFunc("/editor/ws", editor.WSHandler) http.HandleFunc("/go/fmt", editor.GoFmtHandler) http.HandleFunc("/autocomplete", editor.AutocompleteHandler) + http.HandleFunc("/html/fmt", editor.HTMLFmtHandler) // Shell http.HandleFunc("/shell/ws", shell.WSHandler)