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

Works so much better

Former-commit-id: a9b40f31f0753157af8500353395274b474642a2 [formerly 57875519283c5bfbfa6d852a27d72dc091157faa] [formerly bc0dde2306cf0e189d69349f344dffda393de1cc [formerly 19aaf7bb44]]
Former-commit-id: eb77f04e333250fef6799da788b460b77468dddf [formerly c3ddbc51302fbb8fc89410f114013b3a19f33cb8]
Former-commit-id: 814d1b257f6529e8ec45ba7acfa7269967900397
This commit is contained in:
Zack 2016-02-07 11:05:38 -05:00
parent d1c4a0b1f3
commit 6f445a7668
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
import (
"fmt"
"log"
"os"
@ -21,10 +22,17 @@ func main() {
ExternalIP = os.Args[1]
Open()
defer Close()
// Default page
p := CowyoData{"about", about_page}
p.save()
fmt.Println(about_page)
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/", newNote)
r.GET("/:title", editNote)
r.GET("/:title/*option", everythingElse)
r.DELETE("/listitem", deleteListItem)
r.Run(":12312")
}

94
routes.go Executable file → Normal file
View File

@ -1,10 +1,10 @@
package main
import (
"fmt"
"html/template"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
@ -21,7 +21,7 @@ func editNote(c *gin.Context) {
title := c.Param("title")
if title == "ws" {
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")
} else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
@ -34,7 +34,6 @@ func editNote(c *gin.Context) {
func everythingElse(c *gin.Context) {
option := c.Param("option")
title := c.Param("title")
fmt.Println(title, "["+option+"]")
if option == "/view" {
renderMarkdown(c, title)
} 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) {
if strings.ToLower(title) == "about" { //}&& strings.Contains(AllowedIPs, c.ClientIP()) != true {
c.Redirect(302, "/about/view")
}
p := CowyoData{strings.ToLower(title), ""}
err := p.load()
if err != nil {
panic(err)
}
listItems := []string{}
for _, line := range strings.Split(p.Text, "\n") {
if len(line) > 1 {
listItems = append(listItems, line)
}
}
fmt.Println(listItems)
listItems, _ := reorderList(p.Text)
c.HTML(http.StatusOK, "list.tmpl", gin.H{
"Title": title,
"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;
}
.yue a code,
.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;
-moz-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;
}
}
body {
margin: 0;
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>
<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>
<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">
{{ template "header" }}
<script src="/static/js/jquery-1.8.1.min.js"></script>
<script src="/static/js/jquery.autogrowtextarea.min.js"></script>
<script src="/static/js/cowyo.js"></script>
<script>
@ -47,19 +31,52 @@
box-shadow: none;
font-family: Tahoma, sans-serif;
}
body {
margin: 0;
padding: 0.4em 1em 6em;
background: #fff;
max-width: 650px;
margin: 0 auto;
}
</style>
</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 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">
<div>
<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 }}
Do not post anything private, as anyone with the URL may be able to access it.
Learn more at github.com/schollz/cowyo"></textarea>
Go to cowyo.com/{{ .Title }} to reload this page.
Do not post anything private.
Anyone with the URL can access this note."></textarea>
</div>
</form>
@ -68,6 +85,8 @@ Learn more at github.com/schollz/cowyo"></textarea>
$("#emit_data").autoGrow();
});
</script>
</body>
</html>

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

@ -3,41 +3,69 @@
<head>
<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="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>
<!-- 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 .ListItems }}
<li>&nbsp; {{ . }}</li>
{{ 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>

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

@ -3,25 +3,9 @@
<head>
<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>
<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">
{{ template "header" }}
<link rel="stylesheet" type="text/css" href="/static/css/view.css">
@ -29,6 +13,30 @@
</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 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">
{{ .Body }}
</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