Skip to content

Commit

Permalink
Update shortcodes to use pipe-separated syntax
Browse files Browse the repository at this point in the history
The shortcodes package now uses pipes to separate attributes, allowing for better attribute naming etc.
  • Loading branch information
samwilson authored Jun 27, 2023
1 parent 71034e0 commit 473d8fa
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Set up TeXLive
uses: teatimeguest/setup-texlive-action@v2
with:
packages: scheme-basic latexmk listings
packages: scheme-basic latexmk listings float

- name: Install
run: |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"league/flysystem": "^1.0",
"myclabs/php-enum": "^1.5",
"samwilson/commonmark-latex": "^0.3",
"samwilson/commonmark-shortcodes": "^0.2",
"samwilson/commonmark-shortcodes": "^0.3",
"samwilson/phpflickr": "^4.13",
"symfony/console": "^5.0",
"symfony/filesystem": "^5.4",
Expand Down
179 changes: 92 additions & 87 deletions composer.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions docs/content/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ see [the *Assets* section](index.html) of the documentation overview.
## Shortcodes

This section documents 'shortcodes', which are what we call specific replacable parts in a Markdown document.
They are inline phrases or blocks of text such as `{foo}` or `{{{bar id=123}}}` which get replaced
They are inline phrases or blocks of text such as `{foo}` or `{{{bar|id=123}}}` which get replaced
by the contents of templates such as `templates/shortcodes/foo.html.twig` or `templates/shortcodes/bar.tex.twig`.

* Inline shortcodes are delimited by single braces and can contain any number of attributes, e.g.:
* Lorem `{foo}` ipsum with no parameters.
* Lorem `{foo bar=baz bif="foo bar"}` ipsum with two parameters, the second of which contains a space.
* Lorem `{foo | bar=baz|bif="foo bar"}` ipsum with two parameters, the second of which contains a space.
* Lorem `{foo bif}` ipsum with a parameter with no value.
* Block shortcodes are delimited by triple braces at the beggining of lines, e.g.:
* A block of one line, with one parameter:
```
{{{quotation cite="Author name"
{{{quotation | cite="Author name"
Lorem ipsum
}}}
```
Expand All @@ -73,11 +73,11 @@ which has a [similar function](https://codex.wordpress.org/shortcode).
Shortcodes are a simple way to include images, videos, and summaries of other web pages.
For example, this is a photo from Wikimedia Commons:

{{{commons file=Co-Op,_Post_Office,_Courthouse.jpg }}}
{{{commons|Co-Op,_Post_Office,_Courthouse.jpg}}}

It is added to the source Markdown with this:

{{{commons file=Co-Op,_Post_Office,_Courthouse.jpg }}}
{{{commons|Co-Op,_Post_Office,_Courthouse.jpg}}}

All of the other information (image URL, caption, etc.) is retrieved from the Commons API when the Markdown is rendered.

Expand All @@ -93,7 +93,8 @@ The file `templates/shortcodes/<shortcode-name>.<format>.twig` to contain the HT
The following variables are available in shortcode templates:

* `shortcode.name`: the name of the shortcode, which will always be the same as the template's name.
* `shortcode.attr('foo')`: fetches an attribute by name.
* `shortcode.attrs.foo`: fetches an attribute by name.
* `shortcode.attrs.1`: fetches an unnamed attribute by number (starting from 1).
* `shortcode.body`: for block shortcodes, fetches the entire body text.

### Example: Wikimedia Commons
Expand Down Expand Up @@ -122,11 +123,11 @@ Note that this is also using the `commons()` Twig function, which is [documented

In any Markdown file:

{{{flickr id=123456}}}
{{{flickr|id=123456}}}

In `templates/shortcodes/flickr.html.twig`:

{% set flickr = flickr(shortcode.attr('id')) %}
{% set flickr = flickr(shortcode.attrs.id) %}

<figure itemscope itemtype="http://schema.org/ImageObject">
<a href="{{ flickr.urls.photopage }}"><img alt="An image from Flickr." src="{{ flickr.urls.medium_image }}" /></a>
Expand All @@ -145,4 +146,3 @@ In `templates/shortcodes/flickr.html.twig`:
</span>
</figcaption>
</figure>

7 changes: 6 additions & 1 deletion docs/templates/pdf.tex.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{% autoescape 'tex' %}
\documentclass[a4paper]{article}

\usepackage{graphicx, url, listings}
\usepackage{graphicx, url, listings, float}

\lstset{
basicstyle=\normalsize\fontencoding{T1}\ttfamily,
showstringspaces=true
}

\title{Basildon}

Expand Down
2 changes: 1 addition & 1 deletion docs/templates/shortcodes/commons.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set commons = commons(shortcode.attr('file')) %}
{% set commons = commons(shortcode.attrs.1) %}

<figure itemscope itemtype="http://schema.org/ImageObject">
<a href="{{ commons.imageinfo.0.descriptionurl }}">
Expand Down
4 changes: 2 additions & 2 deletions docs/templates/shortcodes/commons.tex.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% autoescape 'tex' %}
{% set commons = commons( shortcode.attrs.file ) %}
\begin{figure}
{% set commons = commons( shortcode.attrs.1 ) %}
\begin{figure}[H]
\begin{center}
\includegraphics[width=\linewidth]{{ '{' }}{{ tex_url( commons.imageinfo.0.thumburl )|raw }}{{ '}' }}
\caption{ {{ commons.labels.en.value }} }
Expand Down
2 changes: 1 addition & 1 deletion example/content/2020/0116_lorem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ This is a second post in January.

With a goat!

{{{commons file=RoveZiege_00037.jpg}}}
{{{commons|file=RoveZiege_00037.jpg}}}
2 changes: 1 addition & 1 deletion tests/test_site/content/shortcodes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Test shortcodes. A file from Wikimedia Commons:

{{{commons file=Test.png}}}
{{{commons |file=Test.png }}}
2 changes: 1 addition & 1 deletion tests/test_site/templates/shortcodes/commons.html.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<figure>
This is Commons file {{shortcode.attr('file')}}
This is Commons file {{shortcode.attrs.file}}
</figure>

0 comments on commit 473d8fa

Please sign in to comment.