package database import ( "database/sql" "fmt" "github.com/gosimple/slug" "regexp" "strings" "time" ) type Post struct { ID int Title string `db:"post_title"` Content string Date string GemtextContent sql.NullString `db:"gemtext_content"` TitleSlug string `db:"title_slug"` Year, Month, Day int } func (p *Post) GemtextPage() string { return fmt.Sprintf("# %s\n\n", p.Title) + p.GemtextContent.String + "\n\n" + p.Date } func (p *Post) HTMLPage() string { var content string if p.GemtextContent.Valid { content = p.GemtextContent.String } lines := strings.Split(content, "\n") var htmlLines []string htmlLines = append(htmlLines, "
"+line[1:]+"") } else if strings.HasPrefix(line, "=>") { parts := strings.Fields(line[2:]) if len(parts) > 1 { url := parts[0] text := strings.Join(parts[1:], " ") htmlLines = append(htmlLines, fmt.Sprintf(` ‣ %s`, url, text)) } else if len(parts) == 1 { url := parts[0] htmlLines = append(htmlLines, fmt.Sprintf(` ‣ %s`, url, url)) } } else { htmlLines = append(htmlLines, "
"+line+"
") } } // add link to editor for the post with id p.ID to the end of the page htmlLines = append(htmlLines, "•") htmlDocument := strings.Join(htmlLines, "\n") // replace all instances of ***sometext*** with sometext where sometext is any text using regex in one line htmlDocument = regexp.MustCompile(`\*\*\*(.*?)\*\*\*`).ReplaceAllString(htmlDocument, " $1 ") return htmlDocument } func GetPost(id int) (*Post, error) { db := GetDB() post := &Post{} err := db.Get(post, "SELECT id, post_title, date, gemtext_content, title_slug, year, month, day FROM posts WHERE id = ?", id) if err != nil { return nil, err } return post, nil } func UpdatePost(p *Post) error { db := GetDB() // sligify title and put it in p.TitleSlug p.TitleSlug = slug.Make(p.Title) _, err := db.Exec("UPDATE posts SET post_title = ?, gemtext_content = ?, title_slug = ?, year = ?, month = ?, day = ? WHERE id = ?", p.Title, p.GemtextContent, p.TitleSlug, p.Year, p.Month, p.Day, p.ID) if err != nil { return err } return nil } func CreateNewPost() (*Post, error) { db := GetDB() day := time.Now().Day() month := time.Now().Month() year := time.Now().Year() post, err := GetLastPostWithEmptyContent() if err != nil { result, err := db.Exec("INSERT INTO posts (post_title, date, gemtext_content, title_slug, year, month, day) VALUES (?, ?, ?, ?, ?, ?, ?)", "Novi Clanak", time.Now().String(), "", "new-post", year, month, day) if err != nil { return nil, err } postID, err := result.LastInsertId() // log postID if err != nil { return nil, err } post, err = GetPostByRowid(int(postID)) if err != nil { return nil, err } } return post, nil } func GetPostByRowid(i int) (*Post, error) { db := GetDB() post := &Post{} err := db.Get(post, "SELECT ID, post_title, ifnull(date, 'nodate') as date, gemtext_content, title_slug, year, month, day FROM posts WHERE rowid = ?", i) if err != nil { return post, err } print(post) return post, nil } func GetLastPostWithEmptyContent() (*Post, error) { db := GetDB() post := &Post{} err := db.Get(post, "SELECT ID, post_title, date, gemtext_content, title_slug, year, month, day FROM posts WHERE gemtext_content = '' ORDER BY date DESC LIMIT 1") if err != nil { return nil, err } return post, nil }