Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcodes #17

Merged
merged 10 commits into from
Feb 10, 2024
Merged

Shortcodes #17

merged 10 commits into from
Feb 10, 2024

Conversation

rchaput
Copy link
Owner

@rchaput rchaput commented Feb 10, 2024

Added support for Quarto shortcodes, a different syntax ({{< acr KEY >}}) than the legacy one, inherited from filters.

  • This syntax is less error-prone, as it directly uses Quarto features, rather than trying to regex-find an arbitrary string in the document.
  • It also natively supports arguments and keyworded arguments (whereas our custom syntax would have been difficult to extend), which means that we can more finely control the behaviour of individual acronyms and list of acronyms.

The styles functions (`long-short`, `short-long`, and so on) determined
whether this was a first occurrence directly by calling the method
on the acronym object. This did not allow to override this behaviour for
a specific instance (a feature that can be added thanks to Quarto
shortcodes' keyworded-arguments).

This commit thus adds a new parameter `is_first_use` for most of these
styles functions (except those that do not differentiate between first
and next uses), as well as for the "public API" `replaceWithStyle`
function. For this function, the parameter can be set to `nil`, in which
case the correct value will be determined by a call to
`acronym:isFirstUse`, just like before.

The commit thus keeps consistency with previous versions (no breaking
changes), but prepares for a new feature.
In order to identify more errors when developing:
- added an assert to ensure that the acronym is not `nil` when adding
  a new acronym;
- fixed an existing error message by using `tostring` to avoid errors
  when the faulty value is not a string.
Previously, the `parse-acronyms.lua` script was responsible for setting
the acronyms' usage order, when an acronym appears.
This feature is now moved to the `Acronyms` table, so that the necessary
variables are grouped together (rather than relying on script-local
variables), although it is still invoked from `parse-acronyms.lua`.
This change will allow to re-use the same functionality when using
shortcodes, without repeating the code between several scripts.
This commit prepares the future "shortcodes" feature. Quarto Shortcodes
and Pandoc Filters will share common functionalities, namely generating
Pandoc elements (acronyms, list of acronyms) when invoked. However, the
way they are invoked differs: Filters have to parse each element, and
react to the element itself only; Shortcodes use a special syntax with
a name that determines which function to call, and have arguments and
keyworded arguments.

The following functions have been moved from the "parse-acronyms.lua"
script to the new "acronyms_pandoc.lua" script:
- `replaceNonExistingAcronym` (replacing an acronym that is not found);
- `replaceExistingAcronym` (replacing an acronym that is found);
- `generateLoA` (generating a List Of Acronyms).

In addition, these functions now take additional parameters that will
allow to customize the behaviour, if necessary (instead of relying
directly on the `Options` table, which is parsed from metadata).
Shortcodes will have the ability to override these parameters (based
on keyworded arguments), whereas Filters still use the values in
`Options` (because it would be harder to parse arguments and keyworded
arguments).
Historically (because this extension was initially a RMarkdown package),
**acronyms** used a Lua filter that replaces a specific markup of text.

Quarto extensions may provide such filters, but may also provide
"shortcodes" that use a clearer markup (`{{< name args >}}`, instead
of providing our own markup and regex-ing our way in the Pandoc AST).

In addition, Quarto shortcodes natively support arguments and keyworded
arguments (again, instead of regex-ing our way through the filtered
text).

This commit adds the new "shortcodes_acronyms.lua" file, which defines
the same features as the existing Pandoc filter, but using the new
Quarto shortcode syntax, by relyiong on functions that were refactored
in previous commits into the common `acronyms_pandoc.lua` helper script.

Specifically, the following shortcodes are added:
- `{{< acronym KEY >}}`: replaces an acronym identified by its `KEY`.
- `{{< acr key >}}`: same but shorter for convenience.
- `{{< print-acronyms >}}`: generates the List Of Acronyms.

WARNING: shortcodes do not seem to load (`require`) other files in the
same way as filters. Because we have helper files, this means that we must
either:
- change the `package.path` variable; however, we already know that this
  does not work on Windows!
- upgrade our requirements to Quarto v1.4.0, which changed the way files
  are required, and in particular added support for relative path.
We chose to continue supporting Windows, and to upgrade requirements,
especially because Quarto v1.4.0 is now officially released and
available. Users with a previous version of Quarto may still download a
previous version of **acronyms**.

Future commits will extend functionalities.
This allows shortcodes to override the parameters defined in the
`Options` table, which is parsed from the metadata.

For example, a specific acronym can be replaced with a style that
differs from the others acronyms, by specifying the `style=...` keyword
argument when using the shortcode.

Strangely, the keyworded arguments return an empty Inlines when they are
not specified, rather than `nil`. When they are specified, they return
a non-empty Inlines. This means that we must take care when handling
them, first to check whether they were specified or not, and secondly to
return their string representation if they exist.
- `22-shortcode-simple` simply tests for replacing an acronym using a
  shortcode.
- `23-shortcode-specific-style` replaces an acronym and overrides the
  default style (the one specified in the `Options` table, from the
  metadata).
- `24-shortcode-listofacronyms` tests the generation of the LoA using a
  shortcode.
We also have to tell Quarto that our `README.md` file contains emojis,
otherwise they are printed as-is (for example, `:fire:`).
@rchaput rchaput merged commit e9e3019 into master Feb 10, 2024
4 checks passed
@rchaput rchaput deleted the shortcodes branch February 10, 2024 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant