markup.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --[[
  2. Licensed under MIT License
  3. * (c) 2013, Luca CPZ
  4. * (c) 2009, Uli Schlachter
  5. * (c) 2009, Majic
  6. --]]
  7. local format = string.format
  8. local setmetatable = setmetatable
  9. -- Lain markup util submodule
  10. -- lain.util.markup
  11. local markup = { fg = {}, bg = {} }
  12. -- Convenience tags
  13. function markup.bold(text) return format("<b>%s</b>", text) end
  14. function markup.italic(text) return format("<i>%s</i>", text) end
  15. function markup.strike(text) return format("<s>%s</s>", text) end
  16. function markup.underline(text) return format("<u>%s</u>", text) end
  17. function markup.monospace(text) return format("<tt>%s</tt>", text) end
  18. function markup.big(text) return format("<big>%s</big>", text) end
  19. function markup.small(text) return format("<small>%s</small>", text) end
  20. -- Set the font
  21. function markup.font(font, text)
  22. return format("<span font='%s'>%s</span>", font, text)
  23. end
  24. -- Set the foreground
  25. function markup.fg.color(color, text)
  26. return format("<span foreground='%s'>%s</span>", color, text)
  27. end
  28. -- Set the background
  29. function markup.bg.color(color, text)
  30. return format("<span background='%s'>%s</span>", color, text)
  31. end
  32. -- Set foreground and background
  33. function markup.color(fg, bg, text)
  34. return format("<span foreground='%s' background='%s'>%s</span>", fg, bg, text)
  35. end
  36. -- Set font and foreground
  37. function markup.fontfg(font, fg, text)
  38. return format("<span font='%s' foreground='%s'>%s</span>", font, fg, text)
  39. end
  40. -- Set font and background
  41. function markup.fontbg(font, bg, text)
  42. return format("<span font='%s' background='%s'>%s</span>", font, bg, text)
  43. end
  44. -- Set font, foreground and background
  45. function markup.fontcolor(font, fg, bg, text)
  46. return format("<span font='%s' foreground='%s' background='%s'>%s</span>", font, fg, bg, text)
  47. end
  48. -- link markup.{fg,bg}(...) calls to markup.{fg,bg}.color(...)
  49. setmetatable(markup.fg, { __call = function(_, ...) return markup.fg.color(...) end })
  50. setmetatable(markup.bg, { __call = function(_, ...) return markup.bg.color(...) end })
  51. -- link markup(...) calls to markup.fg.color(...)
  52. return setmetatable(markup, { __call = function(_, ...) return markup.fg.color(...) end })