2023-07-30 19:21:16 +02:00
package database
import (
"database/sql"
"fmt"
2023-11-27 19:44:23 +01:00
"github.com/gosimple/slug"
"regexp"
2023-09-16 16:59:16 +02:00
"strings"
2023-09-09 05:43:31 +02:00
"time"
2023-07-30 19:21:16 +02:00
)
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 {
2023-08-13 19:48:19 +02:00
return fmt . Sprintf ( "# %s\n\n" , p . Title ) + p . GemtextContent . String + "\n\n" + p . Date
2023-07-30 19:21:16 +02:00
}
2023-08-05 10:58:20 +02:00
func ( p * Post ) HTMLPage ( ) string {
2023-09-16 16:59:16 +02:00
var content string
if p . GemtextContent . Valid {
content = p . GemtextContent . String
}
lines := strings . Split ( content , "\n" )
var htmlLines [ ] string
2023-11-27 19:44:23 +01:00
htmlLines = append ( htmlLines , "<h1>" + p . Title + "</h1>" )
2023-09-16 16:59:16 +02:00
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 : ] , " " )
2023-11-27 19:44:23 +01:00
htmlLines = append ( htmlLines , fmt . Sprintf ( ` ‣ <a href="%s.html">%s</a> ` , url , text ) )
2023-09-16 16:59:16 +02:00
} else if len ( parts ) == 1 {
url := parts [ 0 ]
2023-11-27 19:44:23 +01:00
htmlLines = append ( htmlLines , fmt . Sprintf ( ` ‣ <a href="%s.html">%s</a> ` , url , url ) )
2023-09-16 16:59:16 +02:00
}
2023-11-20 21:58:11 +01:00
2023-09-16 16:59:16 +02:00
} else {
htmlLines = append ( htmlLines , "<p>" + line + "</p>" )
}
}
2023-11-27 19:44:23 +01:00
// 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>" )
2023-09-16 16:59:16 +02:00
2023-11-27 19:44:23 +01:00
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
2023-08-05 10:58:20 +02:00
}
2023-07-30 19:21:16 +02:00
func GetPost ( id int ) ( * Post , error ) {
db := GetDB ( )
post := & Post { }
2023-11-27 19:44:23 +01:00
err := db . Get ( post , "SELECT id, post_title, date, gemtext_content, title_slug, year, month, day FROM posts WHERE id = ?" , id )
2023-07-30 19:21:16 +02:00
if err != nil {
return nil , err
}
return post , nil
}
2023-09-09 05:43:31 +02:00
func UpdatePost ( p * Post ) error {
db := GetDB ( )
2023-11-27 19:44:23 +01:00
// 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 )
2023-09-09 05:43:31 +02:00
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 ( )
2023-11-27 19:44:23 +01:00
post , err := GetLastPostWithEmptyContent ( )
2023-09-09 05:43:31 +02:00
if err != nil {
2023-11-27 19:44:23 +01:00
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
}
2023-09-09 05:43:31 +02:00
}
2023-11-27 19:44:23 +01:00
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 )
2023-09-09 05:43:31 +02:00
if err != nil {
2023-11-27 19:44:23 +01:00
return post , err
2023-09-09 05:43:31 +02:00
}
2023-11-27 19:44:23 +01:00
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" )
2023-09-09 05:43:31 +02:00
if err != nil {
return nil , err
}
return post , nil
}