-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown_extractor.go
More file actions
94 lines (80 loc) · 2.5 KB
/
markdown_extractor.go
File metadata and controls
94 lines (80 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package devflow
import (
"fmt"
"path/filepath"
"strings"
)
// Extract extracts code blocks from the configured input and writes to outputFile
// The output file extension determines which code type to extract (.go, .js, .css)
func (m *MarkDown) Extract(outputFile string) error {
if m.destination == "" {
return fmt.Errorf("destination not set; provide destination when calling NewMarkDown(rootDir, destination)")
}
// Read markdown from the configured input
markdown, err := m.readFile(m.inputPath)
if err != nil {
return fmt.Errorf("reading file %s: %v", m.inputPath, err)
}
// Determine code type from output file extension
codeType := m.getCodeType(outputFile)
if codeType == "" {
return fmt.Errorf("unsupported file extension: %s", filepath.Ext(outputFile))
}
// Extract code blocks
code := m.extractCodeBlocks(string(markdown), codeType)
if code == "" {
return fmt.Errorf("no %s code blocks found in markdown", codeType)
}
// Write to output file
outputPath := filepath.Join(m.destination, outputFile)
if err := m.writeIfDifferent(outputPath, code); err != nil {
return fmt.Errorf("writing output file: %v", err)
}
m.log("Extracted", codeType, "code to", outputPath)
return nil
}
// getCodeType determines the code type from file extension
func (m *MarkDown) getCodeType(outputFile string) string {
ext := filepath.Ext(outputFile)
switch ext {
case ".go":
return "go"
case ".js":
return "javascript"
case ".css":
return "css"
default:
return ""
}
}
// extractCodeBlocks extracts code blocks of a specific type from markdown content
func (m *MarkDown) extractCodeBlocks(markdown, codeType string) string {
var result strings.Builder
lines := strings.Split(markdown, "\n")
inBlock := false
// Format of code block start: ```codeType or ``` codeType
codeBlockStart := "```" + codeType
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "```") {
// Check if it's start or end
if !inBlock {
// Check if it's the start of our code type
if strings.HasPrefix(trimmed, codeBlockStart) {
inBlock = true
}
} else {
// It's the end of a block
// But we need to be careful if it's nested (unlikely in this simple parser)
// Assuming standard markdown where blocks are not nested
if trimmed == "```" {
inBlock = false
result.WriteString("\n") // Add separation between blocks
}
}
} else if inBlock {
result.WriteString(line + "\n")
}
}
return strings.TrimSpace(result.String())
}