Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
szktkfm committed Apr 13, 2024
1 parent b0be3c4 commit 928b020
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 153 deletions.
22 changes: 15 additions & 7 deletions cmd/mdtt/mdtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,39 @@ var (

func createModel(args []string, inplace bool) mdtt.Model {

var model mdtt.Model

if !isatty.IsTerminal(os.Stdin.Fd()) {

content, _ := io.ReadAll(os.Stdin)
model = mdtt.NewRoot(
model, err := mdtt.NewUI(
mdtt.WithMarkdown(content),
)
if err != nil {
log.Fatal(err)
}
return model

} else if len(args) == 0 {

model = mdtt.NewRoot()
model, err := mdtt.NewUI()
if err != nil {
log.Fatal(err)
}
return model

} else {
f, _ := os.Open(args[0])
defer f.Close()
content, _ := io.ReadAll(f)
model = mdtt.NewRoot(
model, err := mdtt.NewUI(
mdtt.WithMarkdown(content),
mdtt.WithInplace(inplace),
mdtt.WithFilePath(args[0]),
)
if err != nil {
log.Fatal(err)
}
return model
}

return model
}

func Execute() {
Expand Down
63 changes: 31 additions & 32 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ import (
astext "github.com/yuin/goldmark/extension/ast"
)

type Element struct {
Entering string
Renderer func(b *bytes.Buffer)
Finisher func(b *bytes.Buffer)
type element struct {
open func(b *bytes.Buffer)
finish func(b *bytes.Buffer)
}

// NewElement returns the appropriate render Element for a given node.
func (tr *TableModelBuilder) NewElement(node ast.Node, source []byte) Element {
func (tr *tableModelBuilder) NewElement(node ast.Node, source []byte) element {

switch node.Kind() {

case ast.KindLink:
n := node.(*ast.Link)
log.Debug(n)
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
b.WriteString("[")
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
b.WriteString("](")
b.WriteString(string(n.Destination))
b.WriteString(")")
Expand All @@ -41,36 +40,36 @@ func (tr *TableModelBuilder) NewElement(node ast.Node, source []byte) Element {
u = "mailto:" + u
}

return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
b.WriteString(u)
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
b.WriteString("")
},
}

case ast.KindCodeSpan:
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
b.WriteString("`")
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
b.WriteString("`")
},
}

case ast.KindEmphasis:
n := node.(*ast.Emphasis)
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
if n.Level == 2 {
b.WriteString("**")
return
}
b.WriteString("_")
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
if n.Level == 2 {
b.WriteString("**")
return
Expand All @@ -80,47 +79,47 @@ func (tr *TableModelBuilder) NewElement(node ast.Node, source []byte) Element {
}

case astext.KindTableCell:
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
},
}

case astext.KindTableRow:
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
},
}

case ast.KindText:
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
b.WriteString(string(node.Text(source)))
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
b.WriteString("")
},
}

case east.KindEmoji:
n := node.(*east.Emoji)
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
b.WriteString(string(n.Value.Unicode))
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
b.WriteString("")
},
}

