-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (110 loc) · 3.57 KB
/
main.go
File metadata and controls
139 lines (110 loc) · 3.57 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// codemd-lite is a lightweight tool to convert between markdown files
// containing code blocks and individual source code files.
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
func main() {
cmd := CommandMdCodeLite()
if err := cmd.Execute(); err != nil {
cmd.PrintErr(err)
os.Exit(1)
}
}
func CommandMdCodeLite() *cobra.Command {
var cmd = &cobra.Command{
Use: "codemd",
Short: "A tool to convert between markdown and source files",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
}
cmd.AddCommand(CommandToCode())
cmd.AddCommand(CommandToMarkdown())
return cmd
}
func CommandToCode() *cobra.Command {
var mdFile string
var outputDir string
cmd := &cobra.Command{
Use: "tocode",
Short: "Convert markdown file to source files",
Example: `codemd tocode -i docs.md -o src`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if mdFile == "" {
return fmt.Errorf("markdown file is required (use --input or -i)")
}
if outputDir == "" {
return fmt.Errorf("output directory is required (use --output or -o)")
}
doc := NewDocument()
cmd.Printf("Parsing markdown file: %s\n", mdFile)
if err := doc.ParseMarkdown(mdFile); err != nil {
return fmt.Errorf("failed to parse markdown: %w", err)
}
cmd.Printf("Extracting %d code blocks to: %s\n", len(doc.Blocks), outputDir)
if err := doc.ToSourceFiles(outputDir); err != nil {
return fmt.Errorf("failed to extract files: %w", err)
}
cmd.Printf("Successfully extracted:")
for _, block := range doc.Blocks {
cmd.Printf(" - %s (%s)\n", block.Filename, block.Language)
}
return nil
},
}
cmd.Flags().StringVarP(&mdFile, "input", "i", "", "Input markdown file to extract from")
_ = cmd.MarkFlagRequired("input")
cmd.Flags().StringVarP(&outputDir, "output", "o", "", "Output directory for extracted files")
_ = cmd.MarkFlagRequired("output")
return cmd
}
func CommandToMarkdown() *cobra.Command {
var outputFile string
var directory string
var files []string
cmd := &cobra.Command{
Use: "tomd",
Short: "Convert source files to markdown",
Example: `codemd tomd -f file1 -f file2 -o output.md`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if outputFile == "" {
return fmt.Errorf("output file is required (use --output or -o)")
}
doc := NewDocument()
if directory == "" && len(files) == 0 {
return fmt.Errorf("must specify either --dir or --files (or both)")
}
if directory != "" {
cmd.Printf("Scanning directory: %s\n", directory)
if err := doc.ParseDir(directory); err != nil {
return fmt.Errorf("failed to scan directory: %w", err)
}
}
if len(files) > 0 {
cmd.Printf("Parsing %d files\n", len(files))
if err := doc.ParseFiles(files...); err != nil {
return fmt.Errorf("failed to parse files: %w", err)
}
}
cmd.Printf("Generating markdown with %d code blocks: %s\n", len(doc.Blocks), outputFile)
if err := doc.ToMarkdown(outputFile); err != nil {
return fmt.Errorf("failed to generate markdown: %w", err)
}
cmd.Printf("Successfully generated markdown with:")
for _, block := range doc.Blocks {
cmd.Printf(" - %s (%s)\n", block.Filename, block.Language)
}
return nil
},
}
cmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output markdown file")
_ = cmd.MarkFlagRequired("output")
cmd.Flags().StringVarP(&directory, "dir", "d", "", "Directory to scan for source files")
cmd.Flags().StringSliceVarP(&files, "files", "f", nil, "Files to include")
return cmd
}