page_injector.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package web
  2. import (
  3. "fmt"
  4. h "github.com/theplant/htmlgo"
  5. )
  6. type headKey int
  7. const (
  8. titleKey headKey = iota
  9. )
  10. type MetaKey string
  11. type keyComp struct {
  12. key interface{}
  13. comp h.HTMLComponent
  14. }
  15. type PageInjector struct {
  16. comps map[injectPosition][]*keyComp
  17. }
  18. type injectPosition int
  19. const (
  20. head injectPosition = iota
  21. tail
  22. extra
  23. )
  24. func (b *PageInjector) putComp(key interface{}, comp h.HTMLComponent, pos injectPosition, replace bool) {
  25. if b.comps == nil {
  26. b.comps = make(map[injectPosition][]*keyComp)
  27. }
  28. for _, h := range b.comps[pos] {
  29. if h.key == key {
  30. if !replace {
  31. return
  32. }
  33. h.comp = comp
  34. return
  35. }
  36. }
  37. b.comps[pos] = append(b.comps[pos], &keyComp{key: key, comp: comp})
  38. }
  39. func (b *PageInjector) getComp(key interface{}, pos injectPosition) *keyComp {
  40. for _, h := range b.comps[pos] {
  41. if h.key == key {
  42. return h
  43. }
  44. }
  45. return nil
  46. }
  47. func (b *PageInjector) Title(title string) {
  48. b.addNode(h.Title(""), titleKey, true, title)
  49. return
  50. }
  51. func (b *PageInjector) HasTitle() (r bool) {
  52. return b.getComp(titleKey, head) != nil
  53. }
  54. func (b *PageInjector) MetaNameContent(name, content string) {
  55. b.Meta(MetaKey(name), "name", name, "content", content)
  56. return
  57. }
  58. func (b *PageInjector) Meta(key interface{}, attrs ...string) {
  59. b.addNode(h.Meta(), key, true, "", attrs...)
  60. return
  61. }
  62. func (b *PageInjector) TailHTML(v string) {
  63. b.TailHTMLComponent(v, h.RawHTML(v), true)
  64. return
  65. }
  66. func (b *PageInjector) TailHTMLComponent(key interface{}, comp h.HTMLComponent, replace bool) {
  67. b.putComp(key, comp, tail, replace)
  68. return
  69. }
  70. func (b *PageInjector) Clear() (r *PageInjector) {
  71. b.comps = make(map[injectPosition][]*keyComp)
  72. return b
  73. }
  74. func (b *PageInjector) HeadHTML(v string) {
  75. b.HeadHTMLComponent(v, h.RawHTML(v), true)
  76. return
  77. }
  78. func (b *PageInjector) HeadHTMLComponent(key interface{}, comp h.HTMLComponent, replace bool) {
  79. b.putComp(key, comp, head, replace)
  80. return
  81. }
  82. func toHTMLComponent(list []*keyComp) h.HTMLComponent {
  83. var r []h.HTMLComponent
  84. for _, h := range list {
  85. r = append(r, h.comp)
  86. }
  87. return h.Components(r...)
  88. }
  89. func (b *PageInjector) GetHeadHTMLComponent() h.HTMLComponent {
  90. b.addCharsetViewPortIfMissing()
  91. return toHTMLComponent(b.comps[head])
  92. }
  93. func (b *PageInjector) GetTailHTMLComponent() h.HTMLComponent {
  94. return toHTMLComponent(b.comps[tail])
  95. }
  96. func (b *PageInjector) GetExtraHTMLComponent() h.HTMLComponent {
  97. return toHTMLComponent(b.comps[extra])
  98. }
  99. func (b *PageInjector) addCharsetViewPortIfMissing() {
  100. if b.getComp(MetaKey("charset"), head) == nil {
  101. b.Meta(MetaKey("charset"), "charset", "utf8")
  102. }
  103. if b.getComp(MetaKey("viewport"), head) == nil {
  104. b.MetaNameContent("viewport", "width=device-width, initial-scale=1, shrink-to-fit=no")
  105. }
  106. }
  107. func (b *PageInjector) addNode(tag *h.HTMLTagBuilder, key interface{}, replace bool, body string, attrs ...string) {
  108. l := len(attrs)
  109. if l%2 != 0 {
  110. panic(fmt.Sprintf("attrs should be pairs: %+v, length: %d", attrs, l))
  111. }
  112. for i := 0; i < l; i = i + 2 {
  113. tag.Attr(attrs[i], attrs[i+1])
  114. }
  115. if len(body) > 0 {
  116. tag.Text(body)
  117. }
  118. b.putComp(key, tag, head, replace)
  119. }