updated dependencies and moved to new rendering

This commit is contained in:
ston1th 2020-11-12 22:54:50 +01:00
commit 0a55030ae5
467 changed files with 64111 additions and 111050 deletions

View file

@ -8,7 +8,6 @@ type Page struct {
Title string `json:"title"`
StoreTitle string `json:"store_title"`
Markdown string `json:"markdown"`
TOC template.HTML `json:"toc"`
HTML template.HTML `json:"html"`
Created string `json:"created"`
Updated string `json:"updated"`

View file

@ -3,12 +3,13 @@ package db
import (
"git.giftfish.de/ston1th/gowiki/pkg/core"
"git.giftfish.de/ston1th/gowiki/pkg/log"
"git.giftfish.de/ston1th/gowiki/pkg/render"
"git.giftfish.de/ston1th/gowiki/pkg/store"
)
const (
versionKey = "version/version"
currentVersion = 1
currentVersion = 2
)
type migrator func(*DB, int) error
@ -41,6 +42,7 @@ var migrators = []migrator{
continue
}
}
// create user sections
users, err := db.GetUsers()
if err != nil {
return
@ -53,6 +55,30 @@ var migrators = []migrator{
}
return
},
func(db *DB, version int) (err error) {
// re-render all pages
var pages []string
db.store.ForEachPrefix(pagePrefix, func(k string, _ []byte) error {
pages = append(pages, k)
return nil
})
var p core.Page
for _, v := range pages {
k := pagePrefix + v
err := db.store.Get(k, &p)
if err != nil {
log.Printf("db: migrator[%d]: pageGet %s: %s", version, k, err)
continue
}
render.Render(&p)
err = db.store.Set(k, &p)
if err != nil {
log.Printf("db: migrator[%d]: pageSet %s: %s", version, k, err)
continue
}
}
return
},
}
func (db *DB) RunMigrations() (err error) {

View file

@ -9,7 +9,6 @@ import (
"github.com/russross/blackfriday"
"html/template"
"regexp"
"strconv"
"strings"
)
@ -23,21 +22,43 @@ func makeFilter() analysis.CharFilter {
}
var (
reHeader = regexp.MustCompile("(<h[1-6]>)(.+)(</h[1-6]>)")
reLink = regexp.MustCompile(`id="(.+?)"`)
whiteSpace = regexp.MustCompile(`\s+`)
reIndex = regexp.MustCompile("<h([1-6])>(.+)</h[1-6]>")
titleRe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
whiteSpace = regexp.MustCompile(`\s+`)
)
const (
commonHtmlFlags = 0 |
blackfriday.HTML_USE_XHTML |
blackfriday.HTML_USE_SMARTYPANTS |
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
blackfriday.HTML_SMARTYPANTS_DASHES |
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES |
blackfriday.HTML_TOC |
blackfriday.HTML_HREF_TARGET_BLANK |
blackfriday.HTML_NOREFERRER_LINKS |
blackfriday.HTML_NOFOLLOW_LINKS |
blackfriday.HTML_NOOPENER_LINKS
commonExtensions = 0 |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS |
blackfriday.EXTENSION_AUTO_HEADER_IDS
)
func Render(p *core.Page) string {
rend := blackfriday.MarkdownCommon([]byte(p.Markdown))
toc, html := buildTOC(string(rend))
renderer := blackfriday.HtmlRenderer(commonHtmlFlags, "", "")
rend := blackfriday.MarkdownOptions([]byte(p.Markdown), renderer,
blackfriday.Options{Extensions: commonExtensions},
)
html := string(rend)
html = strings.ReplaceAll(html, "<table>", `<table class="table">`)
html = strings.ReplaceAll(html, "</h1>", `</h1><hr>`)
html = strings.ReplaceAll(html, "<a", `<a target="_blank" rel="noopener noreferrer"`)
html = strings.ReplaceAll(html, "\n</code", "</code")
p.TOC = template.HTML(toc)
p.HTML = template.HTML(html)
return whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " ")
}
@ -64,72 +85,3 @@ func SplitStoreTitle(st string) (section, title string) {
}
return
}
func buildTOC(html string) (string, string) {
var toc string
m := make(map[string]int)
hn := reIndex.FindAllStringSubmatch(html, -1)
html = reHeader.ReplaceAllString(html, `<a id="${2}" class="anchor"></a>${1}${2}${3}`)
html = reLink.ReplaceAllStringFunc(html, func(s string) string {
if len(s) >= 3 {
return `id="` + Title(s[2:]) + `"`
}
return s
})
for k, v := range hn {
link := Title(v[2])
i := m[link]
m[link] = i + 1
newLink := link + strconv.Itoa(i)
html = strings.Replace(html, link+`"`, newLink+`"`, 1)
toc += before(hn, k)
toc += `<li><a href="#` + newLink + `">` + v[2] + "</a>"
toc += after(hn, k)
}
return toc, html
}
func before(arr [][]string, i int) string {
c, _ := strconv.Atoi(arr[i][1])
if c == 1 {
return `<ul class="toc">`
}
return ""
}
func after(arr [][]string, i int) string {
c, _ := strconv.Atoi(arr[i][1])
if len(arr) <= i+1 {
return closeTag(c-1) + "</ul>"
}
n, _ := strconv.Atoi(arr[i+1][1])
switch {
case n == 1:
return closeTag(c-n) + "</ul>"
case n < c:
return closeTag(c - n)
case n > c:
return openTag(n - c)
case n == c:
return "</li>"
}
return ""
}
func openTag(j int) (ret string) {
for i := 0; i < j; i++ {
ret += `<ul class="toc"><li>`
}
ret = ret[0 : len(ret)-4]
return
}
func closeTag(j int) (ret string) {
for i := 0; i < j; i++ {
ret += "</li></ul></li>"
}
if j > 1 {
ret = strings.ReplaceAll(ret, "</li></li>", "</li>")
}
return
}

