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

Explain how "macros" work in djot #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ VIMDIR?=~/.vim

all: doc/syntax.html

doc/syntax.html: doc/syntax.md
doc/syntax.html: doc/syntax.md doc/syntax.css doc/code-examples.lua
pandoc --lua-filter doc/code-examples.lua $< -t html -o $@ -s --css doc/syntax.css --self-contained --wrap=preserve --toc --section-divs -Vpagetitle="Djot syntax reference"

8 changes: 8 additions & 0 deletions doc/code-examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ function renderdjot(txt)
end

function CodeBlock(el)
local lang = el.attr.classes[1]
if lang then
return {
pandoc.Div(
{ pandoc.Div({pandoc.CodeBlock(el.text)}, {class=lang})
}, {class="example"})
}
end
local rendered = renderdjot(el.text)
return {
pandoc.Div(
Expand Down
14 changes: 14 additions & 0 deletions doc/syntax.css
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,17 @@ ul.task-list{list-style: none;}
.example code {
font-size: 78%;
}

kbd {
background-color: #eee;
border-radius: 3px;
border: 1px solid #b4b4b4;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2), 0 2px 0 0 rgba(255, 255, 255, 0.7) inset;
color: #333;
display: inline-block;
font-size: 0.85em;
font-weight: 700;
line-height: 1;
padding: 2px 4px;
white-space: nowrap;
}
45 changes: 45 additions & 0 deletions doc/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,51 @@ See the [Epilogue][].
# Epilogue
```

## Syntax extensions

Djot doesn't have macros or other built-in facilities to extend the syntax of
the language. However, [divs](#div), [spans](#span), and
[attributes](#block-attributes) allow achieving equivalent results by extending
the way a djot document is converted to a target format.

For example, djot doesn't have syntax for keyboard shortcuts like
<kbd>ctrl</kbd> + <kbd>v</kbd>. Nevertheless, the appropriate semantics can be
ascribed to spans with a `.kbd` class.

By default, they will be rendered with `<span>` tags in HTML:

```
[ctrl + f]{.kbd}
```

By customizing the rendering, it's possible to change the output to the desired
`<kbd>`:

```html
<kbd>ctrl</kbd> + <kbd>f</kbd>
```

For example, JavaScript implementation of djot allows overriding rendering of
arbitrary elements:

```ts
{
span: (node: Span, r: HTMLRenderer) => {
if (hasClass(node, "kbd")) {
return getStringContent(node)
.split("+")
.map((key) => `<kbd>${key}</kbd>`)
.join("+");
}
return r.renderAstNodeDefault(node);
}
}
```

Specific mechanisms for customizing rendering are implementation defined.
However, all implementations are expected to provide facilities for customizing
the output based on the Djot abstract syntax tree.

## Nesting limits

Conforming implementations can impose reasonable limits on
Expand Down