Skip to content

Commit

Permalink
refactor: list node
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 17, 2024
1 parent 35ed01b commit b4ff505
Show file tree
Hide file tree
Showing 43 changed files with 626 additions and 547 deletions.
59 changes: 15 additions & 44 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ type NodeType string

// Block nodes.
const (
LineBreakNode NodeType = "LINE_BREAK"
ParagraphNode NodeType = "PARAGRAPH"
CodeBlockNode NodeType = "CODE_BLOCK"
HeadingNode NodeType = "HEADING"
HorizontalRuleNode NodeType = "HORIZONTAL_RULE"
BlockquoteNode NodeType = "BLOCKQUOTE"
OrderedListNode NodeType = "ORDERED_LIST"
UnorderedListNode NodeType = "UNORDERED_LIST"
TaskListNode NodeType = "TASK_LIST"
MathBlockNode NodeType = "MATH_BLOCK"
TableNode NodeType = "TABLE"
EmbeddedContentNode NodeType = "EMBEDDED_CONTENT"
LineBreakNode NodeType = "LINE_BREAK"
ParagraphNode NodeType = "PARAGRAPH"
CodeBlockNode NodeType = "CODE_BLOCK"
HeadingNode NodeType = "HEADING"
HorizontalRuleNode NodeType = "HORIZONTAL_RULE"
BlockquoteNode NodeType = "BLOCKQUOTE"
ListNode NodeType = "LIST"
ListItemNode NodeType = "LIST_ITEM"
OrderedListItemNode NodeType = "ORDERED_LIST_ITEM"
UnorderedListItemNode NodeType = "UNORDERED_LIST_ITEM"
TaskListItemNode NodeType = "TASK_LIST_ITEM"
MathBlockNode NodeType = "MATH_BLOCK"
TableNode NodeType = "TABLE"
EmbeddedContentNode NodeType = "EMBEDDED_CONTENT"
)

// Inline nodes.
Expand Down Expand Up @@ -46,45 +48,14 @@ type Node interface {

// Restore returns a string representation of this node.
Restore() string

// PrevSibling returns a previous sibling node of this node.
PrevSibling() Node

// NextSibling returns a next sibling node of this node.
NextSibling() Node

// SetPrevSibling sets a previous sibling node to this node.
SetPrevSibling(Node)

// SetNextSibling sets a next sibling node to this node.
SetNextSibling(Node)
}

type BaseNode struct {
prevSibling Node

nextSibling Node
}

func (n *BaseNode) PrevSibling() Node {
return n.prevSibling
}

func (n *BaseNode) NextSibling() Node {
return n.nextSibling
}

func (n *BaseNode) SetPrevSibling(node Node) {
n.prevSibling = node
}

func (n *BaseNode) SetNextSibling(node Node) {
n.nextSibling = node
}

func IsBlockNode(node Node) bool {
switch node.Type() {
case ParagraphNode, CodeBlockNode, HeadingNode, HorizontalRuleNode, BlockquoteNode, OrderedListNode, UnorderedListNode, TaskListNode, MathBlockNode, TableNode, EmbeddedContentNode:
case ParagraphNode, CodeBlockNode, HeadingNode, HorizontalRuleNode, BlockquoteNode, ListNode, ListItemNode, OrderedListItemNode, UnorderedListItemNode, TaskListItemNode, TableNode, EmbeddedContentNode:
return true
default:
return false
Expand Down
59 changes: 45 additions & 14 deletions ast/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,61 @@ func (n *Blockquote) Restore() string {
return result
}

type OrderedList struct {
type List struct {
BaseBlock

Children []Node
}

func (*List) Type() NodeType {
return ListNode
}

func (n *List) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return result
}

type ListItem struct {
BaseBlock
}

func (*ListItem) Type() NodeType {
return ListItemNode
}

func (*ListItem) Restore() string {
// Should be overridden.
return ""
}

type OrderedListItem struct {
ListItem

// Number is the number of the list.
Number string
// Indent is the number of spaces.
Indent int
Children []Node
}

func (*OrderedList) Type() NodeType {
return OrderedListNode
func (*OrderedListItem) Type() NodeType {
return OrderedListItemNode
}

func (n *OrderedList) Restore() string {
func (n *OrderedListItem) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return fmt.Sprintf("%s%s. %s", strings.Repeat(" ", n.Indent), n.Number, result)
}

type UnorderedList struct {
BaseBlock
type UnorderedListItem struct {
ListItem

// Symbol is "*" or "-" or "+".
Symbol string
Expand All @@ -145,20 +176,20 @@ type UnorderedList struct {
Children []Node
}

func (*UnorderedList) Type() NodeType {
return UnorderedListNode
func (*UnorderedListItem) Type() NodeType {
return UnorderedListItemNode
}

func (n *UnorderedList) Restore() string {
func (n *UnorderedListItem) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
}
return fmt.Sprintf("%s%s %s", strings.Repeat(" ", n.Indent), n.Symbol, result)
}

type TaskList struct {
BaseBlock
type TaskListItem struct {
ListItem

// Symbol is "*" or "-" or "+".
Symbol string
Expand All @@ -168,11 +199,11 @@ type TaskList struct {
Children []Node
}

func (*TaskList) Type() NodeType {
return TaskListNode
func (*TaskListItem) Type() NodeType {
return TaskListItemNode
}

func (n *TaskList) Restore() string {
func (n *TaskListItem) Restore() string {
var result string
for _, child := range n.Children {
result += child.Restore()
Expand Down
23 changes: 0 additions & 23 deletions ast/utils.go

This file was deleted.

51 changes: 0 additions & 51 deletions parser/horizontal_rule_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions parser/ordered_list.go → parser/ordered_list_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"github.com/usememos/gomark/parser/tokenizer"
)

type OrderedListParser struct{}
type OrderedListItemParser struct{}

func NewOrderedListParser() *OrderedListParser {
return &OrderedListParser{}
func NewOrderedListItemParser() *OrderedListItemParser {
return &OrderedListItemParser{}
}

func (*OrderedListParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
func (*OrderedListItemParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
matchedTokens := tokenizer.GetFirstLine(tokens)
indent := 0
for _, token := range matchedTokens {
Expand All @@ -38,7 +38,7 @@ func (*OrderedListParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
if err != nil {
return nil, 0
}
return &ast.OrderedList{
return &ast.OrderedListItem{
Number: matchedTokens[indent].Value,
Indent: indent,
Children: children,
Expand Down
Loading

0 comments on commit b4ff505

Please sign in to comment.