Fix Blocks and Mark, plus new scan.R

pull/66/head
rwxrob 2 years ago
parent 199a74b181
commit 40b96f4174
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -73,33 +73,35 @@ func Blocks(in string) []*Block {
MAIN:
for s.Scan() {
switch s.Rune {
case '*': // bulleted list
if s.Is(" ") {
m := s.Pos - 1
for s.Scan() {
if s.Is("\n\n") {
blocks = append(blocks, &Block{Bulleted, s.Buf[m:s.Pos]})
s.Pos += 2
continue MAIN
}
if s.Rune == '*' { // bulleted list
if !s.Peek(" ") {
goto PARA
}
m := s.Pos - 1
for s.Scan() {
if s.Peek("\n\n") {
blocks = append(blocks, &Block{Bulleted, s.Buf[m:s.Pos]})
s.Pos += 2
continue MAIN
}
}
}
case '1': // numbered list
if s.Is(". ") {
m := s.Pos - 1
for s.Scan() {
if s.Is("\n\n") {
blocks = append(blocks, &Block{Numbered, s.Buf[m:s.Pos]})
s.Pos += 2
continue MAIN
}
if s.Rune == '1' { // numbered list
if !s.Peek(". ") {
goto PARA
}
m := s.Pos - 1
for s.Scan() {
if s.Peek("\n\n") {
blocks = append(blocks, &Block{Numbered, s.Buf[m:s.Pos]})
s.Pos += 2
continue MAIN
}
}
}
case ' ': // verbatim
if s.Rune == ' ' { // verbatim
s.Pos -= 1
ln := s.Match(verbpre)
s.Pos++
@ -116,7 +118,7 @@ MAIN:
if s.Rune == '\n' {
// add in indented lines
if s.Is(string(pre)) {
if s.Peek(string(pre)) {
block = append(block, '\n')
s.Pos += len(pre)
continue
@ -130,28 +132,38 @@ MAIN:
block = append(block, []byte(string(s.Rune))...)
}
case '\n', '\r', '\t': // inconsequential white space
}
if s.Rune == '\n' || s.Rune == '\r' || s.Rune == '\t' {
continue
}
default: // paragraph
PARA:
{
var block []byte
block = append(block, []byte(string(s.Rune))...)
for s.Scan() {
switch s.Rune {
case '\n', '\r':
block = append(block, ' ')
default:
block = append(block, []byte(string(s.Rune))...)
}
if s.Is("\n\n") {
if s.Peek("\n\n") {
blocks = append(blocks, &Block{Paragraph, block})
s.Scan()
s.Scan()
continue MAIN
}
if s.Rune == '\n' || s.Rune == '\r' {
block = append(block, ' ')
continue
}
block = append(block, []byte(string(s.Rune))...)
}
}
if len(block) > 0 {
blocks = append(blocks, &Block{Paragraph, block})
}
} // PARA
}
return blocks
@ -265,7 +277,8 @@ func InWrap(in string) string {
}
// Mark parses the input as a string of BonzaiMark, multiple blocks with
// optional emphasis (see Blocks and Emph).
// optional emphasis (see Blocks and Emph) and applies IndentBy and
// Columns wrapping to it.
func Mark(in string) string {
if in == "" {
return ""

@ -7,6 +7,14 @@ import (
"github.com/rwxrob/term"
)
func init() {
term.Italic = `<italic>`
term.Bold = `<bold>`
term.BoldItalic = `<bolditalic>`
term.Under = `<under>`
term.Reset = `<reset>`
}
func ExampleLines() {
fmt.Printf("%q\n", Z.Lines("line one\nline two"))
// Output:
@ -24,12 +32,17 @@ func ExampleBlocks_bulleted() {
* another block
* here
*boldnotbullet*
`
fmt.Printf("%q\n", Z.Blocks(in)[1])
blocks := Z.Blocks(in)
fmt.Printf("%v %q\n", blocks[1].T, blocks[1])
fmt.Printf("%v %q\n", blocks[2].T, blocks[2])
//Output:
// "* another block\n* here"
// 3 "* another block\n* here"
// 1 "*boldnotbullet*"
}
func ExampleBlocks_numbered() {
@ -163,6 +176,15 @@ func ExampleInWrap() {
// " some\n that\n is\n indented\n"
}
func ExampleMark_simple() {
fmt.Print(Z.Mark(`**foo**`))
//Output:
// <bold>foo<reset>
}
func ExampleMark() {
in := `

Loading…
Cancel
Save