Skip to content

Commit

Permalink
Merge pull request #19 from Omikhleia/markdown-inputter-options
Browse files Browse the repository at this point in the history
Ability to enable/disable Markdown extensions
  • Loading branch information
Omikhleia authored Nov 13, 2022
2 parents 2f69ba1 + 5a379ae commit cd90084
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
25 changes: 22 additions & 3 deletions examples/sile-and-markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ authors alike.
Within SILE’s aim to produce beautiful printed documents, it's a pretty reasonable assumption that
such writers and authors may want to use this fine typesetter with their Markdown content, without
having to learn the SILE language and its specifics (but not, either, fully excluding it for some
advanced capabilities).
advanced capabilities). Guess what, the very chapter you are currently reading is written in Markdown!

There is actually more than one solution to achieve great results in that direction:

Expand Down Expand Up @@ -49,7 +49,7 @@ Some Markdown content
\end{raw}
```

Guess what, the very chapter you are currently reading is written in Markdown!
See also "[Configuration](#configuration)" further below.

### Basic typesetting {#basic-typesetting}

Expand All @@ -68,7 +68,7 @@ produce an horizontal rule.
Hard line breaks...\
... are supported too, either using the standard "invisible" method from Markdown (i.e. two trailing
spaces at the end of a line) or a backslash-escaped newline (i.e. a backslash occurring at the
end of a line, as in the corresponding default Pandoc extension).
end of a line).

Several Pandoc-like extensions to Markdown are also supported.
Notably, the converter comes by default with smart typography enabled: three dashes (`---`) in an
Expand Down Expand Up @@ -370,6 +370,25 @@ SILE.typesetter:typeset(" is called from Lua.")
You now have the best of two worlds in your hands, bridging together Markdown and SILE
so that you can achieve wonderful things, we have no idea of. Surprise us!

### Configuration {#configuration}

Most Markdown syntax extensions are enabled by default. You can pass additional options to
the `\autodoc:command{\include}`{=sile} command or the `\autodoc:environment[check=false]{raw}`{=sile}
environment to tune the behavior of the Markdown parser.

::: {custom-style=raggedright}
> Available options are: `smart`, `strikeout`, `subscript`, `superscript`, `definition_lists`, `notes`, `inline_notes`,
> `fenced_code_blocks`, `fenced_code_attributes`, `bracketed_spans`, `fenced_divs`, `raw_attribute`, `link_attributes`,
> `startnum`, `fancy_lists`, `task_list`, `hash_enumerators`, `table_captions`, `pipe_tables`, `header_attributes`,
> `line_blocks`, `escaped_line_breaks`.
:::

For instance, to disable the smart typography feature:

```
\include[src=somefile.pandoc, smart=false]
```

## The Pandoc-based converters

Pandoc is a free-software document converter, created by the same John MacFarlane who
Expand Down
28 changes: 19 additions & 9 deletions inputters/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,11 @@ function inputter.appropriate (round, filename, _)
-- though it won't meet expectations!
end

function inputter.parse (_, doc)
local lunamark = require("lunamark")
local reader = lunamark.reader.markdown
local writer = SileAstWriter({
layout = "minimize" -- The default layout is to output \n\n as inter-block separator
-- Let's cancel it completely, and insert our own \par where needed.
})
local parse = reader.new(writer, {
function inputter:parse (doc)
local extensions = {
smart = true,
strikeout = true,
subscript=true,
subscript = true,
superscript = true,
definition_lists = true,
notes = true,
Expand All @@ -332,7 +326,23 @@ function inputter.parse (_, doc)
header_attributes = true,
line_blocks = true,
escaped_line_breaks = true,
}
for k, v in pairs(self.options) do
-- Allow overriding known options
-- (Lunamark has more options than that, but I haven't test disabling anything else,
-- so let's be safer by not having them here for now.)
if extensions[k] then
extensions[k] = SU.boolean(v, true)
end
end

local lunamark = require("lunamark")
local reader = lunamark.reader.markdown
local writer = SileAstWriter({
layout = "minimize" -- The default layout is to output \n\n as inter-block separator
-- Let's cancel it completely, and insert our own \par where needed.
})
local parse = reader.new(writer, extensions)
local tree = parse(doc)
-- The Markdown parsing returns a string or a SILE AST table.
-- Wrap it in some document structure so we can just process it, and if at
Expand Down
4 changes: 2 additions & 2 deletions packages/markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ end

function package:registerRawHandlers ()

self.class:registerRawHandler("markdown", function(_, content)
SILE.processString(content[1], "markdown", nil, {})
self.class:registerRawHandler("markdown", function(options, content)
SILE.processString(content[1], "markdown", nil, options)
end)

end
Expand Down

0 comments on commit cd90084

Please sign in to comment.