Update Emph scanner

pull/97/head
rwxrob 2 years ago
parent e941cc7a96
commit a50f023f74
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strconv" "strconv"
"unicode"
"github.com/rwxrob/scan" "github.com/rwxrob/scan"
"github.com/rwxrob/term" "github.com/rwxrob/term"
@ -180,88 +179,74 @@ MAIN:
// <under> (keeping brackets) // <under> (keeping brackets)
// //
// See Mark for block formatting and term for terminal rendering. // See Mark for block formatting and term for terminal rendering.
func Emph(buf string) string { func Emph[T string | []byte | []rune](buf T) string {
var nbuf []rune var nbuf []rune
var opentok, closetok bool
var otok, ctok string
prev := ' '
for i := 0; i < len([]rune(buf)); i++ { s := scan.R{Buf: []byte(string(buf))}
r := []rune(buf)[i]
if r == '<' { for s.Scan() {
// <under>
if s.Rune == '<' {
nbuf = append(nbuf, '<') nbuf = append(nbuf, '<')
nbuf = append(nbuf, []rune(term.Under)...) nbuf = append(nbuf, []rune(term.Under)...)
for { for s.Scan() {
i++ if s.Rune == '>' {
r = rune(buf[i]) nbuf = append(nbuf, []rune(term.Reset)...)
if r == '>' { nbuf = append(nbuf, '>')
i++
break break
} }
nbuf = append(nbuf, r) nbuf = append(nbuf, s.Rune)
} }
nbuf = append(nbuf, []rune(term.Reset)...)
nbuf = append(nbuf, '>')
i--
continue continue
} }
if r != '*' { // ***BoldItalic***
if s.Rune == '*' && s.Peek("**") {
if opentok { s.Pos += 2
tokval := " " nbuf = append(nbuf, []rune(term.BoldItalic)...)
if !unicode.IsSpace(r) { for s.Scan() {
switch otok { if s.Rune == '*' && s.Peek("**") {
case "*": s.Pos += 2
tokval = term.Italic nbuf = append(nbuf, []rune(term.Reset)...)
case "**": break
tokval = term.Bold
case "***":
tokval = term.BoldItalic
}
} else {
tokval = otok
} }
nbuf = append(nbuf, []rune(tokval)...) nbuf = append(nbuf, s.Rune)
opentok = false
otok = ""
}
if closetok {
nbuf = append(nbuf, []rune(term.Reset)...) // practical, not perfect
ctok = ""
closetok = false
} }
prev = r
nbuf = append(nbuf, r)
continue continue
} }
// everything else for '*' // **Bold**
if unicode.IsSpace(prev) || opentok { if s.Rune == '*' && s.Peek("*") {
opentok = true s.Pos++
otok += string(r) nbuf = append(nbuf, []rune(term.Bold)...)
for s.Scan() {
if s.Rune == '*' && s.Peek("*") {
s.Pos++
nbuf = append(nbuf, []rune(term.Reset)...)
break
}
nbuf = append(nbuf, s.Rune)
}
continue continue
} }
// only closer conditions remain // *Italic*
if !unicode.IsSpace(prev) { if s.Rune == '*' {
closetok = true nbuf = append(nbuf, []rune(term.Italic)...)
ctok += string(r) for s.Scan() {
if s.Rune == '*' {
nbuf = append(nbuf, []rune(term.Reset)...)
break
}
nbuf = append(nbuf, s.Rune)
}
continue continue
} }
// nothing special nbuf = append(nbuf, s.Rune)
closetok = false
nbuf = append(nbuf, r)
}
// for tokens at the end of a block } // end main scan loop
if closetok {
nbuf = append(nbuf, []rune(term.Reset)...)
}
return string(nbuf) return string(nbuf)
} }

@ -120,12 +120,43 @@ func ExampleBlocks_verbatim() {
// -------------------------- main BonzaiMark ------------------------- // -------------------------- main BonzaiMark -------------------------
func ExampleEmph_under() {
term.Under = `<under>`
term.Reset = `<reset>`
fmt.Println(Z.Emph("<UNDER>"))
// Output:
// <<under>UNDER<reset>>
}
func ExampleEmph_boldItalic() {
term.BoldItalic = `<bolditalic>`
term.Reset = `<reset>`
fmt.Println(Z.Emph("***BoldItalic***"))
// Output:
// <bolditalic>BoldItalic<reset>
}
func ExampleEmph_bold() {
term.Bold = `<bold>`
term.Reset = `<reset>`
fmt.Println(Z.Emph("**Bold**"))
// Output:
// <bold>Bold<reset>
}
func ExampleEmph_italic() {
term.Italic = `<italic>`
term.Reset = `<reset>`
fmt.Println(Z.Emph("*Italic*"))
// Output:
// <italic>Italic<reset>
}
func ExampleEmph_basics() { func ExampleEmph_basics() {
// Emph observes the term escapes // Emph observes the term escapes
// (see package documentation for more) // (see package documentation for more)
term.Italic = `<italic>`
term.Bold = `<bold>` term.Bold = `<bold>`
term.BoldItalic = `<bolditalic>` term.BoldItalic = `<bolditalic>`
term.Under = `<under>` term.Under = `<under>`

Loading…
Cancel
Save