|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "html/template" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/yuin/goldmark" |
| 13 | + "github.com/yuin/goldmark/ast" |
| 14 | + "github.com/yuin/goldmark/extension" |
| 15 | + extAst "github.com/yuin/goldmark/extension/ast" |
| 16 | + "github.com/yuin/goldmark/parser" |
| 17 | + "github.com/yuin/goldmark/renderer" |
| 18 | + "github.com/yuin/goldmark/renderer/html" |
| 19 | + "github.com/yuin/goldmark/text" |
| 20 | + "github.com/yuin/goldmark/util" |
| 21 | + "golang.org/x/text/cases" |
| 22 | + "golang.org/x/text/language" |
| 23 | +) |
| 24 | + |
| 25 | +//go:embed template.html |
| 26 | +var templateHTML string |
| 27 | + |
| 28 | +type pageData struct { |
| 29 | + Title string |
| 30 | + Content template.HTML |
| 31 | + CurrentPage string |
| 32 | +} |
| 33 | + |
| 34 | +var linkRewriter = regexp.MustCompile(`\[([^\]]*)\]\(([^)]+)\.md(#[^)]*)?\)`) |
| 35 | + |
| 36 | +// cssClassTransformer adds Bootstrap CSS classes to specific HTML elements |
| 37 | +type cssClassTransformer struct{} |
| 38 | + |
| 39 | +func (t *cssClassTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) { |
| 40 | + _ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) { |
| 41 | + if !entering { |
| 42 | + return ast.WalkContinue, nil |
| 43 | + } |
| 44 | + |
| 45 | + switch n.Kind() { |
| 46 | + case extAst.KindTable: |
| 47 | + // Add Bootstrap table classes to tables |
| 48 | + n.SetAttributeString("class", []byte("table table-striped border")) |
| 49 | + } |
| 50 | + |
| 51 | + return ast.WalkContinue, nil |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +// customCodeBlockRenderer renders code blocks with Bootstrap classes |
| 56 | +type customCodeBlockRenderer struct{ html.Config } |
| 57 | + |
| 58 | +func (r *customCodeBlockRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { |
| 59 | + reg.Register(ast.KindFencedCodeBlock, r.renderCodeBlock) |
| 60 | + reg.Register(ast.KindCodeBlock, r.renderCodeBlock) |
| 61 | +} |
| 62 | + |
| 63 | +func (r *customCodeBlockRenderer) renderCodeBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { |
| 64 | + if entering { |
| 65 | + _, _ = w.WriteString(`<pre class="bg-light border rounded p-3 overflow-x-auto mw-100">`) |
| 66 | + if n, ok := node.(*ast.FencedCodeBlock); ok { |
| 67 | + language := n.Language(source) |
| 68 | + if len(language) > 0 { |
| 69 | + _, _ = w.WriteString(`<code class="language-`) |
| 70 | + _, _ = w.Write(language) |
| 71 | + _, _ = w.WriteString(`">`) |
| 72 | + } else { |
| 73 | + _, _ = w.WriteString("<code>") |
| 74 | + } |
| 75 | + } else { |
| 76 | + _, _ = w.WriteString("<code>") |
| 77 | + } |
| 78 | + |
| 79 | + // Write the code content |
| 80 | + l := node.Lines().Len() |
| 81 | + for i := 0; i < l; i++ { |
| 82 | + line := node.Lines().At(i) |
| 83 | + _, _ = w.Write(util.EscapeHTML(line.Value(source))) |
| 84 | + } |
| 85 | + } else { |
| 86 | + _, _ = w.WriteString("</code></pre>\n") |
| 87 | + } |
| 88 | + return ast.WalkContinue, nil |
| 89 | +} |
| 90 | + |
| 91 | +type customBlockquoteRenderer struct{ html.Config } |
| 92 | + |
| 93 | +func (r *customBlockquoteRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { |
| 94 | + reg.Register(ast.KindBlockquote, r.renderBlockquote) |
| 95 | +} |
| 96 | + |
| 97 | +func (r *customBlockquoteRenderer) renderBlockquote(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { |
| 98 | + if entering { |
| 99 | + _, _ = w.WriteString(`<blockquote class="border-start border-3 ms-3 ps-3 fst-italic">`) |
| 100 | + } else { |
| 101 | + _, _ = w.WriteString("</blockquote>") |
| 102 | + } |
| 103 | + return ast.WalkContinue, nil |
| 104 | +} |
| 105 | + |
| 106 | +func generateDocs() error { |
| 107 | + // Create goldmark converter |
| 108 | + md := goldmark.New( |
| 109 | + goldmark.WithExtensions( |
| 110 | + extension.GFM, |
| 111 | + extension.Table, |
| 112 | + extension.Strikethrough, |
| 113 | + extension.TaskList, |
| 114 | + ), |
| 115 | + goldmark.WithParserOptions( |
| 116 | + parser.WithAutoHeadingID(), |
| 117 | + parser.WithASTTransformers( |
| 118 | + util.Prioritized(&cssClassTransformer{}, 100), |
| 119 | + ), |
| 120 | + ), |
| 121 | + goldmark.WithRendererOptions( |
| 122 | + renderer.WithNodeRenderers( |
| 123 | + util.Prioritized(&customCodeBlockRenderer{}, 100), |
| 124 | + util.Prioritized(&customBlockquoteRenderer{}, 100), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + // Parse template |
| 130 | + tmpl, err := template.New("doc").Parse(templateHTML) |
| 131 | + if err != nil { |
| 132 | + return fmt.Errorf("failed to parse template: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + // Find all markdown files in docs/ |
| 136 | + docsDir := "docs" |
| 137 | + entries, err := os.ReadDir(docsDir) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("failed to read docs directory: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + generatedCount := 0 |
| 143 | + for _, entry := range entries { |
| 144 | + if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".md") { |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + inputPath := filepath.Join(docsDir, entry.Name()) |
| 149 | + outputPath := filepath.Join(docsDir, strings.TrimSuffix(entry.Name(), ".md")+".html") |
| 150 | + |
| 151 | + if err := convertMarkdownToHTML(md, tmpl, inputPath, outputPath); err != nil { |
| 152 | + return fmt.Errorf("failed to convert %s: %w", entry.Name(), err) |
| 153 | + } |
| 154 | + |
| 155 | + generatedCount++ |
| 156 | + } |
| 157 | + |
| 158 | + if generatedCount == 0 { |
| 159 | + return fmt.Errorf("no markdown files found in %s", docsDir) |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func convertMarkdownToHTML(md goldmark.Markdown, tmpl *template.Template, inputPath, outputPath string) error { |
| 166 | + // Read markdown file |
| 167 | + content, err := os.ReadFile(inputPath) |
| 168 | + if err != nil { |
| 169 | + return fmt.Errorf("failed to read file: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + // Rewrite .md links to .html |
| 173 | + contentStr := string(content) |
| 174 | + contentStr = linkRewriter.ReplaceAllString(contentStr, "[$1]($2.html$3)") |
| 175 | + |
| 176 | + // Convert markdown to HTML |
| 177 | + var buf strings.Builder |
| 178 | + if err := md.Convert([]byte(contentStr), &buf); err != nil { |
| 179 | + return fmt.Errorf("failed to convert markdown: %w", err) |
| 180 | + } |
| 181 | + |
| 182 | + // Extract title from first H1 or use filename |
| 183 | + title := extractTitle(contentStr) |
| 184 | + if title == "" { |
| 185 | + title = strings.TrimSuffix(filepath.Base(inputPath), ".md") |
| 186 | + title = strings.ReplaceAll(title, "-", " ") |
| 187 | + title = cases.Title(language.English).String(title) |
| 188 | + } |
| 189 | + |
| 190 | + // Determine current page name (for sidebar highlighting) |
| 191 | + currentPage := strings.TrimSuffix(filepath.Base(inputPath), ".md") |
| 192 | + |
| 193 | + // Prepare template data |
| 194 | + data := pageData{ |
| 195 | + Title: title, |
| 196 | + Content: template.HTML(buf.String()), |
| 197 | + CurrentPage: currentPage, |
| 198 | + } |
| 199 | + |
| 200 | + // Create output file |
| 201 | + outFile, err := os.Create(outputPath) |
| 202 | + if err != nil { |
| 203 | + return fmt.Errorf("failed to create output file: %w", err) |
| 204 | + } |
| 205 | + defer func() { _ = outFile.Close() }() |
| 206 | + |
| 207 | + // Execute template |
| 208 | + if err := tmpl.Execute(outFile, data); err != nil { |
| 209 | + return fmt.Errorf("failed to execute template: %w", err) |
| 210 | + } |
| 211 | + |
| 212 | + return nil |
| 213 | +} |
| 214 | + |
| 215 | +func extractTitle(content string) string { |
| 216 | + // Look for first H1 heading (# Title) |
| 217 | + lines := strings.Split(content, "\n") |
| 218 | + for _, line := range lines { |
| 219 | + line = strings.TrimSpace(line) |
| 220 | + if strings.HasPrefix(line, "# ") { |
| 221 | + return strings.TrimSpace(strings.TrimPrefix(line, "# ")) |
| 222 | + } |
| 223 | + } |
| 224 | + return "" |
| 225 | +} |
0 commit comments