-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathrender.gno
83 lines (79 loc) · 1.98 KB
/
render.gno
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
package boards
import (
"strconv"
"strings"
)
//----------------------------------------
// Render functions
func RenderBoard(bid BoardID) string {
board := getBoard(bid)
if board == nil {
return "missing board"
}
return board.RenderBoard()
}
func Render(path string) string {
if path == "" {
str := "These are all the boards of this realm:\n\n"
gBoards.Iterate("", "", func(key string, value interface{}) bool {
board := value.(*Board)
str += " * [" + board.url + "](" + board.url + ")\n"
return false
})
return str
}
parts := strings.Split(path, "/")
if len(parts) == 1 {
// /r/demo/boards:BOARD_NAME
name := parts[0]
boardI, exists := gBoardsByName.Get(name)
if !exists {
return "board does not exist: " + name
}
return boardI.(*Board).RenderBoard()
} else if len(parts) == 2 {
// /r/demo/boards:BOARD_NAME/THREAD_ID
name := parts[0]
boardI, exists := gBoardsByName.Get(name)
if !exists {
return "board does not exist: " + name
}
pid, err := strconv.Atoi(parts[1])
if err != nil {
return "invalid thread id: " + parts[1]
}
board := boardI.(*Board)
thread := board.GetThread(PostID(pid))
if thread == nil {
return "thread does not exist with id: " + parts[1]
}
return thread.RenderPost("", 5)
} else if len(parts) == 3 {
// /r/demo/boards:BOARD_NAME/THREAD_ID/REPLY_ID
name := parts[0]
boardI, exists := gBoardsByName.Get(name)
if !exists {
return "board does not exist: " + name
}
pid, err := strconv.Atoi(parts[1])
if err != nil {
return "invalid thread id: " + parts[1]
}
board := boardI.(*Board)
thread := board.GetThread(PostID(pid))
if thread == nil {
return "thread does not exist with id: " + parts[1]
}
rid, err := strconv.Atoi(parts[2])
if err != nil {
return "invalid reply id: " + parts[2]
}
reply := thread.GetReply(PostID(rid))
if reply == nil {
return "reply does not exist with id: " + parts[2]
}
return reply.RenderInner()
} else {
return "unrecognized path " + path
}
}