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

Added total time to pages Issue #22

Former-commit-id: cafa73d9ad79299f3bb0049679d400479d655503 [formerly a49c91b6ed2187d40bba323e98b3f40373ecb855] [formerly 30ed4b6aedb03cff094e9a6ccd90f4e55ca8bff5 [formerly 71165300922812d6eb98203774d9bddcf119978e [formerly e6e5d701e60761dee87ef2f845e5e0561beba447]]]
Former-commit-id: 9d4d1b049f4c253868704a377d7e813f0bff4adb [formerly 13817559a3e0c4e156cd85916d77d993ba61eda6]
Former-commit-id: 15d127b0267745ebcbcc4fd2a15abdba78d8efb9
Former-commit-id: 45977e42ffc70eb2c2110a7fa8c3015320210d7d
This commit is contained in:
Zack Scholl 2016-02-11 09:26:02 -05:00
parent 15acd517da
commit 3cd7f84e1b
11 changed files with 31 additions and 25 deletions

10
db.go
View File

@ -77,13 +77,14 @@ func hasPassword(title string) (bool, error) {
return hasPassword, nil return hasPassword, nil
} }
func getCurrentText(title string, version int) (string, []versionsInfo, bool) { func getCurrentText(title string, version int) (string, []versionsInfo, bool, time.Duration) {
title = strings.ToLower(title) title = strings.ToLower(title)
var vi []versionsInfo var vi []versionsInfo
totalTime := time.Now().Sub(time.Now())
isCurrent := true isCurrent := true
currentText := "" currentText := ""
if !open { if !open {
return currentText, vi, isCurrent return currentText, vi, isCurrent, totalTime
} }
err := db.View(func(tx *bolt.Tx) error { err := db.View(func(tx *bolt.Tx) error {
var err error var err error
@ -107,13 +108,14 @@ func getCurrentText(title string, version int) (string, []versionsInfo, bool) {
currentText = rebuildTextsToDiffN(p, version) currentText = rebuildTextsToDiffN(p, version)
isCurrent = false isCurrent = false
} }
vi = getImportantVersions(p) vi, totalTime = getImportantVersions(p)
log.Println(totalTime)
return nil return nil
}) })
if err != nil { if err != nil {
fmt.Printf("Could not get WikiData: %s", err) fmt.Printf("Could not get WikiData: %s", err)
} }
return currentText, vi, isCurrent return currentText, vi, isCurrent, totalTime
} }
func (p *WikiData) load(title string) error { func (p *WikiData) load(title string) error {

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

0
install/awwkoala.nginx Normal file → Executable file
View File

View File

@ -8,6 +8,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -35,7 +36,7 @@ func editNote(c *gin.Context) {
} else { } else {
version := c.DefaultQuery("version", "-1") version := c.DefaultQuery("version", "-1")
versionNum, _ := strconv.Atoi(version) versionNum, _ := strconv.Atoi(version)
currentText, versions, currentVersion := getCurrentText(title, versionNum) currentText, versions, currentVersion, totalTime := getCurrentText(title, versionNum)
numRows := len(strings.Split(currentText, "\n")) + 10 numRows := len(strings.Split(currentText, "\n")) + 10
if currentVersion { if currentVersion {
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
@ -45,6 +46,7 @@ func editNote(c *gin.Context) {
"CurrentText": currentText, "CurrentText": currentText,
"NumRows": numRows, "NumRows": numRows,
"Versions": versions, "Versions": versions,
"TotalTime": totalTime,
}) })
} else { } else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
@ -54,6 +56,7 @@ func editNote(c *gin.Context) {
"CurrentText": currentText, "CurrentText": currentText,
"NumRows": numRows, "NumRows": numRows,
"Versions": versions, "Versions": versions,
"TotalTime": totalTime,
"NoEdit": true, "NoEdit": true,
}) })
} }
@ -71,10 +74,10 @@ func everythingElse(c *gin.Context) {
if strings.ToLower(title) == "about" { if strings.ToLower(title) == "about" {
versionNum = -1 versionNum = -1
} }
currentText, versions, _ := getCurrentText(title, versionNum) currentText, versions, _, totalTime := getCurrentText(title, versionNum)
renderMarkdown(c, currentText, title, versions, "") renderMarkdown(c, currentText, title, versions, "", totalTime)
} 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, RuntimeArgs.AdminKey) renderMarkdown(c, listEverything(), "ls", nil, RuntimeArgs.AdminKey, time.Now().Sub(time.Now()))
} else if option == "/list" { } else if option == "/list" {
renderList(c, title) renderList(c, title)
} else if title == "static" { } else if title == "static" {
@ -93,7 +96,7 @@ func serveStaticFile(c *gin.Context, option string) {
} }
} }
func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string) { func renderMarkdown(c *gin.Context, currentText string, title string, versions []versionsInfo, AdminKey string, totalTime time.Duration) {
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)
@ -125,18 +128,20 @@ func renderMarkdown(c *gin.Context, currentText string, title string, versions [
if AdminKey == "" { 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, "TotalTime": totalTime.String(),
"Versions": versions,
}) })
} else { } else {
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,
"AdminKey": AdminKey, "TotalTime": totalTime.String(),
"AdminKey": AdminKey,
}) })
} }
} }

