~samwhited/xmpp

ref: de40e6f9107a5d25ae8c8edad9289fd0c4b12e2f xmpp/styling/example_test.go -rw-r--r-- 1.7 KiB
de40e6f9Sam Whited roster: add type to IQ response in test 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2020 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package styling_test

import (
	"fmt"
	"html"
	"io"
	"strings"

	"mellium.im/xmpp/styling"
)

func Example_html() {
	r := strings.NewReader(`The full title is
_Twelfth Night, or What You Will_
but *most* people shorten it.`)
	d := styling.NewDecoder(r)

	var out strings.Builder
	out.WriteString("<!doctype HTML>\n")
	for d.Next() {
		tok := d.Token()

		switch {
		case tok.Mask&styling.SpanEmphStart == styling.SpanEmphStart:
			out.WriteString("<em><code>")
			out.Write(tok.Data)
			out.WriteString("</code>")
		case tok.Mask&styling.SpanStrongStart == styling.SpanStrongStart:
			out.WriteString("<strong><code>")
			out.Write(tok.Data)
			out.WriteString("</code>")
		case tok.Mask&styling.SpanEmphEnd == styling.SpanEmphEnd:
			out.WriteString("<code>")
			out.Write(tok.Data)
			out.WriteString("</code></em>")
		case tok.Mask&styling.SpanStrongEnd == styling.SpanStrongEnd:
			out.WriteString("<code>")
			out.Write(tok.Data)
			out.WriteString("</code></strong>")
			// TODO: no other styles implemented to keep the example short.
		default:
			out.WriteString(html.EscapeString(string(tok.Data)))
		}
	}

	err := d.Err()
	if err != nil && err != io.EOF {
		out.WriteString("<mark>")
		out.WriteString(html.EscapeString(fmt.Sprintf("Error encountered while parsing tokens: %v", err)))
		out.WriteString("</mark>")
	}
	fmt.Println(out.String())

	// Output:
	// <!doctype HTML>
	// The full title is
	// <em><code>_</code>Twelfth Night, or What You Will<code>_</code></em>
	// but <strong><code>*</code>most<code>*</code></strong> people shorten it.
}