|
| 1 | +package ssg |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io/fs" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/yuin/goldmark" |
| 12 | + meta "github.com/yuin/goldmark-meta" |
| 13 | + "github.com/yuin/goldmark/extension" |
| 14 | + "github.com/yuin/goldmark/parser" |
| 15 | +) |
| 16 | + |
| 17 | +type Engine struct { |
| 18 | + docFiles []File |
| 19 | + layoutTemplate func(Page, string) string |
| 20 | + outputDir string |
| 21 | + baseDir string |
| 22 | + markdown goldmark.Markdown |
| 23 | +} |
| 24 | + |
| 25 | +type LayoutTemplate func(Page, string) string |
| 26 | + |
| 27 | +func NewEngine(docBaseDir string, layoutTemplate LayoutTemplate, outputDir string) *Engine { |
| 28 | + eng := Engine{ layoutTemplate: layoutTemplate, outputDir: outputDir, baseDir: docBaseDir } |
| 29 | + eng.markdown = goldmark.New( |
| 30 | + goldmark.WithExtensions( |
| 31 | + extension.GFM, |
| 32 | + meta.Meta, |
| 33 | + ), |
| 34 | + goldmark.WithParserOptions(parser.WithAutoHeadingID()), |
| 35 | + ) |
| 36 | + eng.loadDocs(docBaseDir) |
| 37 | + |
| 38 | + return &eng |
| 39 | +} |
| 40 | + |
| 41 | +func (e *Engine) Generate() { |
| 42 | + for i := range e.docFiles { |
| 43 | + log.Println(e.docFiles[i].osFile.Name()) |
| 44 | + var buff bytes.Buffer |
| 45 | + contents, err := os.ReadFile(e.docFiles[i].osFile.Name()) |
| 46 | + if err != nil { |
| 47 | + log.Printf("Could not read file %s: %v", e.docFiles[i].osFile.Name(), err) |
| 48 | + continue |
| 49 | + } |
| 50 | + context := parser.NewContext() |
| 51 | + if err = e.markdown.Convert(contents, &buff, parser.WithContext(context)); err != nil { |
| 52 | + log.Printf("Could not convert markdown file %s: %v", e.docFiles[i].osFile.Name(), err) |
| 53 | + continue |
| 54 | + } |
| 55 | + filePath := createHtmlPath(e.docFiles[i].osFile.Name(), e.baseDir, e.outputDir) |
| 56 | + metadata := meta.Get(context) |
| 57 | + metadata["_filepath"] = filePath |
| 58 | + e.docFiles[i].metadata = NewMetadata(metadata) |
| 59 | + e.docFiles[i].source = string(contents) |
| 60 | + e.docFiles[i].html = buff.String() |
| 61 | + } |
| 62 | + os.RemoveAll(e.outputDir) |
| 63 | + e.writeFiles() |
| 64 | +} |
| 65 | + |
| 66 | +func (e *Engine) ensureDirectoryExists(path string) { |
| 67 | + _, err := os.Stat(path) |
| 68 | + if err != nil { |
| 69 | + err := os.MkdirAll(path, 0777) |
| 70 | + if err != nil { |
| 71 | + log.Fatalf("Error creating views directory: %v", err) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +func (e *Engine) writeFiles() { |
| 78 | + pages := make([]Metadata, 0, len(e.docFiles)) |
| 79 | + for i := range e.docFiles { |
| 80 | + pages = append(pages, e.docFiles[i].metadata) |
| 81 | + } |
| 82 | + for i := range e.docFiles { |
| 83 | + htmlFilePath := strings.TrimSuffix(strings.Replace(e.docFiles[i].osFile.Name(), e.baseDir, e.outputDir, 1), ".md") + ".html" |
| 84 | + dirPath := filepath.Dir(htmlFilePath) |
| 85 | + e.ensureDirectoryExists(dirPath) |
| 86 | + |
| 87 | + page := NewFilePage(e.docFiles[i], pages) |
| 88 | + content := e.layoutTemplate(&page, e.docFiles[i].html) |
| 89 | + err := os.WriteFile(htmlFilePath, []byte(content), 0644) |
| 90 | + if err != nil { |
| 91 | + log.Fatalf("Error writing file %s: %v", htmlFilePath, err) |
| 92 | + } |
| 93 | + log.Printf("%s -> %s", e.docFiles[i].osFile.Name(), htmlFilePath) |
| 94 | + |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (e *Engine) loadDocs(baseDir string) error { |
| 99 | + filenames := getMarkdownFiles(baseDir) |
| 100 | + |
| 101 | + files := make([]File, 0, len(filenames)) |
| 102 | + |
| 103 | + for _, filename := range filenames { |
| 104 | + file, err := os.OpenFile(filename, os.O_RDONLY, 0755) |
| 105 | + if err != nil { |
| 106 | + log.Printf("Could not open \"%s\": %v", filename, err) |
| 107 | + return err |
| 108 | + } |
| 109 | + if file == nil { |
| 110 | + continue |
| 111 | + } |
| 112 | + files = append(files, File{osFile: file}) |
| 113 | + } |
| 114 | + |
| 115 | + e.docFiles = files |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func createHtmlPath(filePath string, baseDir string, destinationDir string) string { |
| 121 | + return strings.TrimSuffix(strings.Replace(filePath, baseDir, destinationDir, 1), ".md") + ".html" |
| 122 | +} |
| 123 | + |
| 124 | +func getMarkdownFiles(baseDir string) []string { |
| 125 | + filenames := make([]string, 0, 10) |
| 126 | + |
| 127 | + var rootProcessed bool |
| 128 | + filepath.WalkDir(baseDir, func(path string, d fs.DirEntry, err error) error { |
| 129 | + if path == baseDir && !rootProcessed { |
| 130 | + rootProcessed = true |
| 131 | + return nil |
| 132 | + } |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + if d.IsDir() { |
| 137 | + filenames = append(filenames, getMarkdownFiles(filepath.Join(baseDir, d.Name()))...) |
| 138 | + return nil |
| 139 | + } |
| 140 | + if filepath.Ext(d.Name()) == ".md" { |
| 141 | + filenames = append(filenames, path) |
| 142 | + return nil |
| 143 | + } |
| 144 | + return nil |
| 145 | + |
| 146 | + }) |
| 147 | + |
| 148 | + return filenames |
| 149 | +} |
0 commit comments