From 0215ac32cf94087fda5acc64dbc42818ee856594 Mon Sep 17 00:00:00 2001 From: Dan Croak Date: Tue, 4 Mar 2025 09:10:45 -0500 Subject: [PATCH] examples: support file glob patterns --- README.md | 96 ++++++++++++++++++++++------- examples/{ => dir1}/f1.rb | 0 examples/{ => dir1}/f2.go | 0 examples/{ => dir1/subdir1}/f3.js | 0 examples/{ => dir2}/f4.css | 0 examples/{ => dir2/subdir2}/f5.html | 0 examples/input1.md | 25 +++++--- examples/input2.md | 2 +- examples/outwant.md | 32 +++++++--- go.mod | 2 + go.sum | 2 + main.go | 51 ++++++++++++--- main_test.go | 2 +- 13 files changed, 161 insertions(+), 51 deletions(-) rename examples/{ => dir1}/f1.rb (100%) rename examples/{ => dir1}/f2.go (100%) rename examples/{ => dir1/subdir1}/f3.js (100%) rename examples/{ => dir2}/f4.css (100%) rename examples/{ => dir2/subdir2}/f5.html (100%) diff --git a/README.md b/README.md index b9d355a..43968e7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Install ```sh -go install github.com/croaky/mdembed +go install github.com/croaky/mdembed@latest ``` For that to work, Go must be installed and @@ -26,27 +26,38 @@ In `input1.md`: Embed a whole file: ```embed -f1.rb +dir1/f1.rb ``` Embed multiple whole files: ```embed -f1.rb -f2.go +dir1/f1.rb +dir1/f2.go ``` Embed specific lines in a file: ```embed -f3.js log +dir1/subdir1/f3.js log ``` -Embed multiple blocks within the same file: +Embed multiple whole files and multiple blocks within files: ```embed -f4.html h1 -f4.html ul +dir1/f1.rb +dir1/f2.go log +dir2/f4.css h1 +dir2/subdir2/f5.html h1 +dir2/subdir2/f5.html ul +f6.sql users +``` + +Embed files using globs and blocks: + +```embed +**/*.rb +**/*.js log ``` Embed Markdown files and their embeds recursively: @@ -56,13 +67,13 @@ input2.md ``` ```` -In `f1.rb`: +In `dir1/f1.rb`: ```rb puts "hi" ``` -In `f2.go`: +In `dir1/f2.go`: ```go package main @@ -74,7 +85,7 @@ func main() { } ``` -In `f3.js`: +In `dir1/subdir1/f3.js`: ```js console.log("Not embedded"); @@ -86,7 +97,7 @@ console.log("hi"); console.log("Not embedded"); ``` -In `f4.html`: +In `dir2/subdir2/f5.html`: ```html @@ -117,7 +128,7 @@ In `input2.md`: Embed from within an embedded Markdown file: ```embed -f1.rb +dir1/f1.rb ``` ```` @@ -135,50 +146,89 @@ The output will be: Embed a whole file: ```rb -# f1.rb +# dir1/f1.rb puts "hi" ``` Embed multiple whole files: ```rb -# f1.rb +# dir1/f1.rb puts "hi" ``` ```go -// f2.go +// dir1/f2.go package main import "fmt" func main() { - fmt.Println("hi") + // emdo log + fmt.Println("hi") + // emdone log } ``` Embed specific lines in a file: ```js -// f3.js +// dir1/subdir1/f3.js console.log("hi"); ``` -Embed multiple blocks within the same file: +Embed multiple whole files and multiple blocks within files: + +```rb +# dir1/f1.rb +puts "hi" +``` + +```go +// dir1/f2.go +fmt.Println("hi") +``` + +```css +/* dir2/f4.css */ +h1 { + color: blue; +} +``` ```html - +

h1

``` ```html - + ``` +```sql +-- f6.sql +SELECT + * +FROM + users; +``` + +Embed files using globs and blocks: + +```rb +# dir1/f1.rb +puts "hi" +``` + +```js +// dir1/subdir1/f3.js +console.log("hi"); +``` + Embed Markdown files and their embeds recursively: ## Input2 @@ -186,7 +236,7 @@ Embed Markdown files and their embeds recursively: Embed from within an embedded Markdown file: ```rb -# f1.rb +# dir1/f1.rb puts "hi" ``` ```` @@ -197,6 +247,8 @@ puts "hi" removing surrounding whitespace. 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. diff --git a/examples/f1.rb b/examples/dir1/f1.rb similarity index 100% rename from examples/f1.rb rename to examples/dir1/f1.rb diff --git a/examples/f2.go b/examples/dir1/f2.go similarity index 100% rename from examples/f2.go rename to examples/dir1/f2.go diff --git a/examples/f3.js b/examples/dir1/subdir1/f3.js similarity index 100% rename from examples/f3.js rename to examples/dir1/subdir1/f3.js diff --git a/examples/f4.css b/examples/dir2/f4.css similarity index 100% rename from examples/f4.css rename to examples/dir2/f4.css diff --git a/examples/f5.html b/examples/dir2/subdir2/f5.html similarity index 100% rename from examples/f5.html rename to examples/dir2/subdir2/f5.html diff --git a/examples/input1.md b/examples/input1.md index b12ded6..d74eb7a 100644 --- a/examples/input1.md +++ b/examples/input1.md @@ -3,33 +3,40 @@ Embed a whole file: ```embed -f1.rb +dir1/f1.rb ``` Embed multiple whole files: ```embed -f1.rb -f2.go +dir1/f1.rb +dir1/f2.go ``` Embed specific lines in a file: ```embed -f3.js log +dir1/subdir1/f3.js log ``` Embed multiple whole files and multiple blocks within files: ```embed -f1.rb -f2.go log -f4.css h1 -f5.html h1 -f5.html ul +dir1/f1.rb +dir1/f2.go log +dir2/f4.css h1 +dir2/subdir2/f5.html h1 +dir2/subdir2/f5.html ul f6.sql users ``` +Embed files using globs and blocks: + +```embed +**/*.rb +**/*.js log +``` + Embed Markdown files and their embeds recursively: ```embed diff --git a/examples/input2.md b/examples/input2.md index f476e08..1d7772c 100644 --- a/examples/input2.md +++ b/examples/input2.md @@ -3,5 +3,5 @@ Embed from within an embedded Markdown file: ```embed -f1.rb +dir1/f1.rb ``` diff --git a/examples/outwant.md b/examples/outwant.md index 639c87e..0f17ffc 100644 --- a/examples/outwant.md +++ b/examples/outwant.md @@ -3,19 +3,19 @@ Embed a whole file: ```rb -# f1.rb +# dir1/f1.rb puts "hi" ``` Embed multiple whole files: ```rb -# f1.rb +# dir1/f1.rb puts "hi" ``` ```go -// f2.go +// dir1/f2.go package main import "fmt" @@ -30,36 +30,36 @@ func main() { Embed specific lines in a file: ```js -// f3.js +// dir1/subdir1/f3.js console.log("hi"); ``` Embed multiple whole files and multiple blocks within files: ```rb -# f1.rb +# dir1/f1.rb puts "hi" ``` ```go -// f2.go +// dir1/f2.go fmt.Println("hi") ``` ```css -/* f4.css */ +/* dir2/f4.css */ h1 { color: blue; } ``` ```html - +

h1

``` ```html - +