111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Post struct {
|
|
ID int
|
|
Title string `db:"post_title"`
|
|
Content string
|
|
Date string
|
|
MarkdownContent string `db:"markdown_content"`
|
|
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
|
|
if strings.HasPrefix(content, "{") {
|
|
content = strings.TrimPrefix(content, "{")
|
|
}
|
|
if strings.HasSuffix(content, "true}") {
|
|
content = strings.TrimSuffix(content, "true}")
|
|
}
|
|
} else {
|
|
content = ""
|
|
}
|
|
|
|
lines := strings.Split(content, "\n")
|
|
var htmlLines []string
|
|
|
|
for _, line := range lines {
|
|
if strings.HasPrefix(line, "#") {
|
|
htmlLines = append(htmlLines, "<h1>"+line[1:]+"</h1>")
|
|
} else if strings.HasPrefix(line, "##") {
|
|
htmlLines = append(htmlLines, "<h2>"+line[2:]+"</h2>")
|
|
} else if strings.HasPrefix(line, "###") {
|
|
htmlLines = append(htmlLines, "<h3>"+line[3:]+"</h3>")
|
|
} else if strings.HasPrefix(line, "* ") {
|
|
htmlLines = append(htmlLines, "<ul><li>"+line[2:]+"</li></ul>")
|
|
} else if strings.HasPrefix(line, ">") {
|
|
htmlLines = append(htmlLines, "<blockquote>"+line[1:]+"</blockquote>")
|
|
} 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(`<a href="%s.html">%s</a>`, url, text))
|
|
} else if len(parts) == 1 {
|
|
url := parts[0]
|
|
htmlLines = append(htmlLines, fmt.Sprintf(`<a href="%s.html">%s</a>`, url, url))
|
|
}
|
|
} else {
|
|
htmlLines = append(htmlLines, "<p>"+line+"</p>")
|
|
}
|
|
}
|
|
|
|
return strings.Join(htmlLines, "\n")
|
|
}
|
|
|
|
func GetPost(id int) (*Post, error) {
|
|
db := GetDB()
|
|
post := &Post{}
|
|
err := db.Get(post, "SELECT id, post_title, date, markdown_content, gemtext_content, title_slug, year, month, day FROM posts WHERE id = ? and gemtext_content is not null", id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return post, nil
|
|
}
|
|
|
|
func UpdatePost(p *Post) error {
|
|
db := GetDB()
|
|
_, err := db.Exec("UPDATE posts SET post_title = ?, markdown_content = ?, gemtext_content = ?, title_slug = ?, year = ?, month = ?, day = ? WHERE id = ?", p.Title, p.MarkdownContent, 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()
|
|
|
|
result, err := db.Exec("INSERT INTO posts (post_title, markdown_content, gemtext_content, title_slug, year, month, day) VALUES (?, ?, ?, ?, ?, ?, ?)", "Novi Clanak", "", "", "new-post", year, month, day)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
postID, err := result.LastInsertId()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
post, err := GetPost(int(postID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return post, nil
|
|
}
|