Skip to content

Commit 4dd60ce

Browse files
committed
eliminate additional newline at end of substitutions
1 parent 9412a82 commit 4dd60ce

File tree

12 files changed

+39
-18
lines changed

12 files changed

+39
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<!-- DO NOT MODIDY -->
1+
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from src/README.md -->
4+
45
# Markdown Reference Generator
56

67
This markdown reference generator uses
@@ -28,7 +29,6 @@ Flags:
2829
--skip-execute omit the evaluation of the execute statement (for test purposes, only)
2930
--skip-source omit source reference in generation comment
3031
--list print reference index and usage list
31-
3232
```
3333
3434
If called without a target folder a consistency check is

cmds.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
type Command interface {
1616
Position() string
17+
EOL() bool
1718
GetSubstitution(path string, opts Options) ([]byte, error)
1819
}
1920

@@ -23,6 +24,7 @@ type Commands map[string]Command
2324

2425
type Include struct {
2526
_Position
27+
nl bool
2628
file string
2729
filter *filter
2830
extractor extractor
@@ -36,6 +38,10 @@ type filter struct {
3638
filter *regexp.Regexp
3739
}
3840

41+
func (i *Include) EOL() bool {
42+
return i.nl
43+
}
44+
3945
func (i *Include) getData(p string) ([]byte, error) {
4046
if !filepath.IsAbs(i.file) {
4147
p = filepath.Join(filepath.Dir(p), i.file)
@@ -106,7 +112,7 @@ var includeExpPat = regexp.MustCompile("^{([^}]+)}{([a-zA-Z][a-zA-Z0-9 -]*)}(?:{
106112
// --- end include args ---
107113
// --- end example ---
108114

109-
func NewInclude(pos Position, args []byte) (Command, error) {
115+
func NewInclude(pos Position, args []byte, nl bool) (Command, error) {
110116
var err error
111117

112118
matches := includeExpNum.FindSubmatch(args)
@@ -137,7 +143,7 @@ func NewInclude(pos Position, args []byte) (Command, error) {
137143
return nil, fmt.Errorf("invalid filter expression: %w", err)
138144
}
139145
}
140-
return &Include{pos, string(matches[1]), &filter{fexp}, &NumExtractor{int(start), int(end)}}, nil
146+
return &Include{pos, nl, string(matches[1]), &filter{fexp}, &NumExtractor{int(start), int(end)}}, nil
141147
}
142148

143149
matches = includeExpPat.FindSubmatch(args)
@@ -149,7 +155,7 @@ func NewInclude(pos Position, args []byte) (Command, error) {
149155
return nil, fmt.Errorf("invalid filter expression: %w", err)
150156
}
151157
}
152-
return &Include{pos, string(matches[1]), &filter{fexp}, &PatternExtractor{string(matches[2])}}, nil
158+
return &Include{pos, nl, string(matches[1]), &filter{fexp}, &PatternExtractor{string(matches[2])}}, nil
153159
}
154160

155161
return nil, fmt.Errorf("invalid include arguments %q", string(args))
@@ -220,6 +226,7 @@ func (i *PatternExtractor) match(data []byte, key string) (int, int, error) {
220226

221227
type Execute struct {
222228
_Position
229+
nl bool
223230
cmd []string
224231
filter *filter
225232
extractor extractor
@@ -228,6 +235,10 @@ type Execute struct {
228235

229236
var _ Command = (*Execute)(nil)
230237

238+
func (e *Execute) EOL() bool {
239+
return e.nl
240+
}
241+
231242
func (e *Execute) GetSubstitution(path string, opts Options) ([]byte, error) {
232243

233244
if opts.SkipExecute {
@@ -268,7 +279,7 @@ var nextarg = regexp.MustCompile("^{([^}]+)}(.*)$")
268279
var extractExpNum = regexp.MustCompile("^([0-9]+)?(?:(:)([0-9]+)?)?$")
269280
var extractExpPat = regexp.MustCompile("^([a-zA-Z -]+)$")
270281

271-
func NewExecute(pos Position, args []byte) (Command, error) {
282+
func NewExecute(pos Position, args []byte, nl bool) (Command, error) {
272283
var cmd []string
273284

274285
for {
@@ -339,5 +350,5 @@ func NewExecute(pos Position, args []byte) (Command, error) {
339350
return nil, fmt.Errorf("invalid filter expression: %w", err)
340351
}
341352
}
342-
return &Execute{pos, cmd, &filter{fexp}, ext, nil}, nil
353+
return &Execute{pos, nl, cmd, &filter{fexp}, ext, nil}, nil
343354
}

doc/chapters/commands.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<!-- DO NOT MODIDY -->
1+
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../../src/doc/chapters/commands.md -->
4+
45
## Commands
56

67
Commands provide additional functionality to Markdown files.
@@ -78,7 +79,6 @@ the content of the first matching group of the all matches is
7879
concatenated. If the expression uses the multi-line mode, the matches
7980
are suffixed with a newline.
8081

81-
8282
The previous paragraph is taken from the source file
8383
```go
8484
// --- begin filter ---
@@ -123,7 +123,6 @@ some demo text
123123
// --- end text ---
124124
this is line 5 of the demo output
125125
and some other text.
126-
127126
```
128127

129128
It can be completely substituted (as shown above) with the command

doc/chapters/references.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<!-- DO NOT MODIDY -->
1+
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../../src/doc/chapters/references.md -->
4+
45
## References
56

67
Any reference in a Markdown document may

doc/chapters/terms.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<!-- DO NOT MODIDY -->
1+
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../../src/doc/chapters/terms.md -->
4+
45
## Terms
56

67
Instead of using a reference as target for a markdown

generate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func generate(files []*File, resolution Resolution, source, target string, opts
4343
if err == nil && !opts.SkipSource {
4444
w.Write(comment(fmt.Sprintf("from %s", rel), l))
4545
}
46+
w.Write([]byte("\n"))
4647
}
4748
if !f.HasSubst() {
4849
_, err := io.Copy(w, r)
@@ -96,6 +97,9 @@ func generate(files []*File, resolution Resolution, source, target string, opts
9697
if err != nil {
9798
return fmt.Errorf("%s: %s: %s; %w", f.relpath, c.Position(), k, err)
9899
}
100+
if c.EOL() && sub[len(sub)-1] == '\n' {
101+
sub = sub[:len(sub)-1]
102+
}
99103
data = exp.ReplaceAll(data, sub)
100104
}
101105

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/mandelsoft/mdref
22

3-
go 1.21
3+
go 1.23.2
44

55
require (
66
github.com/Masterminds/semver/v3 v3.2.1

scan.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,17 @@ func scanRefs(src string, opts Options) (Refs, Refs, Refs, Commands, error) {
186186
matches, indices = info.scanFor(cmdExp)
187187
for i, m := range matches {
188188
var cmd Command
189+
nl := false
189190
pos := info.Position(indices[i][0])
190191
key := string(m[1])
192+
if len(info.data) > indices[i][0]+len(m[0]) {
193+
nl = info.data[indices[i][0]+len(m[0])] == '\n'
194+
}
191195
switch key {
192196
case "include":
193-
cmd, err = NewInclude(pos, m[2])
197+
cmd, err = NewInclude(pos, m[2], nl)
194198
case "execute":
195-
cmd, err = NewExecute(pos, m[2])
199+
cmd, err = NewExecute(pos, m[2], nl)
196200
default:
197201
err = fmt.Errorf("invalid command %q", key)
198202
}

test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../src/README.md -->
4+
45
# Markdown Reference Generator
56

67
This markdown reference generator uses
@@ -28,7 +29,6 @@ Flags:
2829
--skip-execute omit the evaluation of the execute statement (for test purposes, only)
2930
--skip-source omit source reference in generation comment
3031
--list print reference index and usage list
31-
3232
```
3333
3434
If called without a target folder a consistency check is

test/doc/chapters/commands.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../../../src/doc/chapters/commands.md -->
4+
45
## Commands
56

67
Commands provide additional functionality to Markdown files.
@@ -78,7 +79,6 @@ the content of the first matching group of the all matches is
7879
concatenated. If the expression uses the multi-line mode, the matches
7980
are suffixed with a newline.
8081

81-
8282
The previous paragraph is taken from the source file
8383
```go
8484
// --- begin filter ---
@@ -123,7 +123,6 @@ some demo text
123123
// --- end text ---
124124
this is line 5 of the demo output
125125
and some other text.
126-
127126
```
128127

129128
It can be completely substituted (as shown above) with the command

test/doc/chapters/references.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../../../src/doc/chapters/references.md -->
4+
45
## References
56

67
Any reference in a Markdown document may

test/doc/chapters/terms.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- DO NOT MODIFY -->
22
<!-- this file is generated by mdref -->
33
<!-- from ../../../src/doc/chapters/terms.md -->
4+
45
## Terms
56

67
Instead of using a reference as target for a markdown

0 commit comments

Comments
 (0)