lua filter works independent of attribute in pandoc

24 Views Asked by At

This is my latex template:

% Latex REsume/CV Template pandoc

\documentclass[a4paper]{article}

\usepackage{hyperref}
\usepackage{geometry}
\usepackage{enumitem}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[parfill]{parskip}
\usepackage{lmodern}
\usepackage{titlesec}
\input{glyphtounicode}

% fix error while running pandoc
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}
}

\hypersetup{
colorlinks=true,
  linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
  urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
pdftitle={$author-meta$: $title-meta$},
pdfauthor={$author-meta$},
pdfsubject={$title-meta$},
$if(keywords)$
  pdfkeywords={$for(keywords)$$keywords$$sep$, $endfor$},
$endif$
  pdfpagemode = UseNone
}

\pdfgentounicode=1
% no section numbers
\setcounter{secnumdepth}{0}
\geometry{
  body={6.5in, 9.0in},
  left=1.0in,
  top=1.0in
}

% Page number is top right, and it is possible to control the rest of
% the header.

$if(headings)$
\pagestyle{myheadings}
\markright{$author$}
\thispagestyle{empty}
$endif$
$if(pagenumbers)$
$else$
\pagenumbering{gobble}
$endif$


\newcommand{\rightdescription}[2]{%
    \item[#1] \hfill #2%
}
\begin{document}

$body$

\end{document}

this is my lua filter:

function rightAlignDescriptions(elem)
  if elem.t == "DefinitionList" then
    -- Check if the "specialclass" attribute is present
    local specialclass_attribute = elem.attributes and elem.attributes["specialclass"]
    -- If the "specialclass" attribute is present, return the element unchanged
    if specialclass_attribute then
      return elem
    end
    
    local new_items = {}
    for _, item in ipairs(elem.content) do
      if #item > 1 then
        local term = item[1]
        local description = item[2]
        -- Format the term and description in LaTeX style
        local latex_term = pandoc.utils.stringify(term)
        local latex_description = pandoc.utils.stringify(description)
        -- Concatenate the term and description with no space in between
        local latex_item = "\\rightdescription{" .. latex_term .. "}{" .. latex_description .. "}"
        table.insert(new_items, latex_item)
      end
    end
    -- Wrap the new items in a LaTeX description environment
    local latex = "\\begin{description}\n" .. table.concat(new_items, "\n") .. "\n\\end{description}"
    return pandoc.RawBlock("latex", latex)
  end
end

return {
  {
    DefinitionList = rightAlignDescriptions
  }
}

when i provide markdown like this:

# first heading {specialclass="true"}


first stuff

: first desc

first explanation

second stuff

: second desc

second explanation

# Second heading

random stuff

: random desc

random explanation

I have a markdown file with contents having various descriptions. I want some descriptions to be aligned to the right, while some descriptions work as it is. This is the solution I have made. The markdown file is only for reference, the data is different but behaviour same. I dont want to hardcode the data into the latex file because I need to convert the same makrdown to html. Nor do I want to add this particular data into a yaml file.

I run pandoc test.md -o test.pdf --template=test.tex --lua-filter=test.lua

I get the output pdf but the filter applies to all the descriptions, that is , all descriptions align right irrespective of the attribute. How to control the behaviour?

0

There are 0 best solutions below