hypertext.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. --- Utilities for hypertext used in Minetest formspecs.
  2. -- @module advtrains_doc_integration.hypertext
  3. -- @alias H
  4. local H = {}
  5. local S = minetest.get_translator("advtrains_doc_integration")
  6. local utils = advtrains_doc_integration.utils
  7. --- Create a plain text hypertext element.
  8. -- @tparam string str The content of the element.
  9. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  10. -- @treturn string The hypertext element containing the text.
  11. function H.plain(str, noescape)
  12. str = tostring(str)
  13. if noescape then
  14. return str
  15. end
  16. return (string.gsub(str, "([<>])", [[\%1]]))
  17. end
  18. local function addtag(tag, attrs, str, noescape)
  19. local attrlist = {}
  20. for k, v in pairs(attrs) do
  21. table.insert(attrlist, (" %s=%s"):format(k, v))
  22. end
  23. return ("<%s%s>%s</%s>"):format(tag, table.concat(attrlist), H.plain(str, noescape), tag)
  24. end
  25. --- Create a hypertext element with monospace text.
  26. -- @tparam string str The content of the element.
  27. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  28. -- @treturn string The hypertext element containing the text.
  29. function H.mono(str, noescape)
  30. return addtag("mono", {}, str, noescape)
  31. end
  32. --- Create a hypertext element with bold text.
  33. -- @tparam string str The content of the element.
  34. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  35. -- @treturn string The hypertext element containing the text.
  36. function H.bold(str, noescape)
  37. return addtag("b", {}, str, noescape)
  38. end
  39. --- Create a hypertext element with italic text.
  40. -- @tparam string str The content of the element.
  41. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  42. -- @treturn string The hypertext element containing the text.
  43. function H.italic(str, noescape)
  44. return addtag("i", {}, str, noescape)
  45. end
  46. --- Create a hypertext element with text formatted as header.
  47. -- @tparam string str The content of the element.
  48. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  49. -- @treturn string The hypertext element containing the text.
  50. function H.header(str, noescape)
  51. return H.bold(str, noescape)
  52. end
  53. --- Create a hypertext element with text formatted as an item in a list.
  54. -- @tparam string str The content of the element.
  55. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  56. -- @treturn string The hypertext element containing the text.
  57. function H.listitem(str, noescape)
  58. return "• " .. H.plain(str, noescape)
  59. end
  60. --- Create a hypertext element with an action.
  61. -- @tparam string action The name of the action.
  62. -- @tparam string str The content of the element.
  63. -- @tparam[opt] boolean noescape Whether to avoid escaping the content.
  64. -- @treturn string The hypertext element containing the text.
  65. function H.action(action, str, noescape)
  66. return addtag("action", {name = action},
  67. addtag("style", {color = "cyan"}, str, noescape), true)
  68. end
  69. --- Describe a soundspec.
  70. -- @tparam[opt] string action The name of the action.
  71. -- @tparam SimpleSoundSpec soundspec The soundspec to describe.
  72. -- @treturn string The hypertext element describing the soundspec.
  73. function H.describe_sound(action, soundspec)
  74. if not soundspec then
  75. return S("Undefined")
  76. end
  77. local sounddesc = H.mono(soundspec.name)
  78. if action then
  79. sounddesc = H.action(action, sounddesc, true)
  80. end
  81. return sounddesc
  82. end
  83. --- Describe a coupler type.
  84. -- @tparam string coupler The name of the coupler.
  85. -- @treturn string The hypertext element describing the coupler.
  86. function H.describe_coupler(coupler)
  87. local name = utils.get_coupler_name(coupler)
  88. if name then
  89. return ("%s (%s)"):format(H.plain(name), H.mono(coupler))
  90. else
  91. return H.mono(coupler)
  92. end
  93. end
  94. --- Describe an item.
  95. -- @tparam ItemStack item The item to describe
  96. -- @treturn string The hypertext element describing the item.
  97. function H.describe_item(item)
  98. item = ItemStack(item)
  99. if item:is_empty() then
  100. return S("Emptyness")
  101. end
  102. local shortdesc = item:get_short_description()
  103. local name = item:get_name()
  104. local count = item:get_count()
  105. return ("%s (%s): %d"):format(H.plain(shortdesc), H.mono(name), count)
  106. end
  107. --- Describe a function.
  108. -- @tparam function f The function to describe.
  109. -- @treturn string|nil The hypertext element describing the function if a sufficiently informative description is available.
  110. function H.describe_function(f)
  111. local _, bcdesc = advtrains_doc_integration.bc.tostring(f)
  112. if bcdesc then
  113. return S("Compiled function:") .. "\n" .. H.mono(bcdesc)
  114. end
  115. local dinfo = debug.getinfo(f)
  116. if not dinfo then
  117. return nil
  118. elseif dinfo.isvararg then
  119. return S("Variadic function")
  120. elseif dinfo.nparams then
  121. return S("@1-ary function", ("%d"):format(dinfo.nparams))
  122. end
  123. return nil
  124. end
  125. return H