// 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)(.*?)`) matches := titleRegex.FindStringSubmatch(content) if len(matches) > 1 { return matches[1] } return "Без названия" } func main() { // CGI header fmt.Print("Content-Type: text/html; charset=utf-8\r\n\r\n") // Получаем query string из окружения rawQuery := os.Getenv("QUERY_STRING") values, _ := url.ParseQuery(rawQuery) tag := values.Get("tag") if tag == "" { fmt.Println("

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 }) // Составляем