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

Works so much better

This commit is contained in:
Zack 2016-02-07 11:05:38 -05:00
parent a6d89e5585
commit 19aaf7bb44
14 changed files with 287 additions and 97 deletions

0
db.go Executable file → Normal file
View File

0
install/cowyo.init Executable file → Normal file
View File

0
install/install.sh Executable file → Normal file
View File

8
main.go Executable file → Normal file
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
@ -21,10 +22,17 @@ func main() {
ExternalIP = os.Args[1] ExternalIP = os.Args[1]
Open() Open()
defer Close() defer Close()
// Default page
p := CowyoData{"about", about_page}
p.save()
fmt.Println(about_page)
r := gin.Default() r := gin.Default()
r.LoadHTMLGlob("templates/*") r.LoadHTMLGlob("templates/*")
r.GET("/", newNote) r.GET("/", newNote)
r.GET("/:title", editNote) r.GET("/:title", editNote)
r.GET("/:title/*option", everythingElse) r.GET("/:title/*option", everythingElse)
r.DELETE("/listitem", deleteListItem)
r.Run(":12312") r.Run(":12312")
} }

94
routes.go Executable file → Normal file
View File

@ -1,10 +1,10 @@
package main package main
import ( import (
"fmt"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strconv"
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -21,7 +21,7 @@ func editNote(c *gin.Context) {
title := c.Param("title") title := c.Param("title")
if title == "ws" { if title == "ws" {
wshandler(c.Writer, c.Request) wshandler(c.Writer, c.Request)
} else if strings.ToLower(title) == "about" && strings.Contains(AllowedIPs, c.ClientIP()) != true { } else if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view") c.Redirect(302, "/about/view")
} else { } else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
@ -34,7 +34,6 @@ func editNote(c *gin.Context) {
func everythingElse(c *gin.Context) { func everythingElse(c *gin.Context) {
option := c.Param("option") option := c.Param("option")
title := c.Param("title") title := c.Param("title")
fmt.Println(title, "["+option+"]")
if option == "/view" { if option == "/view" {
renderMarkdown(c, title) renderMarkdown(c, title)
} else if option == "/list" { } else if option == "/list" {
@ -69,21 +68,96 @@ func renderMarkdown(c *gin.Context, title string) {
}) })
} }
func reorderList(text string) ([]template.HTML, []string) {
listItemsString := ""
for _, lineString := range strings.Split(text, "\n") {
if len(lineString) > 1 {
if string(lineString[0]) != "-" {
listItemsString += "- " + lineString + "\n"
} else {
listItemsString += lineString + "\n"
}
}
}
// get ordering of template.HTML for rendering
renderedListString := string(blackfriday.MarkdownCommon([]byte(listItemsString)))
listItems := []template.HTML{}
endItems := []template.HTML{}
for _, lineString := range strings.Split(renderedListString, "\n") {
if len(lineString) > 1 {
if strings.Contains(lineString, "<del>") || strings.Contains(lineString, "</ul>") {
endItems = append(endItems, template.HTML(lineString))
} else {
listItems = append(listItems, template.HTML(lineString))
}
}
}
// get ordering of strings for deleting
listItemsStringArray := []string{}
endItemsStringArray := []string{}
for _, lineString := range strings.Split(listItemsString, "\n") {
if len(lineString) > 1 {
if strings.Contains(lineString, "~~") {
endItemsStringArray = append(endItemsStringArray, lineString)
} else {
listItemsStringArray = append(listItemsStringArray, lineString)
}
}
}
return append(listItems, endItems...), append(listItemsStringArray, endItemsStringArray...)
}
func renderList(c *gin.Context, title string) { func renderList(c *gin.Context, title string) {
if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view")
}
p := CowyoData{strings.ToLower(title), ""} p := CowyoData{strings.ToLower(title), ""}
err := p.load() err := p.load()
if err != nil { if err != nil {
panic(err) panic(err)
} }
listItems := []string{}
for _, line := range strings.Split(p.Text, "\n") { listItems, _ := reorderList(p.Text)
if len(line) > 1 {
listItems = append(listItems, line)
}
}
fmt.Println(listItems)
c.HTML(http.StatusOK, "list.tmpl", gin.H{ c.HTML(http.StatusOK, "list.tmpl", gin.H{
"Title": title, "Title": title,
"ListItems": listItems, "ListItems": listItems,
}) })
} }
func deleteListItem(c *gin.Context) {
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
title := c.Query("title") // shortcut for c.Request.URL.Query().Get("lastname")
if err == nil {
p := CowyoData{strings.ToLower(title), ""}
err := p.load()
if err != nil {
panic(err)
}
_, listItems := reorderList(p.Text)
for i, lineString := range listItems {
if i+1 == lineNum {
if strings.Contains(lineString, "~~") == false {
p.Text = strings.Replace(p.Text, lineString[2:]+"\n", "~~"+lineString[2:]+"~~"+"\n", 1)
} else {
p.Text = strings.Replace(p.Text, lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
}
p.save()
break
}
}
c.JSON(200, gin.H{
"message": "Done.",
})
} else {
c.JSON(404, gin.H{
"message": "?",
})
}
}

11
static/css/view.css Executable file → Normal file
View File

@ -70,8 +70,16 @@ margin-top: 0;
margin-bottom: 1.46em; margin-bottom: 1.46em;
} }
.yue a code,
.yue a { .yue a {
color: #111; color: #1E90FF;
word-wrap: break-word;
-moz-text-decoration-color: rgba(0, 0, 0, 0.4);
text-decoration-color: rgba(0, 0, 0, 0.4);
}
.yue a li {
color: #555;
word-wrap: break-word; word-wrap: break-word;
-moz-text-decoration-color: rgba(0, 0, 0, 0.4); -moz-text-decoration-color: rgba(0, 0, 0, 0.4);
text-decoration-color: rgba(0, 0, 0, 0.4); text-decoration-color: rgba(0, 0, 0, 0.4);
@ -293,6 +301,7 @@ background: none;
margin-left: 0; margin-left: 0;
} }
} }
body { body {
margin: 0; margin: 0;
padding: 0.4em 1em 6em; padding: 0.4em 1em 6em;

0
static/js/cowyo.js Executable file → Normal file
View File

9
static/js/jquery.js vendored

File diff suppressed because one or more lines are too long

24
templates/base.tmpl Executable file → Normal file
View File

@ -0,0 +1,24 @@
{{define "header"}}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" sizes="57x57" href=/static/img/favicon/apple-icon-57x57.png>
<link rel="apple-touch-icon" sizes="60x60" href=/static/img/favicon/img/favicon/apple-icon-60x60.png>
<link rel="apple-touch-icon" sizes="72x72" href=/static/img/favicon/apple-icon-72x72.png>
<link rel="apple-touch-icon" sizes="76x76" href=/static/img/favicon/apple-icon-76x76.png>
<link rel="apple-touch-icon" sizes="114x114" href=/static/img/favicon/apple-icon-114x114.png>
<link rel="apple-touch-icon" sizes="120x120" href=/static/img/favicon/apple-icon-120x120.png>
<link rel="apple-touch-icon" sizes="144x144" href=/static/img/favicon/apple-icon-144x144.png>
<link rel="apple-touch-icon" sizes="152x152" href=/static/img/favicon/apple-icon-152x152.png>
<link rel="apple-touch-icon" sizes="180x180" href=/static/img/favicon/apple-icon-180x180.png>
<link rel="icon" type="image/png" sizes="192x192" href=/static/img/favicon/android-icon-192x192.png>
<link rel="icon" type="image/png" sizes="32x32" href=/static/img/favicon/favicon-32x32.png>
<link rel="icon" type="image/png" sizes="96x96" href=/static/img/favicon/favicon-96x96.png>
<link rel="icon" type="image/png" sizes="16x16" href=/static/img/favicon/favicon-16x16.png>
<link rel="manifest" href=/static/img/favicon/manifest.json>
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
<script src="/static/js/jquery.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
{{end}}

63
templates/index.tmpl Executable file → Normal file
View File

@ -3,27 +3,11 @@
<head> <head>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" sizes="57x57" href=/static/img/favicon/apple-icon-57x57.png> {{ template "header" }}
<link rel="apple-touch-icon" sizes="60x60" href=/static/img/favicon/img/favicon/apple-icon-60x60.png>
<link rel="apple-touch-icon" sizes="72x72" href=/static/img/favicon/apple-icon-72x72.png>
<link rel="apple-touch-icon" sizes="76x76" href=/static/img/favicon/apple-icon-76x76.png>
<link rel="apple-touch-icon" sizes="114x114" href=/static/img/favicon/apple-icon-114x114.png>
<link rel="apple-touch-icon" sizes="120x120" href=/static/img/favicon/apple-icon-120x120.png>
<link rel="apple-touch-icon" sizes="144x144" href=/static/img/favicon/apple-icon-144x144.png>
<link rel="apple-touch-icon" sizes="152x152" href=/static/img/favicon/apple-icon-152x152.png>
<link rel="apple-touch-icon" sizes="180x180" href=/static/img/favicon/apple-icon-180x180.png>
<link rel="icon" type="image/png" sizes="192x192" href=/static/img/favicon/android-icon-192x192.png>
<link rel="icon" type="image/png" sizes="32x32" href=/static/img/favicon/favicon-32x32.png>
<link rel="icon" type="image/png" sizes="96x96" href=/static/img/favicon/favicon-96x96.png>
<link rel="icon" type="image/png" sizes="16x16" href=/static/img/favicon/favicon-16x16.png>
<link rel="manifest" href=/static/img/favicon/manifest.json>
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<script src="/static/js/jquery-1.8.1.min.js"></script>
<script src="/static/js/jquery.autogrowtextarea.min.js"></script> <script src="/static/js/jquery.autogrowtextarea.min.js"></script>
<script src="/static/js/cowyo.js"></script> <script src="/static/js/cowyo.js"></script>
<script> <script>
@ -47,19 +31,52 @@
box-shadow: none; box-shadow: none;
font-family: Tahoma, sans-serif; font-family: Tahoma, sans-serif;
} }
body {
margin: 0;
padding: 0.4em 1em 6em;
background: #fff;
max-width: 650px;
margin: 0 auto;
}
</style> </style>
</head> </head>
<body> <body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Cowyo</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/{{ .Title }}">Edit</a></li>
<li><a href="/{{ .Title }}/view">View</a></li>
<li><a href="/{{ .Title }}/list">List</a></li>
<li><a href="/about/view">About</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<form action='#' id="emit" method="post" name="emit"> <form action='#' id="emit" method="post" name="emit">
<div> <div>
<textarea autofocus rows=500 class='auto_submit_item' id="emit_data" name="emit_data" placeholder="Start typing, it will save automatically. <textarea autofocus rows=500 class='auto_submit_item' id="emit_data" name="emit_data" placeholder="Start typing, it will save automatically.
To reload this note goto cowyo.com/{{ .Title }} Go to cowyo.com/{{ .Title }} to reload this page.
Do not post anything private, as anyone with the URL may be able to access it. Do not post anything private.
Learn more at github.com/schollz/cowyo"></textarea> Anyone with the URL can access this note."></textarea>
</div> </div>
</form> </form>
@ -68,6 +85,8 @@ Learn more at github.com/schollz/cowyo"></textarea>
$("#emit_data").autoGrow(); $("#emit_data").autoGrow();
}); });
</script> </script>
</body> </body>
</html> </html>

