page_injector.go 3.2 KB

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