diff --git a/README.md b/README.md index 8609b9d..ba07189 100644 --- a/README.md +++ b/README.md @@ -249,8 +249,8 @@ The file extension is used as the code fence attribute. It parses exact file paths or file glob patterns. -If `emdo` and `emdone` magic comments were used, it will only embed the code -block wrapped by the magic comments. +If `emdo ` and `emdone ` block markers are used in a source file, +you can embed only that named block by specifying the block name after the file path. It is aware of code comment styles for Ada, Assembly, Awk, Bash, C, Clojure, COBOL, C++, C#, CSS, CSV, D, Dart, Elm, Erlang, Elixir, Fortran, F#, Gleam, Go, diff --git a/main.go b/main.go index ab14ae0..99177d5 100644 --- a/main.go +++ b/main.go @@ -190,12 +190,9 @@ func processEmbed(lines []string, output io.Writer, state *ProcessState) error { } fileContent := string(content) - // Adjust filename to include OS-specific path separators for display - displayFilename := filepath.FromSlash(filename) - - if err := processFile(displayFilename, blockName, fileContent, output, state); err != nil { - return err - } + if err := processFile(filename, blockName, fileContent, output, state); err != nil { + return err + } // Add newline between multiple code blocks if j < len(matches)-1 { @@ -248,15 +245,15 @@ func processCodeFile(filename, blockName, fileContent string, output io.Writer) return fmt.Errorf("unsupported file type: %s", ext) } - // Prepare filename comment - var fileName string + // Prepare header comment with filename + var headerComment string if style.LineComment != "" { - fileName = style.LineComment + " " + filename + headerComment = style.LineComment + " " + filename } else if style.BlockDo != "" && style.BlockDone != "" { - fileName = fmt.Sprintf("%s %s %s", style.BlockDo, filename, style.BlockDone) + headerComment = fmt.Sprintf("%s %s %s", style.BlockDo, filename, style.BlockDone) } else { // For file types without comments (like JSON), just use the filename - fileName = filename + headerComment = filename } // 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) // Write code block to output fmt.Fprintf(output, "```%s\n", lang) - fmt.Fprintf(output, "%s\n", fileName) + fmt.Fprintf(output, "%s\n", headerComment) fmt.Fprintf(output, "%s", fileContent) fmt.Fprintf(output, "\n```\n") diff --git a/main_test.go b/main_test.go index a245327..77b73d0 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( "bytes" "os" + "path/filepath" "strings" "testing" ) @@ -60,8 +61,8 @@ func TestProcessMD_ErrorCases(t *testing.T) { tests := []struct { name string input string - setup func() - teardown func() + file string + fileData []byte wantErrMsg string }{ { @@ -80,48 +81,40 @@ func TestProcessMD_ErrorCases(t *testing.T) { wantErrMsg: "no files match pattern nonexistent.go", }, { - name: "Unsupported file type", - input: "Some text before.\n\n```embed\nfile.unknown\n```\n", - setup: func() { - os.WriteFile("file.unknown", []byte("// content"), 0644) - }, - teardown: func() { - os.Remove("file.unknown") - }, + name: "Unsupported file type", + input: "Some text before.\n\n```embed\nfile.unknown\n```\n", + file: "file.unknown", + fileData: []byte("// content"), wantErrMsg: "unsupported file type: .unknown", }, { - name: "Do mark not found", - input: "Some text before.\n\n```embed\nfile.go block1\n```\n", - setup: func() { - os.WriteFile("file.go", []byte(`// This is a test file`), 0644) - }, - teardown: func() { - os.Remove("file.go") - }, + name: "Do mark not found", + input: "Some text before.\n\n```embed\nfile.go block1\n```\n", + file: "file.go", + fileData: []byte(`// This is a test file`), wantErrMsg: "do mark", }, { name: "Done mark not found", input: "Some text before.\n\n```embed\nfile.go block2\n```\n", - setup: func() { - os.WriteFile("file.go", []byte(`// emdo block2 - // Code block content`), 0644) - }, - teardown: func() { - os.Remove("file.go") - }, + file: "file.go", + fileData: []byte(`// emdo block2 + // Code block content`), wantErrMsg: "done mark", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.setup != nil { - tt.setup() + dir := t.TempDir() + if err := os.Chdir(dir); err != nil { + t.Fatal(err) } - if tt.teardown != nil { - defer tt.teardown() + + if tt.file != "" { + if err := os.WriteFile(filepath.Join(dir, tt.file), tt.fileData, 0644); err != nil { + t.Fatal(err) + } } var outBuf bytes.Buffer