HTML 格式化

This commit is contained in:
Liang Ding 2014-09-07 17:31:57 +08:00
parent 4440001b69
commit 530837f96a
5 changed files with 63 additions and 25 deletions

View File

@ -39,7 +39,6 @@ func HTMLFmtHandler(w http.ResponseWriter, r *http.Request) {
code := args["code"].(string) code := args["code"].(string)
fout.WriteString(code) fout.WriteString(code)
if err := fout.Close(); nil != err { if err := fout.Close(); nil != err {
glog.Error(err) glog.Error(err)
data["succ"] = false data["succ"] = false
@ -48,12 +47,21 @@ func HTMLFmtHandler(w http.ResponseWriter, r *http.Request) {
} }
output := gohtml.Format(code) output := gohtml.Format(code)
if "" == output { if "" == output {
data["succ"] = false data["succ"] = false
return 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
}
} }

View File

@ -2,16 +2,17 @@ package file
import ( import (
"encoding/json" "encoding/json"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog"
) )
func GetFiles(w http.ResponseWriter, r *http.Request) { func GetFiles(w http.ResponseWriter, r *http.Request) {
@ -168,6 +169,7 @@ type FileNode struct {
Path string `json:"path"` Path string `json:"path"`
IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格 IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格
Type string `json:"type"` Type string `json:"type"`
Mode string `json:"mode"`
FileNodes []*FileNode `json:"children"` FileNodes []*FileNode `json:"children"`
} }
@ -198,6 +200,7 @@ func walk(path string, info os.FileInfo, node *FileNode) {
ext := filepath.Ext(fpath) ext := filepath.Ext(fpath)
child.IconSkin = getIconSkin(ext) child.IconSkin = getIconSkin(ext)
child.Mode = getEditorMode(ext)
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -121,26 +121,51 @@ var wide = {
}); });
}, },
fmt: function() { fmt: function() {
var path = wide.curNode.path;
var mode = wide.curNode.mode;
var request = { var request = {
"file": wide.curNode.path, "file": path,
"code": wide.curEditor.getValue(), "code": wide.curEditor.getValue(),
"cursorLine": wide.curEditor.getCursor().line, "cursorLine": wide.curEditor.getCursor().line,
"cursorCh": wide.curEditor.getCursor().ch "cursorCh": wide.curEditor.getCursor().ch
}; };
// TODO: HTML/XML/JSON 格式化处理 switch (mode) {
case "text/x-go":
$.ajax({
type: 'POST',
url: '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
if (data.succ) {
wide.curEditor.setValue(data.code);
}
}
});
break;
case "text/html":
$.ajax({
type: 'POST',
url: '/html/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
if (data.succ) {
wide.curEditor.setValue(data.code);
}
}
});
break;
default :
// TODO: XML/JSON 格式化处理
break;
}
$.ajax({
type: 'POST',
url: '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
if (data.succ) {
wide.curEditor.setValue(data.code);
}
}
});
} }
}; };

View File

@ -23,10 +23,10 @@
<a href="/doc/{{.locale}}/index.html" target="_blank">{{.i18n.help}}</a> <a href="/doc/{{.locale}}/index.html" target="_blank">{{.i18n.help}}</a>
<span class="fn-none ico-fullscreen" onclick="editors.fullscreen()">{{.i18n.full_screen}}</span> <span class="fn-none ico-fullscreen" onclick="editors.fullscreen()">{{.i18n.full_screen}}</span>
<div class="toolbars fn-right"> <div class="toolbars fn-right">
<button onclick="wide.run()">{{.i18n.build_n_run}}</button> <button onclick="wide.run()">{{.i18n.build_n_run}}</button>
<button onclick="wide.save()">{{.i18n.save}}</button> <button onclick="wide.save()">{{.i18n.save}}</button>
<button onclick="wide.fmt()">{{.i18n.format}}</button> <button onclick="wide.fmt()">{{.i18n.format}}</button>
<button onclick="wide.goget()">{{.i18n.goget}}</button> <button onclick="wide.goget()">{{.i18n.goget}}</button>
</div> </div>
</div> </div>
@ -93,6 +93,7 @@
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/ztree/jquery.ztree.all-3.5.min.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/underscore.string-2.3.3/underscore.string.min.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/codemirror.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/codemirror.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/addon/lint/lint.js"></script> <script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/addon/lint/lint.js"></script>