1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
--------------------------------------------------------------------------------
{-# LANGUAGE CPP #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Patat.Presentation.Display
( displayPresentation
, displayPresentationError
, dumpPresentation
) where
--------------------------------------------------------------------------------
import Control.Applicative ((<$>))
import Control.Monad (mplus, unless)
import qualified Data.Aeson.Extended as A
import Data.Data.Extended (grecQ)
import Data.List (intersperse)
import Data.Maybe (fromMaybe)
import Data.Monoid (mconcat, mempty, (<>))
import qualified Data.Text as T
import Patat.Presentation.Display.CodeBlock
import Patat.Presentation.Display.Table
import Patat.Presentation.Internal
import Patat.PrettyPrint ((<$$>), (<+>))
import qualified Patat.PrettyPrint as PP
import Patat.Theme (Theme (..))
import qualified Patat.Theme as Theme
import qualified System.Console.ANSI as Ansi
import qualified System.Console.Terminal.Size as Terminal
import qualified Text.Pandoc.Extended as Pandoc
import Prelude
--------------------------------------------------------------------------------
-- | Display something within the presentation borders that draw the title and
-- the active slide number and so on.
displayWithBorders :: Presentation -> (Theme -> PP.Doc) -> IO ()
displayWithBorders Presentation {..} f = do
Ansi.clearScreen
Ansi.setCursorPosition 0 0
-- Get terminal width/title
mbWindow <- Terminal.size
let columns = fromMaybe 72 $
(A.unFlexibleNum <$> psColumns pSettings) `mplus`
(Terminal.width <$> mbWindow)
rows = fromMaybe 24 $
(A.unFlexibleNum <$> psRows pSettings) `mplus`
(Terminal.height <$> mbWindow)
let settings = pSettings {psColumns = Just $ A.FlexibleNum columns}
theme = fromMaybe Theme.defaultTheme (psTheme settings)
title = PP.toString (prettyInlines theme pTitle)
titleWidth = length title
titleOffset = (columns - titleWidth) `div` 2
borders = themed (themeBorders theme)
unless (null title) $ do
Ansi.setCursorColumn titleOffset
PP.putDoc $ borders $ PP.string title
putStrLn ""
putStrLn ""
PP.putDoc $ withWrapSettings settings $ f theme
putStrLn ""
let active = show (pActiveSlide + 1) ++ " / " ++ show (length pSlides)
activeWidth = length active
Ansi.setCursorPosition (rows - 2) 0
PP.putDoc $ " " <> borders (prettyInlines theme pAuthor)
Ansi.setCursorColumn (columns - activeWidth - 1)
PP.putDoc $ borders $ PP.string active
putStrLn ""
--------------------------------------------------------------------------------
displayPresentation :: Presentation -> IO ()
displayPresentation pres@Presentation {..} = displayWithBorders pres $ \theme ->
let slide = case drop pActiveSlide pSlides of
[] -> mempty
(s : _) -> s in
prettySlide theme slide
--------------------------------------------------------------------------------
-- | Displays an error in the place of the presentation. This is useful if we
-- want to display an error but keep the presentation running.
displayPresentationError :: Presentation -> String -> IO ()
displayPresentationError pres err = displayWithBorders pres $ \Theme {..} ->
themed themeStrong "Error occurred in the presentation:" <$$>
"" <$$>
(PP.string err)
--------------------------------------------------------------------------------
dumpPresentation :: Presentation -> IO ()
dumpPresentation pres =
let theme = fromMaybe Theme.defaultTheme (psTheme $ pSettings pres) in
PP.putDoc $ withWrapSettings (pSettings pres) $
PP.vcat $ intersperse "----------" $
map (prettySlide theme) $ pSlides pres
--------------------------------------------------------------------------------
withWrapSettings :: PresentationSettings -> PP.Doc -> PP.Doc
withWrapSettings ps = case (psWrap ps, psColumns ps) of
(Just True, Just (A.FlexibleNum col)) -> PP.wrapAt (Just col)
_ -> id
--------------------------------------------------------------------------------
prettySlide :: Theme -> Slide -> PP.Doc
prettySlide theme slide@(Slide blocks) =
prettyBlocks theme blocks <>
case prettyReferences theme slide of
[] -> mempty
refs -> PP.hardline <> PP.vcat refs
--------------------------------------------------------------------------------
prettyBlock :: Theme -> Pandoc.Block -> PP.Doc
prettyBlock theme (Pandoc.Plain inlines) = prettyInlines theme inlines
prettyBlock theme (Pandoc.Para inlines) =
prettyInlines theme inlines <> PP.hardline
prettyBlock theme@Theme {..} (Pandoc.Header i _ inlines) =
themed themeHeader (PP.string (replicate i '#') <+> prettyInlines theme inlines) <>
PP.hardline
prettyBlock theme (Pandoc.CodeBlock (_, classes, _) txt) =
prettyCodeBlock theme classes txt
prettyBlock theme (Pandoc.BulletList bss) = PP.vcat
[ PP.indent
(PP.NotTrimmable $ themed (themeBulletList theme) prefix)
(PP.Trimmable " ")
(prettyBlocks theme' bs)
| bs <- bss
] <> PP.hardline
where
prefix = " " <> PP.string [marker] <> " "
marker = case T.unpack <$> themeBulletListMarkers theme of
Just (x : _) -> x
_ -> '-'
-- Cycle the markers.
theme' = theme
{ themeBulletListMarkers =
(\ls -> T.drop 1 ls <> T.take 1 ls) <$> themeBulletListMarkers theme
}
prettyBlock theme@Theme {..} (Pandoc.OrderedList _ bss) = PP.vcat
[ PP.indent
(PP.NotTrimmable $ themed themeOrderedList $ PP.string prefix)
(PP.Trimmable " ")
(prettyBlocks theme bs)
| (prefix, bs) <- zip padded bss
] <> PP.hardline
where
padded = [n ++ replicate (4 - length n) ' ' | n <- numbers]
numbers =
[ show i ++ "."
| i <- [1 .. length bss]
]
prettyBlock _theme (Pandoc.RawBlock _ t) = PP.string t <> PP.hardline
prettyBlock _theme Pandoc.HorizontalRule = "---"
prettyBlock theme@Theme {..} (Pandoc.BlockQuote bs) =
let quote = PP.NotTrimmable (themed themeBlockQuote "> ") in
PP.indent quote quote (prettyBlocks theme bs)
prettyBlock theme@Theme {..} (Pandoc.DefinitionList terms) =
PP.vcat $ map prettyDefinition terms
where
prettyDefinition (term, definitions) =
themed themeDefinitionTerm (prettyInlines theme term) <$$>
PP.hardline <> PP.vcat
[ PP.indent
(PP.NotTrimmable (themed themeDefinitionList ": "))
(PP.Trimmable " ") $
prettyBlocks theme (Pandoc.plainToPara definition)
| definition <- definitions
]
prettyBlock theme (Pandoc.Table caption aligns _ headers rows) =
PP.wrapAt Nothing $
prettyTable theme Table
{ tCaption = prettyInlines theme caption
, tAligns = map align aligns
, tHeaders = map (prettyBlocks theme) headers
, tRows = map (map (prettyBlocks theme)) rows
}
where
align Pandoc.AlignLeft = PP.AlignLeft
align Pandoc.AlignCenter = PP.AlignCenter
align Pandoc.AlignDefault = PP.AlignLeft
align Pandoc.AlignRight = PP.AlignRight
prettyBlock theme (Pandoc.Div _attrs blocks) = prettyBlocks theme blocks
prettyBlock _theme Pandoc.Null = mempty
#if MIN_VERSION_pandoc(1,18,0)
-- 'LineBlock' elements are new in pandoc-1.18
prettyBlock theme@Theme {..} (Pandoc.LineBlock inliness) =
let ind = PP.NotTrimmable (themed themeLineBlock "| ") in
PP.wrapAt Nothing $
PP.indent ind ind $
PP.vcat $
map (prettyInlines theme) inliness
#endif
--------------------------------------------------------------------------------
prettyBlocks :: Theme -> [Pandoc.Block] -> PP.Doc
prettyBlocks theme = PP.vcat . map (prettyBlock theme)
--------------------------------------------------------------------------------
prettyInline :: Theme -> Pandoc.Inline -> PP.Doc
prettyInline _theme Pandoc.Space = PP.space
prettyInline _theme (Pandoc.Str str) = PP.string str
prettyInline theme@Theme {..} (Pandoc.Emph inlines) =
themed themeEmph $
prettyInlines theme inlines
prettyInline theme@Theme {..} (Pandoc.Strong inlines) =
themed themeStrong $
prettyInlines theme inlines
prettyInline Theme {..} (Pandoc.Code _ txt) =
themed themeCode $
" " <> PP.string txt <> " "
prettyInline theme@Theme {..} link@(Pandoc.Link _attrs text (target, _title))
| isReferenceLink link =
"[" <> themed themeLinkText (prettyInlines theme text) <> "]"
| otherwise =
"<" <> themed themeLinkTarget (PP.string target) <> ">"
prettyInline _theme Pandoc.SoftBreak = PP.softline
prettyInline _theme Pandoc.LineBreak = PP.hardline
prettyInline theme@Theme {..} (Pandoc.Strikeout t) =
"~~" <> themed themeStrikeout (prettyInlines theme t) <> "~~"
prettyInline theme@Theme {..} (Pandoc.Quoted Pandoc.SingleQuote t) =
"'" <> themed themeQuoted (prettyInlines theme t) <> "'"
prettyInline theme@Theme {..} (Pandoc.Quoted Pandoc.DoubleQuote t) =
"'" <> themed themeQuoted (prettyInlines theme t) <> "'"
prettyInline Theme {..} (Pandoc.Math _ t) =
themed themeMath (PP.string t)
prettyInline theme@Theme {..} (Pandoc.Image _attrs text (target, _title)) =
" <> ")"
-- These elements aren't really supported.
prettyInline theme (Pandoc.Cite _ t) = prettyInlines theme t
prettyInline theme (Pandoc.Span _ t) = prettyInlines theme t
prettyInline _theme (Pandoc.RawInline _ t) = PP.string t
prettyInline theme (Pandoc.Note t) = prettyBlocks theme t
prettyInline theme (Pandoc.Superscript t) = prettyInlines theme t
prettyInline theme (Pandoc.Subscript t) = prettyInlines theme t
prettyInline theme (Pandoc.SmallCaps t) = prettyInlines theme t
-- prettyInline unsupported = PP.ondullred $ PP.string $ show unsupported
--------------------------------------------------------------------------------
prettyInlines :: Theme -> [Pandoc.Inline] -> PP.Doc
prettyInlines theme = mconcat . map (prettyInline theme)
--------------------------------------------------------------------------------
prettyReferences :: Theme -> Slide -> [PP.Doc]
prettyReferences theme@Theme {..} =
map prettyReference . getReferences . unSlide
where
getReferences :: [Pandoc.Block] -> [Pandoc.Inline]
getReferences = filter isReferenceLink . grecQ
prettyReference :: Pandoc.Inline -> PP.Doc
prettyReference (Pandoc.Link _attrs text (target, title)) =
"[" <>
themed themeLinkText (prettyInlines theme $ Pandoc.newlineToSpace text) <>
"](" <>
themed themeLinkTarget (PP.string target) <>
(if null title
then mempty
else PP.space <> "\"" <> PP.string title <> "\"")
<> ")"
prettyReference _ = mempty
--------------------------------------------------------------------------------
isReferenceLink :: Pandoc.Inline -> Bool
isReferenceLink (Pandoc.Link _attrs text (target, _)) =
[Pandoc.Str target] /= text
isReferenceLink _ = False
|