better render performance and code cleanup
This commit is contained in:
parent
3cd460318a
commit
fb41dd75c4
3 changed files with 28 additions and 27 deletions
26
engine.go
26
engine.go
|
|
@ -62,7 +62,7 @@ func (e *Engine) Index(title, text, user string, public bool) (string, error) {
|
|||
title = titleRe.ReplaceAllString(title, "")
|
||||
newTitle := makeLinkTitle(title)
|
||||
if err := e.Exists(false, newTitle); err == nil {
|
||||
return "", errors.New("engine: article already exists")
|
||||
return "", errors.New("engine: article '" + title + "' already exists")
|
||||
}
|
||||
art := render(text)
|
||||
art.Title = title
|
||||
|
|
@ -81,8 +81,8 @@ func (e *Engine) Exists(public bool, title string) error {
|
|||
}
|
||||
|
||||
func (e *Engine) Delete(title string) error {
|
||||
if title == "Index" {
|
||||
return errors.New("engine: the index page cannot be deleted")
|
||||
if title == indexName {
|
||||
return errors.New("engine: page '" + indexName + "' cannot be deleted")
|
||||
}
|
||||
if err := e.Exists(false, title); err != nil {
|
||||
return err
|
||||
|
|
@ -94,8 +94,8 @@ func (e *Engine) Update(title, text, user string, public bool) error {
|
|||
if user == "" {
|
||||
return errors.New("engine: user cannot be empty")
|
||||
}
|
||||
if title == "Index" && !public {
|
||||
return errors.New("engine: page 'Index' must stay public")
|
||||
if title == indexName && !public {
|
||||
return errors.New("engine: page '" + indexName + "' must stay public")
|
||||
}
|
||||
if err := e.Exists(false, title); err != nil {
|
||||
return err
|
||||
|
|
@ -149,11 +149,10 @@ func (e *Engine) Get(public bool, title string) (Article, error) {
|
|||
art.Public = p
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
art.LinkTitle = title
|
||||
if public && !art.Public {
|
||||
err = errors.New("engine: article does not exist")
|
||||
err = errors.New("engine: article '" + title + "' does not esist")
|
||||
}
|
||||
return art, err
|
||||
}
|
||||
|
|
@ -240,13 +239,12 @@ func (e *Engine) GetAll(public bool) (t []Title, err error) {
|
|||
continue
|
||||
}
|
||||
for _, field := range doc.Fields {
|
||||
if field.Name() != "title" {
|
||||
continue
|
||||
}
|
||||
f, ok := field.(*document.TextField)
|
||||
if ok {
|
||||
t = append(t, Title{string(f.Value()), v.ID})
|
||||
break
|
||||
if field.Name() == "title" {
|
||||
f, ok := field.(*document.TextField)
|
||||
if ok {
|
||||
t = append(t, Title{string(f.Value()), v.ID})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
19
render.go
19
render.go
|
|
@ -18,19 +18,23 @@ func makeFilter() analysis.CharFilter {
|
|||
return f
|
||||
}
|
||||
|
||||
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]>")
|
||||
)
|
||||
|
||||
func render(text string) Article {
|
||||
re := regexp.MustCompile("(<h[1-6])>(.+)(</h[1-6]>)")
|
||||
link := regexp.MustCompile(`id="(.+)">`)
|
||||
ws := regexp.MustCompile("\\s+")
|
||||
rend := blackfriday.MarkdownCommon([]byte(text))
|
||||
html := string(rend)
|
||||
index := buildIndex(html)
|
||||
html = strings.Replace(html, "<table>", `<table class="table">`, -1)
|
||||
html = re.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
|
||||
html = link.ReplaceAllStringFunc(html, makeLinkTitle)
|
||||
html = reHeader.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
|
||||
html = reLink.ReplaceAllStringFunc(html, makeLinkTitle)
|
||||
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
|
||||
return Article{
|
||||
Search: ws.ReplaceAllString(string(filter.Filter(rend)), " "),
|
||||
Search: whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " "),
|
||||
Index: template.HTML(index),
|
||||
Text: template.HTML(html),
|
||||
MD: text,
|
||||
|
|
@ -42,8 +46,7 @@ func makeLinkTitle(title string) string {
|
|||
}
|
||||
|
||||
func buildIndex(html string) (ret string) {
|
||||
re := regexp.MustCompile("<h([1-6])>(.+)</h[1-6]>")
|
||||
hn := re.FindAllStringSubmatch(html, -1)
|
||||
hn := reIndex.FindAllStringSubmatch(html, -1)
|
||||
for k, v := range hn {
|
||||
link := makeLinkTitle(v[2])
|
||||
ret += before(hn, k)
|
||||
|
|
|
|||
10
userdb.go
10
userdb.go
|
|
@ -56,7 +56,7 @@ func NewBoltStore(file string) (*BoltStore, error) {
|
|||
return nil, err
|
||||
}
|
||||
bs := &BoltStore{NewGOB(), db}
|
||||
bs.UnlockUser("admin")
|
||||
bs.UnlockUser(defUser)
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
|
|
@ -217,8 +217,8 @@ func (bs *BoltStore) UpdateUserPassword(username, password string) error {
|
|||
}
|
||||
|
||||
func (bs *BoltStore) AdminUpdateUser(username, password string, admin bool) error {
|
||||
if username == "admin" && !admin {
|
||||
return errors.New("user 'admin' cannot lose admin privileges")
|
||||
if username == defUser && !admin {
|
||||
return errors.New("user '" + defUser + "' cannot lose admin privileges")
|
||||
}
|
||||
return bs.db.Update(func(tx *bolt.Tx) error {
|
||||
us, err := bs.GetUser(username)
|
||||
|
|
@ -242,8 +242,8 @@ func (bs *BoltStore) AdminUpdateUser(username, password string, admin bool) erro
|
|||
}
|
||||
|
||||
func (bs *BoltStore) DeleteUser(username string) error {
|
||||
if username == "admin" {
|
||||
return errors.New("user 'admin' cannot be deleted")
|
||||
if username == defUser {
|
||||
return errors.New("user '" + defUser + "' cannot be deleted")
|
||||
}
|
||||
return bs.db.Update(func(tx *bolt.Tx) error {
|
||||
if bs.UserExists(username) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue