Skip to content

Commit

Permalink
newpaint: fix add end special, better printing, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Feb 7, 2025
1 parent 663e864 commit adf1b5a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
4 changes: 3 additions & 1 deletion text/htmltext/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ func HTMLToRich(str []byte, sty *rich.Style, cssProps map[string]any) (rich.Text
curSp.AddRunes([]rune{'\n'})
nextIsParaStart = false
case "a", "q", "math", "sub", "sup": // important: any special must be ended!
curSp.EndSpecial()
nsp := rich.Text{}
nsp.EndSpecial()
spstack.Push(nsp)
}

if len(fstack) > 0 {
Expand Down
47 changes: 42 additions & 5 deletions text/htmltext/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,48 @@ func TestHTML(t *testing.T) {
tx, err := HTMLToRich([]byte(src), rich.NewStyle(), nil)
assert.NoError(t, err)

trg := `[]: The
[italic]: lazy
[]: fox typed in some
[1.50x bold]: familiar
[]: text
trg := `[]: "The "
[italic]: "lazy"
[]: " fox typed in some "
[1.50x bold]: "familiar"
[]: " text"
`
// fmt.Println(tx.String())
assert.Equal(t, trg, tx.String())
}

func TestLink(t *testing.T) {
src := `The <a href="https://example.com">link</a> and`
tx, err := HTMLToRich([]byte(src), rich.NewStyle(), nil)
assert.NoError(t, err)

trg := `[]: "The "
[link [https://example.com] underline fill-color]: "link"
[{End Special}]: ""
[]: " and"
`
// fmt.Println(tx.String())
// tx.DebugDump()

assert.Equal(t, trg, tx.String())
}

func TestDemo(t *testing.T) {
src := `A <b>demonstration</b> of the <i>various</i> features of the <a href="https://cogentcore.org/core">Cogent Core</a> 2D and 3D Go GUI <u>framework</u>`
tx, err := HTMLToRich([]byte(src), rich.NewStyle(), nil)
assert.NoError(t, err)

trg := `[]: "A "
[bold]: "demonstration"
[]: " of the "
[italic]: "various"
[]: " features of the "
[link [https://cogentcore.org/core] underline fill-color]: "Cogent Core"
[{End Special}]: ""
[]: " 2D and 3D Go GUI "
[underline]: "framework"
[]: ""
`

assert.Equal(t, trg, tx.String())
}
6 changes: 6 additions & 0 deletions text/rich/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ func (s *Style) SetBackground(clr color.Color) *Style {

func (s *Style) String() string {
str := ""
if s.Special == End {
return "{End Special}"
}
if s.Size != 1 {
str += fmt.Sprintf("%5.2fx ", s.Size)
}
Expand All @@ -418,6 +421,9 @@ func (s *Style) String() string {
}
if s.Special != Nothing {
str += s.Special.String() + " "
if s.Special == Link {
str += "[" + s.URL + "] "
}
}
for d := Underline; d <= Background; d++ {
if s.Decoration.HasFlag(d) {
Expand Down
16 changes: 14 additions & 2 deletions text/rich/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package rich

import "slices"
import (
"fmt"
"slices"
)

// Text is the basic rich text representation, with spans of []rune unicode characters
// that share a common set of text styling properties, which are represented
Expand Down Expand Up @@ -255,7 +258,7 @@ func (tx Text) String() string {
s := &Style{}
ss := s.FromRunes(rs)
sstr := s.String()
str += "[" + sstr + "]: " + string(ss) + "\n"
str += "[" + sstr + "]: \"" + string(ss) + "\"\n"
}
return str
}
Expand All @@ -268,3 +271,12 @@ func Join(txts ...Text) Text {
}
return nt
}

func (tx Text) DebugDump() {
for i := range tx {
s, r := tx.Span(i)
fmt.Println(i, len(tx[i]), tx[i])
fmt.Printf("style: %#v\n", s)
fmt.Printf("chars: %q\n", string(r))
}
}

0 comments on commit adf1b5a

Please sign in to comment.