3

Background

A Markdown document has a div to inline an image:

::: image-inline
![](../climate/co2_graph)
:::

The Markdown is passed through Pandoc to produce a ConTeXt document. The output resembles:

\inlineexternalfigure[../climate/co2_graph]

Lua Code

The Lua code that is called when the document is parsed follows:

local lines_to_blocks = {
  Image = function( el )
    return {
      pandoc.RawInline( "tex", "\\inlineexternalfigure[" .. el.src .. "]" )
    }
  end
}

function Div( el )
  local kls, _ = el.classes:find_if(
    function ( c )
      return string.match( c, "^image%-" )
    end
  )

  if kls then
    return pandoc.walk_block( el, lines_to_blocks )
  end
end

This code works and produces the expected output.

ConTeXt Code

For context, the ConTeXt code resembles:

% Force external figures to fill an entire page.
\let\oldexternalfigure\externalfigure
\def\externalfigure[#1]{%
  \startTEXpage[width=\paperwidth,height=\paperheight,align=middle]
    \oldexternalfigure[#1][width=\paperwidth,height=\paperheight]%
  \stopTEXpage
}

\def\inlineexternalfigure[#1]{%
  \startalignment[middle]
    \dontleavehmode
    \oldexternalfigure[#1][width=.475\textwidth]%
  \stopalignment
}

This allows the presentation of figures to be independent of the Markdown code itself.

Output

The output is exactly as expected:

Output

Problem

The code to inline the image is overly complex. It seems that it would be simpler to write code that doesn't walk the tree (because I know what is expected from the Markdown document):

function Div( el )
  local kls, _ = el.classes:find_if(
    function ( c )
      return string.match( c, "^image%-" )
    end
  )

  if kls then
      return {
        pandoc.RawInline( "tex", "\\inlineexternalfigure[" .. el.SOMETHING.src .. "]" )
      }
  end
end

I cannot determine what to use for el.SOMETHING.src.

Questions

A couple of questions:

  • Where is the documentation for what attributes are available on the element passed into the Div function?
  • How can walking the document tree be avoided?

closed as off-topic by Wolfgang Schuster, Phelype Oleinik, Stefan Pinnow, Raaja, Dave Jarvis Jan 30 at 0:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – Wolfgang Schuster, Phelype Oleinik, Stefan Pinnow, Raaja, Dave Jarvis
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This seems more like a pandoc than a ConTeXt problem. – Henri Menke Jan 29 at 20:22
3

Where is the documentation for what attributes are available on the element passed into the Div function?

If I understand the documentation correctly, the element el that is passed to the Div function is always a Div. Therefore el has all the attributes of a Div, which can be looked up in the Lua filters documentation.

http://pandoc.org/lua-filters.html#type-ref-Div

How can walking the document tree be avoided?

If you will always only have a single image in an image-* Div then you can just index the contents of the Div directly. The following code does not check whether the tags match or whether any element is empty so it will likely crash on a malformed Div. It's also going to crash if the Image is nested deeper than within one Para. I actually don't recommend the solution below. Walking the block is much safer.

function Div( el )
  local kls, _ = el.classes:find_if(
    function ( c )
      return string.match( c, "^image%-" )
    end
  )

  if kls then
      local Para = el.content[1]
      local Image = Para.content[1]
      local src = Image.src
      return pandoc.Para{
          pandoc.RawInline( "tex", "\\inlineexternalfigure[" .. src .. "]" )
      }
  end
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.