helper_method_conditional_comment.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package ace
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "strings"
  7. )
  8. // Comment types
  9. const (
  10. commentTypeHidden = "hidden"
  11. commentTypeRevealed = "revealed"
  12. )
  13. // helperMethodConditionalComment represents a helper method
  14. // conditional comment.
  15. type helperMethodConditionalComment struct {
  16. elementBase
  17. commentType string
  18. condition string
  19. }
  20. // WriteTo writes data to w.
  21. func (e *helperMethodConditionalComment) WriteTo(w io.Writer) (int64, error) {
  22. var bf bytes.Buffer
  23. // Write an open tag.
  24. bf.WriteString(e.opts.DelimLeft)
  25. bf.WriteString(preDefinedFuncNameHTML)
  26. bf.WriteString(space)
  27. bf.WriteString(doubleQuote)
  28. bf.WriteString(lt)
  29. bf.WriteString(exclamation)
  30. bf.WriteString(doubleQuote)
  31. bf.WriteString(e.opts.DelimRight)
  32. if e.commentType == commentTypeHidden {
  33. bf.WriteString(hyphen)
  34. bf.WriteString(hyphen)
  35. }
  36. bf.WriteString(bracketOpen)
  37. bf.WriteString("if ")
  38. bf.WriteString(e.condition)
  39. bf.WriteString(bracketClose)
  40. bf.WriteString(gt)
  41. bf.WriteString(lf)
  42. // Write the children's HTML.
  43. if i, err := e.writeChildren(&bf); err != nil {
  44. return i, err
  45. }
  46. // Write a close tag.
  47. bf.WriteString(e.opts.DelimLeft)
  48. bf.WriteString(preDefinedFuncNameHTML)
  49. bf.WriteString(space)
  50. bf.WriteString(doubleQuote)
  51. bf.WriteString(lt)
  52. bf.WriteString(exclamation)
  53. bf.WriteString(doubleQuote)
  54. bf.WriteString(e.opts.DelimRight)
  55. bf.WriteString(bracketOpen)
  56. bf.WriteString("endif")
  57. bf.WriteString(bracketClose)
  58. if e.commentType == commentTypeHidden {
  59. bf.WriteString(hyphen)
  60. bf.WriteString(hyphen)
  61. }
  62. bf.WriteString(gt)
  63. // Write the buffer.
  64. i, err := w.Write(bf.Bytes())
  65. return int64(i), err
  66. }
  67. // ContainPlainText returns true.
  68. func (e *helperMethodConditionalComment) ContainPlainText() bool {
  69. return true
  70. }
  71. // newHelperMethodConditionalComment creates and returns an HTML comment.
  72. func newHelperMethodConditionalComment(ln *line, rslt *result, src *source, parent element, opts *Options) (*helperMethodConditionalComment, error) {
  73. switch len(ln.tokens) {
  74. case 2:
  75. return nil, fmt.Errorf("no comment type is specified [file: %s][line: %d]", ln.fileName(), ln.no)
  76. case 3:
  77. return nil, fmt.Errorf("no condition is specified [file: %s][line: %d]", ln.fileName(), ln.no)
  78. }
  79. commentType := ln.tokens[2]
  80. if commentType != commentTypeHidden && commentType != commentTypeRevealed {
  81. return nil, fmt.Errorf("the comment type is invalid [file: %s][line: %d]", ln.fileName(), ln.no)
  82. }
  83. e := &helperMethodConditionalComment{
  84. elementBase: newElementBase(ln, rslt, src, parent, opts),
  85. commentType: commentType,
  86. condition: strings.Join(ln.tokens[3:], space),
  87. }
  88. return e, nil
  89. }