// Should be compiled to binary to be used as CGI script package main import ( "fmt" "html" "io/fs" "net/url" "os" "path/filepath" "regexp" "strings" ) func extractTitle(content string) string { titleRegex := regexp.MustCompile(`(?i)
Error: no tag specified
") return } postsDir := "../posts" tagRegex := regexp.MustCompile(`data-tag="` + regexp.QuoteMeta(tag) + `"`) type Post struct { Path string Title string } var matches []Post filepath.WalkDir(postsDir, func(path string, d fs.DirEntry, err error) error { if err != nil { return nil } if !d.IsDir() { contentBytes, err := os.ReadFile(path) if err == nil && tagRegex.Match(contentBytes) { content := string(contentBytes) title := extractTitle(content) relPath := "/posts/" + filepath.Base(path) matches = append(matches, Post{Path: relPath, Title: title}) } } return nil }) // СоставляемError: template not found
") return } template := string(templateBytes) // Заменяем маркеры output := strings.Replace(template, "", b.String(), 1) output = strings.Replace(output, "", html.EscapeString(tag), 1) // Отдаём результат fmt.Print(output) }