highlight-plugin.lua 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. -- This code adapted from https://gitlab.com/saalen/highlight/-/wikis/Plug-Ins
  2. -- first add a description of what the plug-in does
  3. Description="Add wiki.libsdl.org reference links to HTML, LaTeX or RTF output"
  4. -- define the plugin categories (ie. supported output formats; languages)
  5. Categories = { "c", "c++" }
  6. -- the syntaxUpdate function contains code related to syntax recognition
  7. function syntaxUpdate(desc)
  8. -- if the current file is not C/C++ file we exit
  9. if desc~="C and C++" then
  10. return
  11. end
  12. -- this function returns a qt-project reference link of the given token
  13. function getURL(token)
  14. -- generate the URL
  15. url='https://wiki.libsdl.org/SDL3/'.. token
  16. -- embed the URL in a hyperlink according to the output format
  17. -- first HTML, then LaTeX and RTF
  18. if (HL_OUTPUT== HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
  19. return '<a class="hl" target="new" href="'
  20. .. url .. '">'.. token .. '</a>'
  21. elseif (HL_OUTPUT == HL_FORMAT_LATEX) then
  22. return '\\href{'..url..'}{'..token..'}'
  23. elseif (HL_OUTPUT == HL_FORMAT_RTF) then
  24. return '{{\\field{\\*\\fldinst HYPERLINK "'
  25. ..url..'" }{\\fldrslt\\ul\\ulc0 '..token..'}}}'
  26. end
  27. end
  28. -- the Decorate function will be invoked for every recognized token
  29. function Decorate(token, state)
  30. -- we are only interested in keywords, preprocessor or default items
  31. if (state ~= HL_STANDARD and state ~= HL_KEYWORD and
  32. state ~=HL_PREPROC) then
  33. return
  34. end
  35. -- SDL keywords start with SDL_
  36. -- if this pattern applies to the token, we return the URL
  37. -- if we return nothing, the token is outputted as is
  38. if ( (token == "Uint8") or (token == "Uint16") or (token == "Uint32") or (token == "Uint64") or
  39. (token == "Sint8") or (token == "Sint16") or (token == "Sint32") or (token == "Sint64") or
  40. (string.find(token, "SDL_") == 1) ) then
  41. return getURL(token)
  42. end
  43. end
  44. end
  45. -- the themeUpdate function contains code related to the theme
  46. function themeUpdate(desc)
  47. -- the Injections table can be used to add style information to the theme
  48. -- HTML: we add additional CSS style information to beautify hyperlinks,
  49. -- they should have the same color as their surrounding tags
  50. if (HL_OUTPUT == HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
  51. Injections[#Injections+1]=
  52. "a.hl, a.hl:visited {color:inherit;font-weight:inherit;text-decoration:none}"
  53. -- LaTeX: hyperlinks require the hyperref package, so we add this here
  54. -- the colorlinks and pdfborderstyle options remove ugly boxes in the output
  55. elseif (HL_OUTPUT==HL_FORMAT_LATEX) then
  56. Injections[#Injections+1]=
  57. "\\usepackage[colorlinks=false, pdfborderstyle={/S/U/W 1}]{hyperref}"
  58. end
  59. end
  60. -- let highlight load the chunks
  61. Plugins={
  62. { Type="lang", Chunk=syntaxUpdate },
  63. { Type="theme", Chunk=themeUpdate },
  64. }