public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: "'Mark Johnno' via pandoc-discuss" <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
To: "pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org"
	<pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Fw: Fixing lua filter to number paragraphs
Date: Fri, 23 Sep 2022 23:09:42 +0000	[thread overview]
Message-ID: <e2suTdCtcLJ3u0PBmkSX9YRRukwnvuWJPhOFU1iL8lR84w_f8f9j1UrjpHEW_3oHRO5aC_mDpkazLs5dfMXA1EKkXvf4eZs3_tgIXsFq1uU=@proton.me> (raw)
In-Reply-To: <78BBgUlgdEJOaNOIxhfkTKPgf07yZREVfvlbeq1kqYQ59CCPSpaXEX4T_806rUzuPjbb5aIvpmynEu04PsBHsJTnWtkNNiG5dZQYfDGsO0Y=@proton.me>

Hi, I have been using a lua filter for a few months with no issue, however, for the last few days, I've not been able to produce a PDF. I can't find hints online. I am hoping someone can help me.
 
That is the error message I get:
 
! Undefined control sequence.
<argument> ... \hspace {\z@ }\ignorespaces \color
 
{lightgray}\tiny {[1]}\end...
l.72 \paragraphnumber{[1]}
 
I had some error messages in the past, usually due to missing squared brackets which I resolved by using --trace to check where parsing was slowing down (that is where the brackets were missing), but this time --trace does not provide any hints.
 
I can convert the document from .md to .pdf (without the filter); that is the command I use:

$ pandoc file.md --pdf-engine=xelatex -o file.pdf

...but when I add count-para.lua filter:
 
$ pandoc file.md --pdf-engine=xelatex --lua-filter=count-para.lua -o file.pdf
 
I get the error message mentioned above.
 
I also tried with another document. I get the same error message.
 
I tried contacting the author of the filter, in vain.
 
Do you have a hint about how I could troubleshoot or fix this issue?
 
Does the filter need to be updated?
 
$ pandoc -v
pandoc 2.5
Compiled with pandoc-types 1.17.5.4, texmath 0.11.2.2, skylighting 0.7.7
 
I use Trisquel GNU/Linux 10.0.1.
 
This is the filter:

--[[
Make all 'regular' paragraphs into a div and assign a numeric ID
Format this number in the margin
Copyright © 2021 Michael Cysouw 
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]

local count = 0
local chapter = 0
local indexUserID = {}

------------------------------
-- Options with default values
------------------------------

local resetAtChapter = false
local enclosing = "[]"
local chapterSep = "."
local refName = "paragraph "
local addPageNr = true

function getUserSettings (meta)

  if meta.resetAtChapter ~= nil then
    resetAtChapter = meta.resetAtChapter
  end

  if meta.enclosing ~= nil then
    enclosing = pandoc.utils.stringify(meta.enclosing)
  end

  if meta.chapterSep ~= nil then
    chapterSep = pandoc.utils.stringify(meta.chapterSep)
  end

  if meta.refName ~= nil then
    refName = pandoc.utils.stringify(meta.refName)
    if FORMAT:match "latex" then
      if refName == "#" then refName = "\\#" end
    end
  end

  if meta.addPageNr ~= nil then
    addPageNr = meta.addPageNr
  end

end

------------------------
-- Add global formatting
------------------------

function addFormatting (meta)

  local tmp = pandoc.MetaList{meta['header-includes']}
  if meta['header-includes'] ~= nil then
    tmp = meta['header-includes']
  end
  
  if FORMAT:match "html" then
    local css = [[ 
<!-- CSS added by lua-filter 'count-para' -->
<style>
.paragraph-number { 
  float: left;
  margin-left: -5em;
  width: 4.5em;
  text-align: right;
  color: grey;
  font-size: x-small;
  padding-top: 5px;
}
</style>
    ]]
    tmp[#tmp+1] = pandoc.MetaBlocks(pandoc.RawBlock("html", css))
  end
  
  function addTexPreamble (tex)
    tmp[#tmp+1] = pandoc.MetaBlocks(pandoc.RawBlock("tex", tex))
  end

  if FORMAT:match "latex" then
    addTexPreamble("\\usepackage{marginnote}")
    addTexPreamble("\\reversemarginpar")
    addTexPreamble("\\newcommand{\\paragraphnumber}[1]{\\marginnote{\\color{lightgray}\\tiny{#1}}[0pt]}")
  end
  
  meta['header-includes'] = tmp
  return(meta)
end

-------------------------
-- count Para and add Div
-------------------------

function countPara (doc)

  for i=1,#doc.blocks do

    -- optionally reset counter
    if  doc.blocks[i].tag == "Header"
        and doc.blocks[i].level == 1
        and doc.blocks[i].classes[1] ~= "unnumbered" 
        and resetAtChapter
    then
        chapter = chapter + 1
        count = 0
    end

    -- get Para, but not if there is an Image inside
    if  doc.blocks[i].tag == "Para"
        and doc.blocks[i].content[1].tag ~= "Image"
    then

      -- count paragraphs
      count = count + 1	
      local ID = count
      if resetAtChapter then 
        ID = chapter..chapterSep..count 
      end

      -- format number to insert
      local number = ID
      if enclosing:len() == 1 then
        number = enclosing..ID..enclosing
      else
        number = enclosing:sub(1,1)..ID..enclosing:sub(2,2)
      end

      -- check for user-inserted ids at the start of the paragraph
      local firstElem = pandoc.utils.stringify(doc.blocks[i].content[1])
      local userID = firstElem:match("{#(.*)}")
      if userID ~= nil then
        -- add to index
        indexUserID[userID] = ID
        -- remove reference from text
        table.remove(doc.blocks[i].content, 1)
        -- remove possible space
        if doc.blocks[i].content[1].tag == "Space" then
          table.remove(doc.blocks[i].content, 1)
        end
      end

      -- insert number
      if FORMAT:match "latex" then
        -- use marginnote for formatting number in margin
        local texCount = "\\paragraphnumber{"..number.."}"
        if userID ~= nil then
          -- add target for link to the number
          texCount = "\\hypertarget{"..userID.."}{\n"..texCount.."\\label{"..userID.."}}"
        end
        table.insert(doc.blocks[i].content, 1, pandoc.RawInline("tex", texCount))
      else
        table.insert(doc.blocks[i].content, 1, pandoc.Space())
        table.insert(doc.blocks[i].content, 1, pandoc.Span(number, pandoc.Attr(ID, {"paragraph-number"})))
      end

    end
  end
  return doc
end

------------------------------
-- set in-text cross-references
------------------------------

function setCrossRefs (cite)

  local userID = cite.citations[1].id
  local paraID = indexUserID[userID] 

  -- ignore other "cite" elements
  if paraID ~= nil then
  
    -- make in-document cross-references
    if FORMAT:match "latex" then

      local texInsert = refName.."\\hyperlink{"..userID.."}{"..paraID.."}"
      if addPageNr then
        texInsert = texInsert.." on page~\\pageref{"..userID.."}"
      end
      return pandoc.RawInline("tex", texInsert)

    else
      return pandoc.Link(refName..paraID, "#"..paraID)
    end

  end
end

--------------------
-- basic Pandoc loop
--------------------

return {
  { Meta = addFormatting },
  { Meta = getUserSettings },
  { Pandoc = countPara },
  { Cite = setCrossRefs }
}

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/e2suTdCtcLJ3u0PBmkSX9YRRukwnvuWJPhOFU1iL8lR84w_f8f9j1UrjpHEW_3oHRO5aC_mDpkazLs5dfMXA1EKkXvf4eZs3_tgIXsFq1uU%3D%40proton.me.


       reply	other threads:[~2022-09-23 23:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <78BBgUlgdEJOaNOIxhfkTKPgf07yZREVfvlbeq1kqYQ59CCPSpaXEX4T_806rUzuPjbb5aIvpmynEu04PsBHsJTnWtkNNiG5dZQYfDGsO0Y=@proton.me>
2022-09-23 23:09 ` 'Mark Johnno' via pandoc-discuss [this message]
2022-09-24  5:39   ` Bastien DUMONT
2022-09-24  6:04     ` AW: " denis.maier-NSENcxR/0n0
     [not found]       ` <9c110d41d7b24b39b98b999c78d6210b-NSENcxR/0n0@public.gmane.org>
2022-09-24 14:29         ` 'Mark Johnno' via pandoc-discuss

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='e2suTdCtcLJ3u0PBmkSX9YRRukwnvuWJPhOFU1iL8lR84w_f8f9j1UrjpHEW_3oHRO5aC_mDpkazLs5dfMXA1EKkXvf4eZs3_tgIXsFq1uU=@proton.me' \
    --to=pandoc-discuss-/jypxa39uh5tlh3mbocffw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).