Skip to content

Commit

Permalink
refactor: rewrite the whole API (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanouil authored Dec 10, 2023
1 parent bc12337 commit f2b9c9e
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 162 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Mickaël Canouil
Copyright (c) 2023 Mickaël Canouil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
53 changes: 26 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
# Version Badge Extension For Quarto
# Badge Extension For Quarto

`version-badge` is an extension for Quarto to provide a shortcode to display software version.
`badge` is an extension for Quarto to provide a shortcode to display software version.

## Installing

```bash
quarto add mcanouil/quarto-version-badge
quarto add mcanouil/quarto-badge
```

This will install the extension under the `_extensions` subdirectory.
If you're using version control, you will want to check in this directory.

## Using

The shortcode `{{< v <version> style=<CSS style> >}}` will display a badge with the given version number.
The shortcode `{{< badge <key> <value> >}}` will display a badge with the given version number.

If `<version>` matches `version-badge` from the YAML frontmatter, the badge will be displayed with `bg-success` CSS class from Bootstrap, otherwise it will be displayed with `bg-danger`.
You can provide any number of badges by specifying an array under the `badge` key in the `_quarto.yml` file or in the front matter of the document.

- Short specification:

```yaml
version-badge: 1.4
```
- Complete specification:
```yaml
version-badge:
version: 1.4
type: pre-release
default: release
```
- Optional specification:
```yaml
badge:
- key: current
colour: firebrick
- key: future
class: bg-danger
href: https://github.com/mcanouil/quarto-version-badge
- key: old
class: bg-warning
href: https://github.com/mcanouil/quarto-version-badge/releases/tag/{{value}}
```
```yaml
version-badge:
changelog: https://github.com/mcanouil/quarto-version-badge/releases/tag/{{version}}
```
- `{{value}}` will be replaced by the value of the badge.
- You can use `colour` or `color` to specify the colour of the badge.
- You can also use `class` to specify a class to add to the badge.

Additional CSS styles can be provided with the `style` parameter which will be added to the badge as inline CSS.
```markdown
{{< badge current 1.0.0 >}}
{{< badge future 2.0.0 >}}
{{< badge old 0.1.0 >}}
```

The extension also provides two CSS classes: `.badge-default` and `.badge-target` that can be used to style the badge.
> [!NOTE]
> The `href` attribute is optional and currently breaks the table of contents links when used in headers.

## Example

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Mickaël Canouil
Copyright (c) 2023 Mickaël Canouil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
title: version-badge
title: badge
author: Mickaël Canouil
version: 1.2.0
version: 2.0.0
quarto-required: ">=1.3.0"
contributes:
shortcodes:
- version-badge.lua
- badge.lua
17 changes: 17 additions & 0 deletions _extensions/badge/badge.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.quarto-badge {
vertical-align: text-bottom;
font-size: .65em;
}

.nav-link .quarto-badge {
display: none;
}

.quarto-badge-href {
text-decoration: none;
color: inherit;
}

a.nav-link.quarto-badge-href {
display: none;
}
44 changes: 44 additions & 0 deletions _extensions/badge/badge.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
return {
['badge'] = function(args, kwargs, meta)
if quarto.doc.is_format("html") then
quarto.doc.add_html_dependency({
name = 'badge',
stylesheets = {"badge.css"}
})
local badgeKey = pandoc.utils.stringify(args[1])
local badgeValue = pandoc.utils.stringify(args[2])
local badgeContent = ""
local metaBadgeHref = ""
for _, badge in ipairs(meta["badge"]) do
local metaBadgeKey = pandoc.utils.stringify(badge["key"])
local metaBadgeClass = ""
if badge["class"] ~= "" and badge["class"] ~= nil then
metaBadgeClass = pandoc.utils.stringify(badge["class"])
end
if metaBadgeKey == badgeKey then
if badge["href"] ~= "" and badge["href"] ~= nil then
local metaBadgeHref = pandoc.utils.stringify(badge["href"])
if metaBadgeHref:find("{{value}}") then
metaBadgeHref = metaBadgeHref:gsub("{{value}}", badgeValue)
end
badgeValue = '<a ' ..
'href="' .. metaBadgeHref .. '"' ..
'class="quarto-badge-href"' ..
'>' ..
badgeValue ..
'</a>'
end
local style = ""
if (badge["colour"] ~= "" and badge["colour"] ~= nil) or (badge["color"] ~= "" and badge["color"] ~= nil) then
metaBadgeColor = pandoc.utils.stringify(badge["colour"] or badge["color"])
style = 'style="background-color: ' .. metaBadgeColor .. ';' .. '"'
end
badgeContent = '<span class="badge rounded-pill quarto-badge ' .. metaBadgeClass .. '" ' .. style .. '>' ..
badgeValue ..
'</span>'
end
end
return pandoc.RawInline('html', badgeContent)
end
end
}
23 changes: 0 additions & 23 deletions _extensions/version-badge/version-badge.css

This file was deleted.

97 changes: 0 additions & 97 deletions _extensions/version-badge/version-badge.lua

This file was deleted.

23 changes: 13 additions & 10 deletions example.qmd
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
---
title: "Version Badge Example"
title: "Quarto Badge Example"
toc: true
code-tools: true
version-badge:
version: 1.4
type: pre-release
default: release
changelog: https://github.com/mcanouil/quarto-version-badge/releases/tag/{{version}}
prefix: "v"
badge:
- key: current
colour: springgreen
- key: future
class: bg-danger
href: https://github.com/mcanouil/quarto-badge
- key: old
class: bg-warning
href: https://github.com/mcanouil/quarto-badge/releases/tag/{{value}}
---

## {{< v 1.0 >}} A feature here from ages ago
## {{< badge current 1.1 >}} A feature here from a while ago

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam euismod, nisl
eget aliquam ultricies, nunc nisl aliquet nunc, quis aliquam nunc nisl eu

## {{< v 1.1 >}} A feature here from a while ago
## {{< badge future 1.4 >}} A feature not quite here yet

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam euismod, nisl
eget aliquam ultricies, nunc nisl aliquet nunc, quis aliquam nunc nisl eu

## {{< v 1.4 >}} A feature not quite here yet
## {{< badge old 1.0 >}} A feature here from ages ago

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam euismod, nisl
eget aliquam ultricies, nunc nisl aliquet nunc, quis aliquam nunc nisl eu

0 comments on commit f2b9c9e

Please sign in to comment.