default:
return Element{
Renderer: func(b *bytes.Buffer) {
return element{
open: func(b *bytes.Buffer) {
},
Finisher: func(b *bytes.Buffer) {
finish: func(b *bytes.Buffer) {
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/log v0.3.1
github.com/google/go-cmp v0.5.8
github.com/mattn/go-isatty v0.0.18
github.com/mattn/go-runewidth v0.0.15
github.com/muesli/termenv v0.15.2
github.com/spf13/cobra v1.8.0
Expand All @@ -22,7 +23,6 @@ require (
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
Expand Down
84 changes: 42 additions & 42 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

var highPriority = 100

// TableModelBuilder build TableModel from markdown
type TableModelBuilder struct {
// tableModelBuilder build TableModel from markdown
type tableModelBuilder struct {
inTable bool
buf *bytes.Buffer
// temprary storage of table rows
Expand Down Expand Up @@ -56,7 +56,7 @@ func parse(s []byte) []TableModel {

}

func (b *TableModelBuilder) build() []TableModel {
func (b *tableModelBuilder) build() []TableModel {

var models []TableModel
for _, t := range b.tables {
Expand Down Expand Up @@ -94,74 +94,74 @@ func (b *TableModelBuilder) build() []TableModel {
return models
}

func NewTableModelBuilder() *TableModelBuilder {
func NewTableModelBuilder() *tableModelBuilder {
var buf []byte
return &TableModelBuilder{
return &tableModelBuilder{
buf: bytes.NewBuffer(buf),
}
}

// RegisterFuncs implements NodeRenderer.RegisterFuncs.
func (r *TableModelBuilder) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
func (r *tableModelBuilder) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
// blocks
reg.Register(ast.KindDocument, r.renderNode)
reg.Register(ast.KindHeading, r.renderNode)
reg.Register(ast.KindBlockquote, r.renderNode)
reg.Register(ast.KindCodeBlock, r.renderNode)
reg.Register(ast.KindFencedCodeBlock, r.renderNode)
reg.Register(ast.KindHTMLBlock, r.renderNode)
reg.Register(ast.KindList, r.renderNode)
reg.Register(ast.KindListItem, r.renderNode)
reg.Register(ast.KindParagraph, r.renderNode)
reg.Register(ast.KindTextBlock, r.renderNode)
reg.Register(ast.KindThematicBreak, r.renderNode)
reg.Register(ast.KindDocument, r.consumeNode)
reg.Register(ast.KindHeading, r.consumeNode)
reg.Register(ast.KindBlockquote, r.consumeNode)
reg.Register(ast.KindCodeBlock, r.consumeNode)
reg.Register(ast.KindFencedCodeBlock, r.consumeNode)
reg.Register(ast.KindHTMLBlock, r.consumeNode)
reg.Register(ast.KindList, r.consumeNode)
reg.Register(ast.KindListItem, r.consumeNode)
reg.Register(ast.KindParagraph, r.consumeNode)
reg.Register(ast.KindTextBlock, r.consumeNode)
reg.Register(ast.KindThematicBreak, r.consumeNode)

// inlines
reg.Register(ast.KindAutoLink, r.renderNode)
reg.Register(ast.KindCodeSpan, r.renderNode)
reg.Register(ast.KindEmphasis, r.renderNode)
reg.Register(ast.KindImage, r.renderNode)
reg.Register(ast.KindLink, r.renderNode)
reg.Register(ast.KindRawHTML, r.renderNode)
reg.Register(ast.KindText, r.renderNode)
reg.Register(ast.KindString, r.renderNode)
reg.Register(ast.KindAutoLink, r.consumeNode)
reg.Register(ast.KindCodeSpan, r.consumeNode)
reg.Register(ast.KindEmphasis, r.consumeNode)
reg.Register(ast.KindImage, r.consumeNode)
reg.Register(ast.KindLink, r.consumeNode)
reg.Register(ast.KindRawHTML, r.consumeNode)
reg.Register(ast.KindText, r.consumeNode)
reg.Register(ast.KindString, r.consumeNode)

// tables
reg.Register(astext.KindTable, r.renderNode)
reg.Register(astext.KindTableHeader, r.renderNode)
reg.Register(astext.KindTableRow, r.renderNode)
reg.Register(astext.KindTableCell, r.renderNode)
reg.Register(astext.KindTable, r.consumeNode)
reg.Register(astext.KindTableHeader, r.consumeNode)
reg.Register(astext.KindTableRow, r.consumeNode)
reg.Register(astext.KindTableCell, r.consumeNode)

// definitions
reg.Register(astext.KindDefinitionList, r.renderNode)
reg.Register(astext.KindDefinitionTerm, r.renderNode)
reg.Register(astext.KindDefinitionDescription, r.renderNode)
reg.Register(astext.KindDefinitionList, r.consumeNode)
reg.Register(astext.KindDefinitionTerm, r.consumeNode)
reg.Register(astext.KindDefinitionDescription, r.consumeNode)

// footnotes
reg.Register(astext.KindFootnote, r.renderNode)
reg.Register(astext.KindFootnoteList, r.renderNode)
reg.Register(astext.KindFootnoteLink, r.renderNode)
reg.Register(astext.KindFootnoteBacklink, r.renderNode)
reg.Register(astext.KindFootnote, r.consumeNode)
reg.Register(astext.KindFootnoteList, r.consumeNode)
reg.Register(astext.KindFootnoteLink, r.consumeNode)
reg.Register(astext.KindFootnoteBacklink, r.consumeNode)

// checkboxes
reg.Register(astext.KindTaskCheckBox, r.renderNode)
reg.Register(astext.KindTaskCheckBox, r.consumeNode)

// strikethrough
reg.Register(astext.KindStrikethrough, r.renderNode)
reg.Register(astext.KindStrikethrough, r.consumeNode)

// emoji
reg.Register(east.KindEmoji, r.renderNode)
reg.Register(east.KindEmoji, r.consumeNode)
}

func (tb *TableModelBuilder) renderNode(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
func (tb *tableModelBuilder) consumeNode(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
if node.Kind() == astext.KindTable {
tb.inTable = true
}

if tb.inTable {
e := tb.NewElement(node, source)
e.Renderer(tb.buf)
e.open(tb.buf)
}

} else {
Expand All @@ -188,7 +188,7 @@ func (tb *TableModelBuilder) renderNode(w util.BufWriter, source []byte, node as
}
default:
e := tb.NewElement(node, source)
e.Finisher(tb.buf)
e.finish(tb.buf)
}
}
}
Expand Down
Loading

0 comments on commit 928b020

Please sign in to comment.