Skip to content

Commit 0da688f

Browse files
croakywarp-agent
andauthored
doc: simplify code and improve clarity
- rename fileName variable to headerComment (it holds a comment, not a filename) - remove unnecessary filepath.FromSlash conversion (use forward slashes consistently since this is a Unix filter tool) - use t.TempDir() in tests instead of manual setup/teardown - clarify README terminology: "block markers" with "block name" instead of "magic comments" These changes reduce complexity and better align with Go idioms for a ~370 line single-file CLI tool. #9 Co-authored-by: Warp <agent@warp.dev>
1 parent 3eb1044 commit 0da688f

3 files changed

Lines changed: 33 additions & 43 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ The file extension is used as the code fence attribute.
249249

250250
It parses exact file paths or file glob patterns.
251251

252-
If `emdo` and `emdone` magic comments were used, it will only embed the code
253-
block wrapped by the magic comments.
252+
If `emdo <name>` and `emdone <name>` block markers are used in a source file,
253+
you can embed only that named block by specifying the block name after the file path.
254254

255255
It is aware of code comment styles for Ada, Assembly, Awk, Bash, C, Clojure,
256256
COBOL, C++, C#, CSS, CSV, D, Dart, Elm, Erlang, Elixir, Fortran, F#, Gleam, Go,

main.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,9 @@ func processEmbed(lines []string, output io.Writer, state *ProcessState) error {
190190
}
191191
fileContent := string(content)
192192

193-
// Adjust filename to include OS-specific path separators for display
194-
displayFilename := filepath.FromSlash(filename)
195-
196-
if err := processFile(displayFilename, blockName, fileContent, output, state); err != nil {
197-
return err
198-
}
193+
if err := processFile(filename, blockName, fileContent, output, state); err != nil {
194+
return err
195+
}
199196

200197
// Add newline between multiple code blocks
201198
if j < len(matches)-1 {
@@ -248,15 +245,15 @@ func processCodeFile(filename, blockName, fileContent string, output io.Writer)
248245
return fmt.Errorf("unsupported file type: %s", ext)
249246
}
250247

251-
// Prepare filename comment
252-
var fileName string
248+
// Prepare header comment with filename
249+
var headerComment string
253250
if style.LineComment != "" {
254-
fileName = style.LineComment + " " + filename
251+
headerComment = style.LineComment + " " + filename
255252
} else if style.BlockDo != "" && style.BlockDone != "" {
256-
fileName = fmt.Sprintf("%s %s %s", style.BlockDo, filename, style.BlockDone)
253+
headerComment = fmt.Sprintf("%s %s %s", style.BlockDo, filename, style.BlockDone)
257254
} else {
258255
// For file types without comments (like JSON), just use the filename
259-
fileName = filename
256+
headerComment = filename
260257
}
261258

262259
// If a block name is specified and the file doesn't support comments, return an error
@@ -282,7 +279,7 @@ func processCodeFile(filename, blockName, fileContent string, output io.Writer)
282279

283280
// Write code block to output
284281
fmt.Fprintf(output, "```%s\n", lang)
285-
fmt.Fprintf(output, "%s\n", fileName)
282+
fmt.Fprintf(output, "%s\n", headerComment)
286283
fmt.Fprintf(output, "%s", fileContent)
287284
fmt.Fprintf(output, "\n```\n")
288285

main_test.go

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"os"
6+
"path/filepath"
67
"strings"
78
"testing"
89
)
@@ -60,8 +61,8 @@ func TestProcessMD_ErrorCases(t *testing.T) {
6061
tests := []struct {
6162
name string
6263
input string
63-
setup func()
64-
teardown func()
64+
file string
65+
fileData []byte
6566
wantErrMsg string
6667
}{
6768
{
@@ -80,48 +81,40 @@ func TestProcessMD_ErrorCases(t *testing.T) {
8081
wantErrMsg: "no files match pattern nonexistent.go",
8182
},
8283
{
83-
name: "Unsupported file type",
84-
input: "Some text before.\n\n```embed\nfile.unknown\n```\n",
85-
setup: func() {
86-
os.WriteFile("file.unknown", []byte("// content"), 0644)
87-
},
88-
teardown: func() {
89-
os.Remove("file.unknown")
90-
},
84+
name: "Unsupported file type",
85+
input: "Some text before.\n\n```embed\nfile.unknown\n```\n",
86+
file: "file.unknown",
87+
fileData: []byte("// content"),
9188
wantErrMsg: "unsupported file type: .unknown",
9289
},
9390
{
94-
name: "Do mark not found",
95-
input: "Some text before.\n\n```embed\nfile.go block1\n```\n",
96-
setup: func() {
97-
os.WriteFile("file.go", []byte(`// This is a test file`), 0644)
98-
},
99-
teardown: func() {
100-
os.Remove("file.go")
101-
},
91+
name: "Do mark not found",
92+
input: "Some text before.\n\n```embed\nfile.go block1\n```\n",
93+
file: "file.go",
94+
fileData: []byte(`// This is a test file`),
10295
wantErrMsg: "do mark",
10396
},
10497
{
10598
name: "Done mark not found",
10699
input: "Some text before.\n\n```embed\nfile.go block2\n```\n",
107-
setup: func() {
108-
os.WriteFile("file.go", []byte(`// emdo block2
109-
// Code block content`), 0644)
110-
},
111-
teardown: func() {
112-
os.Remove("file.go")
113-
},
100+
file: "file.go",
101+
fileData: []byte(`// emdo block2
102+
// Code block content`),
114103
wantErrMsg: "done mark",
115104
},
116105
}
117106

118107
for _, tt := range tests {
119108
t.Run(tt.name, func(t *testing.T) {
120-
if tt.setup != nil {
121-
tt.setup()
109+
dir := t.TempDir()
110+
if err := os.Chdir(dir); err != nil {
111+
t.Fatal(err)
122112
}
123-
if tt.teardown != nil {
124-
defer tt.teardown()
113+
114+
if tt.file != "" {
115+
if err := os.WriteFile(filepath.Join(dir, tt.file), tt.fileData, 0644); err != nil {
116+
t.Fatal(err)
117+
}
125118
}
126119

127120
var outBuf bytes.Buffer

0 commit comments

Comments
 (0)