block.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package ast
  2. import (
  3. "fmt"
  4. "strings"
  5. textm "github.com/yuin/goldmark/text"
  6. )
  7. // A BaseBlock struct implements the Node interface partialliy.
  8. type BaseBlock struct {
  9. BaseNode
  10. blankPreviousLines bool
  11. lines *textm.Segments
  12. }
  13. // Type implements Node.Type.
  14. func (b *BaseBlock) Type() NodeType {
  15. return TypeBlock
  16. }
  17. // IsRaw implements Node.IsRaw.
  18. func (b *BaseBlock) IsRaw() bool {
  19. return false
  20. }
  21. // HasBlankPreviousLines implements Node.HasBlankPreviousLines.
  22. func (b *BaseBlock) HasBlankPreviousLines() bool {
  23. return b.blankPreviousLines
  24. }
  25. // SetBlankPreviousLines implements Node.SetBlankPreviousLines.
  26. func (b *BaseBlock) SetBlankPreviousLines(v bool) {
  27. b.blankPreviousLines = v
  28. }
  29. // Lines implements Node.Lines.
  30. func (b *BaseBlock) Lines() *textm.Segments {
  31. if b.lines == nil {
  32. b.lines = textm.NewSegments()
  33. }
  34. return b.lines
  35. }
  36. // SetLines implements Node.SetLines.
  37. func (b *BaseBlock) SetLines(v *textm.Segments) {
  38. b.lines = v
  39. }
  40. // A Document struct is a root node of Markdown text.
  41. type Document struct {
  42. BaseBlock
  43. meta map[string]interface{}
  44. }
  45. // KindDocument is a NodeKind of the Document node.
  46. var KindDocument = NewNodeKind("Document")
  47. // Dump implements Node.Dump .
  48. func (n *Document) Dump(source []byte, level int) {
  49. DumpHelper(n, source, level, nil, nil)
  50. }
  51. // Type implements Node.Type .
  52. func (n *Document) Type() NodeType {
  53. return TypeDocument
  54. }
  55. // Kind implements Node.Kind.
  56. func (n *Document) Kind() NodeKind {
  57. return KindDocument
  58. }
  59. // OwnerDocument implements Node.OwnerDocument.
  60. func (n *Document) OwnerDocument() *Document {
  61. return n
  62. }
  63. // Meta returns metadata of this document.
  64. func (n *Document) Meta() map[string]interface{} {
  65. if n.meta == nil {
  66. n.meta = map[string]interface{}{}
  67. }
  68. return n.meta
  69. }
  70. // SetMeta sets given metadata to this document.
  71. func (n *Document) SetMeta(meta map[string]interface{}) {
  72. if n.meta == nil {
  73. n.meta = map[string]interface{}{}
  74. }
  75. for k, v := range meta {
  76. n.meta[k] = v
  77. }
  78. }
  79. // AddMeta adds given metadata to this document.
  80. func (n *Document) AddMeta(key string, value interface{}) {
  81. if n.meta == nil {
  82. n.meta = map[string]interface{}{}
  83. }
  84. n.meta[key] = value
  85. }
  86. // NewDocument returns a new Document node.
  87. func NewDocument() *Document {
  88. return &Document{
  89. BaseBlock: BaseBlock{},
  90. meta: nil,
  91. }
  92. }
  93. // A TextBlock struct is a node whose lines
  94. // should be rendered without any containers.
  95. type TextBlock struct {
  96. BaseBlock
  97. }
  98. // Dump implements Node.Dump .
  99. func (n *TextBlock) Dump(source []byte, level int) {
  100. DumpHelper(n, source, level, nil, nil)
  101. }
  102. // KindTextBlock is a NodeKind of the TextBlock node.
  103. var KindTextBlock = NewNodeKind("TextBlock")
  104. // Kind implements Node.Kind.
  105. func (n *TextBlock) Kind() NodeKind {
  106. return KindTextBlock
  107. }
  108. // NewTextBlock returns a new TextBlock node.
  109. func NewTextBlock() *TextBlock {
  110. return &TextBlock{
  111. BaseBlock: BaseBlock{},
  112. }
  113. }
  114. // A Paragraph struct represents a paragraph of Markdown text.
  115. type Paragraph struct {
  116. BaseBlock
  117. }
  118. // Dump implements Node.Dump .
  119. func (n *Paragraph) Dump(source []byte, level int) {
  120. DumpHelper(n, source, level, nil, nil)
  121. }
  122. // KindParagraph is a NodeKind of the Paragraph node.
  123. var KindParagraph = NewNodeKind("Paragraph")
  124. // Kind implements Node.Kind.
  125. func (n *Paragraph) Kind() NodeKind {
  126. return KindParagraph
  127. }
  128. // NewParagraph returns a new Paragraph node.
  129. func NewParagraph() *Paragraph {
  130. return &Paragraph{
  131. BaseBlock: BaseBlock{},
  132. }
  133. }
  134. // IsParagraph returns true if the given node implements the Paragraph interface,
  135. // otherwise false.
  136. func IsParagraph(node Node) bool {
  137. _, ok := node.(*Paragraph)
  138. return ok
  139. }
  140. // A Heading struct represents headings like SetextHeading and ATXHeading.
  141. type Heading struct {
  142. BaseBlock
  143. // Level returns a level of this heading.
  144. // This value is between 1 and 6.
  145. Level int
  146. }
  147. // Dump implements Node.Dump .
  148. func (n *Heading) Dump(source []byte, level int) {
  149. m := map[string]string{
  150. "Level": fmt.Sprintf("%d", n.Level),
  151. }
  152. DumpHelper(n, source, level, m, nil)
  153. }
  154. // KindHeading is a NodeKind of the Heading node.
  155. var KindHeading = NewNodeKind("Heading")
  156. // Kind implements Node.Kind.
  157. func (n *Heading) Kind() NodeKind {
  158. return KindHeading
  159. }
  160. // NewHeading returns a new Heading node.
  161. func NewHeading(level int) *Heading {
  162. return &Heading{
  163. BaseBlock: BaseBlock{},
  164. Level: level,
  165. }
  166. }
  167. // A ThematicBreak struct represents a thematic break of Markdown text.
  168. type ThematicBreak struct {
  169. BaseBlock
  170. }
  171. // Dump implements Node.Dump .
  172. func (n *ThematicBreak) Dump(source []byte, level int) {
  173. DumpHelper(n, source, level, nil, nil)
  174. }
  175. // KindThematicBreak is a NodeKind of the ThematicBreak node.
  176. var KindThematicBreak = NewNodeKind("ThematicBreak")
  177. // Kind implements Node.Kind.
  178. func (n *ThematicBreak) Kind() NodeKind {
  179. return KindThematicBreak
  180. }
  181. // NewThematicBreak returns a new ThematicBreak node.
  182. func NewThematicBreak() *ThematicBreak {
  183. return &ThematicBreak{
  184. BaseBlock: BaseBlock{},
  185. }
  186. }
  187. // A CodeBlock interface represents an indented code block of Markdown text.
  188. type CodeBlock struct {
  189. BaseBlock
  190. }
  191. // IsRaw implements Node.IsRaw.
  192. func (n *CodeBlock) IsRaw() bool {
  193. return true
  194. }
  195. // Dump implements Node.Dump .
  196. func (n *CodeBlock) Dump(source []byte, level int) {
  197. DumpHelper(n, source, level, nil, nil)
  198. }
  199. // KindCodeBlock is a NodeKind of the CodeBlock node.
  200. var KindCodeBlock = NewNodeKind("CodeBlock")
  201. // Kind implements Node.Kind.
  202. func (n *CodeBlock) Kind() NodeKind {
  203. return KindCodeBlock
  204. }
  205. // NewCodeBlock returns a new CodeBlock node.
  206. func NewCodeBlock() *CodeBlock {
  207. return &CodeBlock{
  208. BaseBlock: BaseBlock{},
  209. }
  210. }
  211. // A FencedCodeBlock struct represents a fenced code block of Markdown text.
  212. type FencedCodeBlock struct {
  213. BaseBlock
  214. // Info returns a info text of this fenced code block.
  215. Info *Text
  216. language []byte
  217. }
  218. // Language returns an language in an info string.
  219. // Language returns nil if this node does not have an info string.
  220. func (n *FencedCodeBlock) Language(source []byte) []byte {
  221. if n.language == nil && n.Info != nil {
  222. segment := n.Info.Segment
  223. info := segment.Value(source)
  224. i := 0
  225. for ; i < len(info); i++ {
  226. if info[i] == ' ' {
  227. break
  228. }
  229. }
  230. n.language = info[:i]
  231. }
  232. return n.language
  233. }
  234. // IsRaw implements Node.IsRaw.
  235. func (n *FencedCodeBlock) IsRaw() bool {
  236. return true
  237. }
  238. // Dump implements Node.Dump .
  239. func (n *FencedCodeBlock) Dump(source []byte, level int) {
  240. m := map[string]string{}
  241. if n.Info != nil {
  242. m["Info"] = fmt.Sprintf("\"%s\"", n.Info.Text(source))
  243. }
  244. DumpHelper(n, source, level, m, nil)
  245. }
  246. // KindFencedCodeBlock is a NodeKind of the FencedCodeBlock node.
  247. var KindFencedCodeBlock = NewNodeKind("FencedCodeBlock")
  248. // Kind implements Node.Kind.
  249. func (n *FencedCodeBlock) Kind() NodeKind {
  250. return KindFencedCodeBlock
  251. }
  252. // NewFencedCodeBlock return a new FencedCodeBlock node.
  253. func NewFencedCodeBlock(info *Text) *FencedCodeBlock {
  254. return &FencedCodeBlock{
  255. BaseBlock: BaseBlock{},
  256. Info: info,
  257. }
  258. }
  259. // A Blockquote struct represents an blockquote block of Markdown text.
  260. type Blockquote struct {
  261. BaseBlock
  262. }
  263. // Dump implements Node.Dump .
  264. func (n *Blockquote) Dump(source []byte, level int) {
  265. DumpHelper(n, source, level, nil, nil)
  266. }
  267. // KindBlockquote is a NodeKind of the Blockquote node.
  268. var KindBlockquote = NewNodeKind("Blockquote")
  269. // Kind implements Node.Kind.
  270. func (n *Blockquote) Kind() NodeKind {
  271. return KindBlockquote
  272. }
  273. // NewBlockquote returns a new Blockquote node.
  274. func NewBlockquote() *Blockquote {
  275. return &Blockquote{
  276. BaseBlock: BaseBlock{},
  277. }
  278. }
  279. // A List struct represents a list of Markdown text.
  280. type List struct {
  281. BaseBlock
  282. // Marker is a marker character like '-', '+', ')' and '.'.
  283. Marker byte
  284. // IsTight is a true if this list is a 'tight' list.
  285. // See https://spec.commonmark.org/0.30/#loose for details.
  286. IsTight bool
  287. // Start is an initial number of this ordered list.
  288. // If this list is not an ordered list, Start is 0.
  289. Start int
  290. }
  291. // IsOrdered returns true if this list is an ordered list, otherwise false.
  292. func (l *List) IsOrdered() bool {
  293. return l.Marker == '.' || l.Marker == ')'
  294. }
  295. // CanContinue returns true if this list can continue with
  296. // the given mark and a list type, otherwise false.
  297. func (l *List) CanContinue(marker byte, isOrdered bool) bool {
  298. return marker == l.Marker && isOrdered == l.IsOrdered()
  299. }
  300. // Dump implements Node.Dump.
  301. func (l *List) Dump(source []byte, level int) {
  302. m := map[string]string{
  303. "Ordered": fmt.Sprintf("%v", l.IsOrdered()),
  304. "Marker": fmt.Sprintf("%c", l.Marker),
  305. "Tight": fmt.Sprintf("%v", l.IsTight),
  306. }
  307. if l.IsOrdered() {
  308. m["Start"] = fmt.Sprintf("%d", l.Start)
  309. }
  310. DumpHelper(l, source, level, m, nil)
  311. }
  312. // KindList is a NodeKind of the List node.
  313. var KindList = NewNodeKind("List")
  314. // Kind implements Node.Kind.
  315. func (l *List) Kind() NodeKind {
  316. return KindList
  317. }
  318. // NewList returns a new List node.
  319. func NewList(marker byte) *List {
  320. return &List{
  321. BaseBlock: BaseBlock{},
  322. Marker: marker,
  323. IsTight: true,
  324. }
  325. }
  326. // A ListItem struct represents a list item of Markdown text.
  327. type ListItem struct {
  328. BaseBlock
  329. // Offset is an offset position of this item.
  330. Offset int
  331. }
  332. // Dump implements Node.Dump.
  333. func (n *ListItem) Dump(source []byte, level int) {
  334. m := map[string]string{
  335. "Offset": fmt.Sprintf("%d", n.Offset),
  336. }
  337. DumpHelper(n, source, level, m, nil)
  338. }
  339. // KindListItem is a NodeKind of the ListItem node.
  340. var KindListItem = NewNodeKind("ListItem")
  341. // Kind implements Node.Kind.
  342. func (n *ListItem) Kind() NodeKind {
  343. return KindListItem
  344. }
  345. // NewListItem returns a new ListItem node.
  346. func NewListItem(offset int) *ListItem {
  347. return &ListItem{
  348. BaseBlock: BaseBlock{},
  349. Offset: offset,
  350. }
  351. }
  352. // HTMLBlockType represents kinds of an html blocks.
  353. // See https://spec.commonmark.org/0.30/#html-blocks
  354. type HTMLBlockType int
  355. const (
  356. // HTMLBlockType1 represents type 1 html blocks.
  357. HTMLBlockType1 HTMLBlockType = iota + 1
  358. // HTMLBlockType2 represents type 2 html blocks.
  359. HTMLBlockType2
  360. // HTMLBlockType3 represents type 3 html blocks.
  361. HTMLBlockType3
  362. // HTMLBlockType4 represents type 4 html blocks.
  363. HTMLBlockType4
  364. // HTMLBlockType5 represents type 5 html blocks.
  365. HTMLBlockType5
  366. // HTMLBlockType6 represents type 6 html blocks.
  367. HTMLBlockType6
  368. // HTMLBlockType7 represents type 7 html blocks.
  369. HTMLBlockType7
  370. )
  371. // An HTMLBlock struct represents an html block of Markdown text.
  372. type HTMLBlock struct {
  373. BaseBlock
  374. // Type is a type of this html block.
  375. HTMLBlockType HTMLBlockType
  376. // ClosureLine is a line that closes this html block.
  377. ClosureLine textm.Segment
  378. }
  379. // IsRaw implements Node.IsRaw.
  380. func (n *HTMLBlock) IsRaw() bool {
  381. return true
  382. }
  383. // HasClosure returns true if this html block has a closure line,
  384. // otherwise false.
  385. func (n *HTMLBlock) HasClosure() bool {
  386. return n.ClosureLine.Start >= 0
  387. }
  388. // Dump implements Node.Dump.
  389. func (n *HTMLBlock) Dump(source []byte, level int) {
  390. indent := strings.Repeat(" ", level)
  391. fmt.Printf("%s%s {\n", indent, "HTMLBlock")
  392. indent2 := strings.Repeat(" ", level+1)
  393. fmt.Printf("%sRawText: \"", indent2)
  394. for i := 0; i < n.Lines().Len(); i++ {
  395. s := n.Lines().At(i)
  396. fmt.Print(string(source[s.Start:s.Stop]))
  397. }
  398. fmt.Printf("\"\n")
  399. for c := n.FirstChild(); c != nil; c = c.NextSibling() {
  400. c.Dump(source, level+1)
  401. }
  402. if n.HasClosure() {
  403. cl := n.ClosureLine
  404. fmt.Printf("%sClosure: \"%s\"\n", indent2, string(cl.Value(source)))
  405. }
  406. fmt.Printf("%s}\n", indent)
  407. }
  408. // KindHTMLBlock is a NodeKind of the HTMLBlock node.
  409. var KindHTMLBlock = NewNodeKind("HTMLBlock")
  410. // Kind implements Node.Kind.
  411. func (n *HTMLBlock) Kind() NodeKind {
  412. return KindHTMLBlock
  413. }
  414. // NewHTMLBlock returns a new HTMLBlock node.
  415. func NewHTMLBlock(typ HTMLBlockType) *HTMLBlock {
  416. return &HTMLBlock{
  417. BaseBlock: BaseBlock{},
  418. HTMLBlockType: typ,
  419. ClosureLine: textm.NewSegment(-1, -1),
  420. }
  421. }