Skip to content

Commit

Permalink
[Pandoc Writer] Misc. Improvements
Browse files Browse the repository at this point in the history
* New `html_attributes()` func. to convert pandoc
  attributes to PML HTML Attributes by prefixing
  `html_` to their key. Unfortunately can't be used
  due PMLC 3.1.0 bug (see: pml-lang/pml-companion#91)
* Adapt `Span()` element, but temporarily ignore any
  HTML attributes due to current PMLC bug.
* Implement `SingleQuoted()` and `DoubleQuoted()` by
  emitting literal curly quotes characters.
* Tweak `Link()` and stop escaping the value of `text`
  attribute, since it would vanquish any inline styles
  (although it seems that no styles are supported).
  • Loading branch information
tajmone committed Nov 3, 2022
1 parent 580953e commit ca7c41e
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 18 deletions.
38 changes: 30 additions & 8 deletions pandoc/filters-lua/pml-writer/pml-writer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- "pml-writer.lua" v0.0.11 | 2022/11/03 | PML 3.1.0 | pandoc 2.19.2
-- "pml-writer.lua" v0.0.12 | 2022/11/03 | PML 3.1.0 | pandoc 2.19.2
-- =============================================================================
-- ** WARNING ** This PML writer is being built on top of the sample writer that
-- ships with pandoc; generated via:
Expand Down Expand Up @@ -154,6 +154,23 @@ local function pml_node_verbatim(s)
return '[verbatim ' .. s .. ']'
end

-- Helper function to convert an attributes table into
-- an HTML attribute string that can be injected into PML nodes.
-- @WIP: html_attributes()
local function html_attributes(attr)
-- @NOTE: A bug in PML 3.1.0 causes multiple 'html_*' attributes to crash,
-- so this function can't be used safely until next PMLC release.
-- See: pml-lang/pml-companion#91
local attr_table = {}
for x,y in pairs(attr) do
if y and y ~= '' then
-- @TODO: Use dedicated quoted attributes escaping function
table.insert(attr_table, ' html_' .. x .. '="' .. escape(y,true) .. '"')
end
end
return table.concat(attr_table)
end


-- *****************************************************************************
--
Expand Down Expand Up @@ -219,7 +236,10 @@ function Link(s, tgt, tit, attr)
return escape(s)
end
-- @TODO: Add a dedicated URL escape function:
return '[link url=' .. tgt .. ' text="' .. escape(s) .. '"]'
-- @NOTE: Removed escaping of 'text' value since it might contain
-- styled contents, although these don't seem to work in PML:
return '[link url=' .. tgt .. ' text="' .. s .. '"]'
--return '[link url=' .. tgt .. ' text="' .. escape(s) .. '"]'
--[[
return '<a href="' .. escape(tgt,true) .. '" title="' ..
escape(tit,true) .. '"' .. attributes(attr) .. '>' .. s .. '</a>'
Expand Down Expand Up @@ -254,14 +274,12 @@ function DisplayMath(s)
return '\\[' .. escape(s) .. '\\]'
end

-- @TBD: SingleQuoted()
function SingleQuoted(s)
return '&lsquo;' .. s .. '&rsquo;'
return '' .. s .. ''
end

-- @TBD: DoubleQuoted()
function DoubleQuoted(s)
return '&ldquo;' .. s .. '&rdquo;'
return '' .. s .. ''
end

-- @TBD: Note()
Expand All @@ -277,9 +295,13 @@ function Note(s)
'"><sup>' .. num .. '</sup></a>'
end

-- @TBD: Span()
-- @WIP: Span()
function Span(s, attr)
return '<span' .. attributes(attr) .. '>' .. s .. '</span>'
-- @TODO: A bug in PML 3.1.0 causes multiple 'html_*' attributes to crash,
-- so we temporarily need to suppress html attributes until fixed.
-- See: pml-lang/pml-companion#91
return '[span ' .. s .. ']'
-- return '[span (' .. html_attributes(attr) .. ' ) ' .. s .. ']'
end

-- @WIP: RawInline()
Expand Down
29 changes: 25 additions & 4 deletions pandoc/filters-lua/pml-writer/tests/STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ Overall progress of the pandoc filter, pending tasks and known issues.
- [Metadata](#metadata)
- [TOC Settings](#toc-settings)
- [Known Issues](#known-issues)
- [Links Text](#links-text)
- [Headings Conversion](#headings-conversion)
- [List Start Number](#list-start-number)
- [HTML Attributes](#html-attributes)
- [Code Improvements and Sanitation](#code-improvements-and-sanitation)

<!-- /MarkdownTOC -->
Expand All @@ -41,7 +43,7 @@ Status of pandoc AST nodes conversion functions, by function names to ease sourc
- [ ] `DefinitionList()` &rarr; no PML equivalent!
- [ ] `DisplayMath()`
- [ ] `Div()`
- [ ] `DoubleQuoted()`
- [x] `DoubleQuoted()` &rarr; via literal `` and `` curly quote characters.
- [x] `Emph()`
- [x] `Header()` — works, but only if doc has Level 1 internal headings.
- [x] `HorizontalRule()` &rarr; `[html` + `<hr/>`
Expand All @@ -56,11 +58,11 @@ Status of pandoc AST nodes conversion functions, by function names to ease sourc
- [ ] `Plain()`
- [x] `RawBlock()` — only HTML contents are preserved.
- [x] `RawInline()` &rarr; `[verbatim` — only HTML contents are preserved.
- [ ] `SingleQuoted()`
- [x] `SingleQuoted()` &rarr; via literal `` and `` curly quote characters.
- [x] `SmallCaps()` &rarr; `[span (html_style=`
- [x] `SoftBreak()` &rarr; `\n`
- [x] `Space()`
- [ ] `Span()`
- [ ] `Span()` &rarr; `[span` — WIP, but HTML Attributes are suppressed to due PMLC bug (See: [Issue #91]).
- [x] `Str()`
- [x] `Strikeout()`
- [x] `Strong()`
Expand Down Expand Up @@ -109,6 +111,16 @@ Some of pandoc TOC settings should be portable to PML via `[doc [options` sub-no

Problems that need addressing and which we are already aware of.

## Links Text

When rendering links as `[link` nodes, if we escape the contents of the `text` attributes it will also escape any formatting nodes therein, so I temporarily skipped escaping altogether, in order to preserve the tags.

But I noticed that the resulting PML code is not being styled, and I'm not sure if it's because the `[link` node doesn't support further styling. In any case, since pandoc supports styles in links text, I'll have to address the problem, by either:

- correctly styling link text according to PML rules,
- stripping away the styles, or
- rendering the links as raw HTML (when styled only?)

## Headings Conversion

When converting from pandoc markdown, the filter expects all in-document headings to be Level 1. If the author considers the document `title` (metadata) as the Level-1 entry, and uses Level 2 headings only withing the document (i.e. `##`) then the filter will produce an extra closing bracket at the document end, resulting in an error.
Expand All @@ -117,8 +129,13 @@ I need to find a way to track when there's a discrepancy between expected and ac

## List Start Number

The code that handles the list `start` attribute has been implemented in the filter, but it had to be circumvented because `html_start` crashes PMLC 3.1.0 due to a bug. (See: [Issue #91](https://github.com/pml-lang/pml-companion/issues/91))
The code that handles the list `start` attribute has been implemented in the filter, but it had to be circumvented because `html_start` crashes PMLC 3.1.0 due to a bug. (See: [Issue #91])

## HTML Attributes

Currently, due to the PMLC 3.1.0 bug mentioned in [Issue #91], which crashes PMLC when a node has multiple HTML attributes, we had to temporary suppress emitting HTML attributes in the output. This is affecting:

- `Span()` and [text highlighting via `.mark`](https://pandoc.org/MANUAL.html#highlighting)

# Code Improvements and Sanitation

Expand Down Expand Up @@ -149,4 +166,8 @@ The Lua writer code could still be optimized, and there are aspects of the conve

[node escaping rules]: https://www.pml-lang.dev/docs/user_manual/index.html#node_escape_characters "PML User Manual » Escape Characters » Nodes"

<!-- Issues & Discussions -->

[Issue #91]: https://github.com/pml-lang/pml-companion/issues/91 "Issue #91 — Bug in PMLC 3.1.0 causes crash with multiple HTML attributes"

<!-- EOF -->
21 changes: 21 additions & 0 deletions pandoc/filters-lua/pml-writer/tests/elements/mark.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: 'Span Test'
subtitle: "Rendering span nodes"
author: Tristano Ajmone
date: 2022-11-03
...


To highlight text, use the `mark` class (via `bracketed_spans` ext.):

> Some [highlighted text]{.mark} example.

Or, via the `native_spans` extension, only for HTML output:

> Some <span class="mark">highlighted text</span> example.

**References:**

- [Pandoc Manual » Highlighting](https://pandoc.org/MANUAL.html#highlighting)
- [`bracketed_spans`](https://pandoc.org/MANUAL.html#extension-bracketed_spans)
- [`native_spans`](https://pandoc.org/MANUAL.html#extension-native_spans)
30 changes: 30 additions & 0 deletions pandoc/filters-lua/pml-writer/tests/elements/mark.pml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[doc [title Span Test]
[subtitle Rendering span nodes]

Tristano Ajmone |
2022-11-03


To highlight text, use the [c mark] class (via [c bracketed_spans] ext.):

[quote
Some [span highlighted text] example.
]

Or, via the [c native_spans] extension, only for HTML output:

[quote
Some [span highlighted text] example.
]

[b References:]

[list
[el [link url=https://pandoc.org/MANUAL.html#highlighting text="Pandoc Manual » Highlighting"]]
[el [link url=https://pandoc.org/MANUAL.html#extension-bracketed_spans text="[c bracketed_spans]"]]
[el [link url=https://pandoc.org/MANUAL.html#extension-native_spans text="[c native_spans]"]]
]



]
11 changes: 11 additions & 0 deletions pandoc/filters-lua/pml-writer/tests/elements/quotes.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: 'Curly Quotes Test'
subtitle: "Rendering typographic single- and double-quotes."
author: Tristano Ajmone
date: 2022-11-03
...


'single quoted text'

"double quoted text"
14 changes: 14 additions & 0 deletions pandoc/filters-lua/pml-writer/tests/elements/quotes.pml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[doc [title Curly Quotes Test]
[subtitle Rendering typographic single- and double-quotes.]

Tristano Ajmone |
2022-11-03


‘single quoted text’

“double quoted text”



]
8 changes: 8 additions & 0 deletions pandoc/filters-lua/pml-writer/tests/elements/span.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: 'Span Test'
subtitle: "Rendering span nodes"
author: Tristano Ajmone
date: 2022-11-03
...

This is [spanned text]{.my_class} with class `my_class`.
12 changes: 12 additions & 0 deletions pandoc/filters-lua/pml-writer/tests/elements/span.pml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[doc [title Span Test]
[subtitle Rendering span nodes]

Tristano Ajmone |
2022-11-03


This is [span spanned text] with class [c my_class].



]
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ div contents</div>

and more text.

Next paragraph with a <span>span</span> and a word-thatincludesa<span>span</span>right?
Next paragraph with a [span span] and a word-thatincludesa[span span]right?

][ch [title Directionality]

Expand All @@ -24,9 +24,9 @@ rtl div contents</div>
and more text.

<div dir="ltr">
and a ltr div. with a <span dir="rtl">rtl span</span>.</div>
and a ltr div. with a [span rtl span].</div>

Next paragraph with a <span dir="rtl">rtl span</span> and a word-that-includesa<span dir="ltr">ltrspan</span>right?
Next paragraph with a [span rtl span] and a word-that-includesa[span ltrspan]right?

][ch [title Languages]

Expand All @@ -37,9 +37,9 @@ German div contents</div>

and more text.

Next paragraph with a <span lang="en-GB">British span</span> and a word-that-includesa<span lang="de-CH">Swiss German span</span>right?
Next paragraph with a [span British span] and a word-that-includesa[span Swiss German span]right?

Some <span lang="es">Spanish text</span>.
Some [span Spanish text].

][ch [title Combined]

Expand All @@ -50,7 +50,7 @@ French rtl div contents</div>

and more text.

Next paragraph with a <span lang="en-GB" dir="ltr">British ltr span</span> and a word-that-includesa<span lang="de-CH" dir="ltr">Swiss German ltr span</span>right?
Next paragraph with a [span British ltr span] and a word-that-includesa[span Swiss German ltr span]right?


]
Expand Down

0 comments on commit ca7c41e

Please sign in to comment.