utils.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/qor5/web"
  6. "github.com/shurcooL/sanitized_anchor_name"
  7. . "github.com/theplant/htmlgo"
  8. )
  9. func Anchor(h *HTMLTagBuilder, text string) HTMLComponent {
  10. anchorName := sanitized_anchor_name.Create(text)
  11. return h.Children(
  12. Text(text),
  13. A().Class("anchor").Href(fmt.Sprintf("#%s", anchorName)),
  14. ).Id(anchorName)
  15. }
  16. type Example struct {
  17. Title string
  18. DemoPath string
  19. SourcePath string
  20. }
  21. var LiveExamples []*Example
  22. func Demo(title string, demoPath string, sourcePath string) HTMLComponent {
  23. ex := &Example{
  24. Title: title,
  25. DemoPath: demoPath,
  26. SourcePath: fmt.Sprintf("https://github.com/qor5/docs/tree/main/docsrc/examples/%s", sourcePath),
  27. }
  28. LiveExamples = append(LiveExamples, ex)
  29. return Div(
  30. Div(
  31. A().Text("Check the demo").Href(ex.DemoPath).Target("_blank"),
  32. Text(" | "),
  33. A().Text("Source on GitHub").
  34. Href(ex.SourcePath).
  35. Target("_blank"),
  36. ).Class("demo"),
  37. )
  38. }
  39. func ExamplesDoc() HTMLComponent {
  40. u := Ul()
  41. for _, le := range LiveExamples {
  42. u.AppendChildren(
  43. Li(
  44. A().Href(le.DemoPath).Text(le.Title).Target("_blank"),
  45. Text(" | "),
  46. A().Href(le.SourcePath).Text("Source").Target("_blank"),
  47. ),
  48. )
  49. }
  50. return u
  51. }
  52. func PrettyFormAsJSON(ctx *web.EventContext) HTMLComponent {
  53. if ctx.R.MultipartForm == nil {
  54. return nil
  55. }
  56. formData, err := json.MarshalIndent(ctx.R.MultipartForm, "", "\t")
  57. if err != nil {
  58. panic(err)
  59. }
  60. return Pre(
  61. string(formData),
  62. )
  63. }