mark.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package mark
  2. import "strings"
  3. // Mark
  4. type Mark struct {
  5. *parse
  6. Input string
  7. }
  8. // Mark options used to configure your Mark object
  9. // set `Smartypants` and `Fractions` to true to enable
  10. // smartypants and smartfractions rendering.
  11. type Options struct {
  12. Gfm bool
  13. Tables bool
  14. Smartypants bool
  15. Fractions bool
  16. }
  17. // DefaultOptions return an options struct with default configuration
  18. // it's means that only Gfm, and Tables set to true.
  19. func DefaultOptions() *Options {
  20. return &Options{
  21. Gfm: true,
  22. Tables: true,
  23. }
  24. }
  25. // New return a new Mark
  26. func New(input string, opts *Options) *Mark {
  27. // Preprocessing
  28. input = strings.Replace(input, "\t", " ", -1)
  29. if opts == nil {
  30. opts = DefaultOptions()
  31. }
  32. return &Mark{
  33. Input: input,
  34. parse: newParse(input, opts),
  35. }
  36. }
  37. // parse and render input
  38. func (m *Mark) Render() string {
  39. m.parse.parse()
  40. m.render()
  41. return m.output
  42. }
  43. // AddRenderFn let you pass NodeType, and RenderFn function
  44. // and override the default Node rendering
  45. func (m *Mark) AddRenderFn(typ NodeType, fn RenderFn) {
  46. m.renderFn[typ] = fn
  47. }
  48. // Staic render function
  49. func Render(input string) string {
  50. m := New(input, nil)
  51. return m.Render()
  52. }