Skip to content

Commit

Permalink
fix: add OpenXML code for PowerPoint/pptx (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanouil committed May 18, 2024
1 parent 1423a0e commit e7bfb99
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 59 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This is the output of `example.qmd` for:
- [HTML](https://m.canouil.dev/quarto-highlight-text/)
- [Typst/PDF](https://m.canouil.dev/quarto-highlight-text/highlight-typst.pdf)
- [LaTeX/PDF](https://m.canouil.dev/quarto-highlight-text/highlight-latex.pdf)
- [Word/Docx](https://m.canouil.dev/quarto-highlight-text/highlight-openxml.docx)
- [Word/Docx](https://m.canouil.dev/quarto-highlight-text/highlight-openxml.docx) (**only supports plain text, *i.e.*, no URLs**)
- [Reveal.js](https://m.canouil.dev/quarto-highlight-text/highlight-revealjs.html)
- [Beamer/PDF](https://m.canouil.dev/quarto-highlight-text/highlight-beamer.pdf)
- [PowerPoint/Pptx](https://m.canouil.dev/quarto-highlight-text/highlight-pptx.pptx) (*not supported yet*)
- [PowerPoint/Pptx](https://m.canouil.dev/quarto-highlight-text/highlight-pptx.pptx) (**only supports plain text, *i.e.*, no URLs**)
27 changes: 24 additions & 3 deletions _extensions/highlight-text/highlight-text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ local function highlight_latex(span, colour, bg_colour)
return span.content
end

local function highlight_openxml(span, colour, bg_colour)
local function highlight_openxml_docx(span, colour, bg_colour)
local spec = '<w:r><w:rPr>'
if bg_colour ~= nil then
spec = spec .. '<w:shd w:val="clear" w:fill="' .. bg_colour:gsub("^#", "") .. '"/>'
Expand All @@ -85,6 +85,27 @@ local function highlight_openxml(span, colour, bg_colour)
return span.content
end

local function highlight_openxml_pptx(span, colour, bg_colour)
local spec = '<a:r><a:rPr dirty="0">'
if bg_colour ~= nil then
spec = spec .. '<a:highlight><a:srgbClr val="' .. bg_colour:gsub("^#", "") .. '" /></a:highlight>'
end
if colour ~= nil then
spec = spec .. '<a:solidFill><a:srgbClr val="' .. colour:gsub("^#", "") .. '" /></a:solidFill>'
end
spec = spec .. '</a:rPr><a:t>'

-- table.insert(span.content, 1, pandoc.RawInline('openxml', spec))
-- table.insert(span.content, pandoc.RawInline('openxml', '</a:t></a:r>'))

local span_content_string = ""
for i, inline in ipairs(span.content) do
span_content_string = span_content_string .. pandoc.utils.stringify(inline)
end

return pandoc.RawInline('openxml', spec .. span_content_string .. '</a:t></a:r>')
end

local function highlight_typst(span, colour, bg_colour)
if colour == nil then
colour_open = ''
Expand Down Expand Up @@ -132,9 +153,9 @@ function Span(span)
elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then
return highlight_latex(span, colour, bg_colour)
elseif FORMAT:match 'docx' then
return highlight_openxml(span, colour, bg_colour)
return highlight_openxml_docx(span, colour, bg_colour)
elseif FORMAT:match 'pptx' then
return span -- Not implemented/supported
return highlight_openxml_pptx(span, colour, bg_colour)
elseif FORMAT:match 'typst' then
return highlight_typst(span, colour, bg_colour)
else
Expand Down
84 changes: 30 additions & 54 deletions example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ format:
output-file: index
typst:
output-file: highlight-typst
papersize: a4
margin:
x: 2.5cm
y: 2.5cm
pdf:
output-file: highlight-latex
papersize: a4
margin:
x: 2.5cm
y: 2.5cm
docx:
output-file: highlight-docx
revealjs:
output-file: highlight-revealjs
beamer:
output-file: highlight-beamer
aspectratio: 169
pptx:
output-file: highlight-pptx
format-links: true
Expand Down Expand Up @@ -45,68 +54,40 @@ filters:

Then you can use the span syntax markup to highlight text in your document.

::: {.content-hidden when-format="pptx"}
:::: {layout-ncol="2"}
::::: {#UK}
```markdown
[Red text]{colour="#b22222"}
[Text]{colour="#FFFFFF" bg-colour="#b22222"}
[Text]{color="#FFFFFF" bg-color="#b22222"}
```

[Red text]{colour="#b22222"}
## Font Colour

```markdown
[Red background]{bg-colour="#b22222"}
```

[Red background]{bg-colour="#b22222"}

```markdown
[White text, Red background]{
colour="#FFFFFF" bg-colour="#b22222"
}
[Red text]{colour="#b22222"}
```

[White text, Red background]{colour="#FFFFFF" bg-colour="#b22222"}
:::::
[Red text]{colour="#b22222"}

::::: {#US}
```markdown
[Blue text]{color="#0000FF"}
```

[Blue text]{color="#0000FF"}

```markdown
[Blue background]{bg-color="#0000FF"}
```

[Blue background]{bg-color="#0000FF"}
## Background Colour

```markdown
[White text, Blue background]{
color="#FFFFFF" bg-color="#0000FF"
}
[Red background]{bg-colour="#b22222"}
```

[White text, Blue background]{color="#FFFFFF" bg-color="#0000FF"}
:::::
::::
:::
[Red background]{bg-colour="#b22222"}

::: {.content-visible when-format="pptx"}
:::: {.columns}
::::: {.column width="50%"}
```markdown
[Red text]{colour="#b22222"}
[Blue background]{bg-color="#0000FF"}
```

[Red text]{colour="#b22222"}

```markdown
[Red background]{bg-colour="#b22222"}
```
[Blue background]{bg-color="#0000FF"}

[Red background]{bg-colour="#b22222"}
## Font and Background Colour {.smaller}

```markdown
[White text, Red background]{
Expand All @@ -115,28 +96,23 @@ Then you can use the span syntax markup to highlight text in your document.
```

[White text, Red background]{colour="#FFFFFF" bg-colour="#b22222"}
:::::

::::: {.column width="50%"}
```markdown
[Blue text]{color="#0000FF"}
[White text, Blue background]{
color="#FFFFFF" bg-color="#0000FF"
}
```

[Blue text]{color="#0000FF"}

```markdown
[Blue background]{bg-color="#0000FF"}
```
[White text, Blue background]{color="#FFFFFF" bg-color="#0000FF"}

[Blue background]{bg-color="#0000FF"}
## More Examples {.smaller}

```markdown
[White text, Blue background]{
color="#FFFFFF" bg-color="#0000FF"
[text [with a link](https://quarto.org/)]{
color="#FFFFFF" bg-color="#00FFFF"
}
```

[White text, Blue background]{color="#FFFFFF" bg-color="#0000FF"}
:::::
::::
:::
[text [with a link](https://quarto.org/)]{
color="#FFFFFF" bg-color="#00FFFF"
}

0 comments on commit e7bfb99

Please sign in to comment.