Compare commits

..

3 Commits
v0.0.1 ... main

Author SHA1 Message Date
jolheiser 3d7e79031f
Fix line embedding
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2021-12-25 22:53:20 -06:00
jolheiser 476639ee9b
Add space to fix generation of README
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2021-12-23 22:18:57 -06:00
jolheiser a47ce7b08d
Add README examples
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2021-12-23 22:13:10 -06:00
3 changed files with 27 additions and 5 deletions

View File

@ -4,14 +4,25 @@ Embed "things" in your markdown files.
Given the same input, `emdbed` should give idempotent results. Given the same input, `emdbed` should give idempotent results.
To create an `emdbed` section, use a header and footer as follows
```text
<!-- emdbed: <file> [~<language>] [L<start-line>|/<start-regex>/] [L<end-line>|/<end-regex>/]
<!-- /emdbed -->
```
**NOTE:** The above snippet has an extra space at the end in order to keep it from recognizing the footer when generating this README.
The regex is [Go flavored](https://golang.org/s/re2syntax), which you can test on [regex101](https://regex101.com/).
## Examples ## Examples
The following are generated using [readme.go](readme.go) and `go generate ./...`. The following are generated using [readme.go](readme.go) and `go generate ./...`.
Check the raw markdown for the header selectors used.
An entire file: An entire file:
<!-- emdbed: emdbed_example_test.go --> <!-- emdbed: emdbed_example_test.go -->
```go ```go
// Package emdbed is for embedding "things" in your markdown
package emdbed package emdbed
import ( import (
@ -36,6 +47,7 @@ func Example() {
//func main() { //func main() {
// fmt.Println("Hello, world!") // fmt.Println("Hello, world!")
//} //}
//
// ``` // ```
//<!-- /emdbed --> //<!-- /emdbed -->
} }
@ -45,15 +57,15 @@ func Example() {
Just the package: Just the package:
<!-- emdbed: emdbed_example_test.go L1 L1 --> <!-- emdbed: emdbed_example_test.go L2 L2 -->
```go ```go
package emdbed package emdbed
``` ```
<!-- /emdbed --> <!-- /emdbed -->
First line until the end of imports: Package line until the end of imports:
<!-- emdbed: emdbed_example_test.go L1 /\)/ --> <!-- emdbed: emdbed_example_test.go L2 /\)/ -->
```go ```go
package emdbed package emdbed
@ -85,6 +97,7 @@ func Example() {
//func main() { //func main() {
// fmt.Println("Hello, world!") // fmt.Println("Hello, world!")
//} //}
//
// ``` // ```
//<!-- /emdbed --> //<!-- /emdbed -->
} }

View File

@ -1,3 +1,4 @@
// Package emdbed is for embedding "things" in your markdown
package emdbed package emdbed
import ( import (

10
io.go
View File

@ -25,7 +25,15 @@ func (e emdbed) line(line int, end bool) (int, error) {
if len(indexes) < line { if len(indexes) < line {
return 0, fmt.Errorf("file %q has no line %d", e.name, line) return 0, fmt.Errorf("file %q has no line %d", e.name, line)
} }
return indexes[line-1][0], nil dec := 1
if !end {
dec++
}
var inc int
if !end {
inc++
}
return indexes[line-dec][0] + inc, nil
} }
func (e emdbed) regex(pattern string, end bool) (int, error) { func (e emdbed) regex(pattern string, end bool) (int, error) {