aboutsummaryrefslogtreecommitdiff
path: root/cmd/http/load.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/http/load.go')
-rw-r--r--cmd/http/load.go73
1 files changed, 35 insertions, 38 deletions
diff --git a/cmd/http/load.go b/cmd/http/load.go
index d63d7ce..6ab2eba 100644
--- a/cmd/http/load.go
+++ b/cmd/http/load.go
@@ -1,24 +1,23 @@
package main
import (
+ "errors"
"os"
- "regexp"
"sort"
"strings"
)
// Post struct contains necessary data for a post
type Post struct {
- FileName string
- Title string
- Date string
- Tags []string
- Image string
+ File string
+ Title string
+ Date string
+ Tags []string
}
// Posts stuct contains a collection of type Post
type Posts struct {
- Contents []*Post
+ Collection []*Post
}
// Read all found files and load them into a stuct
@@ -33,10 +32,7 @@ func (app *application) aggregate(location string) (p *Posts, err error) {
// Loop over every file in the directory and read the contents.
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".tmpl.html") {
- newPost, err := app.readFile(location + "/" + file.Name())
- if err != nil {
- return nil, err
- }
+ newPost := app.parseFileName(strings.TrimSuffix(file.Name(), ".tmpl.html"))
posts = append(posts, newPost)
}
@@ -46,35 +42,32 @@ func (app *application) aggregate(location string) (p *Posts, err error) {
return posts[i].Date > posts[j].Date
})
- return &Posts{Contents: posts}, nil
+ return &Posts{Collection: posts}, nil
}
-func (app *application) readFile(location string) (p *Post, err error) {
- fileContent, err := os.ReadFile(location)
- if err != nil {
- return nil, err
- }
-
+func (app *application) parseFileName(file string) (p *Post) {
var post *Post = new(Post)
- fileName := strings.TrimSuffix(strings.Split(location, "/")[2], ".tmpl.html")
-
- // title
- title := strings.ReplaceAll(fileName, "_", " ")
+ svList := strings.Split(file, "+")
+ usvLength := len(svList)
// date
- datePattern := regexp.MustCompile(`{{define "uploaded-on"}}(\d{4}-\d{2}-\d{2}){{end}}`)
+ /*datePattern := regexp.MustCompile(`{{define "uploaded-on"}}(\d{4}-\d{2}-\d{2}){{end}}`)
dateMatching := datePattern.FindStringSubmatch(string(fileContent))
- var date string
+ //var date string
if len(dateMatching) > 1 {
date = dateMatching[1]
} else {
date = ""
- }
+ }*/
+ date := svList[0]
+
+ // title
+ title := strings.ReplaceAll(svList[1], "_", " ")
// tags
- tagsPattern := regexp.MustCompile(`{{define "keywords"}}([\w\s]+){{end}}`)
+ /*tagsPattern := regexp.MustCompile(`{{define "keywords"}}([\w\s]+){{end}}`)
matchingTags := tagsPattern.FindStringSubmatch(string(fileContent))
var tags []string
@@ -82,25 +75,29 @@ func (app *application) readFile(location string) (p *Post, err error) {
tags = strings.Fields(matchingTags[1])
} else {
tags = []string{}
- }
-
- // thumbnail image
- imagePattern := regexp.MustCompile(`<img src="(.+)" class="mainImage"( alt="(.+)")* />`)
- matchingImage := imagePattern.FindStringSubmatch(string(fileContent))
+ }*/
- var image string
- if len(matchingImage) > 1 {
- image = matchingImage[0]
+ var tags []string
+ if usvLength > 2 {
+ for i := 2; i < usvLength; i++ {
+ tags = append(tags, svList[i])
+ }
} else {
- image = ""
+ tags = []string{}
}
- post.FileName = fileName
+ post.File = file
post.Title = title
post.Date = date
post.Tags = tags
- post.Image = image
- return post, nil
+ return post
+
+}
+func (app *application) fileExists(file string) bool {
+ if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
+ return false
+ }
+ return true
}