mirror of https://github.com/usememos/memos
feat: implement subscript and superscript parsers
parent
1f5899d238
commit
7236552b6c
@ -0,0 +1,53 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SubscriptParser struct{}
|
||||||
|
|
||||||
|
func NewSubscriptParser() *SubscriptParser {
|
||||||
|
return &SubscriptParser{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SubscriptParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||||
|
if len(tokens) < 3 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if tokens[0].Type != tokenizer.Tilde {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
contentTokens := []*tokenizer.Token{}
|
||||||
|
matched := false
|
||||||
|
for _, token := range tokens[1:] {
|
||||||
|
if token.Type == tokenizer.Newline {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if token.Type == tokenizer.Tilde {
|
||||||
|
matched = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
contentTokens = append(contentTokens, token)
|
||||||
|
}
|
||||||
|
if !matched || len(contentTokens) == 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(contentTokens) + 2, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *SubscriptParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||||
|
size, ok := p.Match(tokens)
|
||||||
|
if size == 0 || !ok {
|
||||||
|
return nil, errors.New("not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
contentTokens := tokens[1 : size-1]
|
||||||
|
return &ast.Subscript{
|
||||||
|
Content: tokenizer.Stringify(contentTokens),
|
||||||
|
}, nil
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/restore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSubscriptParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
text string
|
||||||
|
subscript ast.Node
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
text: "~Hello world!",
|
||||||
|
subscript: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "~Hello~",
|
||||||
|
subscript: &ast.Subscript{
|
||||||
|
Content: "Hello",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "~ Hello ~",
|
||||||
|
subscript: &ast.Subscript{
|
||||||
|
Content: " Hello ",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "~1~ Hello ~ ~",
|
||||||
|
subscript: &ast.Subscript{
|
||||||
|
Content: "1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tokens := tokenizer.Tokenize(test.text)
|
||||||
|
node, _ := NewSubscriptParser().Parse(tokens)
|
||||||
|
require.Equal(t, restore.Restore([]ast.Node{test.subscript}), restore.Restore([]ast.Node{node}))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SuperscriptParser struct{}
|
||||||
|
|
||||||
|
func NewSuperscriptParser() *SuperscriptParser {
|
||||||
|
return &SuperscriptParser{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*SuperscriptParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
||||||
|
if len(tokens) < 3 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if tokens[0].Type != tokenizer.Caret {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
contentTokens := []*tokenizer.Token{}
|
||||||
|
matched := false
|
||||||
|
for _, token := range tokens[1:] {
|
||||||
|
if token.Type == tokenizer.Newline {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if token.Type == tokenizer.Caret {
|
||||||
|
matched = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
contentTokens = append(contentTokens, token)
|
||||||
|
}
|
||||||
|
if !matched || len(contentTokens) == 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(contentTokens) + 2, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *SuperscriptParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||||
|
size, ok := p.Match(tokens)
|
||||||
|
if size == 0 || !ok {
|
||||||
|
return nil, errors.New("not matched")
|
||||||
|
}
|
||||||
|
|
||||||
|
contentTokens := tokens[1 : size-1]
|
||||||
|
return &ast.Superscript{
|
||||||
|
Content: tokenizer.Stringify(contentTokens),
|
||||||
|
}, nil
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/gomark/ast"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
|
||||||
|
"github.com/usememos/memos/plugin/gomark/restore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSuperscriptParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
text string
|
||||||
|
superscript ast.Node
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
text: "^Hello world!",
|
||||||
|
superscript: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "^Hello^",
|
||||||
|
superscript: &ast.Superscript{
|
||||||
|
Content: "Hello",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "^ Hello ^",
|
||||||
|
superscript: &ast.Superscript{
|
||||||
|
Content: " Hello ",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "^1^ Hello ^ ^",
|
||||||
|
superscript: &ast.Superscript{
|
||||||
|
Content: "1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tokens := tokenizer.Tokenize(test.text)
|
||||||
|
node, _ := NewSuperscriptParser().Parse(tokens)
|
||||||
|
require.Equal(t, restore.Restore([]ast.Node{test.superscript}), restore.Restore([]ast.Node{node}))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue