juliet
juliet

Reputation: 193

Pandoc numbered example list doesn't work with custom writer

Pandoc has amazing extension example_lists for continuously numbered list throughout the document. We try to use custom writer to produce html, but numbering is broken in result html. Consider the following md-code:

(@) item 1
(@) item 2

## header ##

(@) item 3

Pandoc produces the following html-page by default:

1. item 1
2. item 2
  header
3. item 3

But with custom writer (we took example with pandoc --print-default-data-file sample.lua) it produces:

1. item 1
2. item 2
  header
1. item 3

Sample lua-writer contains the following code for ordered list processing:

function OrderedList(items)
  local buffer = {}
  for _, item in pairs(items) do
    table.insert(buffer, "<li>" .. item .. "</li>")
  end
  return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
end

If add print for first elements of pairs from items table

function OrderedList(items)
  local buffer = {}
  for elem, item in pairs(items) do
    print(elem)
    table.insert(buffer, "<li>" .. item .. "</li>")
  end
  return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
end

we'll see just final list items' numbers:

1
2
1 

So I don't think the problem is in the writer itself. Have you any ideas how to solve this problem?

Upvotes: 4

Views: 864

Answers (2)

Artem Pelenitsyn
Artem Pelenitsyn

Reputation: 2578

Looking through pandoc sources for custom writer (src/Text/Pandoc/Writers/Custom.hs) you may find that OrderedList function actually gets four arguments, third of which is list style. You should be interested in Example list style. So you could update OrderedList implementation accordingly: introduce global variable for counting total items in Example-list, alter function code basing on list style (add start attribute in ol html tag for Example-list).

-- for counting examples (@)
local ExampleIdx = 1

function OrderedList(items, num, sty, delim)
  local buffer = {}
  for _, item in pairs(items) do
    table.insert(buffer, "<li>" .. item .. "</li>")
  end
  local start = ""
  if sty == "Example" then
    if ExampleIdx > 1 then
      start = ' start="' .. ExampleIdx .. '" '
    end
    ExampleIdx = ExampleIdx + table.getn(items)
  end
  return '<ol' .. start .. '>\n' .. table.concat(buffer, "\n") .. "\n</ol>"
end

Upvotes: 4

John MacFarlane
John MacFarlane

Reputation: 8937

You actually don't need to keep an ExampleIdx global variable as in Artem Pelenitsyn's answer. All you have to do is make your list item writer sensitive to the second parameter (the start number: num in Pelenitsyn's code). Note that you can use pandoc -t native to inspect the AST that is being passed to the writer; you'll see that the start number is set appropriately by the reader.

function OrderedList(items, num)
  local buffer = {}
  for _, item in pairs(items) do
     table.insert(buffer, "<li>" .. item .. "</li>")
  end
  local start = ""
  if num > 1 then
    start = ' start="' .. num .. '" '
  end
  return '<ol' .. start .. '>\n' .. table.concat(buffer, "\n") .. "\n</ol>"
end

Upvotes: 1

Related Questions