View file

@ -36,7 +36,7 @@ const (
<title>GoWiki | {{.Title}}</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" integrity="sha256-bgotk/IPq4Yvt6s8AQGPTM5ZjPjQ6rrBRa2EyxpnFSc="></link>
<link rel="stylesheet" type="text/css" href="/bootstrap.css" media="screen" integrity="sha256-3YeveuySvBgp2GItUS8eV1R/9T1zcPMyvw22SyVf8qo="></link>
<link rel="stylesheet" type="text/css" href="/custom.css" media="screen" integrity="sha256-RDvnwBbrc+kJfqaqqTMsI/idgLOQ5XEm0ABAk3gAQLo="></link>
<link rel="stylesheet" type="text/css" href="/custom.css" media="screen" integrity="sha256-IwnADqysqqVpTpQ4sbG3Bz7v8FM4lqMiozQPozsFBUQ="></link>
<meta name="theme-color" content="#375a7f">
<meta name="description" content="{{.Title}}">
</head>
@ -289,11 +289,6 @@ const (
<br>
</div>
</div>
<div class="row mt-5">
<div class="col">
{{.Data.TOC}}
</div>
</div>
{{.Data.HTML}}
{{end}}`
pageMD = `{{define "body"}}
@ -423,11 +418,6 @@ const (
<br>
</div>
</div>
<div class="row mt-5">
<div class="col">
{{.Data.TOC}}
</div>
</div>
{{.Data.HTML}}
{{end}}`
search = `{{define "body"}}
@ -926,10 +916,14 @@ a.para {
text-decoration: none;
font-size: 18px;
}
a.anchor {
nav {
padding-top: 20px;
}
*[id]:before {
display: block;
position: relative;
top: -70px;
content: " ";
margin-top: -75px;
height: 75px;
visibility: hidden;
}
.alert-primary {
@ -939,8 +933,6 @@ a.anchor {
}
h1,h2,h3,h4,h5,h6 {
font-weight: bold;
/*padding-top: 60px !important;*/
/*margin-top: -55px !important;*/
-webkit-background-clip: content-box !important;
background-clip: content-box !important;
}
@ -956,20 +948,13 @@ h3 {
h4 {
font-size: 1.25em;
}
h5 {
font-size: 1em;
}
h6 {
h5,h6 {
font-size: 1em;
}
.md-text {
font-family: 'Courier New';
font-size: 14;
}
ul.toc {
list-style-type: disc;
margin-bottom: 0 !important;
}
pre {
display: block;
padding: 10px;
@ -1010,10 +995,10 @@ input.menu {
.menu-search-input {
background-color: #2f2f2f !important;
}
html, body, .container, .content {
html,body,.container,.content {
height: 100% !important;
}
.container, .content {
.container,.content {
position: relative;
}
.proper-content {