-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
226 lines (178 loc) · 5.26 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"flag"
"github.com/fogleman/gg"
// Fix Google Font issue
// https://www.reddit.com/r/golang/comments/14ldjiu/comment/jq1du7t/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
"github.com/goki/freetype"
"github.com/goki/freetype/truetype"
)
const (
FontFile = "NotoSansTC-Black.ttf"
OutputDir = "static/images"
OgImageWidth = 1200
OgImageHeight = 600
OgTitleFontSize = 48
OgSubTitleFontSize = 32
OgPaddingLeft = 72.0
OgPaddingTop = 90.0
OgBottomLineY = 576
SubTitle = "Bear Su's blog"
IconImagePath = "icon.png"
)
// Load font with goki freetype
func loadFont(path string) *truetype.Font {
var fontBytes []byte
fontBytes, err := os.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not load font file: %s", path)
os.Exit(1)
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not parse font file: %s", path)
os.Exit(1)
}
return font
}
func loadPostContent(postFilePath string) string {
postFile, err := os.ReadFile(postFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not read input file: %s", postFilePath)
os.Exit(1)
}
return string(postFile)
}
func extractTitle(postContent string) string {
re := regexp.MustCompile(`\ntitle:(.+)\n`)
matches := re.FindStringSubmatch(string(postContent))
if len(matches) < 1 {
fmt.Fprintf(os.Stderr, "Can not find post title")
os.Exit(1)
}
return strings.Trim(matches[1], " \"")
}
func extractDate(postContent string) string {
re := regexp.MustCompile(`\ndate: (\d{4}-\d{2}-\d{2})T`)
matches := re.FindStringSubmatch(string(postContent))
if len(matches) < 1 {
fmt.Fprintf(os.Stderr, "Can not find post date")
os.Exit(1)
}
return matches[1]
}
func imageOutputPath(outputDir string, postFilePath string) string {
re := regexp.MustCompile(`posts(/\d{4}-\d{2}-\d{2})-(.+).md`)
matches := re.FindStringSubmatch(postFilePath)
if len(matches) < 2 {
fmt.Fprintf(os.Stderr, "Can not parse file name: %s", postFilePath)
os.Exit(1)
}
dir := outputDir + matches[1]
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.Mkdir(dir, os.ModePerm); err != nil {
fmt.Fprintf(os.Stderr, "Can not create output directory: %s", dir)
os.Exit(1)
}
}
return dir + "/" + matches[2] + ".png"
}
func executableDir() string {
executablePath, err := os.Executable()
if err != nil {
return ""
}
return filepath.Dir(executablePath)
}
type OGRenRequest struct {
InputPath string
IconImagePath string
OutputDir string
}
func drawTitle(dc *gg.Context, postTitle string, font *truetype.Font, x float64, y float64) {
titleFace := truetype.NewFace(font, &truetype.Options{Size: OgTitleFontSize})
dc.SetFontFace(titleFace)
dc.SetRGB255(0, 0, 0)
dc.DrawStringWrapped(postTitle, x, y, 0, 0, 1100, 1, gg.AlignLeft)
}
func drawIcon(dc *gg.Context, iconImagePath string, y float64) {
iconImage, err := gg.LoadImage(iconImagePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not load icon image: %s", iconImage)
os.Exit(1)
} else {
w := iconImage.Bounds().Size().X
iconImageAxisX := OgImageWidth - OgPaddingLeft - w
dc.DrawImage(iconImage, int(iconImageAxisX), int(y))
}
}
func drawSubTitle(dc *gg.Context, postTitle string, font *truetype.Font, x float64, y float64) {
subTitleFace := truetype.NewFace(font, &truetype.Options{Size: OgSubTitleFontSize})
dc.SetFontFace(subTitleFace)
dc.SetRGB255(150, 150, 150)
dc.DrawStringWrapped(SubTitle, x, y, 0, 0, 1100, 1, gg.AlignLeft)
}
func (og OGRenRequest) Do() string {
// Load Post Content
postContent := loadPostContent(og.InputPath)
// Get Title
postTitle := extractTitle(postContent)
// Get Date
postDate := extractDate(postContent)
// Create context
dc := gg.NewContext(OgImageWidth, OgImageHeight)
dc.SetRGB(1, 1, 1)
dc.Clear()
// Load Font
font := loadFont(executableDir() + "/" + FontFile)
axisX := OgPaddingLeft
axisY := OgPaddingTop
// Draw Title
drawTitle(dc, postTitle, font, axisX, axisY)
axisY += OgTitleFontSize * 4
// Draw Icon
drawIcon(dc, og.IconImagePath, axisY)
// Draw SubTitle
drawSubTitle(dc, SubTitle, font, axisX, axisY)
axisY += OgTitleFontSize
// Draw Date
dc.DrawStringWrapped(postDate, axisX, axisY, 0, 0, 1100, 1, gg.AlignLeft)
// Draw Bottom Line
dc.DrawRectangle(0, OgBottomLineY, OgImageWidth, OgImageHeight-OgBottomLineY)
dc.SetRGB255(227, 76, 37)
dc.Fill()
// Save
ogImage := imageOutputPath(og.OutputDir, og.InputPath)
dc.SavePNG(ogImage)
return ogImage
}
func main() {
var dir string
var webdir string
flag.StringVar(&dir, "dir", "./", "The hugo project root path")
flag.StringVar(&webdir, "webdir", "static", "The hugo website root path")
flag.Parse()
// Read file
nonFlagArgs := flag.Args()
if len(nonFlagArgs) == 0 {
fmt.Fprint(os.Stderr, "Error: Miss input file path")
os.Exit(1)
}
postFilePath := nonFlagArgs[0]
req := OGRenRequest{
InputPath: postFilePath,
IconImagePath: path.Join(dir, IconImagePath),
OutputDir: path.Join(dir, OutputDir),
}
ogImagePath := req.Do()
pattern := regexp.MustCompile(`^.*/static(.*)$`)
// 使用正規表達式替換
resultString := pattern.ReplaceAllString(ogImagePath, "$1")
fmt.Println(resultString)
}