From 4b827e7723c6a028b606245e0988b5b17febc2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Mon, 11 Sep 2017 22:39:36 +0200 Subject: [PATCH] adds title derivation --- server.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/server.go b/server.go index d2d334f..509dcff 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" "net/http" + "regexp" "time" "database/sql" @@ -56,6 +57,21 @@ type Note struct { Content template.HTML } +func (n Note) withTitle() Note { + fstLine := rexpNewLine.Split(n.Text, -1)[0] + maxLength := 25 + if len(fstLine) < 25 { + maxLength = len(fstLine) + } + n.Title = rexpNonAlphaNum.ReplaceAllString(fstLine[:maxLength], "") + return n +} + +var ( + rexpNewLine = regexp.MustCompile("[\n\r]") + rexpNonAlphaNum = regexp.MustCompile("[`~!@#$%^&*_|+=?;:'\",.<>{}\\/]") +) + func note(c echo.Context, db *sql.DB) (Note, int) { stmt, err := db.Prepare("select id, text, strftime('%s', published) as published," + " strftime('%s',edited) as edited, password, views from notes where id = ?") @@ -71,12 +87,11 @@ func note(c echo.Context, db *sql.DB) (Note, int) { c.Logger().Error(err) return note404, http.StatusNotFound } - // cand := regexp.MustCompile("[\n\r]").Split(text, 1) - // fmt.Println("CANDIDATE", cand[0]) return Note{ ID: id, + Text: text, Content: mdTmplHTML([]byte(text)), - }, http.StatusOK + }.withTitle(), http.StatusOK } func md2html(c echo.Context, name string) (Note, int) {