aboutsummaryrefslogtreecommitdiff
path: root/_extensions/js/en2em/en2em.lua
blob: 57e2ddd06cb73fbac7dd39101c1a8ba1b38d483f (plain)
  1. --- en2em - replace space-enclosed en-dash with nospace em-dash
  2. ---
  3. --- Some typographical styles, including danish,
  4. --- use en-dash with spaces around to indicate a short pause,
  5. --- whereas others, including some british and american styles,
  6. --- use an em-dash without spaces.
  7. ---
  8. --- This plugin allows to always use en-dash at the source --
  9. --- which also eases writing with semantic line breaks, like here --
  10. --- and then convert while rendering as needed.
  11. ---
  12. --- SPDX-FileCopyrightText: 2025 Jonas Smedegaard <dr@jones.dk>
  13. --- SPDX-License-Identifier: GPL-3.0-or-later
  14. local em_dash = pandoc.Str("\u{2014}")
  15. local nothing = pandoc.Str("")
  16. local TypeIsSpacy = {
  17. Space = true,
  18. SoftBreak = true
  19. }
  20. function Block(el)
  21. if el.content == nil then return end
  22. for i = 1, #el.content - 1 do
  23. if el.content[i].t == "Str" and el.content[i].text == "\u{2013}" then
  24. local el_prev = el.content[i - 1]
  25. local el_next = el.content[i + 1]
  26. if el_prev and el_next
  27. and TypeIsSpacy[el_prev.t] and TypeIsSpacy[el_next.t]
  28. then
  29. el.content[i] = em_dash
  30. el.content[i - 1] = nothing
  31. el.content[i + 1] = nothing
  32. end
  33. end
  34. end
  35. return el
  36. end