grim/xtrac

committing some old stuff
draft default tip
21 months ago, Gary Kramlich
2bd0417025e6
Parents 239059e5372a
Children
committing some old stuff
  • +13 -0
    blockquote.go
  • +27 -0
    blockquote_test.go
  • +19 -0
    break.go
  • +53 -0
    break_test.go
  • +2 -0
    lexer.go
  • +9 -0
    statefunc.go
  • +13 -0
    util.go
  • +28 -0
    util_test.go
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/blockquote.go Fri Aug 05 01:49:11 2022 -0500
    @@ -0,0 +1,13 @@
    +package xtrac
    +
    +import (
    + "strings"
    +)
    +
    +func lexBlockQuote(l *lexer) stateFunc {
    + // on our first call we need to move past the opening double space
    + l.pos += len(tokenBlockQuote)
    + l.ignore()
    +
    + return lexLine
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/blockquote_test.go Fri Aug 05 01:49:11 2022 -0500
    @@ -0,0 +1,27 @@
    +package xtrac
    +
    +import (
    + "testing"
    +)
    +
    +func TestBlockQuote(t *testing.T) {
    + input := " block quote"
    +
    + items := []item{
    + item{typ: itemBlockQuote, val: "block quote"},
    + item{typ: itemEOF},
    + }
    +
    + tokenTest(t, input, items)
    +}
    +
    +func TestBlockQuoteMultipleLines(t *testing.T) {
    + input := " line 1\r\n line2"
    +
    + items := []item{
    + item{typ: itemBlockQuote, val: "line1 line2"},
    + item{typ: itemEOF},
    + }
    +
    + tokenTest(t, input, items)
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/break.go Fri Aug 05 01:49:11 2022 -0500
    @@ -0,0 +1,19 @@
    +package xtrac
    +
    +func lexBreak(l *lexer) stateFunc {
    + l.pos += len(tokenBreakMacro)
    + l.ignore()
    +
    + l.emit(itemBreak)
    +
    + return lexText
    +}
    +
    +func lexBreakSlash(l *lexer) stateFunc {
    + l.pos += len(tokenBreakCreole)
    + l.ignore()
    +
    + l.emit(itemBreak)
    +
    + return lexText
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/break_test.go Fri Aug 05 01:49:11 2022 -0500
    @@ -0,0 +1,53 @@
    +package xtrac
    +
    +import (
    + "testing"
    +)
    +
    +func TestBreakMacro(t *testing.T) {
    + inputs := []string{
    + "Line 1[[BR]]Line 2",
    + "Line 1[[Br]]Line 2",
    + "Line 1[[bR]]Line 2",
    + "Line 1[[br]]Line 2",
    + }
    +
    + items := []item{
    + item{typ: itemText, val: "Line 1"},
    + item{typ: itemBreak},
    + item{typ: itemText, val: "Line 2"},
    + item{typ: itemEOF},
    + }
    +
    + for _, input := range inputs {
    + tokenTest(t, input, items)
    + }
    +}
    +
    +func TestBreakCreole(t *testing.T) {
    + input := "Line 1 \\\\ Line 2"
    +
    + items := []item{
    + item{typ: itemText, val: "Line 1 "},
    + item{typ: itemBreak},
    + item{typ: itemText, val: " Line 2"},
    + item{typ: itemEOF},
    + }
    +
    + tokenTest(t, input, items)
    +}
    +
    +func TestBreakMixed(t *testing.T) {
    + input := "Line [[br]] break Line \\\\ break"
    +
    + items := []item{
    + item{typ: itemText, val: "Line "},
    + item{typ: itemBreak},
    + item{typ: itemText, val: " break Line "},
    + item{typ: itemBreak},
    + item{typ: itemText, val: " break"},
    + item{typ: itemEOF},
    + }
    +
    + tokenTest(t, input, items)
    +}
    --- a/lexer.go Mon Aug 31 16:51:05 2020 -0500
    +++ b/lexer.go Fri Aug 05 01:49:11 2022 -0500
    @@ -20,6 +20,8 @@
    width int
    state stateFunc
    items chan item
    +
    + quoteDepth int
    }
    func (l *lexer) next() (r rune) {
    --- a/statefunc.go Mon Aug 31 16:51:05 2020 -0500
    +++ b/statefunc.go Fri Aug 05 01:49:11 2022 -0500
    @@ -29,6 +29,9 @@
    tokenHeader5Close = " ====="
    tokenHeader6Open = "====== "
    tokenHeader6Close = " ======"
    + tokenBreakMacro = "[[br]]"
    + tokenBreakCreole = "\\\\"
    + tokebBlockQuote = " "
    )
    func lexEof(l *lexer) stateFunc {
    @@ -54,6 +57,8 @@
    return lexHorizontalRule
    } else if strings.HasPrefix(start, tokenCommentOpen) {
    return lexComment
    + } else if strings.HasPrefix(start, tokenBlockQuote) {
    + return lexBlockQuote
    }
    return lexText
    @@ -67,6 +72,10 @@
    if strings.HasPrefix(start, tokenMonospaceBraceOpen) {
    nextState = lexMonospaceBrace
    + } else if hasPrefix(start, tokenBreakMacro) {
    + nextState = lexBreak
    + } else if strings.HasPrefix(start, tokenBreakCreole) {
    + nextState = lexBreakSlash
    }
    if nextState == nil {
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/util.go Fri Aug 05 01:49:11 2022 -0500
    @@ -0,0 +1,13 @@
    +package xtrac
    +
    +import (
    + "strings"
    +)
    +
    +func hasPrefix(str, prefix string) bool {
    + if len(prefix) > len(str) {
    + return false
    + }
    +
    + return strings.HasPrefix(strings.ToLower(str[:len(prefix)]), prefix)
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/util_test.go Fri Aug 05 01:49:11 2022 -0500
    @@ -0,0 +1,28 @@
    +package xtrac
    +
    +import (
    + "testing"
    +
    + "github.com/stretchr/testify/assert"
    +)
    +
    +func TestHasPrefixSimple(t *testing.T) {
    + tests := map[string]bool{
    + "foo": true,
    + "Foo": true,
    + "FOo": true,
    + "FOO": true,
    + "FoO": true,
    + "fOO": true,
    + "foO": true,
    + "fOo": true,
    + "bar": false,
    + "Bar": false,
    + "fo": false,
    + "Football": true,
    + }
    +
    + for str, exp := range tests {
    + assert.Equal(t, hasPrefix(str, "foo"), exp)
    + }
    +}