aboutsummaryrefslogtreecommitdiff
path: root/_extensions/js/en2em/en2em.lua
blob: 520474d0ec26ab7b0ba97dad88a9d233dccc7b52 (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 Para(el)
  21. for i = 1, #el.content - 1 do
  22. if el.content[i].t == "Str" and el.content[i].text == "\u{2013}" then
  23. local el_prev = el.content[i - 1]
  24. local el_next = el.content[i + 1]
  25. if el_prev and el_next
  26. and TypeIsSpacy[el_prev.t] and TypeIsSpacy[el_next.t]
  27. then
  28. el.content[i] = em_dash
  29. el.content[i - 1] = nothing
  30. el.content[i + 1] = nothing
  31. end
  32. end
  33. end
  34. return el
  35. end