144 lines
4.1 KiB
Go
144 lines
4.1 KiB
Go
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, "<h1>"+p.Title+"</h1>")
|
|
|
|
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>")
|
|
}
|
|
}
|
|
// add link to editor for the post with id p.ID to the end of the page
|
|
htmlLines = append(htmlLines, "<a class=\"hidden_link\" href=\"/editor/e/"+fmt.Sprintf("%d", p.ID)+"\">•</a>")
|
|
|
|
htmlDocument := strings.Join(htmlLines, "\n")
|
|
|
|
// replace all instances of ***sometext*** with <strong>sometext</strong> where sometext is any text using regex in one line
|
|
htmlDocument = regexp.MustCompile(`\*\*\*(.*?)\*\*\*`).ReplaceAllString(htmlDocument, "<strong> $1 </strong>")
|
|
|
|
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
|
|
}
|