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

View File

@ -2,16 +2,17 @@ package file
import (
"encoding/json"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/user"
"github.com/b3log/wide/util"
"github.com/golang/glog"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sort"
"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) {
@ -168,6 +169,7 @@ type FileNode struct {
Path string `json:"path"`
IconSkin string `json:"iconSkin"` // 值的末尾应该有一个空格
Type string `json:"type"`
Mode string `json:"mode"`
FileNodes []*FileNode `json:"children"`
}
@ -198,6 +200,7 @@ func walk(path string, info os.FileInfo, node *FileNode) {
ext := filepath.Ext(fpath)
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() {
var path = wide.curNode.path;
var mode = wide.curNode.mode;
var request = {
"file": wide.curNode.path,
"file": path,
"code": wide.curEditor.getValue(),
"cursorLine": wide.curEditor.getCursor().line,
"cursorCh": wide.curEditor.getCursor().ch
};
// TODO: HTML/XML/JSON 格式化处理
$.ajax({
type: 'POST',
url: '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function(data) {
if (data.succ) {
wide.curEditor.setValue(data.code);
}
}
});
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;
}
}
};

View File

@ -23,10 +23,10 @@
<a href="/doc/{{.locale}}/index.html" target="_blank">{{.i18n.help}}</a>
<span class="fn-none ico-fullscreen" onclick="editors.fullscreen()">{{.i18n.full_screen}}</span>
<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.fmt()">{{.i18n.format}}</button>
<button onclick="wide.goget()">{{.i18n.goget}}</button>
<button onclick="wide.goget()">{{.i18n.goget}}</button>
</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/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/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/addon/lint/lint.js"></script>
@ -120,7 +121,7 @@
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/mode/css/css.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/mode/shell/shell.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.5/mode/sql/sql.js"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/lint/json-lint.js?{{.Wide.StaticResourceVersion}}"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/tabs.js?{{.Wide.StaticResourceVersion}}"></script>
<script type="text/javascript" src="{{.Wide.StaticServer}}/static/js/editor.js?{{.Wide.StaticResourceVersion}}"></script>