-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (131 loc) · 3.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"path/filepath"
"image"
"image/jpeg"
"github.com/nfnt/resize"
)
func main() {
serveFlag := flag.Bool("serve", false, "Start the server")
flag.Parse()
if *serveFlag {
serve()
return
}
build()
serve()
}
func serve() {
fs := http.FileServer(http.Dir("docs"))
http.Handle("/", http.StripPrefix("/", fs))
log.Println("Server starting at :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Server failed to start:", err)
}
}
// Post represents the structure of each post in the JSON data.
type Post struct {
Title string `json:"title"`
Caption string `json:"caption"`
Image string `json:"image"`
}
// PostsData represents the structure of the JSON data.
type PostsData struct {
Posts []Post `json:"posts"`
}
func build() {
// Define paths
indexJSONPath := "source/index.json"
imagesPath := "source/images"
templatePath := "template/index.html"
outputHTMLPath := "docs/index.html"
imagesOutputDir := "docs/images"
// Read and parse the JSON data
var postsData PostsData
jsonFile, err := os.Open(indexJSONPath)
if err != nil {
fmt.Printf("Error opening JSON file: %v\n", err)
return
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
fmt.Printf("Error reading JSON file: %v\n", err)
return
}
err = json.Unmarshal(byteValue, &postsData)
if err != nil {
fmt.Printf("Error parsing JSON data: %v\n", err)
return
}
fmt.Printf("JSON data: %+v\n", postsData)
// Parse the template
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
fmt.Printf("Error parsing template: %v\n", err)
return
}
// Create the output HTML file
outputFile, err := os.Create(outputHTMLPath)
if err != nil {
fmt.Printf("Error creating output HTML file: %v\n", err)
return
}
defer outputFile.Close()
// Execute template with the data
err = tmpl.Execute(outputFile, postsData)
if err != nil {
fmt.Printf("Error executing template: %v\n", err)
return
}
// Create the images output directory if it doesn't exist
if _, err := os.Stat(imagesOutputDir); os.IsNotExist(err) {
os.MkdirAll(imagesOutputDir, os.ModePerm)
}
// Copy and resize images
for _, post := range postsData.Posts {
srcImagePath := filepath.Join(imagesPath, post.Image)
dstImagePath := filepath.Join(imagesOutputDir, filepath.Base(post.Image))
if _, err := os.Stat(dstImagePath); err == nil {
fmt.Printf("Image %s already exists, skipping...\n", post.Image)
continue
}
// Open the source image
srcImageFile, err := os.Open(srcImagePath)
if err != nil {
fmt.Printf("Error opening source image %s: %v\n", post.Image, err)
continue
}
defer srcImageFile.Close()
// Decode the image
img, _, err := image.Decode(srcImageFile)
if err != nil {
fmt.Printf("Error decoding image %s: %v\n", post.Image, err)
continue
}
// Resize the image (e.g., to 800x600 for web)
resizedImg := resize.Resize(1440, 0, img, resize.Lanczos3)
// Save the resized image
dstImageFile, err := os.Create(dstImagePath)
if err != nil {
fmt.Printf("Error creating destination image %s: %v\n", dstImagePath, err)
continue
}
defer dstImageFile.Close()
err = jpeg.Encode(dstImageFile, resizedImg, nil)
if err != nil {
fmt.Printf("Error saving resized image %s: %v\n", dstImagePath, err)
}
fmt.Printf("Resized image saved to %s\n", dstImagePath)
}
fmt.Println("HTML and images have been generated successfully.")
}