richtext.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package richtext
  2. type Block struct {
  3. ClassList [] string `kmd:"class-list"`
  4. Lines [] Line `kmd:"lines"`
  5. Children [] Block `kmd:"children"`
  6. }
  7. func (b *Block) AddClass(classes ...string) {
  8. b.ClassList = append(b.ClassList, classes...)
  9. }
  10. func (b *Block) WriteLine(content string, tags ...string) {
  11. b.Lines = append(b.Lines, Line { [] Span { {
  12. Content: content,
  13. Tags: tags,
  14. } } })
  15. }
  16. func (b *Block) WriteSpan(content string, tags ...string) {
  17. var span = Span {
  18. Content: content,
  19. Tags: tags,
  20. }
  21. if len(b.Lines) > 0 {
  22. var last = &(b.Lines[len(b.Lines) - 1])
  23. last.Spans = append(last.Spans, span)
  24. } else {
  25. b.Lines = append(b.Lines, Line { Spans: [] Span { span } })
  26. }
  27. }
  28. func (b *Block) WriteLineFeed() {
  29. b.Lines = append(b.Lines, Line { Spans: [] Span {} })
  30. }
  31. func (b *Block) Append(another Block) {
  32. b.Children = append(b.Children, another)
  33. }
  34. type Line struct {
  35. Spans [] Span `kmd:"spans"`
  36. }
  37. type Span struct {
  38. Content string `kmd:"content"`
  39. Tags [] string `kmd:"tags"`
  40. Link MaybeLink `kmd:"link"` // for doc links
  41. }
  42. const (
  43. TAG_DOC = "doc"
  44. TAG_EM = "em"
  45. TAG_SRC_NORMAL = "source"
  46. TAG_SRC_KEYWORD = "keyword"
  47. TAG_SRC_NAME = "name"
  48. TAG_SRC_TYPE = "type"
  49. TAG_SRC_FUNCTION = "function"
  50. TAG_SRC_CONST = "const"
  51. TAG_SRC_COMMENT = "comment"
  52. TAG_HIGHLIGHT = "highlight"
  53. TAG_ERR_NORMAL = "error"
  54. TAG_ERR_NOTE = "note"
  55. TAG_ERR_INLINE = "inline"
  56. )
  57. type MaybeLink interface { Maybe(Link, MaybeLink) }
  58. func (Link) Maybe(Link, MaybeLink) {}
  59. type Link struct {
  60. Page string `kmd:"page"`
  61. Anchor string `kmd:"anchor"`
  62. }