Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>` and `emdone <name>` 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,
Expand Down
21 changes: 9 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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")

Expand Down
51 changes: 22 additions & 29 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
Expand Down Expand Up @@ -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
}{
{
Expand All @@ -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
Expand Down