highlight-plugin.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 string.find(token, "SDL_")==1 then
  39. return getURL(token)
  40. end
  41. end
  42. end
  43. -- the themeUpdate function contains code related to the theme
  44. function themeUpdate(desc)
  45. -- the Injections table can be used to add style information to the theme
  46. -- HTML: we add additional CSS style information to beautify hyperlinks,
  47. -- they should have the same color as their surrounding tags
  48. if (HL_OUTPUT == HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
  49. Injections[#Injections+1]=
  50. "a.hl, a.hl:visited {color:inherit;font-weight:inherit;text-decoration:none}"
  51. -- LaTeX: hyperlinks require the hyperref package, so we add this here
  52. -- the colorlinks and pdfborderstyle options remove ugly boxes in the output
  53. elseif (HL_OUTPUT==HL_FORMAT_LATEX) then
  54. Injections[#Injections+1]=
  55. "\\usepackage[colorlinks=false, pdfborderstyle={/S/U/W 1}]{hyperref}"
  56. end
  57. end
  58. -- let highlight load the chunks
  59. Plugins={
  60. { Type="lang", Chunk=syntaxUpdate },
  61. { Type="theme", Chunk=themeUpdate },
  62. }