Added simple quotes and headers to Plugin Markdown.

Added 11 tests for Plugin Markdown.
This commit is contained in:
Azareal 2019-04-09 21:30:08 +10:00
parent 733a802357
commit d52def14eb
2 changed files with 160 additions and 57 deletions

View File

@ -17,6 +17,10 @@ var markdownUnderlineTagOpen []byte
var markdownUnderlineTagClose []byte var markdownUnderlineTagClose []byte
var markdownStrikeTagOpen []byte var markdownStrikeTagOpen []byte
var markdownStrikeTagClose []byte var markdownStrikeTagClose []byte
var markdownQuoteTagOpen []byte
var markdownQuoteTagClose []byte
var markdownH1TagOpen []byte
var markdownH1TagClose []byte
func init() { func init() {
common.Plugins.Add(&common.Plugin{UName: "markdown", Name: "Markdown", Author: "Azareal", URL: "https://github.com/Azareal", Init: initMarkdown, Deactivate: deactivateMarkdown}) 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("</u>") markdownUnderlineTagClose = []byte("</u>")
markdownStrikeTagOpen = []byte("<s>") markdownStrikeTagOpen = []byte("<s>")
markdownStrikeTagClose = []byte("</s>") markdownStrikeTagClose = []byte("</s>")
markdownQuoteTagOpen = []byte("<blockquote>")
markdownQuoteTagClose = []byte("</blockquote>")
markdownH1TagOpen = []byte("<h2>")
markdownH1TagClose = []byte("</h2>")
return nil return nil
} }
@ -60,67 +68,89 @@ func _markdownParse(msg string, n int) string {
var outbytes []byte var outbytes []byte
var lastElement int var lastElement int
var breaking = false
common.DebugLogf("Initial Message: %+v\n", strings.Replace(msg, "\r", "\\r", -1)) common.DebugLogf("Initial Message: %+v\n", strings.Replace(msg, "\r", "\\r", -1))
for index := 0; index < len(msg); index++ { 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] { switch msg[index] {
// TODO: Do something slightly less hacky for skipping URLs // TODO: Do something slightly less hacky for skipping URLs
case '/': case '/':
if len(msg) > (index+2) && msg[index+1] == '/' { if len(msg) > (index+2) && msg[index+1] == '/' {
for ; index < len(msg) && msg[index] != ' '; index++ { for ; index < len(msg) && msg[index] != ' '; index++ {
} }
index-- index--
continue continue
} }
case '_': case '_':
var startIndex = index simpleMatch('_', markdownUnderlineTagOpen, markdownUnderlineTagClose)
if (index + 1) >= len(msg) { if breaking {
break 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 '~': case '~':
var startIndex = index simpleMatch('~', markdownStrikeTagOpen, markdownStrikeTagClose)
if (index + 1) >= len(msg) { if breaking {
break 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 '*': case '*':
var startIndex = index var startIndex = index
var italic = true var italic = true
@ -148,28 +178,30 @@ func _markdownParse(msg string, n int) string {
break break
} }
var preBreak = func() {
outbytes = append(outbytes, msg[lastElement:startIndex]...)
lastElement = startIndex
}
sIndex := startIndex sIndex := startIndex
lIndex := index lIndex := index
if bold && italic { if bold && italic {
if (index + 3) >= len(msg) { if (index + 3) >= len(msg) {
outbytes = append(outbytes, msg[lastElement:startIndex]...) preBreak()
lastElement = startIndex
break break
} }
index += 3 index += 3
sIndex += 3 sIndex += 3
} else if bold { } else if bold {
if (index + 2) >= len(msg) { if (index + 2) >= len(msg) {
outbytes = append(outbytes, msg[lastElement:startIndex]...) preBreak()
lastElement = startIndex
break break
} }
index += 2 index += 2
sIndex += 2 sIndex += 2
} else { } else {
if (index + 1) >= len(msg) { if (index + 1) >= len(msg) {
outbytes = append(outbytes, msg[lastElement:startIndex]...) preBreak()
lastElement = startIndex
break break
} }
index++ index++
@ -177,17 +209,13 @@ func _markdownParse(msg string, n int) string {
} }
if lIndex <= sIndex { if lIndex <= sIndex {
outbytes = append(outbytes, msg[lastElement:startIndex]...) preBreak()
lastElement = startIndex
break break
} }
if sIndex < 0 || lIndex < 0 { if sIndex < 0 || lIndex < 0 {
outbytes = append(outbytes, msg[lastElement:startIndex]...) preBreak()
lastElement = startIndex
break break
} }
outbytes = append(outbytes, msg[lastElement:startIndex]...) outbytes = append(outbytes, msg[lastElement:startIndex]...)
if bold { if bold {
@ -217,8 +245,33 @@ func _markdownParse(msg string, n int) string {
lastElement = index lastElement = index
} }
} }
//case '`': // TODO: Add a inline quote variant
//case 10: // newline 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 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 { func markdownSkipUntilAsterisk(data string, index int) int {
SwitchLoop: SwitchLoop:
for ; index < len(data); index++ { for ; index < len(data); index++ {

View File

@ -222,6 +222,7 @@ func TestMarkdownRender(t *testing.T) {
var res string var res string
var msgList = &MEPairList{nil} var msgList = &MEPairList{nil}
var msgList2 = &MEPairList{nil}
// TODO: Fix more of these odd cases // TODO: Fix more of these odd cases
msgList.Add("", "") msgList.Add("", "")
msgList.Add(" ", " ") msgList.Add(" ", " ")
@ -230,6 +231,7 @@ func TestMarkdownRender(t *testing.T) {
msgList.Add("\t", "\t") msgList.Add("\t", "\t")
msgList.Add("\n", "\n") msgList.Add("\n", "\n")
msgList.Add("*", "*") msgList.Add("*", "*")
msgList.Add("`", "`")
//msgList.Add("**", "<i></i>") //msgList.Add("**", "<i></i>")
msgList.Add("h", "h") msgList.Add("h", "h")
msgList.Add("hi", "hi") msgList.Add("hi", "hi")
@ -241,6 +243,18 @@ func TestMarkdownRender(t *testing.T) {
msgList.Add("*hi*", "<i>hi</i>") msgList.Add("*hi*", "<i>hi</i>")
msgList.Add("~h~", "<s>h</s>") msgList.Add("~h~", "<s>h</s>")
msgList.Add("~hi~", "<s>hi</s>") msgList.Add("~hi~", "<s>hi</s>")
msgList.Add("`hi`", "<blockquote>hi</blockquote>")
// 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", "<h2>h</h2>")
msgList2.Add("#hi", "<h2>hi</h2>")
msgList.Add("\n#", "\n#")
msgList.Add("\n#h", "\n<h2>h</h2>")
msgList.Add("\n#hi", "\n<h2>hi</h2>")
msgList.Add("\n#h\n", "\n<h2>h</h2>")
msgList.Add("\n#hi\n", "\n<h2>hi</h2>")
msgList.Add("*hi**", "<i>hi</i>*") msgList.Add("*hi**", "<i>hi</i>*")
msgList.Add("**hi***", "<b>hi</b>*") msgList.Add("**hi***", "<b>hi</b>*")
//msgList.Add("**hi*", "*<i>hi</i>") //msgList.Add("**hi*", "*<i>hi</i>")
@ -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 { /*for _, item := range msgList.Items {
res = markdownParse("\n" + item.Msg) res = markdownParse("\n" + item.Msg)
if res != "\n"+item.Expects { if res != "\n"+item.Expects {