better render performance and code cleanup

This commit is contained in:
ston1th 2016-11-17 21:46:12 +01:00
commit fb41dd75c4
3 changed files with 28 additions and 27 deletions

View file

@ -62,7 +62,7 @@ func (e *Engine) Index(title, text, user string, public bool) (string, error) {
title = titleRe.ReplaceAllString(title, "") title = titleRe.ReplaceAllString(title, "")
newTitle := makeLinkTitle(title) newTitle := makeLinkTitle(title)
if err := e.Exists(false, newTitle); err == nil { 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 := render(text)
art.Title = title art.Title = title
@ -81,8 +81,8 @@ func (e *Engine) Exists(public bool, title string) error {
} }
func (e *Engine) Delete(title string) error { func (e *Engine) Delete(title string) error {
if title == "Index" { if title == indexName {
return errors.New("engine: the index page cannot be deleted") return errors.New("engine: page '" + indexName + "' cannot be deleted")
} }
if err := e.Exists(false, title); err != nil { if err := e.Exists(false, title); err != nil {
return err return err
@ -94,8 +94,8 @@ func (e *Engine) Update(title, text, user string, public bool) error {
if user == "" { if user == "" {
return errors.New("engine: user cannot be empty") return errors.New("engine: user cannot be empty")
} }
if title == "Index" && !public { if title == indexName && !public {
return errors.New("engine: page 'Index' must stay public") return errors.New("engine: page '" + indexName + "' must stay public")
} }
if err := e.Exists(false, title); err != nil { if err := e.Exists(false, title); err != nil {
return err return err
@ -149,11 +149,10 @@ func (e *Engine) Get(public bool, title string) (Article, error) {
art.Public = p art.Public = p
} }
} }
} }
art.LinkTitle = title art.LinkTitle = title
if public && !art.Public { if public && !art.Public {
err = errors.New("engine: article does not exist") err = errors.New("engine: article '" + title + "' does not esist")
} }
return art, err return art, err
} }
@ -240,9 +239,7 @@ func (e *Engine) GetAll(public bool) (t []Title, err error) {
continue continue
} }
for _, field := range doc.Fields { for _, field := range doc.Fields {
if field.Name() != "title" { if field.Name() == "title" {
continue
}
f, ok := field.(*document.TextField) f, ok := field.(*document.TextField)
if ok { if ok {
t = append(t, Title{string(f.Value()), v.ID}) t = append(t, Title{string(f.Value()), v.ID})
@ -250,6 +247,7 @@ func (e *Engine) GetAll(public bool) (t []Title, err error) {
} }
} }
} }
}
return return
} }

View file

@ -18,19 +18,23 @@ func makeFilter() analysis.CharFilter {
return f 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 { 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)) rend := blackfriday.MarkdownCommon([]byte(text))
html := string(rend) html := string(rend)
index := buildIndex(html) index := buildIndex(html)
html = strings.Replace(html, "<table>", `<table class="table">`, -1) html = strings.Replace(html, "<table>", `<table class="table">`, -1)
html = re.ReplaceAllString(html, `${1} id="${2}">${2}${3}`) html = reHeader.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
html = link.ReplaceAllStringFunc(html, makeLinkTitle) html = reLink.ReplaceAllStringFunc(html, makeLinkTitle)
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1) html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
return Article{ return Article{
Search: ws.ReplaceAllString(string(filter.Filter(rend)), " "), Search: whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " "),
Index: template.HTML(index), Index: template.HTML(index),
Text: template.HTML(html), Text: template.HTML(html),
MD: text, MD: text,
@ -42,8 +46,7 @@ func makeLinkTitle(title string) string {
} }
func buildIndex(html string) (ret string) { func buildIndex(html string) (ret string) {
re := regexp.MustCompile("<h([1-6])>(.+)</h[1-6]>") hn := reIndex.FindAllStringSubmatch(html, -1)
hn := re.FindAllStringSubmatch(html, -1)
for k, v := range hn { for k, v := range hn {
link := makeLinkTitle(v[2]) link := makeLinkTitle(v[2])
ret += before(hn, k) ret += before(hn, k)

View file

@ -56,7 +56,7 @@ func NewBoltStore(file string) (*BoltStore, error) {
return nil, err return nil, err
} }
bs := &BoltStore{NewGOB(), db} bs := &BoltStore{NewGOB(), db}
bs.UnlockUser("admin") bs.UnlockUser(defUser)
return bs, nil 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 { func (bs *BoltStore) AdminUpdateUser(username, password string, admin bool) error {
if username == "admin" && !admin { if username == defUser && !admin {
return errors.New("user 'admin' cannot lose admin privileges") return errors.New("user '" + defUser + "' cannot lose admin privileges")
} }
return bs.db.Update(func(tx *bolt.Tx) error { return bs.db.Update(func(tx *bolt.Tx) error {
us, err := bs.GetUser(username) 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 { func (bs *BoltStore) DeleteUser(username string) error {
if username == "admin" { if username == defUser {
return errors.New("user 'admin' cannot be deleted") return errors.New("user '" + defUser + "' cannot be deleted")
} }
return bs.db.Update(func(tx *bolt.Tx) error { return bs.db.Update(func(tx *bolt.Tx) error {
if bs.UserExists(username) { if bs.UserExists(username) {