This commit is contained in:
2023-11-27 19:44:23 +01:00
parent 477db1afa0
commit 091c333081
38 changed files with 48333 additions and 8334 deletions

View File

@@ -24,21 +24,21 @@ func (l *Link) GemtextArchiveMonthLink() string {
}
func (l *Link) HTMLLink() string {
return fmt.Sprintf("<a href=\"/p/%d/%s\">%d-%d-%d - %s</a><br>\n", l.ID, l.TitleSlug, l.Year, l.Month, l.Day, l.Title)
return fmt.Sprintf("&#8227; <a href=\"/p/%d/%s\">%d-%d-%d - %s</a><br>\n", l.ID, l.TitleSlug, l.Year, l.Month, l.Day, l.Title)
}
func (l *Link) HTMLArchiveLink() string {
return fmt.Sprintf("<a href=\"/y/%d/\">Godina %d.</a><br>\n", l.Year, l.Year)
return fmt.Sprintf(" &#8227; <a href=\"/y/%d/\">Godina %d.</a><br>\n", l.Year, l.Year)
}
func (l *Link) HTMLArchiveMonthLink() string {
return fmt.Sprintf("<a href=\"/y/%d/m/%d/\">%d-%d</a><br>\n", l.Year, l.Month, l.Year, l.Month)
return fmt.Sprintf(" &#8227; <a href=\"/y/%d/m/%d/\">%d-%d</a><br>\n", l.Year, l.Month, l.Year, l.Month)
}
func GetRecentLinks() ([]Link, error) {
db := GetDB()
links := []Link{}
err := db.Select(&links, "SELECT id, title_slug, year, month, day, post_title FROM posts ORDER BY date DESC LIMIT 10")
err := db.Select(&links, "SELECT id, title_slug, year, month, day, post_title FROM posts where not (gemtext_content is null or gemtext_content = '') ORDER BY date DESC LIMIT 10")
if err != nil {
return nil, err
}

View File

@@ -3,6 +3,8 @@ package database
import (
"database/sql"
"fmt"
"github.com/gosimple/slug"
"regexp"
"strings"
"time"
)
@@ -12,7 +14,6 @@ type Post struct {
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
@@ -31,6 +32,7 @@ func (p *Post) HTMLPage() string {
lines := strings.Split(content, "\n")
var htmlLines []string
htmlLines = append(htmlLines, "<h1>"+p.Title+"</h1>")
for _, line := range lines {
if strings.HasPrefix(line, "#") {
@@ -48,24 +50,31 @@ func (p *Post) HTMLPage() string {
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))
htmlLines = append(htmlLines, fmt.Sprintf(` &#8227; <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))
htmlLines = append(htmlLines, fmt.Sprintf(` &#8227; <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)+"\">&bull;</a>")
return strings.Join(htmlLines, "\n")
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, markdown_content, gemtext_content, title_slug, year, month, day FROM posts WHERE id = ? and gemtext_content is not null", id)
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
}
@@ -74,7 +83,10 @@ func GetPost(id int) (*Post, error) {
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)
// 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
}
@@ -87,15 +99,43 @@ func CreateNewPost() (*Post, error) {
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)
post, err := GetLastPostWithEmptyContent()
if err != nil {
return nil, err
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
}
}
postID, err := result.LastInsertId()
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 nil, err
return post, err
}
post, err := GetPost(int(postID))
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
}