1package boards
2
3import (
4 "strconv"
5 "strings"
6)
7
8//----------------------------------------
9// Render functions
10
11func RenderBoard(bid BoardID) string {
12 board := getBoard(bid)
13 if board == nil {
14 return "missing board"
15 }
16 return board.RenderBoard()
17}
18
19func Render(path string) string {
20 if path == "" {
21 str := "These are all the boards of this realm:\n\n"
22 gBoards.Iterate("", "", func(key string, value interface{}) bool {
23 board := value.(*Board)
24 str += " * [" + board.url + "](" + board.url + ")\n"
25 return false
26 })
27 return str
28 }
29 parts := strings.Split(path, "/")
30 if len(parts) == 1 {
31 // /r/demo/boards:BOARD_NAME
32 name := parts[0]
33 boardI, exists := gBoardsByName.Get(name)
34 if !exists {
35 return "board does not exist: " + name
36 }
37 return boardI.(*Board).RenderBoard()
38 } else if len(parts) == 2 {
39 // /r/demo/boards:BOARD_NAME/THREAD_ID
40 name := parts[0]
41 boardI, exists := gBoardsByName.Get(name)
42 if !exists {
43 return "board does not exist: " + name
44 }
45 pid, err := strconv.Atoi(parts[1])
46 if err != nil {
47 return "invalid thread id: " + parts[1]
48 }
49 board := boardI.(*Board)
50 thread := board.GetThread(PostID(pid))
51 if thread == nil {
52 return "thread does not exist with id: " + parts[1]
53 }
54 return thread.RenderPost("", 5)
55 } else if len(parts) == 3 {
56 // /r/demo/boards:BOARD_NAME/THREAD_ID/REPLY_ID
57 name := parts[0]
58 boardI, exists := gBoardsByName.Get(name)
59 if !exists {
60 return "board does not exist: " + name
61 }
62 pid, err := strconv.Atoi(parts[1])
63 if err != nil {
64 return "invalid thread id: " + parts[1]
65 }
66 board := boardI.(*Board)
67 thread := board.GetThread(PostID(pid))
68 if thread == nil {
69 return "thread does not exist with id: " + parts[1]
70 }
71 rid, err := strconv.Atoi(parts[2])
72 if err != nil {
73 return "invalid reply id: " + parts[2]
74 }
75 reply := thread.GetReply(PostID(rid))
76 if reply == nil {
77 return "reply does not exist with id: " + parts[2]
78 }
79 return reply.RenderInner()
80 } else {
81 return "unrecognized path " + path
82 }
83}
render.gno
1.98 Kb ยท 83 lines