template.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package handler
  2. import (
  3. "fmt"
  4. "html/template"
  5. "math/rand"
  6. "net/url"
  7. "regexp"
  8. "strconv"
  9. "time"
  10. )
  11. func GetRandomColor() string {
  12. // Some color shade I stole
  13. colors := []string{
  14. // Green
  15. "#C8847E",
  16. "#C8A87E",
  17. "#C8B87E",
  18. "#C8C67E",
  19. "#C7C87E",
  20. "#C2C87E",
  21. "#BDC87E",
  22. "#82C87E",
  23. "#82C87E",
  24. "#7EC8AF",
  25. "#7EAEC8",
  26. "#7EA6C8",
  27. "#7E99C8",
  28. "#7E87C8",
  29. "#897EC8",
  30. "#967EC8",
  31. "#AE7EC8",
  32. "#B57EC8",
  33. "#C87EA5",
  34. }
  35. // Randomly choose one and return
  36. return colors[rand.Intn(len(colors))]
  37. }
  38. func ParseEmojis(s string) template.HTML {
  39. emojiList := map[string]string{
  40. "normal": "101",
  41. "surprise": "102",
  42. "serious": "103",
  43. "heaven": "104",
  44. "happy": "105",
  45. "excited": "106",
  46. "sing": "107",
  47. "cry": "108",
  48. "normal2": "201",
  49. "shame2": "202",
  50. "love2": "203",
  51. "interesting2": "204",
  52. "blush2": "205",
  53. "fire2": "206",
  54. "angry2": "207",
  55. "shine2": "208",
  56. "panic2": "209",
  57. "normal3": "301",
  58. "satisfaction3": "302",
  59. "surprise3": "303",
  60. "smile3": "304",
  61. "shock3": "305",
  62. "gaze3": "306",
  63. "wink3": "307",
  64. "happy3": "308",
  65. "excited3": "309",
  66. "love3": "310",
  67. "normal4": "401",
  68. "surprise4": "402",
  69. "serious4": "403",
  70. "love4": "404",
  71. "shine4": "405",
  72. "sweat4": "406",
  73. "shame4": "407",
  74. "sleep4": "408",
  75. "heart": "501",
  76. "teardrop": "502",
  77. "star": "503",
  78. }
  79. regex := regexp.MustCompile(`\(([^)]+)\)`)
  80. parsedString := regex.ReplaceAllStringFunc(s, func(s string) string {
  81. s = s[1 : len(s)-1] // Get the string inside
  82. id := emojiList[s]
  83. return fmt.Sprintf(`<img src="https://s.pximg.net/common/images/emoji/%s.png" alt="(%s)" class="emoji" />`, id, s)
  84. })
  85. return template.HTML(parsedString)
  86. }
  87. func ParsePixivRedirect(s string) template.HTML {
  88. regex := regexp.MustCompile(`\/jump\.php\?(http[^"]+)`)
  89. parsedString := regex.ReplaceAllStringFunc(s, func(s string) string {
  90. s = s[10:]
  91. return s
  92. })
  93. escaped, err := url.QueryUnescape(parsedString)
  94. if err != nil {
  95. return template.HTML(s)
  96. }
  97. return template.HTML(escaped)
  98. }
  99. func EscapeString(s string) string {
  100. escaped := url.QueryEscape(s)
  101. return escaped
  102. }
  103. func ParseTime(date time.Time) string {
  104. return date.Format("2006-01-02 15:04")
  105. }
  106. func CreatePaginator(base, ending string, current_page, max_page int) template.HTML {
  107. peek := 2
  108. limit := peek*peek + 1
  109. count := 0
  110. pages := ""
  111. pages += fmt.Sprintf(`<a href="%s1%s" class="pagination-button">&laquo;</a>`, base, ending)
  112. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button">&lsaquo;</a>`, base, max(1, current_page-1), ending)
  113. for i := current_page - peek; (i <= max_page || max_page == -1) && count < limit; i++ {
  114. if i < 1 {
  115. continue
  116. }
  117. if i == current_page {
  118. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button" id="highlight">%d</a>`, base, i, ending, i)
  119. } else {
  120. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button">%d</a>`, base, i, ending, i)
  121. }
  122. count++
  123. }
  124. if max_page == -1 {
  125. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button">&rsaquo;</a>`, base, current_page+1, ending)
  126. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button" id="disabled">&raquo;</a>`, base, max_page, ending)
  127. } else {
  128. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button">&rsaquo;</a>`, base, min(max_page, current_page+1), ending)
  129. pages += fmt.Sprintf(`<a href="%s%d%s" class="pagination-button">&raquo;</a>`, base, max_page, ending)
  130. }
  131. return template.HTML(pages)
  132. }
  133. func GetTemplateFunctions() template.FuncMap {
  134. return template.FuncMap{
  135. "toInt": func(s string) int {
  136. n, _ := strconv.Atoi(s)
  137. return n
  138. },
  139. "parseEmojis": func(s string) template.HTML {
  140. return ParseEmojis(s)
  141. },
  142. "parsePixivRedirect": func(s string) template.HTML {
  143. return ParsePixivRedirect(s)
  144. },
  145. "escapeString": func(s string) string {
  146. return EscapeString(s)
  147. },
  148. "randomColor": func() string {
  149. return GetRandomColor()
  150. },
  151. "isEmpty": func(s string) bool {
  152. return len(s) < 1
  153. },
  154. "isEmphasize": func(s string) bool {
  155. switch s {
  156. case
  157. "R-18",
  158. "R-18G":
  159. return true
  160. }
  161. return false
  162. },
  163. "reformatDate": func(s string) string {
  164. if len(s) != 8 {
  165. return s
  166. }
  167. return fmt.Sprintf("%s-%s-%s", s[4:], s[2:4], s[:2])
  168. },
  169. "parseTime": func(date time.Time) string {
  170. return ParseTime(date)
  171. },
  172. "createPaginator": func(base, ending string, current_page, max_page int) template.HTML {
  173. return CreatePaginator(base, ending, current_page, max_page)
  174. },
  175. }
  176. }