|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// XML structures for sitemap |
| 12 | +type URLSet struct { |
| 13 | + XMLName xml.Name `xml:"urlset"` |
| 14 | + Xmlns string `xml:"xmlns,attr"` |
| 15 | + URLs []URL `xml:"url"` |
| 16 | +} |
| 17 | + |
| 18 | +type URL struct { |
| 19 | + Loc string `xml:"loc"` |
| 20 | + LastMod string `xml:"lastmod,omitempty"` |
| 21 | + ChangeFreq string `xml:"changefreq,omitempty"` |
| 22 | + Priority string `xml:"priority,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +func main() { |
| 26 | + if len(os.Args) < 3 { |
| 27 | + fmt.Fprintf(os.Stderr, "Usage: %s <book-dir> <base-url>\n", os.Args[0]) |
| 28 | + os.Exit(1) |
| 29 | + } |
| 30 | + |
| 31 | + bookDir := os.Args[1] |
| 32 | + baseURL := strings.TrimSuffix(os.Args[2], "/") |
| 33 | + |
| 34 | + urlset := URLSet{ |
| 35 | + Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", |
| 36 | + URLs: []URL{}, |
| 37 | + } |
| 38 | + |
| 39 | + // Walk through the book directory to find HTML files |
| 40 | + err := filepath.Walk(bookDir, func(path string, info os.FileInfo, err error) error { |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + // Skip non-HTML files |
| 46 | + if !strings.HasSuffix(path, ".html") { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + // Skip print.html if it exists |
| 51 | + if strings.HasSuffix(path, "print.html") { |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + // Get relative path from book directory |
| 56 | + relPath, err := filepath.Rel(bookDir, path) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + // Convert Windows paths to URL paths |
| 62 | + urlPath := filepath.ToSlash(relPath) |
| 63 | + |
| 64 | + // Create full URL |
| 65 | + fullURL := baseURL + "/" + urlPath |
| 66 | + |
| 67 | + // Get file modification time |
| 68 | + lastMod := info.ModTime().Format("2006-01-02T15:04:05-07:00") |
| 69 | + |
| 70 | + // Determine priority and change frequency |
| 71 | + priority := "0.5" |
| 72 | + changeFreq := "weekly" |
| 73 | + |
| 74 | + // Higher priority for important pages |
| 75 | + if urlPath == "index.html" { |
| 76 | + priority = "1.0" |
| 77 | + changeFreq = "daily" |
| 78 | + } else if strings.Contains(urlPath, "quick-start") || |
| 79 | + strings.Contains(urlPath, "getting-started") || |
| 80 | + strings.Contains(urlPath, "introduction") { |
| 81 | + priority = "0.8" |
| 82 | + changeFreq = "weekly" |
| 83 | + } else if strings.Contains(urlPath, "tutorial") { |
| 84 | + priority = "0.7" |
| 85 | + } |
| 86 | + |
| 87 | + // Add URL to sitemap |
| 88 | + urlset.URLs = append(urlset.URLs, URL{ |
| 89 | + Loc: fullURL, |
| 90 | + LastMod: lastMod, |
| 91 | + ChangeFreq: changeFreq, |
| 92 | + Priority: priority, |
| 93 | + }) |
| 94 | + |
| 95 | + return nil |
| 96 | + }) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + fmt.Fprintf(os.Stderr, "Error walking directory: %v\n", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + |
| 103 | + // Generate XML |
| 104 | + output, err := xml.MarshalIndent(urlset, "", " ") |
| 105 | + if err != nil { |
| 106 | + fmt.Fprintf(os.Stderr, "Error generating XML: %v\n", err) |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + |
| 110 | + // Write XML header and content |
| 111 | + fmt.Printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n%s\n", output) |
| 112 | +} |
0 commit comments