nip54: djot parser.
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/sivukhin/godjot/djot_parser"
|
||||
"github.com/sivukhin/godjot/html_writer"
|
||||
"golang.org/x/text/transform"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
@@ -24,3 +26,13 @@ func NormalizeIdentifier(name string) string {
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func ArticleAsHTML(content string) string {
|
||||
ast := djot_parser.BuildDjotAst([]byte(content))
|
||||
context := djot_parser.NewConversionContext("html", djot_parser.DefaultConversionRegistry)
|
||||
writer := &html_writer.HtmlWriter{}
|
||||
for _, node := range ast {
|
||||
context.ConvertDjotToHtml(writer, node)
|
||||
}
|
||||
return writer.String()
|
||||
}
|
||||
|
||||
@@ -1,23 +1,41 @@
|
||||
package nip54
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalization(t *testing.T) {
|
||||
for _, vector := range []struct {
|
||||
before string
|
||||
after string
|
||||
func TestArticleAsHTML(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
contains []string
|
||||
}{
|
||||
{" hello ", "hello"},
|
||||
{"Goodbye", "goodbye"},
|
||||
{"the long and winding road / that leads to your door", "the-long-and-winding-road---that-leads-to-your-door"},
|
||||
{"it's 平仮名", "it-s-平仮名"},
|
||||
} {
|
||||
if norm := NormalizeIdentifier(vector.before); norm != vector.after {
|
||||
fmt.Println([]byte(vector.after), []byte(norm))
|
||||
t.Fatalf("%s: %s != %s", vector.before, norm, vector.after)
|
||||
}
|
||||
{
|
||||
name: "simple paragraph",
|
||||
input: "Hello world",
|
||||
contains: []string{"<p>", "Hello world", "</p>"},
|
||||
},
|
||||
{
|
||||
name: "emphasis",
|
||||
input: "*Hello* _world_",
|
||||
contains: []string{"<strong>", "Hello", "</strong>", "<em>", "world", "</em>"},
|
||||
},
|
||||
{
|
||||
name: "heading",
|
||||
input: "# Title",
|
||||
contains: []string{"<h1>", "Title", "</h1>"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ArticleAsHTML(tt.input)
|
||||
for _, expected := range tt.contains {
|
||||
if !strings.Contains(result, expected) {
|
||||
t.Errorf("ArticleAsHTML() output does not contain %q\nGot: %s", expected, result)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user