112
templates/list.tmpl Executable file → Normal file
View File

@ -1,44 +1,72 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1"> {{ template "header" }}
<link rel="apple-touch-icon" sizes="57x57" href=/static/img/favicon/apple-icon-57x57.png> <link rel="stylesheet" type="text/css" href="/static/css/view.css">
<link rel="apple-touch-icon" sizes="60x60" href=/static/img/favicon/img/favicon/apple-icon-60x60.png>
<link rel="apple-touch-icon" sizes="72x72" href=/static/img/favicon/apple-icon-72x72.png>
<link rel="apple-touch-icon" sizes="76x76" href=/static/img/favicon/apple-icon-76x76.png>
<link rel="apple-touch-icon" sizes="114x114" href=/static/img/favicon/apple-icon-114x114.png>
<link rel="apple-touch-icon" sizes="120x120" href=/static/img/favicon/apple-icon-120x120.png>
<link rel="apple-touch-icon" sizes="144x144" href=/static/img/favicon/apple-icon-144x144.png>
<link rel="apple-touch-icon" sizes="152x152" href=/static/img/favicon/apple-icon-152x152.png>
<link rel="apple-touch-icon" sizes="180x180" href=/static/img/favicon/apple-icon-180x180.png>
<link rel="icon" type="image/png" sizes="192x192" href=/static/img/favicon/android-icon-192x192.png>
<link rel="icon" type="image/png" sizes="32x32" href=/static/img/favicon/favicon-32x32.png>
<link rel="icon" type="image/png" sizes="96x96" href=/static/img/favicon/favicon-96x96.png>
<link rel="icon" type="image/png" sizes="16x16" href=/static/img/favicon/favicon-16x16.png>
<link rel="manifest" href=/static/img/favicon/manifest.json>
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" type="text/css" href="/static/css/view.css">
<script src="/static/js/jquery-1.8.1.min.js"></script>
</head>
<body>
<div class="yue">
<ul>
{{ range .ListItems }}
<li>&nbsp; {{ . }}</li>
{{ end }}
</ul>
</div>
</body>
</head>
<body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Cowyo</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/{{ .Title }}">Edit</a></li>
<li><a href="/{{ .Title }}/view">View</a></li>
<li class="active"><a href="/{{ .Title }}/list">List</a></li>
<li><a href="/about/view">About</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="yue">
<ul>
{{ range $index, $element := .ListItems }}
<a href="#" id="{{ $index }}" class="deletable">{{ $element }}</a>
{{ end }}
</ul>
</div>
<script>
$('.deletable').click(function(event) {
event.preventDefault();
var lineNum = $(this).attr('id')
var href = $(this).attr('href')
if (href == "#") {
console.log(lineNum)
$.ajax({
url: "/listitem" + '?' + $.param({
"lineNum": lineNum,
"title": "{{ .Title }}"
}),
type: 'DELETE',
success: function() {
window.location.reload(true);
}
});
}
});
</script>
</body>
</html> </html>

