meta.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // package meta is a extension for the goldmark(http://github.com/yuin/goldmark).
  2. //
  3. // This extension parses YAML metadata blocks and store metadata to a
  4. // parser.Context.
  5. package meta
  6. import (
  7. "bytes"
  8. "fmt"
  9. "github.com/yuin/goldmark"
  10. gast "github.com/yuin/goldmark/ast"
  11. "github.com/yuin/goldmark/parser"
  12. "github.com/yuin/goldmark/text"
  13. "github.com/yuin/goldmark/util"
  14. "notabug.org/gearsix/dati"
  15. )
  16. type metadata map[string]interface{}
  17. type data struct {
  18. Map metadata
  19. Error error
  20. Node gast.Node
  21. }
  22. var contextKey = parser.NewContextKey()
  23. // Get returns a metadata.
  24. func Get(pc parser.Context) metadata {
  25. v := pc.Get(contextKey)
  26. if v == nil {
  27. return nil
  28. }
  29. d := v.(*data)
  30. return d.Map
  31. }
  32. // TryGet tries to get a metadata.
  33. // If there are parsing errors, then nil and error are returned
  34. func TryGet(pc parser.Context) (metadata, error) {
  35. dtmp := pc.Get(contextKey)
  36. if dtmp == nil {
  37. return nil, nil
  38. }
  39. d := dtmp.(*data)
  40. if d.Error != nil {
  41. return nil, d.Error
  42. }
  43. return d.Map, nil
  44. }
  45. const openToken = "<!--"
  46. const closeToken = "-->"
  47. const formatYaml = ':'
  48. const formatToml = '#'
  49. const formatJsonOpen = '{'
  50. const formatJsonClose = '}'
  51. type metaParser struct {
  52. format byte
  53. }
  54. var defaultParser = &metaParser{}
  55. // NewParser returns a BlockParser that can parse metadata blocks.
  56. func NewParser() parser.BlockParser {
  57. return defaultParser
  58. }
  59. func isOpen(line []byte) bool {
  60. line = util.TrimRightSpace(util.TrimLeftSpace(line))
  61. for i := 0; i < len(line); i++ {
  62. if len(line[i:]) >= len(openToken)+1 && line[i] == openToken[0] {
  63. signal := line[i+len(openToken)]
  64. switch signal {
  65. case formatYaml:
  66. fallthrough
  67. case formatToml:
  68. fallthrough
  69. case formatJsonOpen:
  70. return true
  71. default:
  72. break
  73. }
  74. }
  75. }
  76. return false
  77. }
  78. // isClose will check `line` for the closing token.
  79. // If found, the integer returned will be the *nth* byte of `line` that the close token starts at.
  80. // If not found, then -1 is returned.
  81. func isClose(line []byte, signal byte) int {
  82. //line = util.TrimRightSpace(util.TrimLeftSpace(line))
  83. for i := 0; i < len(line); i++ {
  84. if line[i] == signal && len(line[i:]) >= len(closeToken)+1 {
  85. i++
  86. if string(line[i:i+len(closeToken)]) == closeToken {
  87. if signal == formatJsonClose {
  88. return i
  89. } else {
  90. return i - 1
  91. }
  92. }
  93. }
  94. }
  95. return -1
  96. }
  97. func (b *metaParser) Trigger() []byte {
  98. return []byte{openToken[0]}
  99. }
  100. func (b *metaParser) Open(parent gast.Node, reader text.Reader, pc parser.Context) (gast.Node, parser.State) {
  101. if linenum, _ := reader.Position(); linenum != 0 {
  102. return nil, parser.NoChildren
  103. }
  104. line, _ := reader.PeekLine()
  105. if isOpen(line) {
  106. reader.Advance(len(openToken))
  107. if b.format = reader.Peek(); b.format == formatJsonOpen {
  108. b.format = formatJsonClose
  109. } else {
  110. reader.Advance(1)
  111. }
  112. node := gast.NewTextBlock()
  113. if b.Continue(node, reader, pc) != parser.Close {
  114. return node, parser.NoChildren
  115. }
  116. parent.AppendChild(parent, node)
  117. b.Close(node, reader, pc)
  118. }
  119. return nil, parser.NoChildren
  120. }
  121. func (b *metaParser) Continue(node gast.Node, reader text.Reader, pc parser.Context) parser.State {
  122. line, segment := reader.PeekLine()
  123. if n := isClose(line, b.format); n != -1 && !util.IsBlank(line) {
  124. segment.Stop -= len(line[n:])
  125. node.Lines().Append(segment)
  126. reader.Advance(n + len(closeToken) + 1)
  127. return parser.Close
  128. }
  129. node.Lines().Append(segment)
  130. return parser.Continue | parser.NoChildren
  131. }
  132. func (b *metaParser) loadMetadata(buf []byte) (meta metadata, err error) {
  133. var format dati.DataFormat
  134. switch b.format {
  135. case formatYaml:
  136. format = dati.YAML
  137. case formatToml:
  138. format = dati.TOML
  139. case formatJsonClose:
  140. format = dati.JSON
  141. default:
  142. return meta, dati.ErrUnsupportedData(string(b.format))
  143. }
  144. err = dati.LoadData(format, bytes.NewReader(buf), &meta)
  145. return meta, err
  146. }
  147. func (b *metaParser) Close(node gast.Node, reader text.Reader, pc parser.Context) {
  148. lines := node.Lines()
  149. var buf bytes.Buffer
  150. for i := 0; i < lines.Len(); i++ {
  151. segment := lines.At(i)
  152. buf.Write(segment.Value(reader.Source()))
  153. }
  154. d := &data{Node: node}
  155. d.Map, d.Error = b.loadMetadata(buf.Bytes())
  156. pc.Set(contextKey, d)
  157. if d.Error == nil {
  158. node.Parent().RemoveChild(node.Parent(), node)
  159. }
  160. }
  161. func (b *metaParser) CanInterruptParagraph() bool {
  162. return true
  163. }
  164. func (b *metaParser) CanAcceptIndentedLine() bool {
  165. return true
  166. }
  167. type astTransformer struct {
  168. transformerConfig
  169. }
  170. type transformerConfig struct {
  171. // Stores metadata in ast.Document.Meta().
  172. StoresInDocument bool
  173. }
  174. type transformerOption interface {
  175. Option
  176. // SetMetaOption sets options for the metadata parser.
  177. SetMetaOption(*transformerConfig)
  178. }
  179. var _ transformerOption = &withStoresInDocument{}
  180. type withStoresInDocument struct {
  181. value bool
  182. }
  183. // WithStoresInDocument is a functional option that parser will store meta in ast.Document.Meta().
  184. func WithStoresInDocument() Option {
  185. return &withStoresInDocument{
  186. value: true,
  187. }
  188. }
  189. func newTransformer(opts ...transformerOption) parser.ASTTransformer {
  190. p := &astTransformer{
  191. transformerConfig: transformerConfig{
  192. StoresInDocument: false,
  193. },
  194. }
  195. for _, o := range opts {
  196. o.SetMetaOption(&p.transformerConfig)
  197. }
  198. return p
  199. }
  200. func (a *astTransformer) Transform(node *gast.Document, reader text.Reader, pc parser.Context) {
  201. dtmp := pc.Get(contextKey)
  202. if dtmp == nil {
  203. return
  204. }
  205. d := dtmp.(*data)
  206. if d.Error != nil {
  207. msg := gast.NewString([]byte(fmt.Sprintf("<!-- meta error, %s -->", d.Error)))
  208. msg.SetCode(true)
  209. d.Node.AppendChild(d.Node, msg)
  210. return
  211. }
  212. if a.StoresInDocument {
  213. for k, v := range d.Map {
  214. node.AddMeta(k, v)
  215. }
  216. }
  217. }
  218. // Option interface sets options for this extension.
  219. type Option interface {
  220. metaOption()
  221. }
  222. func (o *withStoresInDocument) metaOption() {}
  223. func (o *withStoresInDocument) SetMetaOption(c *transformerConfig) {
  224. c.StoresInDocument = o.value
  225. }
  226. type meta struct {
  227. options []Option
  228. }
  229. // Meta is a extension for the goldmark.
  230. var Meta = &meta{}
  231. // New returns a new Meta extension.
  232. func New(opts ...Option) goldmark.Extender {
  233. e := &meta{
  234. options: opts,
  235. }
  236. return e
  237. }
  238. // Extend implements goldmark.Extender.
  239. func (e *meta) Extend(m goldmark.Markdown) {
  240. topts := []transformerOption{}
  241. for _, opt := range e.options {
  242. if topt, ok := opt.(transformerOption); ok {
  243. topts = append(topts, topt)
  244. }
  245. }
  246. m.Parser().AddOptions(
  247. parser.WithBlockParsers(
  248. util.Prioritized(NewParser(), 0),
  249. ),
  250. )
  251. m.Parser().AddOptions(
  252. parser.WithASTTransformers(
  253. util.Prioritized(newTransformer(topts...), 0),
  254. ),
  255. )
  256. }