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

Added feature for Issue #12

Former-commit-id: 9ed802993056942c52b2eb4da6f99d6d05294ba1 [formerly a2879765caa065d93752fe9bf962be038e4d9de0] [formerly d28fe69ed895977f779b4813c1b25109a6e111a0 [formerly c95720fe54]]
Former-commit-id: 12d7e6b5204733db90a752112a6bb8a05a8fed14 [formerly 45ea44b618d5b32a4a60710ea8edaf05ae0819ae]
Former-commit-id: 3db8f3ad39941b46441eab818e9e6211d20c6b90
This commit is contained in:
Zack Scholl 2016-02-10 21:16:54 -05:00
parent 8b9009237d
commit d16f4924d5
3 changed files with 68 additions and 13 deletions

View File

@ -78,6 +78,7 @@ Options:`)
r.GET("/:title", editNote) r.GET("/:title", editNote)
r.GET("/:title/*option", everythingElse) r.GET("/:title/*option", everythingElse)
r.DELETE("/listitem", deleteListItem) r.DELETE("/listitem", deleteListItem)
r.DELETE("/deletepage", deletePage)
if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" { if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" {
r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey) r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey)
} else { } else {

View File

@ -72,9 +72,9 @@ func everythingElse(c *gin.Context) {
versionNum = -1 versionNum = -1
} }
currentText, versions, _ := getCurrentText(title, versionNum) currentText, versions, _ := getCurrentText(title, versionNum)
renderMarkdown(c, currentText, title, versions) renderMarkdown(c, currentText, title, versions, "")
} else if title == "ls" && option == "/"+RuntimeArgs.AdminKey && len(RuntimeArgs.AdminKey) > 1 { } else if title == "ls" && option == "/"+RuntimeArgs.AdminKey && len(RuntimeArgs.AdminKey) > 1 {
renderMarkdown(c, listEverything(), "ls", nil) renderMarkdown(c, listEverything(), "ls", nil, RuntimeArgs.AdminKey)
} else if option == "/list" { } else if option == "/list" {
renderList(c, title) renderList(c, title)
} else if title == "static" { } else if title == "static" {
@ -93,7 +93,7 @@ func serveStaticFile(c *gin.Context, option string) {
} }
} }
func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo) { func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string) {
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)
@ -103,6 +103,9 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [
pClean.AllowElements("img") pClean.AllowElements("img")
pClean.AllowAttrs("alt").OnElements("img") pClean.AllowAttrs("alt").OnElements("img")
pClean.AllowAttrs("src").OnElements("img") pClean.AllowAttrs("src").OnElements("img")
pClean.AllowAttrs("class").OnElements("a")
pClean.AllowAttrs("href").OnElements("a")
pClean.AllowAttrs("id").OnElements("a")
pClean.AllowDataURIImages() pClean.AllowDataURIImages()
html := pClean.SanitizeBytes(unsafe) html := pClean.SanitizeBytes(unsafe)
html2 := string(html) html2 := string(html)
@ -118,12 +121,24 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [
html2 = strings.Replace(html2, "$", "$", -1) html2 = strings.Replace(html2, "$", "$", -1)
html2 = strings.Replace(html2, "[", "[", -1) html2 = strings.Replace(html2, "[", "[", -1)
html2 = strings.Replace(html2, "]", "]", -1) html2 = strings.Replace(html2, "]", "]", -1)
html2 = strings.Replace(html2, "&amp35;", "#", -1)
if AdminKey == "" {
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)),
"Versions": versions, "Versions": versions,
}) })
} else {
c.HTML(http.StatusOK, "view.tmpl", gin.H{
"Title": title,
"WikiName": RuntimeArgs.WikiName,
"Body": template.HTML([]byte(html2)),
"Versions": versions,
"AdminKey": AdminKey,
})
}
} }
func reorderList(text string) ([]template.HTML, []string) { func reorderList(text string) ([]template.HTML, []string) {
@ -223,9 +238,25 @@ func deleteListItem(c *gin.Context) {
} }
} }
func deletePage(c *gin.Context) {
deleteName := c.DefaultQuery("DeleteName", "None")
adminKey := c.DefaultQuery("AdminKey", "None")
if adminKey == RuntimeArgs.AdminKey {
p := WikiData{deleteName, "", []string{}, []string{}}
p.save("")
c.JSON(200, gin.H{
"message": "Done.",
})
} else {
c.JSON(404, gin.H{
"message": "?",
})
}
}
func listEverything() string { func listEverything() string {
everything := `| Title | Current size | Changes | Total Size | everything := `| Title | Current size | Changes | Total Size | |
| --------- |-------------| -----| ------------- | | --------- |-------------| -----| ------------- | ------------- |
` `
db.View(func(tx *bolt.Tx) error { db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys // Assume bucket exists and has keys
@ -238,7 +269,7 @@ func listEverything() string {
contentSize := strconv.Itoa(len(p.CurrentText)) contentSize := strconv.Itoa(len(p.CurrentText))
numChanges := strconv.Itoa(len(p.Diffs)) numChanges := strconv.Itoa(len(p.Diffs))
totalSize := strconv.Itoa(len(v)) totalSize := strconv.Itoa(len(v))
everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + "|\n" everything += "| [" + p.Title + "](/" + p.Title + "/view) | " + contentSize + " | " + numChanges + " | " + totalSize + ` | <a class="deleteable" id="` + p.Title + `">Delete</a> | ` + "\n"
} }
} }
return nil return nil

View File

@ -10,7 +10,11 @@
<link rel="stylesheet" type="text/css" href="/static/css/katex.min.css"> <link rel="stylesheet" type="text/css" href="/static/css/katex.min.css">
<script src="/static/js/katex.min.js"></script> <script src="/static/js/katex.min.js"></script>
<style>
a.deleteable {
cursor: pointer;
}
</style>
</head> </head>
<body> <body>
@ -82,6 +86,25 @@ $(document).keydown(function(e){
} }
}); });
{{ if .AdminKey }}
$('.deleteable').click(function(event) {
event.preventDefault();
var deleteName = $(this).attr('id')
var href = $(this).attr('href')
console.log(deleteName)
$.ajax({
url: "/deletepage" + '?' + $.param({
"DeleteName": deleteName,
"AdminKey": "{{ .AdminKey }}"
}),
type: 'DELETE',
success: function() {
window.location.reload(true);
}
});
});
{{ end }}
</script> </script>
</body> </body>