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

Merge pull request #113 from DanielHeath/master

Make the 'view' link do something helpful
This commit is contained in:
Zack 2018-02-09 15:13:08 -08:00 committed by GitHub
commit dfd9aea863
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 795 additions and 595 deletions

View File

@ -2,7 +2,7 @@
# make -j4 release
VERSION=$(shell git describe)
LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}" -a -installsuffix cgo
LDFLAGS=-ldflags "-s -w -X main.version=${VERSION} -X main.hotCodeReloading=false" -a -installsuffix cgo
.PHONY: build
build:
@ -11,8 +11,8 @@ build:
.PHONY: quick
quick:
go-bindata static/... templates/...
go build
go-bindata -debug static/... templates/...
go build -ldflags "-X main.hotCodeReloading=true"
.PHONY: linuxarm
linuxarm:

File diff suppressed because one or more lines are too long

View File

@ -37,16 +37,49 @@ func serve(
secret string,
secretCode string,
allowInsecure bool,
hotTemplateReloading bool,
) {
if hotTemplateReloading {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
if hotTemplateReloading {
router.LoadHTMLGlob("templates/*.tmpl")
} else {
router.HTMLRender = loadTemplates("index.tmpl")
}
store := sessions.NewCookieStore([]byte(secret))
router.Use(sessions.Sessions("mysession", store))
if secretCode != "" {
router.Use(secretRequired.RequiresSecretAccessCode(secretCode, "/login/"))
cfg := &secretRequired.Config{
Secret: secretCode,
Path: "/login/",
RequireAuth: func(c *gin.Context) bool {
page := c.Param("page")
cmd := c.Param("command")
if page == "sitemap.xml" || page == "favicon.ico" || page == "static" {
return false // no auth for sitemap
}
router.HTMLRender = loadTemplates("index.tmpl")
if page != "" && cmd == "/read" {
p := Open(page)
fmt.Printf("p: '%+v'\n", p)
if p != nil && p.IsPublished {
return false // Published pages don't require auth.
}
}
return true
},
}
router.Use(cfg.Middleware)
}
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
router.GET("/", func(c *gin.Context) {
if defaultPage != "" {
@ -335,6 +368,7 @@ func handlePageRequest(c *gin.Context) {
"DontKnowPage": command[0:2] != "/e" &&
command[0:2] != "/v" &&
command[0:2] != "/l" &&
command[0:2] != "/r" &&
command[0:2] != "/h",
"DirectoryPage": page == "ls",
"DirectoryEntries": DirectoryEntries,
@ -355,6 +389,8 @@ func handlePageRequest(c *gin.Context) {
"Debounce": debounceTime,
"DiaryMode": diaryMode,
"Date": time.Now().Format("2006-01-02"),
"UnixTime": time.Now().Unix(),
"ChildPageNames": p.ChildPageNames(),
})
}
@ -414,6 +450,7 @@ func handlePageUpdate(c *gin.Context) {
type QueryJSON struct {
Page string `json:"page"`
NewText string `json:"new_text"`
FetchedAt int64 `json:"fetched_at"`
IsEncrypted bool `json:"is_encrypted"`
IsPrimed bool `json:"is_primed"`
Meta string `json:"meta"`
@ -441,6 +478,8 @@ func handlePageUpdate(c *gin.Context) {
message = "Locked, must unlock first"
} else if p.IsEncrypted {
message = "Encrypted, must decrypt first"
} else if json.FetchedAt > 0 && p.LastEditUnixTime() > json.FetchedAt {
message = "Refusing to overwrite others work"
} else {
p.Meta = json.Meta
p.Update(json.NewText)
@ -454,7 +493,7 @@ func handlePageUpdate(c *gin.Context) {
message = "Saved"
success = true
}
c.JSON(http.StatusOK, gin.H{"success": success, "message": message})
c.JSON(http.StatusOK, gin.H{"success": success, "message": message, "unix_time": time.Now().Unix()})
}
func handlePrime(c *gin.Context) {

View File

@ -9,6 +9,7 @@ import (
)
var version string
var hotTemplateReloading bool
var pathToData string
func main() {
@ -53,6 +54,7 @@ func main() {
c.GlobalString("cookie-secret"),
c.GlobalString("access-code"),
c.GlobalBool("allow-insecure-markup"),
hotTemplateReloading,
)
return nil
}

29
page.go
View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
@ -27,6 +28,14 @@ type Page struct {
IsPublished bool
}
func (p Page) LastEditTime() time.Time {
return time.Unix(p.LastEditUnixTime(), 0)
}
func (p Page) LastEditUnixTime() int64 {
return p.Text.LastEditTime() / 1000000000
}
func Open(name string) (p *Page) {
p = new(Page)
p.Name = name
@ -120,6 +129,26 @@ func (p *Page) Save() error {
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
}
func (p *Page) ChildPageNames() []string {
prefix := strings.ToLower(p.Name + ": ")
files, err := filepath.Glob(path.Join(pathToData, "*"))
if err != nil {
panic("Filepath pattern cannot be malformed")
}
result := []string{}
for i := range files {
basename := filepath.Base(files[i])
if strings.HasSuffix(basename, ".json") {
cname, err := decodeFromBase32(basename[:len(basename)-len(".json")])
if err == nil && strings.HasPrefix(strings.ToLower(cname), prefix) {
result = append(result, cname)
}
}
}
return result
}
func (p *Page) IsNew() bool {
return !exists(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
}

View File

@ -55,4 +55,18 @@ func TestGeneral(t *testing.T) {
t.Errorf("Did not render: '%s'", p2.RenderedPage)
}
p3 := Open("testpage: childpage")
err = p3.Update("**child content**")
if err != nil {
t.Error(err)
}
children := p.ChildPageNames()
if len(children) != 1 {
t.Errorf("Expected 1 child page to be found, got %d", len(children))
return
}
if children[0] != "testpage: childpage" {
t.Errorf("Expected child page %s to be found (got %s)", "testpage: childpage", children[0])
}
}

128
static/css/default.css Normal file
View File

@ -0,0 +1,128 @@
body.ListPage span {
cursor: pointer;
}
body {
background: #fff;
}
.success {
color: #5cb85c;
font-weight: bold;
}
.failure {
color: #d9534f;
font-weight: bold;
}
.pure-menu a {
color: #777;
}
.deleting {
opacity: 0.5;
}
#wrap {
position: absolute;
top: 50px;
left: 0px;
right: 0px;
bottom: 0px;
}
#pad {
height:100%;
}
body.EditPage {
overflow:hidden;
}
body#pad textarea {
width: 100%;
height: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
border: 0;
border: none;
outline: none;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
resize: none;
font-size: 1.0em;
font-family: 'Open Sans','Segoe UI',Tahoma,Arial,sans-serif;
}
body#pad.HasDotInName textarea {
font-family: "Lucida Console", Monaco, monospace;
}
.markdown-body ul, .markdown-body ol {
padding-left: 0em;
}
@media (min-width: 5em) {
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
padding-left: 2%;
padding-right: 2%;
}
.pure-menu .pure-menu-horizontal {
max-width: 300px;
}
.pure-menu-disabled, .pure-menu-heading, .pure-menu-link {
padding-left:1.2em;
padding-right:em;
}
.ChildPageNames ul {
grid-template-columns: repeat(1, 1fr);
}
}
@media (min-width: 50em) {
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
padding-left: 10%;
padding-right: 10%;
}
.pure-menu-disabled, .pure-menu-heading, .pure-menu-link {
padding: .5em 1em;
}
.ChildPageNames ul {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 70em) {
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
padding-left: 15%;
padding-right: 15%;
}
.ChildPageNames ul {
grid-template-columns: repeat(3, 1fr);
}
}
@media (min-width: 100em) {
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
padding-left: 20%;
padding-right: 20%;
}
.ChildPageNames ul {
grid-template-columns: repeat(4, 1fr);
}
}
.ChildPageNames ul {
margin: 0.3em 0 0 1.6em;
padding: 0;
display: grid;
grid-gap: 0.5rem;
}
.ChildPageNames li {
margin: 0;
}
.ChildPageNames a {
color: #0645ad;
background: none;
}

354
static/js/cowyo.js Normal file
View File

@ -0,0 +1,354 @@
"use strict";
var oulipo = false;
$(window).load(function() {
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
$('#saveEditButton').removeClass()
$('#saveEditButton').text("Editing");
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
// This will apply the debounce effect on the keyup event
// And it only fires 500ms or half a second after the user stopped typing
var prevText = $('#userInput').val();
$('#userInput').on('keyup', debounce(function() {
if (prevText == $('#userInput').val()) {
return // no changes
}
prevText = $('#userInput').val();
if (oulipo) {
$('#userInput').val($('#userInput').val().replace(/e/g,""));
}
$('#saveEditButton').removeClass()
$('#saveEditButton').text("Saving")
upload();
}, window.debounceMS));
var latestUpload = null, needAnother = false;
function upload() {
// Prevent concurrent uploads
if (latestUpload != null) {
needAnother = true;
return
}
latestUpload = $.ajax({
type: 'POST',
url: '/update',
data: JSON.stringify({
new_text: $('#userInput').val(),
page: window.cowyo.pageName,
fetched_at: window.lastFetch,
}),
success: function(data) {
latestUpload = null;
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
window.lastFetch = data.unix_time;
if (needAnother) {
upload();
};
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
needAnother = false;
},
error: function(xhr, error) {
latestUpload = null;
needAnother = false;
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function primeForSelfDestruct() {
$.ajax({
type: 'POST',
url: '/prime',
data: JSON.stringify({
page: window.cowyo.pageName,
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function lockPage(passphrase) {
$.ajax({
type: 'POST',
url: '/lock',
data: JSON.stringify({
page: window.cowyo.pageName,
passphrase: passphrase
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.success == true && $('#lockPage').text() == "Lock") {
window.location = "/" + window.cowyo.pageName + "/view";
}
if (data.success == true && $('#lockPage').text() == "Unlock") {
window.location = "/" + window.cowyo.pageName + "/edit";
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function publishPage() {
$.ajax({
type: 'POST',
url: '/publish',
data: JSON.stringify({
page: window.cowyo.pageName,
publish: $('#publishPage').text() == "Publish"
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.message == "Unpublished") {
$('#publishPage').text("Publish");
} else {
$('#publishPage').text("Unpublish");
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function encryptPage(passphrase) {
$.ajax({
type: 'POST',
url: '/encrypt',
data: JSON.stringify({
page: window.cowyo.pageName,
passphrase: passphrase
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.success == true && $('#encryptPage').text() == "Encrypt") {
window.location = "/" + window.cowyo.pageName + "/view";
}
if (data.success == true && $('#encryptPage').text() == "Decrypt") {
window.location = "/" + window.cowyo.pageName + "/edit";
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function clearOld() {
$.ajax({
type: 'DELETE',
url: '/oldlist',
data: JSON.stringify({
page: window.cowyo.pageName
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.success == true) {
window.location = "/" + window.cowyo.pageName + "/list";
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass();
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
$("#encryptPage").click(function(e) {
e.preventDefault();
var passphrase = prompt("Please enter a passphrase. Note: Encrypting will remove all previous history.", "");
if (passphrase != null) {
encryptPage(passphrase);
}
});
$("#erasePage").click(function(e) {
e.preventDefault();
var r = confirm("Are you sure you want to erase?");
if (r == true) {
window.location = "/" + window.cowyo.pageName + "/erase";
} else {
x = "You pressed Cancel!";
}
});
$("#selfDestructPage").click(function(e) {
e.preventDefault();
var r = confirm("This will erase the page the next time it is opened, are you sure you want to do that?");
if (r == true) {
primeForSelfDestruct();
} else {
x = "You pressed Cancel!";
}
});
$("#lockPage").click(function(e) {
e.preventDefault();
var passphrase = prompt("Please enter a passphrase to lock", "");
if (passphrase != null) {
if ($('#lockPage').text() == "Lock") {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Locking");
} else {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Unlocking");
}
lockPage(passphrase);
// POST encrypt page
// reload page
}
});
$("#publishPage").click(function(e) {
e.preventDefault();
var message = " This will add your page to the sitemap.xml so it will be indexed by search engines.";
if ($('#publishPage').text() == "Unpublish") {
message = "";
}
var confirmed = confirm("Are you sure?" + message);
if (confirmed == true) {
if ($('#publishPage').text() == "Unpublish") {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Unpublishing");
} else {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Publishing");
}
publishPage();
}
});
$("#clearOld").click(function(e) {
e.preventDefault();
var r = confirm("This will erase all cleared list items, are you sure you want to do that? (Versions will stay in history).");
if (r == true) {
clearOld()
} else {
x = "You pressed Cancel!";
}
});
$("textarea").keydown(function(e) {
if(e.keyCode === 9) { // tab was pressed
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var $this = $(this);
var value = $this.val();
// set textarea value to: text before caret + tab + text after caret
$this.val(value.substring(0, start)
+ "\t"
+ value.substring(end));
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;
// prevent the focus lose
e.preventDefault();
}
});
$('.deletable').click(function(event) {
event.preventDefault();
var lineNum = $(this).attr('id');
$.ajax({
url: "/listitem" + '?' + $.param({
"lineNum": lineNum,
"page": window.cowyo.pageName
}),
type: 'DELETE',
success: function() {
window.location.reload(true);
}
});
event.target.classList.add('deleting');
});
});

View File

@ -1,6 +1,5 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@ -22,481 +21,46 @@
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#fff">
{{ if and .CustomCSS .ReadPage }}
<link rel="stylesheet" type="text/css" href="/static/css/custom.css">
{{ else }}
<script type="text/javascript" src="/static/js/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css" href="/static/css/github-markdown.css">
<link rel="stylesheet" type="text/css" href="/static/css/menus-min.css">
<link rel="stylesheet" type="text/css" href="/static/css/base-min.css">
<link rel="stylesheet" href="/static/css/highlight.css">
<script src="/static/js/highlight.min.js"></script>
<script type="text/javascript" src="/static/js/highlight.pack.js"></script>
<style type="text/css">
{{ if .ListPage }}
/* Required for lists */
span { cursor: pointer; }
{{ end }}
body {
background: #fff;
}
.success {
color: #5cb85c;
font-weight: bold;
}
.failure {
color: #d9534f;
font-weight: bold;
}
.pure-menu a {
color: #777;
}
.deleting {
opacity: 0.5;
}
#wrap {
position: absolute;
top: 50px;
left: 0px;
right: 0px;
bottom: 0px;
}
#pad {
height:100%;
}
{{ if .EditPage }}
body {
overflow:hidden;
}
{{ end }}
body#pad textarea {
width: 100%;
height: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
border: 0;
border: none;
outline: none;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
resize: none;
font-size: 1.0em;
{{ if .HasDotInName }}
font-family: "Lucida Console", Monaco, monospace;
{{else}}
font-family: 'Open Sans','Segoe UI',Tahoma,Arial,sans-serif;
{{ if and .CustomCSS .ReadPage }}
<link rel="stylesheet" type="text/css" href="/static/css/custom.css">
{{ else }}
<link rel="stylesheet" type="text/css" href="/static/css/github-markdown.css">
<link rel="stylesheet" type="text/css" href="/static/css/menus-min.css">
<link rel="stylesheet" type="text/css" href="/static/css/base-min.css">
<link rel="stylesheet" href="/static/css/highlight.css">
<link rel="stylesheet" href="/static/css/default.css">
<script type="text/javascript" src="/static/js/jquery-1.8.3.js"></script>
<script src="/static/js/highlight.min.js"></script>
<script type="text/javascript" src="/static/js/highlight.pack.js"></script>
{{ end }}
}
.markdown-body ul, .markdown-body ol {
padding-left: 0em;
}
@media (min-width: 5em) {
div#menu, div#rendered, body#pad textarea {
padding-left: 2%;
padding-right: 2%;
}
.pure-menu .pure-menu-horizontal {
max-width: 300px;
}
.pure-menu-disabled, .pure-menu-heading, .pure-menu-link {
padding-left:1.2em;
padding-right:em;
}
}
@media (min-width: 50em) {
div#menu, div#rendered, body#pad textarea {
padding-left: 10%;
padding-right: 10%;
}
.pure-menu-disabled, .pure-menu-heading, .pure-menu-link {
padding: .5em 1em;
}
}
@media (min-width: 70em) {
div#menu, div#rendered, body#pad textarea {
padding-left: 15%;
padding-right: 15%;
}
}
@media (min-width: 100em) {
div#menu, div#rendered, body#pad textarea {
padding-left: 20%;
padding-right: 20%;
}
}
</style>
{{ end }}
<title>{{ .Page }}</title>
<script type='text/javascript'>
oulipo = false;
//<![CDATA[
$(window).load(function() {
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function() {
$('#saveEditButton').removeClass()
$('#saveEditButton').text("Editing");
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
// This will apply the debounce effect on the keyup event
// And it only fires 500ms or half a second after the user stopped typing
$('#userInput').on('keyup', debounce(function() {
console.log('typing occurred');
if (oulipo == true) { $('#userInput').val($('#userInput').val().replace(/e/g,"")); }
$('#saveEditButton').removeClass()
$('#saveEditButton').text("Saving")
upload();
}, {{ .Debounce }}));
function upload() {
$.ajax({
type: 'POST',
url: '/update',
data: JSON.stringify({
new_text: $('#userInput').val(),
page: "{{ .Page }}"
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
hljs.initHighlightingOnLoad();
window.cowyo = {
debounceMS: {{ .Debounce }},
lastFetch: {{ .UnixTime }},
pageName: "{{ .Page }}",
}
$('#saveEditButton').text(data.message);
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function primeForSelfDestruct() {
$.ajax({
type: 'POST',
url: '/prime',
data: JSON.stringify({
page: "{{ .Page }}"
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function lockPage(passphrase) {
$.ajax({
type: 'POST',
url: '/lock',
data: JSON.stringify({
page: "{{ .Page }}",
passphrase: passphrase
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.success == true && $('#lockPage').text() == "Lock") {
window.location = "/{{ .Page }}/view";
}
if (data.success == true && $('#lockPage').text() == "Unlock") {
window.location = "/{{ .Page }}/edit";
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function publishPage() {
$.ajax({
type: 'POST',
url: '/publish',
data: JSON.stringify({
page: "{{ .Page }}",
publish: $('#publishPage').text() == "Publish"
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.message == "Unpublished") {
$('#publishPage').text("Publish");
} else {
$('#publishPage').text("Unpublish");
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function encryptPage(passphrase) {
$.ajax({
type: 'POST',
url: '/encrypt',
data: JSON.stringify({
page: "{{ .Page }}",
passphrase: passphrase
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.success == true && $('#encryptPage').text() == "Encrypt") {
window.location = "/{{ .Page }}/view";
}
if (data.success == true && $('#encryptPage').text() == "Decrypt") {
window.location = "/{{ .Page }}/edit";
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass()
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
function clearOld() {
$.ajax({
type: 'DELETE',
url: '/oldlist',
data: JSON.stringify({
page: "{{ .Page }}"
}),
success: function(data) {
$('#saveEditButton').removeClass()
if (data.success == true) {
$('#saveEditButton').addClass("success");
} else {
$('#saveEditButton').addClass("failure");
}
$('#saveEditButton').text(data.message);
if (data.success == true) {
window.location = "/{{ .Page }}/list";
}
},
error: function(xhr, error) {
$('#saveEditButton').removeClass();
$('#saveEditButton').addClass("failure");
$('#saveEditButton').text(error);
},
contentType: "application/json",
dataType: 'json'
});
}
$("#encryptPage").click(function(e) {
e.preventDefault();
var passphrase = prompt("Please enter a passphrase. Note: Encrypting will remove all previous history.", "");
if (passphrase != null) {
encryptPage(passphrase);
}
});
$("#erasePage").click(function(e) {
e.preventDefault();
var r = confirm("Are you sure you want to erase?");
if (r == true) {
window.location = "/{{ .Page }}/erase";
} else {
x = "You pressed Cancel!";
}
});
$("#selfDestructPage").click(function(e) {
e.preventDefault();
var r = confirm("This will erase the page the next time it is opened, are you sure you want to do that?");
if (r == true) {
primeForSelfDestruct();
} else {
x = "You pressed Cancel!";
}
});
$("#lockPage").click(function(e) {
e.preventDefault();
var passphrase = prompt("Please enter a passphrase to lock", "");
if (passphrase != null) {
if ($('#lockPage').text() == "Lock") {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Locking");
} else {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Unlocking");
}
lockPage(passphrase);
// POST encrypt page
// reload page
}
});
$("#publishPage").click(function(e) {
e.preventDefault();
var message = " This will add your page to the sitemap.xml so it will be indexed by search engines.";
if ($('#publishPage').text() == "Unpublish") {
message = "";
}
var confirmed = confirm("Are you sure?" + message);
if (confirmed == true) {
if ($('#publishPage').text() == "Unpublish") {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Unpublishing");
} else {
$('#saveEditButton').removeClass();
$("#saveEditButton").text("Publishing");
}
publishPage();
}
});
$("#clearOld").click(function(e) {
e.preventDefault();
var r = confirm("This will erase all cleared list items, are you sure you want to do that? (Versions will stay in history).");
if (r == true) {
clearOld()
} else {
x = "You pressed Cancel!";
}
});
$("textarea").keydown(function(e) {
if(e.keyCode === 9) { // tab was pressed
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var $this = $(this);
var value = $this.val();
// set textarea value to: text before caret + tab + text after caret
$this.val(value.substring(0, start)
+ "\t"
+ value.substring(end));
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;
// prevent the focus lose
e.preventDefault();
}
});
$('.deletable').click(function(event) {
event.preventDefault();
var lineNum = $(this).attr('id');
$.ajax({
url: "/listitem" + '?' + $.param({
"lineNum": lineNum,
"page": "{{ .Page }}"
}),
type: 'DELETE',
success: function() {
window.location.reload(true);
}
});
event.target.classList.add('deleting');
});
}); //]]>
</script>
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript" src="/static/js/cowyo.js"></script>
</head>
<body id="pad">
<body id="pad" class="
{{ if .EditPage }} EditPage {{ end }}
{{ if .ViewPage }} ViewPage {{ end }}
{{ if .ListPage }} ListPage {{ end }}
{{ if .HistoryPage }} HistoryPage {{ end }}
{{ if .ReadPage }} ReadPage {{ end }}
{{ if .DontKnowPage }} DontKnowPage {{ end }}
{{ if .DirectoryPage }} DirectoryPage {{ end }}
{{ if .HasDotInName }} HasDotInName {{ end }}
">
<article class="markdown-body">
{{ if .ReadPage }}
<div id="wrap">
<div id="rendered">
{{ .RenderedPage }}
</div>
</div>
<!-- No menu for read page -->
{{ else }}
<div class="pure-menu pure-menu-horizontal" id="menu">
<ul class="pure-menu-list">
<li></li>
@ -531,13 +95,9 @@ body#pad textarea {
{{ end }}
</ul>
</li>
<!--
<li class="pure-menu-item {{ with .ViewPage }}pure-menu-selected{{ end }}">
<a href="/{{ .Page }}/view" class="pure-menu-link">View</a>
</li>-->
<li class="pure-menu-item pure-menu-has-children pure-menu-allow-hover {{ with .ViewPage }}pure-menu-selected{{ end }}">
<a href="#" id="menuLink1" class="pure-menu-link">View</a>
<a href="/{{ .Page }}/view" class="pure-menu-link">View</a>
<ul class="pure-menu-children">
<li class="pure-menu-item">
<a href="/{{ .Page }}/read" class="pure-menu-link">Current</a>
@ -564,30 +124,54 @@ body#pad textarea {
{{ end }}
<li class="pure-menu-item {{ with .EditPage }}pure-menu-selected{{ end }}"><a href="/{{ .Page }}/edit" class="pure-menu-link"><span id="saveEditButton">Edit</span></a></li>
{{end}}
</ul>
</div>
{{ end }}
<div id="wrap">
{{ if .EditPage }} <div id="pad"><textarea autofocus placeholder="Use markdown to write your note! New: you can publish your note when you are done ({{ .Page }} -> Publish)." id="userInput">{{ .RawPage }}</textarea></div>{{ end }}
<div id="rendered">
{{ if .DontKnowPage }} <strong><center>{{ .Route }} not understood!</center></strong>{{ end }}
{{ if .ViewPage }}{{ .RenderedPage }}
{{ if .EditPage }}
<div id="pad">
<textarea
autofocus
placeholder="Use markdown to write your note! New: you can publish your note when you are done ({{ .Page }} -> Publish)."
id="userInput"
>{{ .RawPage }}</textarea>
</div>
{{ end }}
<div id="rendered">
{{ if .DontKnowPage }}
<strong>
<center>
{{ .Route }} not understood!
</center>
</strong>
{{ end }}
{{ if .ViewPage }}
{{ .RenderedPage }}
{{ end }}
{{ if .ReadPage }}
{{ .RenderedPage }}
{{ end }}
{{ if .HistoryPage }}
<h1>History</h1>
<ul>
{{range $i, $e := .Versions}}
<li style="list-style: none;">
<a href="/{{ $.Page }}/view?version={{$e}}">View</a>&nbsp;&nbsp;<a href="/{{ $.Page }}/list?version={{$e}}">List</a>&nbsp;&nbsp;<a href="/{{ $.Page }}/raw?version={{$e}}">Raw</a>&nbsp;&nbsp;
<a href="/{{ $.Page }}/view?version={{$e}}">View</a>
&nbsp;&nbsp;
<a href="/{{ $.Page }}/list?version={{$e}}">List</a>
&nbsp;&nbsp;
<a href="/{{ $.Page }}/raw?version={{$e}}">Raw</a>
&nbsp;&nbsp;
{{index $.VersionsText $i}}&nbsp;({{if lt (index $.VersionsChangeSums $i) 0}}<span style="color:red">{{else}}<span style="color:green">+{{end}}{{index $.VersionsChangeSums $i}}</span>)</li>
{{end}}
</ul>
{{ end }}
{{ if .ListPage }}
{{ range $index, $element := .ListItems }}
{{ $element }}
@ -611,16 +195,19 @@ body#pad textarea {
</tr>
{{ end }}
</table>
{{end}}
</div>
{{ end }}
</div>
{{ if .ChildPageNames }}
<section class="ChildPageNames">
<h2>See also</h2>
<ul>
{{ range .ChildPageNames }}
<li><a href="/{{ . }}/view">{{ . }}</a></li>
{{ end }}
</ul>
</section>
{{ end }}
</div>
{{ end }}
</article>
</body>
</html>

View File

@ -20,6 +20,7 @@ func TestReverseList(t *testing.T) {
t.Errorf("Could not reverse: %v", s2)
}
}
func TestHashing(t *testing.T) {
p := HashPassword("1234")
log.Debug(p)