44
templates/view.tmpl Executable file → Normal file
View File

@ -3,25 +3,9 @@
<head> <head>
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" sizes="57x57" href=/static/img/favicon/apple-icon-57x57.png> {{ template "header" }}
<link rel="apple-touch-icon" sizes="60x60" href=/static/img/favicon/img/favicon/apple-icon-60x60.png>
<link rel="apple-touch-icon" sizes="72x72" href=/static/img/favicon/apple-icon-72x72.png>
<link rel="apple-touch-icon" sizes="76x76" href=/static/img/favicon/apple-icon-76x76.png>
<link rel="apple-touch-icon" sizes="114x114" href=/static/img/favicon/apple-icon-114x114.png>
<link rel="apple-touch-icon" sizes="120x120" href=/static/img/favicon/apple-icon-120x120.png>
<link rel="apple-touch-icon" sizes="144x144" href=/static/img/favicon/apple-icon-144x144.png>
<link rel="apple-touch-icon" sizes="152x152" href=/static/img/favicon/apple-icon-152x152.png>
<link rel="apple-touch-icon" sizes="180x180" href=/static/img/favicon/apple-icon-180x180.png>
<link rel="icon" type="image/png" sizes="192x192" href=/static/img/favicon/android-icon-192x192.png>
<link rel="icon" type="image/png" sizes="32x32" href=/static/img/favicon/favicon-32x32.png>
<link rel="icon" type="image/png" sizes="96x96" href=/static/img/favicon/favicon-96x96.png>
<link rel="icon" type="image/png" sizes="16x16" href=/static/img/favicon/favicon-16x16.png>
<link rel="manifest" href=/static/img/favicon/manifest.json>
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<link rel="stylesheet" type="text/css" href="/static/css/view.css"> <link rel="stylesheet" type="text/css" href="/static/css/view.css">
@ -29,6 +13,30 @@
</head> </head>
<body> <body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-bottom">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Cowyo</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="/{{ .Title }}">Edit</a></li>
<li class="active"><a href="/{{ .Title }}/view">View</a></li>
<li><a href="/{{ .Title }}/list">List</a></li>
<li><a href="/about/view">About</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
<div class="yue"> <div class="yue">
{{ .Body }} {{ .Body }}
</div> </div>

19
utils.go Executable file → Normal file

File diff suppressed because one or more lines are too long

0
websockets.go Executable file → Normal file
View File