Skip to content

Commit

Permalink
Merge pull request #12 from MichaelEischer/verbatim-support
Browse files Browse the repository at this point in the history
Support for verbatim blocks
  • Loading branch information
MichaelEischer authored Nov 11, 2023
2 parents e1d734b + c62a008 commit c4ad488
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
52 changes: 47 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,25 @@ func readFile(filename string) (e Entry) {

var text []string
var sect string
var verbatim bool // inside verbatim section
for sc.Scan() {
if sc.Err() != nil {
die("unable to read lines from %v: %v", filename, sc.Err())
}

if strings.TrimSpace(sc.Text()) == "" {
trimmedText := strings.TrimSpace(sc.Text())
if !verbatim && trimmedText == "```" {
// start new paragraph
if sect != "" {
text = append(text, sect)
}
sect = "```"
verbatim = true
continue
}

// ignore new lines inside verbatim section
if !verbatim && trimmedText == "" {
if sect != "" {
text = append(text, sect)
}
Expand All @@ -307,10 +320,25 @@ func readFile(filename string) (e Entry) {
continue
}

if sect != "" {
sect += " "
if verbatim {
if sect != "" {
sect += "\n"
}
sect += sc.Text()
} else {
if sect != "" {
sect += " "
}
sect += trimmedText
}

if verbatim && trimmedText == "```" {
verbatim = false
}
sect += strings.TrimSpace(sc.Text())
}

if verbatim {
die("unmatched verbatim tag in %v", filename)
}

err = f.Close()
Expand Down Expand Up @@ -420,6 +448,12 @@ func readEntries(dir string, versions []Release) (entries map[string][]Entry) {
// wrapIndent formats the text in a column smaller than width characters,
// indenting each new line with indent spaces.
func wrapIndent(text string, width, indent int) (result string, err error) {
if strings.HasPrefix(text, "```") {
parts := strings.Split(text, "\n")
sep := "\n" + strings.Repeat(" ", indent)
return strings.Join(parts, sep), nil
}

sc := bufio.NewScanner(strings.NewReader(text))
sc.Split(bufio.ScanWords)
cl := 0
Expand All @@ -428,14 +462,22 @@ func wrapIndent(text string, width, indent int) (result string, err error) {
return "", sc.Err()
}

if cl+len(sc.Text()) > width {
spaceLen := 0
if cl > 0 {
// account for space between words, if there's already a word on the
// current line
spaceLen = 1
}

if cl+spaceLen+len(sc.Text()) > width {
result += "\n"
result += strings.Repeat(" ", indent)
cl = 0
}

if cl > 0 {
result += " "
cl++
}
result += sc.Text()
cl += len(sc.Text())
Expand Down
48 changes: 48 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ https://forum.restic.net/t/getting-last-successful-backup-time/531
},
},
},
{
"Security: short and terse summary\n\n```\nexample\n with\n random spaces\n```\n\nLast block contains just\na few\nlinks.\n\nhttps://github.com/restic/restic/issues/12345",
Entry{
Title: "Short and terse summary",
Type: "Security",
TypeShort: "Sec",
Paragraphs: []string{
"```\nexample\n with\n random spaces\n```",
"Last block contains just a few links.",
},
URLs: []*url.URL{
parseURL(t, "https://github.com/restic/restic/issues/12345"),
},
PrimaryID: 12345,
PrimaryURL: parseURL(t, "https://github.com/restic/restic/issues/12345"),
Issues: []string{"12345"},
IssueURLs: []*url.URL{
parseURL(t, "https://github.com/restic/restic/issues/12345"),
},
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -144,3 +165,30 @@ https://forum.restic.net/t/getting-last-successful-backup-time/531
})
}
}

func TestWrapIndent(t *testing.T) {
var tests = []struct {
In string
Width int
Indent int
Out string
}{
{"Example string", 80, 4, "Example string"},
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 70, 3, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n eiusmod tempor incididunt ut labore et dolore magna aliqua."},
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 55, 2, "Lorem ipsum dolor sit amet, consectetur adipiscing\n elit, sed do eiusmod tempor incididunt ut labore et\n dolore magna aliqua."},
{"```\nexample\n with\n random spaces\n```", 10, 3, "```\n example\n with\n random spaces\n ```"},
}

for _, test := range tests {
t.Run("", func(t *testing.T) {
res, err := wrapIndent(test.In, test.Width, test.Indent)
if err != nil {
t.Fatal(err)
}

if diff := deep.Equal(res, test.Out); diff != nil {
t.Error(diff)
}
})
}
}

0 comments on commit c4ad488

Please sign in to comment.