1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Updated for allowing coding stuff

This commit is contained in:
Zack Scholl 2016-03-29 11:50:06 -04:00
parent 47578f526f
commit 65501e95b9
3 changed files with 92 additions and 22 deletions

View File

@ -207,6 +207,19 @@ func newNote(c *gin.Context) {
c.Redirect(302, "/"+title) c.Redirect(302, "/"+title)
} }
func getCodeType(title string) string {
if strings.Contains(title, ".js") {
return "javascript"
} else if strings.Contains(title, ".py") {
return "python"
} else if strings.Contains(title, ".go") {
return "go"
} else if strings.Contains(title, ".html") {
return "htmlmixed"
}
return ""
}
func editNote(c *gin.Context) { func editNote(c *gin.Context) {
title := c.Param("title") title := c.Param("title")
if title == "ws" { if title == "ws" {
@ -240,16 +253,8 @@ func editNote(c *gin.Context) {
if totalTime.Seconds() < 1 { if totalTime.Seconds() < 1 {
totalTimeString = "< 1 s" totalTimeString = "< 1 s"
} }
CodeType := ""
if strings.Contains(title, ".js") { CodeType := getCodeType(title)
CodeType = "javascript"
} else if strings.Contains(title, ".py") {
CodeType = "python"
} else if strings.Contains(title, ".go") {
CodeType = "go"
} else if strings.Contains(title, ".html") {
CodeType = "htmlmixed"
}
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title, "Title": title,
"WikiName": RuntimeArgs.WikiName, "WikiName": RuntimeArgs.WikiName,
@ -305,6 +310,7 @@ func serveStaticFile(c *gin.Context, option string) {
} }
func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration, encrypted bool, noprompt bool, locked bool) { func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration, encrypted bool, noprompt bool, locked bool) {
originalText := currentText
r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]") r, _ := regexp.Compile("\\[\\[(.*?)\\]\\]")
for _, s := range r.FindAllString(currentText, -1) { for _, s := range r.FindAllString(currentText, -1) {
currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1) currentText = strings.Replace(currentText, s, "["+s[2:len(s)-2]+"](/"+s[2:len(s)-2]+"/view)", 1)
@ -337,10 +343,12 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [
if totalTime.Seconds() < 1 { if totalTime.Seconds() < 1 {
totalTimeString = "< 1 s" totalTimeString = "< 1 s"
} }
CodeType := getCodeType(title)
c.HTML(http.StatusOK, "view.tmpl", gin.H{ c.HTML(http.StatusOK, "view.tmpl", gin.H{
"Title": title, "Title": title,
"WikiName": RuntimeArgs.WikiName, "WikiName": RuntimeArgs.WikiName,
"Body": template.HTML([]byte(html2)), "Body": template.HTML([]byte(html2)),
"CurrentText": originalText,
"Versions": versions, "Versions": versions,
"TotalTime": totalTimeString, "TotalTime": totalTimeString,
"AdminKey": AdminKey, "AdminKey": AdminKey,
@ -348,6 +356,8 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [
"Locked": locked, "Locked": locked,
"Prompt": noprompt, "Prompt": noprompt,
"LockedOrEncrypted": locked || encrypted, "LockedOrEncrypted": locked || encrypted,
"Coding": len(CodeType) > 0,
"CodeType": CodeType,
}) })
} }

View File

@ -6,11 +6,7 @@
{{ template "header" }} {{ template "header" }}
{{ if .Coding }}
<script src="/static/js/codemirror.js"></script>
<link rel="stylesheet" href="/static/css/codemirror.css">
<script src="/static/js/{{ .CodeType }}.js"></script>
{{ end }}
<script src="/static/js/jquery.autogrowtextarea.min.js"></script> <script src="/static/js/jquery.autogrowtextarea.min.js"></script>
{{if .NoEdit}} {{else}} {{if .NoEdit}} {{else}}
@ -30,7 +26,7 @@
.CodeMirror { .CodeMirror {
border: 1px solid #eee; border: 1px solid #eee;
height: auto; height: auto;
} }
textarea { textarea {
@ -58,7 +54,6 @@
@media (min-width: 1200px) { @media (min-width: 1200px) {
.container { .container {
max-width: 800px; max-width: 800px;
padding-bottom: 65px;
} }
} }
</style> </style>
@ -305,6 +300,9 @@
{{ if .Coding }} {{ if .Coding }}
<script src="/static/js/codemirror.js"></script>
<link rel="stylesheet" href="/static/css/codemirror.css">
<script src="/static/js/{{ .CodeType }}.js"></script>
<script> <script>
var editor = CodeMirror.fromTextArea(document.getElementById("emit_data"), { var editor = CodeMirror.fromTextArea(document.getElementById("emit_data"), {
lineNumbers: true, lineNumbers: true,
@ -318,11 +316,11 @@
} }
</script> </script>
{{ else }} {{ else }}
<script> <script>
function currentText() { function currentText() {
return $('#emit_data').val() return $('#emit_data').val()
} }
</script> </script>
{{ end }} {{ end }}

View File

@ -15,6 +15,43 @@ a.deleteable {
cursor: pointer; cursor: pointer;
} }
</style> </style>
{{ if .Coding }}
<style>
.CodeMirror {
border: 1px solid #eee;
height: auto;
}
textarea {
width: 100%;
margin: 5px 0;
padding: 10px;
border: none;
overflow: auto;
outline: none;
font-size: large;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
font-family: Tahoma, sans-serif;
}
body {
margin: 0;
background: #fff;
max-width: 800px;
margin: 0 auto;
padding-bottom: 65px;
}
@media (min-width: 1200px) {
.container {
max-width: 800px;
}
}
</style>
{{ end }}
<script src="/static/js/sweetalert-dev.js"></script> <script src="/static/js/sweetalert-dev.js"></script>
<link rel="stylesheet" href="/static/css/sweetalert.css"> <link rel="stylesheet" href="/static/css/sweetalert.css">
</head> </head>
@ -65,6 +102,13 @@ a.deleteable {
</div> </div>
</nav> </nav>
{{ if .Coding }}
<div>
<textarea autofocus rows={{ .NumRows }} class='auto_submit_item' id="emit_data" name="emit_data" placeholder="Start typing, it will save automatically.">{{ .CurrentText }}</textarea>
</div>
{{ else }}
<div class="yue"> <div class="yue">
{{ if .Encrypted }} {{ if .Encrypted }}
<pre> <pre>
@ -74,6 +118,7 @@ a.deleteable {
{{ .Body }} {{ .Body }}
{{ end }} {{ end }}
</div> </div>
{{ end }}
<script> <script>
$( document ).ready(function() { $( document ).ready(function() {
@ -250,6 +295,23 @@ $(document).keydown(function(e){
}); });
{{ end }} {{ end }}
</script> </script>
{{ if .Coding }}
<script src="/static/js/codemirror.js"></script>
<link rel="stylesheet" href="/static/css/codemirror.css">
<script src="/static/js/{{ .CodeType }}.js"></script>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("emit_data"), {
lineNumbers: true,
mode: "{{ .CodeType }}",
matchBrackets: true,
viewportMargin: Infinity,
readOnly: true
});
editor.setSize("100%", "100%")
</script>
{{ end }}
</body> </body>
</html> </html>