Skip to content

Commit

Permalink
fix(types): section id generation
Browse files Browse the repository at this point in the history
support quoted strings and ignore unnecessary characters

fixes bytesparadise#920

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Feb 5, 2022
1 parent 82c5e6c commit e2b23a6
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 79 deletions.
8 changes: 4 additions & 4 deletions pkg/parser/quoted_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,13 @@ var _ = Describe("quoted strings", func() {
&types.Section{
Level: 1,
Attributes: types.Attributes{
types.AttrID: "_a_episode",
types.AttrID: "_a_curly_episode",
},
Title: title,
},
},
ElementReferences: types.ElementReferences{
"_a_episode": title,
"_a_curly_episode": title,
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
Expand Down Expand Up @@ -1096,13 +1096,13 @@ var _ = Describe("quoted strings", func() {
&types.Section{
Level: 1,
Attributes: types.Attributes{
types.AttrID: "_a_episode",
types.AttrID: "_a_curly_episode",
},
Title: title,
},
},
ElementReferences: types.ElementReferences{
"_a_episode": title,
"_a_curly_episode": title,
},
}
Expect(ParseDocument(source)).To(MatchDocument(expected))
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/html5/quoted_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var _ = Describe("quoted strings", func() {
It("curly in title", func() {
source := "== a '`curly`' episode"
expected := `<div class="sect1">
<h2 id="_a_episode">a &#8216;curly&#8217; episode</h2>
<h2 id="_a_curly_episode">a &#8216;curly&#8217; episode</h2>
<div class="sectionbody">
</div>
</div>
Expand Down Expand Up @@ -293,7 +293,7 @@ var _ = Describe("quoted strings", func() {
It("double curly in title", func() {
source := "== a \"`curly`\" episode"
expected := `<div class="sect1">
<h2 id="_a_episode">a &#8220;curly&#8221; episode</h2>
<h2 id="_a_curly_episode">a &#8220;curly&#8221; episode</h2>
<div class="sectionbody">
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions pkg/renderer/sgml/html5/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var _ = Describe("strings", func() {
It("title with explicit apostrophe", func() {
source := "== It`'s A Wonderful Life"
expected := "<div class=\"sect1\">\n" +
"<h2 id=\"_it_s_a_wonderful_life\">It&#8217;s A Wonderful Life</h2>\n" +
"<h2 id=\"_its_a_wonderful_life\">It&#8217;s A Wonderful Life</h2>\n" +
"<div class=\"sectionbody\">\n" +
"</div>\n" +
"</div>\n"
Expand All @@ -108,7 +108,7 @@ var _ = Describe("strings", func() {
It("title with explicit apostrophe (unicode)", func() {
source := ":unicode:\n\n== It`'s A Wonderful Life"
expected := "<div class=\"sect1\">\n" +
"<h2 id=\"_it_s_a_wonderful_life\">It\u2019s A Wonderful Life</h2>\n" +
"<h2 id=\"_its_a_wonderful_life\">It\u2019s A Wonderful Life</h2>\n" +
"<div class=\"sectionbody\">\n" +
"</div>\n" +
"</div>\n"
Expand All @@ -118,7 +118,7 @@ var _ = Describe("strings", func() {
It("title with implicit apostrophe", func() {
source := "== It's A Wonderful Life"
expected := "<div class=\"sect1\">\n" +
"<h2 id=\"_it_s_a_wonderful_life\">It&#8217;s A Wonderful Life</h2>\n" +
"<h2 id=\"_its_a_wonderful_life\">It&#8217;s A Wonderful Life</h2>\n" +
"<div class=\"sectionbody\">\n" +
"</div>\n" +
"</div>\n"
Expand All @@ -128,7 +128,7 @@ var _ = Describe("strings", func() {
It("title with implicit apostrophe (unicode)", func() {
source := ":unicode:\n\n== It's A Wonderful Life"
expected := "<div class=\"sect1\">\n" +
"<h2 id=\"_it_s_a_wonderful_life\">It\u2019s A Wonderful Life</h2>\n" +
"<h2 id=\"_its_a_wonderful_life\">It\u2019s A Wonderful Life</h2>\n" +
"<div class=\"sectionbody\">\n" +
"</div>\n" +
"</div>\n"
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/xhtml5/quoted_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var _ = Describe("quoted strings", func() {
It("curly in title", func() {
source := "== a '`curly`' episode"
expected := `<div class="sect1">
<h2 id="_a_episode">a &#8216;curly&#8217; episode</h2>
<h2 id="_a_curly_episode">a &#8216;curly&#8217; episode</h2>
<div class="sectionbody">
</div>
</div>
Expand Down Expand Up @@ -287,7 +287,7 @@ var _ = Describe("quoted strings", func() {
It("double curly in title", func() {
source := "== a \"`curly`\" episode"
expected := `<div class="sect1">
<h2 id="_a_episode">a &#8220;curly&#8221; episode</h2>
<h2 id="_a_curly_episode">a &#8220;curly&#8221; episode</h2>
<div class="sectionbody">
</div>
</div>
Expand Down
166 changes: 117 additions & 49 deletions pkg/types/non_alphanumeric_replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,141 @@ package types_test
import (
"github.com/bytesparadise/libasciidoc/pkg/types"

. "github.com/onsi/ginkgo" // nolint:golint
. "github.com/onsi/gomega" // nolint:golintt
. "github.com/onsi/ginkgo/extensions/table" // nolint:golint
. "github.com/onsi/gomega" // nolint:golint
)

var _ = Describe("normalizing string", func() {
var _ = DescribeTable("replace non-alphanumeric chars",

It("hello", func() {
source := []interface{}{
&types.StringElement{Content: "hello"},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("hello"))
})
func(source []interface{}, valueWithDefaultSettings, valueWithCustomSettings string) {
Expect(types.ReplaceNonAlphanumerics(source, "_", "_")).To(Equal(valueWithDefaultSettings))
Expect(types.ReplaceNonAlphanumerics(source, "id_", "-")).To(Equal(valueWithCustomSettings))
},

It("héllo with an accent", func() {
source := []interface{}{
&types.StringElement{Content: " héllo 1.2 and 3 Spaces"},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("héllo_1_2_and_3_spaces"))
})
Entry("hello",
[]interface{}{
&types.StringElement{
Content: "hello",
},
},
"_hello",
"id_hello",
),

It("a an accent and a swedish character", func() {
source := []interface{}{
&types.StringElement{Content: `A à ⌘`},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("a_à"))
})
Entry("héllo with an accent",
[]interface{}{
&types.StringElement{
Content: " héllo 1.2 and 3 Spaces",
},
},
"_héllo_1_2_and_3_spaces",
"id_héllo-1-2-and-3-spaces",
),

It("AŁA", func() {
source := []interface{}{
&types.StringElement{Content: `AŁA 0.1 ?`},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("ała_0_1"))
})
Entry("a an accent and a swedish character",
[]interface{}{
&types.StringElement{
Content: `A à ⌘`,
},
},
"_a_à",
"id_a-à",
),

Entry("AŁA",
[]interface{}{
&types.StringElement{
Content: `AŁA 0.1 ?`,
},
},
"_ała_0_1",
"id_ała-0-1",
),

It("it's 2 spaces, here !", func() {
source := []interface{}{
&types.StringElement{Content: `it's 2 spaces, here !`},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("it_s_2_spaces_here"))
})
Entry("it's 2 spaces, here !",
[]interface{}{
&types.StringElement{
Content: `it's 2 spaces, here !`,
},
},
"_its_2_spaces_here",
"id_its-2-spaces-here",
),

It("content with <strong> markup", func() {
// == a section title, with *bold content*
source := []interface{}{
&types.StringElement{Content: "a section title, with"},
Entry("content with <strong> markup",
[]interface{}{
&types.StringElement{
Content: "a section title, with",
},
&types.QuotedText{
Kind: types.SingleQuoteBold,
Elements: []interface{}{
&types.StringElement{Content: "bold content"},
&types.StringElement{
Content: "bold content",
},
},
},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("a_section_title_with_bold_content"))
})
},
"_a_section_title_with_bold_content",
"id_a-section-title-with-bold-content",
),

It("content with link", func() {
// == a section title, with *bold content*
source := []interface{}{
&types.StringElement{Content: "link to "},
Entry("content with link",
[]interface{}{
&types.StringElement{
Content: "link to ",
},
&types.InlineLink{
Attributes: types.Attributes{},
Location: &types.Location{
Scheme: "https://",
Path: "foo.bar",
},
},
}
Expect(types.ReplaceNonAlphanumerics(source, "_")).To(Equal("link_to_httpsfoo_bar")) // asciidoctor will return `_link_to_httpsfoo_bar`
})
})
},
"_link_to_httpsfoo_bar",
"id_link-to-httpsfoo-bar",
),

Entry("content with dots and special characters",
[]interface{}{
&types.StringElement{
Content: "...and we're back!",
},
},
"_and_were_back",
"id_-and-were-back",
),

Entry("content with dots",
[]interface{}{
&types.StringElement{
Content: "Section A.a",
},
},
"_section_a_a",
"id_section-a-a",
),

Entry("content with quoted string",
// Block Quotes and "`Smart`" Ones
[]interface{}{
&types.StringElement{
Content: "Block Quotes and ",
},
&types.QuotedString{
Kind: types.DoubleQuote,
Elements: []interface{}{
&types.StringElement{
Content: "Smart",
},
},
},
&types.StringElement{
Content: "Ones",
},
},
"_block_quotes_and_smart_ones",
"id_block-quotes-and-smart-ones",
),
)
Loading

0 comments on commit e2b23a6

Please sign in to comment.