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

@ -120,12 +120,43 @@ func ExampleBlocks_verbatim() {
// -------------------------- 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() {
// Emph observes the term escapes
// (see package documentation for more)
term.Italic = `<italic>`
term.Bold = `<bold>`
term.BoldItalic = `<bolditalic>`
term.Under = `<under>`

Loading…
Cancel
Save