0
static/img/awwkoala.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 1004 B

After

Width:  |  Height:  |  Size: 1004 B

0
static/img/gomascot.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

0
static/img/nginx.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 659 B

After

Width:  |  Height:  |  Size: 659 B

0
static/img/raspberrypi.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

@ -62,7 +62,7 @@
<li class="dropdown active"> <li class="dropdown active">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="active"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit <span class="caret"></span></a> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="active"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit <span class="caret"></span></a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="dropdown-header">Previous versions</li> <li class="dropdown-header">Total time: {{ .TotalTime }}</li>
<li><a href="/{{ .Title }}?version=0">First</a></li> <li><a href="/{{ .Title }}?version=0">First</a></li>
{{ range .Versions }} {{ range .Versions }}
<li><a href="/{{ $.Title }}?version={{ .VersionNum }}">{{ .VersionDate }}</a></li> <li><a href="/{{ $.Title }}?version={{ .VersionNum }}">{{ .VersionDate }}</a></li>

View File

@ -36,7 +36,7 @@ a.deleteable {
<li class="dropdown active"> <li class="dropdown active">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="active"><span class="glyphicon glyphicon-sunglasses" aria-hidden="true"></span> View <span class="caret"></span></a> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" class="active"><span class="glyphicon glyphicon-sunglasses" aria-hidden="true"></span> View <span class="caret"></span></a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="dropdown-header">Previous versions</li> <li class="dropdown-header">Total time: {{ .TotalTime }}</li>
<li><a href="/{{ .Title }}/view?version=0">First</a></li> <li><a href="/{{ .Title }}/view?version=0">First</a></li>
{{ range .Versions }} {{ range .Versions }}
<li><a href="/{{ $.Title }}/view?version={{ .VersionNum }}">{{ .VersionDate }}</a></li> <li><a href="/{{ $.Title }}/view?version={{ .VersionNum }}">{{ .VersionDate }}</a></li>

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand" "math/rand"
@ -86,7 +85,7 @@ func timeTrack(start time.Time, name string) {
log.Printf("%s took %s", name, elapsed) log.Printf("%s took %s", name, elapsed)
} }
func getImportantVersions(p WikiData) []versionsInfo { func getImportantVersions(p WikiData) ([]versionsInfo, time.Duration) {
// defer timeTrack(time.Now(), "getImportantVersions") // defer timeTrack(time.Now(), "getImportantVersions")
m := map[int]int{} m := map[int]int{}
lastTime := time.Now().AddDate(0, -1, 0) lastTime := time.Now().AddDate(0, -1, 0)
@ -129,7 +128,7 @@ func getImportantVersions(p WikiData) []versionsInfo {
for _, nn := range importantVersions { for _, nn := range importantVersions {
r = append(r, versionsInfo{p.Timestamps[nn], nn}) r = append(r, versionsInfo{p.Timestamps[nn], nn})
} }
return r return r, totalTime
} }
} }
} }
@ -138,8 +137,7 @@ func getImportantVersions(p WikiData) []versionsInfo {
for _, nn := range importantVersions { for _, nn := range importantVersions {
r = append(r, versionsInfo{p.Timestamps[nn], nn}) r = append(r, versionsInfo{p.Timestamps[nn], nn})
} }
fmt.Println(totalTime) return r, totalTime
return r
} }
func rebuildTextsToDiffN(p WikiData, n int) string { func rebuildTextsToDiffN(p WikiData, n int) string {
@ -166,6 +164,7 @@ const (
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
) )
// RandStringBytesMaskImprSrc prints a random string
func RandStringBytesMaskImprSrc(n int) string { func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, n) b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!