From f3c759655aaffb1f6cbbc42e22182501d25c13f0 Mon Sep 17 00:00:00 2001 From: Toby Padilla Date: Mon, 4 Nov 2019 17:17:36 -0600 Subject: [PATCH] init --- .gitignore | 1 + cmd/gold/main.go | 28 ++++++++ go.mod | 9 +++ go.sum | 6 ++ gold.go | 166 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/gold/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 gold.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d90896 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmd/gold/gold diff --git a/cmd/gold/main.go b/cmd/gold/main.go new file mode 100644 index 0000000..09b73fe --- /dev/null +++ b/cmd/gold/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + + "github.com/magicnumbers/gold" + bf "gopkg.in/russross/blackfriday.v2" +) + +const tmd = ` +# Test + +This is a test yo. + +## Dude! + +* one +* two +* three + +[yoda](http://flame.com) +` + +func main() { + r := gold.TermRenderer{} + out := bf.Run([]byte(tmd), bf.WithRenderer(&r)) + fmt.Println(string(out)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d4fbb09 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/magicnumbers/gold + +go 1.13 + +require ( + github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434 + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect + gopkg.in/russross/blackfriday.v2 v2.0.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..58da90e --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434 h1:im9kkmH0WWwxzegiv18gSUJbuXR9y028rXrWuPp6Jug= +github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +gopkg.in/russross/blackfriday.v2 v2.0.0 h1:+FlnIV8DSQnT7NZ43hcVKcdJdzZoeCmJj4Ql8gq5keA= +gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI= diff --git a/gold.go b/gold.go new file mode 100644 index 0000000..530fbe6 --- /dev/null +++ b/gold.go @@ -0,0 +1,166 @@ +package gold + +import ( + "fmt" + "io" + "strconv" + "strings" + + "github.com/logrusorgru/aurora" + bf "gopkg.in/russross/blackfriday.v2" +) + +type RuleKey int + +const ( + Color RuleKey = iota + BackgroundColor + Underline + Bold + Italic + CrossedOut + Faint + Conceal + Overlined + Inverse + Blink +) + +type TermRenderer struct { + style map[bf.NodeType]map[RuleKey]string +} + +func BaseString(node *bf.Node) string { + switch node.Type { + case bf.Document: + return "" + case bf.BlockQuote: + return fmt.Sprintf("```\n%s\n```", string(node.Literal)) + case bf.List: + return "" + case bf.Item: + return fmt.Sprintf("* %s", string(node.Literal)) + case bf.Paragraph: + if node.Prev != nil && node.Prev.Type != bf.Link { + return "\n" + } else { + return "" + } + case bf.Heading: + return fmt.Sprintf("%s ", strings.Repeat("#", node.HeadingData.Level)) + case bf.HorizontalRule: + return "---\n" + case bf.Emph: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Strong: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Del: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Link: + return fmt.Sprintf("[%s](%s)\n", string(node.LastChild.Literal), string(node.LinkData.Destination)) + case bf.Image: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Text: + if node.Parent != nil && node.Parent.Type != bf.Link { + return fmt.Sprintf("%s\n", string(node.Literal)) + } else { + return "" + } + case bf.HTMLBlock: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.CodeBlock: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Softbreak: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Hardbreak: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Code: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.HTMLSpan: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.Table: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.TableCell: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.TableHead: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.TableBody: + return fmt.Sprintf("%s\n", string(node.Literal)) + case bf.TableRow: + return fmt.Sprintf("%s\n", string(node.Literal)) + default: + return "" + } + +} + +func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus { + if !entering { + return bf.GoToNext + } + text := BaseString(node) + if text == "" { + return bf.GoToNext + } + rules := tr.style[node.Type] + if len(rules) == 0 { + fmt.Fprintf(w, "%s", text) + return bf.GoToNext + } + out := aurora.Reset(text) + if r, ok := rules[Color]; ok { + i, err := strconv.Atoi(r) + if err == nil && i >= 0 && i <= 255 { + out = out.Index(uint8(i)) + } + } + if r, ok := rules[BackgroundColor]; ok { + i, err := strconv.Atoi(r) + if err == nil && i >= 0 && i <= 255 { + out = out.Index(uint8(i)) + } + } + if r, ok := rules[Underline]; ok { + if r == "true" { + out = out.Underline() + } + } + if r, ok := rules[Bold]; ok { + if r == "true" { + out = out.Bold() + } + } + if r, ok := rules[Italic]; ok { + if r == "true" { + out = out.Italic() + } + } + if r, ok := rules[CrossedOut]; ok { + if r == "true" { + out = out.CrossedOut() + } + } + if r, ok := rules[Overlined]; ok { + if r == "true" { + out = out.Overlined() + } + } + if r, ok := rules[Inverse]; ok { + if r == "true" { + out = out.Reverse() + } + } + if r, ok := rules[Blink]; ok { + if r == "true" { + out = out.Blink() + } + } + w.Write([]byte(aurora.Sprintf("%s", out))) + return bf.GoToNext +} + +func (tr *TermRenderer) RenderHeader(w io.Writer, ast *bf.Node) { +} + +func (tr *TermRenderer) RenderFooter(w io.Writer, ast *bf.Node) { +}