From d52def14ebdf1881075db3c61f2ff48180363557 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 9 Apr 2019 21:30:08 +1000 Subject: [PATCH] Added simple quotes and headers to Plugin Markdown. Added 11 tests for Plugin Markdown. --- plugin_markdown.go | 193 ++++++++++++++++++++++++++++++++------------- plugin_test.go | 24 ++++++ 2 files changed, 160 insertions(+), 57 deletions(-) diff --git a/plugin_markdown.go b/plugin_markdown.go index cd1d0184..9f069c8f 100644 --- a/plugin_markdown.go +++ b/plugin_markdown.go @@ -17,6 +17,10 @@ var markdownUnderlineTagOpen []byte var markdownUnderlineTagClose []byte var markdownStrikeTagOpen []byte var markdownStrikeTagClose []byte +var markdownQuoteTagOpen []byte +var markdownQuoteTagClose []byte +var markdownH1TagOpen []byte +var markdownH1TagClose []byte func init() { common.Plugins.Add(&common.Plugin{UName: "markdown", Name: "Markdown", Author: "Azareal", URL: "https://github.com/Azareal", Init: initMarkdown, Deactivate: deactivateMarkdown}) @@ -35,6 +39,10 @@ func initMarkdown(plugin *common.Plugin) error { markdownUnderlineTagClose = []byte("") markdownStrikeTagOpen = []byte("") markdownStrikeTagClose = []byte("") + markdownQuoteTagOpen = []byte("
") + markdownQuoteTagClose = []byte("
") + markdownH1TagOpen = []byte("

") + markdownH1TagClose = []byte("

") return nil } @@ -60,67 +68,89 @@ func _markdownParse(msg string, n int) string { var outbytes []byte var lastElement int + var breaking = false common.DebugLogf("Initial Message: %+v\n", strings.Replace(msg, "\r", "\\r", -1)) for index := 0; index < len(msg); index++ { + var simpleMatch = func(char byte, o []byte, c []byte) { + var startIndex = index + if (index + 1) >= len(msg) { + breaking = true + return + } + + index++ + index = markdownSkipUntilChar(msg, index, char) + if (index-(startIndex+1)) < 1 || index >= len(msg) { + breaking = true + return + } + + sIndex := startIndex + 1 + lIndex := index + index++ + + outbytes = append(outbytes, msg[lastElement:startIndex]...) + outbytes = append(outbytes, o...) + // TODO: Implement this without as many type conversions + outbytes = append(outbytes, []byte(_markdownParse(msg[sIndex:lIndex], n+1))...) + outbytes = append(outbytes, c...) + + lastElement = index + index-- + } + + var startLine = func() { + var startIndex = index + if (index + 1) >= len(msg) /*|| (index + 2) >= len(msg)*/ { + breaking = true + return + } + index++ + + index = markdownSkipUntilNotChar(msg, index, 32) + if (index + 1) >= len(msg) { + breaking = true + return + } + //index++ + + index = markdownSkipUntilStrongSpace(msg, index) + sIndex := startIndex + 1 + lIndex := index + index++ + + outbytes = append(outbytes, msg[lastElement:startIndex]...) + outbytes = append(outbytes, markdownH1TagOpen...) + // TODO: Implement this without as many type conversions + //fmt.Println("msg[sIndex:lIndex]:", string(msg[sIndex:lIndex])) + // TODO: Quick hack to eliminate trailing spaces... + outbytes = append(outbytes, []byte(strings.TrimSpace(_markdownParse(msg[sIndex:lIndex], n+1)))...) + outbytes = append(outbytes, markdownH1TagClose...) + + lastElement = index + index-- + } + switch msg[index] { // TODO: Do something slightly less hacky for skipping URLs case '/': if len(msg) > (index+2) && msg[index+1] == '/' { for ; index < len(msg) && msg[index] != ' '; index++ { - } index-- continue } case '_': - var startIndex = index - if (index + 1) >= len(msg) { + simpleMatch('_', markdownUnderlineTagOpen, markdownUnderlineTagClose) + if breaking { break } - - index++ - index = markdownSkipUntilChar(msg, index, '_') - if (index-(startIndex+1)) < 1 || index >= len(msg) { - break - } - - sIndex := startIndex + 1 - lIndex := index - index++ - - outbytes = append(outbytes, msg[lastElement:startIndex]...) - outbytes = append(outbytes, markdownUnderlineTagOpen...) - // TODO: Implement this without as many type conversions - outbytes = append(outbytes, []byte(_markdownParse(msg[sIndex:lIndex], n+1))...) - outbytes = append(outbytes, markdownUnderlineTagClose...) - - lastElement = index - index-- case '~': - var startIndex = index - if (index + 1) >= len(msg) { + simpleMatch('~', markdownStrikeTagOpen, markdownStrikeTagClose) + if breaking { break } - - index++ - index = markdownSkipUntilChar(msg, index, '~') - if (index-(startIndex+1)) < 1 || index >= len(msg) { - break - } - - sIndex := startIndex + 1 - lIndex := index - index++ - - outbytes = append(outbytes, msg[lastElement:startIndex]...) - outbytes = append(outbytes, markdownStrikeTagOpen...) - // TODO: Implement this without as many type conversions - outbytes = append(outbytes, []byte(_markdownParse(msg[sIndex:lIndex], n+1))...) - outbytes = append(outbytes, markdownStrikeTagClose...) - - lastElement = index - index-- case '*': var startIndex = index var italic = true @@ -148,28 +178,30 @@ func _markdownParse(msg string, n int) string { break } + var preBreak = func() { + outbytes = append(outbytes, msg[lastElement:startIndex]...) + lastElement = startIndex + } + sIndex := startIndex lIndex := index if bold && italic { if (index + 3) >= len(msg) { - outbytes = append(outbytes, msg[lastElement:startIndex]...) - lastElement = startIndex + preBreak() break } index += 3 sIndex += 3 } else if bold { if (index + 2) >= len(msg) { - outbytes = append(outbytes, msg[lastElement:startIndex]...) - lastElement = startIndex + preBreak() break } index += 2 sIndex += 2 } else { if (index + 1) >= len(msg) { - outbytes = append(outbytes, msg[lastElement:startIndex]...) - lastElement = startIndex + preBreak() break } index++ @@ -177,17 +209,13 @@ func _markdownParse(msg string, n int) string { } if lIndex <= sIndex { - outbytes = append(outbytes, msg[lastElement:startIndex]...) - lastElement = startIndex + preBreak() break } - if sIndex < 0 || lIndex < 0 { - outbytes = append(outbytes, msg[lastElement:startIndex]...) - lastElement = startIndex + preBreak() break } - outbytes = append(outbytes, msg[lastElement:startIndex]...) if bold { @@ -217,8 +245,33 @@ func _markdownParse(msg string, n int) string { lastElement = index } } - //case '`': - //case 10: // newline + // TODO: Add a inline quote variant + case '`': + simpleMatch('`', markdownQuoteTagOpen, markdownQuoteTagClose) + if breaking { + break + } + case 10: // newline + if (index + 1) >= len(msg) { + break + } + index++ + + if msg[index] != '#' { + continue + } + startLine() + if breaking { + break + } + case '#': + if index != 0 { + continue + } + startLine() + if breaking { + break + } } } @@ -254,6 +307,32 @@ func markdownSkipUntilChar(data string, index int, char byte) int { return index } +func markdownSkipUntilNotChar(data string, index int, char byte) int { + for ; index < len(data); index++ { + if data[index] != char { + break + } + } + return index +} + +func markdownSkipUntilStrongSpace(data string, index int) int { + var inSpace = false + for ; index < len(data); index++ { + if inSpace && data[index] == 32 { + index-- + break + } else if data[index] == 32 { + inSpace = true + } else if data[index] < 32 { + break + } else { + inSpace = false + } + } + return index +} + func markdownSkipUntilAsterisk(data string, index int) int { SwitchLoop: for ; index < len(data); index++ { diff --git a/plugin_test.go b/plugin_test.go index de13d426..44aef066 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -222,6 +222,7 @@ func TestMarkdownRender(t *testing.T) { var res string var msgList = &MEPairList{nil} + var msgList2 = &MEPairList{nil} // TODO: Fix more of these odd cases msgList.Add("", "") msgList.Add(" ", " ") @@ -230,6 +231,7 @@ func TestMarkdownRender(t *testing.T) { msgList.Add("\t", "\t") msgList.Add("\n", "\n") msgList.Add("*", "*") + msgList.Add("`", "`") //msgList.Add("**", "") msgList.Add("h", "h") msgList.Add("hi", "hi") @@ -241,6 +243,18 @@ func TestMarkdownRender(t *testing.T) { msgList.Add("*hi*", "hi") msgList.Add("~h~", "h") msgList.Add("~hi~", "hi") + msgList.Add("`hi`", "
hi
") + // TODO: Hide the backslash after escaping the item + // TODO: Doesn't behave consistently with d in-front of it + msgList2.Add("\\`hi`", "\\`hi`") + msgList2.Add("#", "#") + msgList2.Add("#h", "

h

") + msgList2.Add("#hi", "

hi

") + msgList.Add("\n#", "\n#") + msgList.Add("\n#h", "\n

h

") + msgList.Add("\n#hi", "\n

hi

") + msgList.Add("\n#h\n", "\n

h

") + msgList.Add("\n#hi\n", "\n

hi

") msgList.Add("*hi**", "hi*") msgList.Add("**hi***", "hi*") //msgList.Add("**hi*", "*hi") @@ -294,6 +308,16 @@ func TestMarkdownRender(t *testing.T) { } } + for _, item := range msgList2.Items { + res = markdownParse(item.Msg) + if res != item.Expects { + t.Error("Testing string '" + item.Msg + "'") + t.Error("Bad output:", "'"+res+"'") + //t.Error("Ouput in bytes:", []byte(res)) + t.Error("Expected:", "'"+item.Expects+"'") + } + } + /*for _, item := range msgList.Items { res = markdownParse("\n" + item.Msg) if res != "\n"+item.Expects {