1

As described here, Pandoc records Synctex-like information when the source is commonmark+sourcepos. For example, with this commonmark input,

---
title: "Sample"
---

This is a sample document.

the output in native format starts like this:

Pandoc
  Meta
    { unMeta =
        fromList [ ( "title" , MetaInlines [ Str "Sample" ] ) ]
    }
  [ Div
      ( "" , [] , [ ( "data-pos" , "Sample.knit.md@5:1-6:1" ) ] )
      [ Para
          [ Span
              ( ""
              , []
              , [ ( "data-pos" , "Sample.knit.md@5:1-5:5" ) ]
              )
              [ Str "This" ]
          , Span
              ( ""
              , []
              , [ ( "data-pos" , "Sample.knit.md@5:5-5:6" ) ]
              )
              [ Space ]
          , Span
              ( ""
              , []
              , [ ( "data-pos" , "Sample.knit.md@5:6-5:8" ) ]
              )
              [ Str "is" ]

but all that appears in the .tex file is this:

{This}{ }{is}...

As a step towards Synctex support, I'd like to insert the data-pos information as LaTeX markup, i.e. change the .tex output to look like this:

{This\datapos{Sample.knit.md@5:1-5:5}}{ \datapos{Sample.knit.md@5:5-5:6}}{is\datapos{Sample.knit.md@5:6-5:8}}...

This looks like something a Lua filter could accomplish pretty easily: look for the data-pos records, copy the location information into the Str record. However, I don't know Lua or Pandoc native language. Could someone help with this? Doing it for the Span records would be enough for my purposes. I'm using Pandoc 2.18 and Lua 5.4.

user2554330
  • 37,248
  • 4
  • 43
  • 90

1 Answers1

2

Here is an attempt that appears to work. Comments or corrections would still be welcome!

Span = function(span)
  local datapos = span.attributes['data-pos']
  if datapos then
    table.insert(span.content, pandoc.RawInline('tex', "\\datapos{" .. datapos .. "}"))
  end
  return span
end
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • As the author of pandoc Lua filters, this is *exactly* how I would do it. (OK, maybe I'd use method syntax`span.content:insert(pandoc.RawInline(...))`, but that's just taste.) – tarleb Nov 26 '22 at 09:44
  • 1
    @tarleb: Thanks for the confirmation, and thanks for making Lua filters available! – user2554330 Nov 26 '22 at 10:41