Skip to content

Commit d6816bf

Browse files
authored
Merge pull request #112 from cpuguy83/fix_newline_escape
Fix escape characters for content with newline
2 parents 1e915fd + f0c6142 commit d6816bf

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

md2man/roff.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package md2man
22

33
import (
4+
"bufio"
45
"bytes"
56
"fmt"
67
"io"
@@ -333,6 +334,28 @@ func out(w io.Writer, output string) {
333334
}
334335

335336
func escapeSpecialChars(w io.Writer, text []byte) {
337+
scanner := bufio.NewScanner(bytes.NewReader(text))
338+
339+
// count the number of lines in the text
340+
// we need to know this to avoid adding a newline after the last line
341+
n := bytes.Count(text, []byte{'\n'})
342+
idx := 0
343+
344+
for scanner.Scan() {
345+
dt := scanner.Bytes()
346+
if idx < n {
347+
idx++
348+
dt = append(dt, '\n')
349+
}
350+
escapeSpecialCharsLine(w, dt)
351+
}
352+
353+
if err := scanner.Err(); err != nil {
354+
panic(err)
355+
}
356+
}
357+
358+
func escapeSpecialCharsLine(w io.Writer, text []byte) {
336359
for i := 0; i < len(text); i++ {
337360
// escape initial apostrophe or period
338361
if len(text) >= 1 && (text[0] == '\'' || text[0] == '.') {

md2man/roff_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ func TestEscapeCharacters(t *testing.T) {
367367
tests := []string{
368368
"Test-one_two&three\\four~five",
369369
".nh\n\n.PP\nTest-one_two&three\\\\four~five\n",
370+
"'foo'\n'bar'",
371+
".nh\n\n.PP\n\\&'foo'\n\\&'bar'\n",
370372
}
371373
doTestsInline(t, tests)
372374
}
@@ -445,23 +447,25 @@ func doTestsParam(t *testing.T, tests []string, params TestParams) {
445447
execRecoverableTestSuite(t, tests, params, func(candidate *string) {
446448
for i := 0; i+1 < len(tests); i += 2 {
447449
input := tests[i]
448-
*candidate = input
449-
expected := tests[i+1]
450-
actual := runMarkdown(*candidate, params)
451-
if actual != expected {
452-
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
453-
*candidate, expected, actual)
454-
}
450+
t.Run(input, func(t *testing.T) {
451+
*candidate = input
452+
expected := tests[i+1]
453+
actual := runMarkdown(*candidate, params)
454+
if actual != expected {
455+
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
456+
*candidate, expected, actual)
457+
}
455458

456-
// now test every substring to stress test bounds checking
457-
if !testing.Short() {
458-
for start := 0; start < len(input); start++ {
459-
for end := start + 1; end <= len(input); end++ {
460-
*candidate = input[start:end]
461-
runMarkdown(*candidate, params)
459+
// now test every substring to stress test bounds checking
460+
if !testing.Short() {
461+
for start := 0; start < len(input); start++ {
462+
for end := start + 1; end <= len(input); end++ {
463+
*candidate = input[start:end]
464+
runMarkdown(*candidate, params)
465+
}
462466
}
463467
}
464-
}
468+
})
465469
}
466470
})
467471
}

0 commit comments

Comments
 (0)