-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.go
110 lines (95 loc) · 2.82 KB
/
chess.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package chess
import (
"fmt"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"math/rand"
"strings"
)
type ChessHTMLRenderer struct {
html.Config
}
func NewChessHTMLRenderer(opts ...html.Option) renderer.NodeRenderer {
r := &ChessHTMLRenderer{
Config: html.NewConfig(),
}
for _, opt := range opts {
opt.SetHTMLOption(&r.Config)
}
return r
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func renderCaissaPGN(w util.BufWriter, source []byte, lines *text.Segments) {
id := RandStringRunes(6)
vif := fmt.Sprintf("cwvif_%s", id)
vip := fmt.Sprintf("cwvip_%s", id)
vfm := fmt.Sprintf("cwvfm_%s", id)
vpg := fmt.Sprintf("cwvpg_%s", id)
header := []string{
"<div style='width:100%;height:auto;aspect-ratio:1.5;border:1px solid black'>",
"<iframe name=", vif, " style='width:100%;height:100%;border:0'></iframe>",
"<form id=", vfm, " method=post action='https://caissa.com/bin/gameview' target=", vif, ">",
"<input id=", vip, " name=edat type=hidden>",
"</form>",
"</div><div id=", vpg, " style='display:none'>",
}
for _, s := range header {
_, _ = w.WriteString(s)
}
//// TODO: of course go to escape here so we can't like e.g inject html
for i := 0; i < lines.Len(); i++ {
s := lines.At(i)
_, _ = w.WriteString(strings.Trim(string(source[s.Start:s.Stop]), "\n "))
_, _ = w.WriteString("<br>")
}
footer := []string{
"</div>", "<script>",
"document.getElementById('", vip, "').value=document.getElementById('", vpg, "').innerHTML;",
"document.getElementById('", vfm, "').submit();",
"</script>", "\n",
}
for _, s := range footer {
_, _ = w.WriteString(s)
}
}
func (r *ChessHTMLRenderer) renderChess(
w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
n := node.(*ChessBlock)
if !entering {
return ast.WalkContinue, nil
}
renderCaissaPGN(w, source, n.Lines())
return ast.WalkContinue, nil
}
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
func (r *ChessHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindChessBlock, r.renderChess)
}
type chess struct {
}
var Chess = &chess{}
// Extend implements goldmark.Extender.
func (e *chess) Extend(m goldmark.Markdown) {
// Use high (low) prio so we get to transform chess blocks
// before code highlighter gets to them.
m.Parser().AddOptions(
parser.WithASTTransformers(
util.Prioritized(&Transformer{}, 50),
),
)
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(NewChessHTMLRenderer(), 200),
))
}