pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
600 stars 165 forks source link

[Question] How to improve a lua filter so that it can customise font colour of pptx documents #272

Closed CLRafaelR closed 11 months ago

CLRafaelR commented 12 months ago

The following question was originally posted on StackOverflow. However, the question has not been attended for a week, I would like to ask for help in this lua-filters repository.


I endeavour to modify the font colour within a pptx document that has been generated from a qmd file utilising a lua filter entitled 'color-text.lua', which is from 5.1 Font color | R Markdown Cookbook.

The lua filter demonstrates proficiency in altering the font colour during the rendering process of HTML-based documents, encompassing standard HTMLs and revealjs presentations, as well as latex-based documents, which encompass PDFs and beamer presentations.

Regrettably, I am uncertain about the methodology to enhance the lua filter's capabilities in order to alter font colour in pptx documents. Any insights or suggestions are appreciated.

enter image description here

MWE

test.qmd

---
format:
  pptx:
    keep-md: true
filter:
  - color-text.lua
---

## Slide title 1

First, we define a Lua filter and write it to
the file `color-text.lua`.

## Slide title 2

Now we can test the filter with some text in brackets with
the `color` attribute, e.g.,

> Roses are [red and **bold**]{color="red"} and
> violets are [blue]{color="blue"}.

color-text.lua

The following script is a modified version of color-text.lua that was originally posted on StackOverflow. I added a code block to modify pptx (xml) format (i.e. if FORMAT == 'pptx' then... return run) using ChatGPT (gpt-3.5) but the code block ignores [...]{color="..."} notation.

function Span(elem)
  local color = elem.attributes['color']

  -- If no color attribute, return unchanged
  if not color then
    return elem
  end

  -- Check if we are rendering to PPTX format
  if FORMAT == 'pptx' then
    -- Define the namespace for PPTX elements
    local pptx_ns = 'http://schemas.openxmlformats.org/drawingml/2006/main'

    -- Create attributes for the custom span element
    local run_attrs = pandoc.Attr(nil, {'color-text'})

    -- Create a run element for font color with attributes
    local run = pandoc.Span({}, run_attrs)
    run.content = elem.content

    -- Create attributes for the custom span element inside the run element
    local span_attrs = pandoc.Attr('pptx-font-color', {'pptx-font-color'}, { ['w:color'] = color, ['xmlns:w'] = pptx_ns })

    -- Add the font color as a custom attribute in PPTX namespace
    table.insert(
      run.content,
      1,
      pandoc.Span({pandoc.Str(' ')}, span_attrs)
    )

    return run
  else
    -- For non-PPTX formats, apply font color changes directly
    if FORMAT:match 'html' or FORMAT:match 'revealjs' then
      -- For HTML and Reveal.js, transform to <span style="color: red;"></span>
      -- Remove color attributes
      elem.attributes['color'] = nil
      -- Use style attribute instead
      elem.attributes['style'] = 'color: ' .. color .. ';'
      -- Return full span element
      return elem
    elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then
      -- For LaTeX and Beamer, encapsulate in LaTeX code
      -- Remove color attributes
      elem.attributes['color'] = nil
      -- Add LaTeX code for font color
      table.insert(
        elem.content, 1,
        pandoc.RawInline('latex', '\\textcolor{' .. color .. '}{')
      )
      table.insert(
        elem.content,
        pandoc.RawInline('latex', '}')
      )
      -- Return only span content
      return elem.content
    else
      -- For other formats, return unchanged
      return elem
    end
  end
end
tarleb commented 11 months ago

Unfortunately this is out of scope for us here. This tracker is mainly for bug reports.

The way to go here would probably be to generate raw XML for ppt, but I don't know enough about color handling there and since can't help. Sorry.