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

rename eval.wrap to eval.container #167

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ empty list `[]`.

#### Native Images support

`patat-0.8.0.0` and newer include images support for some terminal emulators.
Version 0.8 and later include images support for some terminal emulators.

```markdown
---
Expand Down Expand Up @@ -656,7 +656,7 @@ write ASCII escape codes directly to the screen with
[code evaluation](#evaluating-code).

In order to do that, for example, we could configure `kitten` code snippets
to evaluate using [Kitty]'s command `icat`. This uses the `rawInline` code
to evaluate using [Kitty]'s command `icat`. This uses the `none` container
setting to ensure that the resulting output is not wrapped in a code block,
and the `fragment` and `replace` settings immediately replace the snippet.

Expand All @@ -667,7 +667,7 @@ and the `fragment` and `replace` settings immediately replace the snippet.
command: sed 's/^/kitten /' | bash
replace: true
fragment: false
wrap: rawInline
container: none
...

See, for example:
Expand Down Expand Up @@ -712,7 +712,7 @@ _evaluator_ by specifying this in the YAML metadata:
command: irb --noecho --noverbose
fragment: true # Optional
replace: false # Optional
wrap: code # Optional
container: code # Optional
...

Here is an example of a code block that is evaluated:
Expand All @@ -728,18 +728,20 @@ attribute on a code block matches the evaluator, it will be used.
code of presentations downloaded from the internet before running them if they
contain `eval` settings.

Aside from the command, there are two more options:
Aside from the command, there are three more options:

- `fragment`: Introduce a pause (see [fragments](#fragmented-slides)) in
between showing the original code block and the output. Defaults to `true`.
- `replace`: Remove the original code block and replace it with the output
rather than appending the output in a new code block. Defaults to `false`.
- `wrap`: By default, the output is wrapped in a code block again with the
original syntax highlighting. You can customize this behaviour by setting
`wrap` to:
- `container`: By default, the output is wrapped in a code block again with
the original syntax highlighting. You can customize this behaviour by
setting `container` to:
* `code`: the default setting.
* `raw`: no formatting applied.
* `rawInline`: no formatting applied and no trailing newline.
* `none`: no formatting applied.
* `inline`: no formatting applied and no trailing newline.
- `wrap`: this is a deprecated name for `container`, used in version 0.11 and
earlier.

Setting `fragment: false` and `replace: true` offers a way to "filter" code
blocks, which can be used to render ASCII graphics.
Expand Down
8 changes: 4 additions & 4 deletions lib/Patat/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ evalBlock settings orig@(Pandoc.CodeBlock attr@(_, classes, _) txt)
evalCommand <> ": exit code " <> T.pack (show i) <> "\n" <>
erStderr
let fmt = "eval"
blocks = case evalWrap of
EvalWrapCode -> [Pandoc.CodeBlock attr out]
EvalWrapRaw -> [Pandoc.RawBlock fmt out]
EvalWrapRawInline -> [Pandoc.Plain [Pandoc.RawInline fmt out]]
blocks = case evalContainer of
EvalContainerCode -> [Pandoc.CodeBlock attr out]
EvalContainerNone -> [Pandoc.RawBlock fmt out]
EvalContainerInline -> [Pandoc.Plain [Pandoc.RawInline fmt out]]
pure $ case (evalFragment, evalReplace) of
(False, True) -> [Append blocks]
(False, False) -> [Append (orig : blocks)]
Expand Down
48 changes: 31 additions & 17 deletions lib/Patat/Presentation/Settings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Patat.Presentation.Settings
, ImageSettings (..)

, EvalSettingsMap
, EvalSettingsWrap (..)
, EvalSettingsContainer (..)
, EvalSettings (..)

, SpeakerNotesSettings (..)
Expand Down Expand Up @@ -221,38 +221,52 @@ type EvalSettingsMap = HMS.HashMap T.Text EvalSettings


--------------------------------------------------------------------------------
data EvalSettingsWrap
= EvalWrapCode
| EvalWrapRaw
| EvalWrapRawInline
data EvalSettingsContainer
= EvalContainerCode
| EvalContainerNone
| EvalContainerInline
deriving (Show)


--------------------------------------------------------------------------------
instance A.FromJSON EvalSettingsWrap where
parseJSON = A.withText "FromJSON EvalSettingsWrap" $ \txt -> case txt of
"code" -> pure EvalWrapCode
"raw" -> pure EvalWrapRaw
"rawInline" -> pure EvalWrapRawInline
_ -> fail $ "unknown wrap: " <> show txt
instance A.FromJSON EvalSettingsContainer where
parseJSON = A.withText "FromJSON EvalSettingsContainer" $ \t -> case t of
"code" -> pure EvalContainerCode
"none" -> pure EvalContainerNone
"inline" -> pure EvalContainerInline
-- Deprecated names
"raw" -> pure EvalContainerNone
"rawInline" -> pure EvalContainerInline
_ -> fail $ "unknown container: " <> show t


--------------------------------------------------------------------------------
data EvalSettings = EvalSettings
{ evalCommand :: !T.Text
, evalReplace :: !Bool
, evalFragment :: !Bool
, evalWrap :: !EvalSettingsWrap
{ evalCommand :: !T.Text
, evalReplace :: !Bool
, evalFragment :: !Bool
, evalContainer :: !EvalSettingsContainer
} deriving (Show)


--------------------------------------------------------------------------------
instance A.FromJSON EvalSettings where
parseJSON = A.withObject "FromJSON EvalSettings" $ \o -> EvalSettings
<$> o A..: "command"
<$> o A..: "command"
<*> o A..:? "replace" A..!= False
<*> o A..:? "fragment" A..!= True
<*> o A..:? "wrap" A..!= EvalWrapCode
<*> deprecated "wrap" "container" EvalContainerCode o
where
deprecated old new def obj = do
mo <- obj A..:? old
mn <- obj A..:? new
case (mo, mn) of
(Just _, Just _) -> fail $
show old ++ " (deprecated) and " ++ show new ++ " " ++
"are both specified, please remove " ++ show old
(Just o, Nothing) -> pure o
(Nothing, Just n) -> pure n
(Nothing, Nothing) -> pure def


--------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion tests/golden/inputs/eval06.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ patat:
eval:
shImplicit:
command: sh
wrap: code
replace: true
fragment: false
shCode:
Expand Down
51 changes: 51 additions & 0 deletions tests/golden/inputs/eval07.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
patat:
eval:
shImplicit:
command: sh
replace: true
fragment: false
shCode:
command: sh
container: code
replace: true
fragment: false
shNone:
command: sh
container: none
replace: true
fragment: false
shInline:
command: sh
container: inline
replace: true
fragment: false
...

# Implicit eval slide

~~~{.shImplicit}
printf '\e[1;34m%-6s\e[m' "This is text"
~~~

# Code eval slide

~~~{.shCode}
printf '\e[1;34m%-6s\e[m' "This is text"
~~~

# None eval slide

~~~{.shNone}
printf '\e[1;34m%-6s\e[m' "This is text"
~~~

Newline here...

# Inline eval slide

~~~{.shInline}
printf '\e[1;34m%-6s\e[m' "This is text"
~~~

No newline here...
41 changes: 41 additions & 0 deletions tests/golden/outputs/eval07.md.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
 eval07.md 

# Implicit eval slide

  
  This is text 
  

 1 / 4 

{slide}
 eval07.md 

# Code eval slide

  
  This is text 
  

 2 / 4 

{slide}
 eval07.md 

# None eval slide

This is text

Newline here...

 3 / 4 

{slide}
 eval07.md 

# Inline eval slide

This is text
No newline here...

 4 / 4 
Loading