Skip to content

Commit

Permalink
fix new line issue
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Jun 18, 2024
1 parent 6fe8624 commit 859a87e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion examples/doc/example.docbook
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

<section>
<title>Booking.proto</title>
<para>Booking related messages.</para><para>This file is really just an example. The data model is completely</para><para>fictional.</para>
<para>Booking related messages.</para><para>This file is really just an example. The data model is completely
fictional.</para>

<section id="com.example.Booking">
<title>Booking</title>
Expand Down
3 changes: 2 additions & 1 deletion examples/doc/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ <h2>Table of Contents</h2>
<div class="file-heading">
<h2 id="Booking.proto">Booking.proto</h2><a href="#title">Top</a>
</div>
<p>Booking related messages.</p><p>This file is really just an example. The data model is completely</p><p>fictional.</p>
<p>Booking related messages.</p><p>This file is really just an example. The data model is completely
fictional.</p>


<h3 id="com.example.Booking">Booking</h3>
Expand Down
10 changes: 6 additions & 4 deletions filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import (
)

var (
paraPattern = regexp.MustCompile(`(\n|\r|\r\n)\s*`)
paraPattern = regexp.MustCompile(`(\n\n)\s*`)
spacePattern = regexp.MustCompile("( )+")
multiNewlinePattern = regexp.MustCompile(`(\r\n|\r|\n){2,}`)
multiNewlinePattern = regexp.MustCompile(`(\n){2,}`)
specialCharsPattern = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
)

// PFilter splits the content by new lines and wraps each one in a <p> tag.
func PFilter(content string) template.HTML {
paragraphs := paraPattern.Split(content, -1)
normalized := strings.Replace(content, "\r\n", "\n", -1)
paragraphs := paraPattern.Split(normalized, -1)

return template.HTML(fmt.Sprintf("<p>%s</p>", strings.Join(paragraphs, "</p><p>")))
}

// ParaFilter splits the content by new lines and wraps each one in a <para> tag.
func ParaFilter(content string) string {
paragraphs := paraPattern.Split(content, -1)
normalized := strings.Replace(content, "\r\n", "\n", -1)
paragraphs := paraPattern.Split(normalized, -1)
return fmt.Sprintf("<para>%s</para>", strings.Join(paragraphs, "</para><para>"))
}

Expand Down
12 changes: 6 additions & 6 deletions filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
func TestPFilter(t *testing.T) {
tests := map[string]string{
"Some content.": "<p>Some content.</p>",
"Some content.\nRight here.": "<p>Some content.</p><p>Right here.</p>",
"Some content.\r\nRight here.": "<p>Some content.</p><p>Right here.</p>",
"Some content.\n\tRight here.": "<p>Some content.</p><p>Right here.</p>",
"Some content.\nRight here.": "<p>Some content.\nRight here.</p>",
"Some content.\r\nRight here.": "<p>Some content.\nRight here.</p>",
"Some content.\n\tRight here.": "<p>Some content.\n\tRight here.</p>",
"Some content.\r\n\n \r\n Right here.": "<p>Some content.</p><p>Right here.</p>",
}

Expand All @@ -25,9 +25,9 @@ func TestPFilter(t *testing.T) {
func TestParaFilter(t *testing.T) {
tests := map[string]string{
"Some content.": "<para>Some content.</para>",
"Some content.\nRight here.": "<para>Some content.</para><para>Right here.</para>",
"Some content.\r\nRight here.": "<para>Some content.</para><para>Right here.</para>",
"Some content.\n\tRight here.": "<para>Some content.</para><para>Right here.</para>",
"Some content.\nRight here.": "<para>Some content.\nRight here.</para>",
"Some content.\r\nRight here.": "<para>Some content.\nRight here.</para>",
"Some content.\n\tRight here.": "<para>Some content.\n\tRight here.</para>",
"Some content.\r\n\n \r\n Right here.": "<para>Some content.</para><para>Right here.</para>",
}

Expand Down

0 comments on commit 859a87e

Please sign in to comment.