Skip to content

Commit

Permalink
fix: Allow to set background-colour (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanouil authored May 4, 2024
1 parent c69a768 commit 8536e7e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 39 deletions.
114 changes: 85 additions & 29 deletions _extensions/highlight-text/highlight-text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,121 @@
# SOFTWARE.
]]

local function highlight_html(span, colour)
span.attributes['colour'] = nil
span.attributes['color'] = nil
span.attributes['style'] = 'color: ' .. colour .. ';'
local function highlight_html(span, colour, bg_colour)
if span.attributes['style'] == nil then
span.attributes['style'] = ''
elseif span.attributes['style']:sub(-1) ~= ";" then
span.attributes['style'] = span.attributes['style'] .. ";"
end

if colour ~= nil then
span.attributes['colour'] = nil
span.attributes['color'] = nil
span.attributes['style'] = span.attributes['style'] .. 'color: ' .. colour .. ';'
end

if bg_colour ~= nil then
span.attributes['bg-colour'] = nil
span.attributes['bg-color'] = nil
span.attributes['style'] = span.attributes['style'] .. 'background-color: ' .. bg_colour .. ';'
end

return span
end

local function highlight_latex(span, colour)
local function highlight_latex(span, colour, bg_colour)
if colour == nil then
colour_open = ''
colour_close = ''
else
colour_open = '\\textcolor[HTML]{' .. colour:gsub("^#", "") .. '}{'
colour_close = '}'
end
if bg_colour == nil then
bg_colour_open = ''
bg_colour_close = ''
else
bg_colour_open = '\\colorbox[HTML]{' .. bg_colour:gsub("^#", "") .. '}{'
bg_colour_close = '}'
end

table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor[HTML]{' .. colour:gsub("^#", "") .. '}{')
)
table.insert(
span.content,
pandoc.RawInline('latex', '}')
pandoc.RawInline('latex', colour_open .. bg_colour_open)
)
table.insert(span.content, pandoc.RawInline('latex', bg_colour_close .. colour_close))

return span.content
end

local function highlight_openxml(span, colour)
table.insert(
span.content, 1,
pandoc.RawInline('openxml', '<w:r><w:rPr><w:color w:val="' .. colour:gsub("^#", "") .. '"/></w:rPr><w:t>')
)
table.insert(
span.content,
pandoc.RawInline('openxml', '</w:t></w:r>')
)
local function highlight_openxml(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("^#", "") .. '"/>'
end
if colour ~= nil then
spec = spec .. '<w:color w:val="' .. colour:gsub("^#", "") .. '"/>'
end
spec = spec .. '</w:rPr><w:t>'

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

return span.content
end

local function highlight_typst(span, colour)
local function highlight_typst(span, colour, bg_colour)
if colour == nil then
colour_open = ''
colour_close = ''
else
colour_open = '#text(rgb("' .. colour .. '"))['
colour_close = ']'
end

if bg_colour == nil then
bg_colour_open = ''
bg_colour_close = ''
else
bg_colour_open = '#highlight(fill: rgb("' .. bg_colour .. '"))['
bg_colour_close = ']'
end

table.insert(
span.content, 1,
pandoc.RawInline('typst', '#text(rgb("' .. colour .. '"))[')
pandoc.RawInline('typst', colour_open .. bg_colour_open)
)
table.insert(
span.content,
pandoc.RawInline('typst', ']')
pandoc.RawInline('typst', bg_colour_close .. colour_close)
)

return span.content
end

function Span(span)
colour = span.attributes['colour']
local colour = span.attributes['colour']
if colour == nil then
colour = span.attributes['color']
end

if colour == nil then return span end
local bg_colour = span.attributes['bg-colour']
if bg_colour == nil then
bg_colour = span.attributes['bg-color']
end

if colour == nil and bg_colour == nil then return span end

if FORMAT:match 'html' or FORMAT:match 'revealjs' then
return highlight_html(span, colour)
return highlight_html(span, colour, bg_colour)
elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then
return highlight_latex(span, colour)
elseif FORMAT:match 'docx' or FORMAT:match 'pptx' then
return highlight_openxml(span, colour)
return highlight_latex(span, colour, bg_colour)
elseif FORMAT:match 'docx' then
return highlight_openxml(span, colour, bg_colour)
elseif FORMAT:match 'pptx' then
return span -- Not implemented/supported
elseif FORMAT:match 'typst' then
return highlight_typst(span, colour)
return highlight_typst(span, colour, bg_colour)
else
return span
end
Expand Down
75 changes: 65 additions & 10 deletions example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ quarto add mcanouil/highlight-text
This will install the extension under the `_extensions` subdirectory.
If you're using version control, you will want to check in this directory.

## Using
## Using {.smaller}

To use the extension, add the following to your document's front matter:

Expand All @@ -45,23 +45,50 @@ 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]{colour="#b22222"}
[Red text]{colour="#b22222"}
```

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

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

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

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

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

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

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

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

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

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

[Blue]{color="#0000FF"}
[White text, Blue background]{color="#FFFFFF" bg-color="#0000FF"}
:::::
::::
:::
Expand All @@ -70,18 +97,46 @@ Then you can use the span syntax markup to highlight text in your document.
:::: {.columns}
::::: {.column width="50%"}
```markdown
[Red]{colour="#b22222"}
[Red text]{colour="#b22222"}
```

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

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

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

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

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

::::: {.column width="50%"}
```markdown
[Blue]{color="#0000FF"}
[Blue text]{color="#0000FF"}
```

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

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

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

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

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

0 comments on commit 8536e7e

Please sign in to comment.