{-# LANGUAGE CPP #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Patat.Presentation.Display
( displayPresentation
, displayPresentationError
, dumpPresentation
) where
import Control.Monad (mplus, unless)
import qualified Data.Aeson.Extended as A
import Data.Data.Extended (grecQ)
import qualified Data.List as L
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import Patat.Cleanup
import qualified Patat.Images as Images
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 Prelude
import qualified System.Console.ANSI as Ansi
import qualified System.Console.Terminal.Size as Terminal
import qualified System.IO as IO
import qualified Text.Pandoc.Extended as Pandoc
data CanvasSize = CanvasSize {CanvasSize -> Int
csRows :: Int, CanvasSize -> Int
csCols :: Int} deriving (Int -> CanvasSize -> ShowS
[CanvasSize] -> ShowS
CanvasSize -> String
(Int -> CanvasSize -> ShowS)
-> (CanvasSize -> String)
-> ([CanvasSize] -> ShowS)
-> Show CanvasSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CanvasSize] -> ShowS
$cshowList :: [CanvasSize] -> ShowS
show :: CanvasSize -> String
$cshow :: CanvasSize -> String
showsPrec :: Int -> CanvasSize -> ShowS
$cshowsPrec :: Int -> CanvasSize -> ShowS
Show)
displayWithBorders
:: Presentation -> (CanvasSize -> Theme -> PP.Doc) -> IO Cleanup
displayWithBorders :: Presentation -> (CanvasSize -> Theme -> Doc) -> IO Cleanup
displayWithBorders Presentation {..} f :: CanvasSize -> Theme -> Doc
f = do
Cleanup
Ansi.clearScreen
Int -> Int -> Cleanup
Ansi.setCursorPosition 0 0
Maybe (Window Int)
mbWindow <- IO (Maybe (Window Int))
forall n. Integral n => IO (Maybe (Window n))
Terminal.size
let columns :: Int
columns = Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe 72 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$
(FlexibleNum Int -> Int
forall a. FlexibleNum a -> a
A.unFlexibleNum (FlexibleNum Int -> Int) -> Maybe (FlexibleNum Int) -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PresentationSettings -> Maybe (FlexibleNum Int)
psColumns PresentationSettings
pSettings) Maybe Int -> Maybe Int -> Maybe Int
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
(Window Int -> Int
forall a. Window a -> a
Terminal.width (Window Int -> Int) -> Maybe (Window Int) -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Window Int)
mbWindow)
rows :: Int
rows = Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe 24 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$
(FlexibleNum Int -> Int
forall a. FlexibleNum a -> a
A.unFlexibleNum (FlexibleNum Int -> Int) -> Maybe (FlexibleNum Int) -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PresentationSettings -> Maybe (FlexibleNum Int)
psRows PresentationSettings
pSettings) Maybe Int -> Maybe Int -> Maybe Int
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
(Window Int -> Int
forall a. Window a -> a
Terminal.height (Window Int -> Int) -> Maybe (Window Int) -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Window Int)
mbWindow)
let settings :: PresentationSettings
settings = PresentationSettings
pSettings {psColumns :: Maybe (FlexibleNum Int)
psColumns = FlexibleNum Int -> Maybe (FlexibleNum Int)
forall a. a -> Maybe a
Just (FlexibleNum Int -> Maybe (FlexibleNum Int))
-> FlexibleNum Int -> Maybe (FlexibleNum Int)
forall a b. (a -> b) -> a -> b
$ Int -> FlexibleNum Int
forall a. a -> FlexibleNum a
A.FlexibleNum Int
columns}
theme :: Theme
theme = Theme -> Maybe Theme -> Theme
forall a. a -> Maybe a -> a
fromMaybe Theme
Theme.defaultTheme (PresentationSettings -> Maybe Theme
psTheme PresentationSettings
settings)
title :: String
title = Doc -> String
PP.toString (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
pTitle)
titleWidth :: Int
titleWidth = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
title
titleOffset :: Int
titleOffset = (Int
columns Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
titleWidth) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2
borders :: Doc -> Doc
borders = Maybe Style -> Doc -> Doc
themed (Theme -> Maybe Style
themeBorders Theme
theme)
Bool -> Cleanup -> Cleanup
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
title) (Cleanup -> Cleanup) -> Cleanup -> Cleanup
forall a b. (a -> b) -> a -> b
$ do
let titleRemainder :: Int
titleRemainder = Int
columns Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
titleWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
titleOffset
wrappedTitle :: Doc
wrappedTitle = Int -> Doc
PP.spaces Int
titleOffset Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
PP.string String
title Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Int -> Doc
PP.spaces Int
titleRemainder
Doc -> Cleanup
PP.putDoc (Doc -> Cleanup) -> Doc -> Cleanup
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
borders Doc
wrappedTitle
String -> Cleanup
putStrLn ""
String -> Cleanup
putStrLn ""
let canvasSize :: CanvasSize
canvasSize = Int -> Int -> CanvasSize
CanvasSize (Int
rows Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2) Int
columns
Doc -> Cleanup
PP.putDoc (Doc -> Cleanup) -> Doc -> Cleanup
forall a b. (a -> b) -> a -> b
$ PresentationSettings -> Doc -> Doc
formatWith PresentationSettings
settings (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ CanvasSize -> Theme -> Doc
f CanvasSize
canvasSize Theme
theme
String -> Cleanup
putStrLn ""
let (sidx :: Int
sidx, _) = Index
pActiveFragment
active :: String
active = Int -> String
forall a. Show a => a -> String
show (Int
sidx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) String -> ShowS
forall a. [a] -> [a] -> [a]
++ " / " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show ([Slide] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Slide]
pSlides)
activeWidth :: Int
activeWidth = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
active
author :: String
author = Doc -> String
PP.toString (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
pAuthor)
authorWidth :: Int
authorWidth = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
author
middleSpaces :: Doc
middleSpaces = Int -> Doc
PP.spaces (Int -> Doc) -> Int -> Doc
forall a b. (a -> b) -> a -> b
$ Int
columns Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
activeWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
authorWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- 2
Int -> Int -> Cleanup
Ansi.setCursorPosition (Int
rows Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) 0
Doc -> Cleanup
PP.putDoc (Doc -> Cleanup) -> Doc -> Cleanup
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
borders (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
PP.space Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
PP.string String
author Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
middleSpaces Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
PP.string String
active Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
PP.space
Handle -> Cleanup
IO.hFlush Handle
IO.stdout
Cleanup -> IO Cleanup
forall (m :: * -> *) a. Monad m => a -> m a
return Cleanup
forall a. Monoid a => a
mempty
displayImage :: Images.Handle -> FilePath -> IO Cleanup
displayImage :: Handle -> String -> IO Cleanup
displayImage images :: Handle
images path :: String
path = do
Cleanup
Ansi.clearScreen
Int -> Int -> Cleanup
Ansi.setCursorPosition 0 0
String -> Cleanup
putStrLn ""
Handle -> Cleanup
IO.hFlush Handle
IO.stdout
Handle -> String -> IO Cleanup
Images.drawImage Handle
images String
path
displayPresentation :: Maybe Images.Handle -> Presentation -> IO Cleanup
displayPresentation :: Maybe Handle -> Presentation -> IO Cleanup
displayPresentation mbImages :: Maybe Handle
mbImages pres :: Presentation
pres@Presentation {..} =
case Presentation -> Maybe ActiveFragment
getActiveFragment Presentation
pres of
Nothing -> Presentation -> (CanvasSize -> Theme -> Doc) -> IO Cleanup
displayWithBorders Presentation
pres CanvasSize -> Theme -> Doc
forall a. Monoid a => a
mempty
Just (ActiveContent fragment :: Fragment
fragment)
| Just images :: Handle
images <- Maybe Handle
mbImages
, Just image :: Text
image <- Fragment -> Maybe Text
onlyImage Fragment
fragment ->
Handle -> String -> IO Cleanup
displayImage Handle
images (String -> IO Cleanup) -> String -> IO Cleanup
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
image
Just (ActiveContent fragment :: Fragment
fragment) ->
Presentation -> (CanvasSize -> Theme -> Doc) -> IO Cleanup
displayWithBorders Presentation
pres ((CanvasSize -> Theme -> Doc) -> IO Cleanup)
-> (CanvasSize -> Theme -> Doc) -> IO Cleanup
forall a b. (a -> b) -> a -> b
$ \_canvasSize :: CanvasSize
_canvasSize theme :: Theme
theme ->
Theme -> Fragment -> Doc
prettyFragment Theme
theme Fragment
fragment
Just (ActiveTitle block :: Block
block) ->
Presentation -> (CanvasSize -> Theme -> Doc) -> IO Cleanup
displayWithBorders Presentation
pres ((CanvasSize -> Theme -> Doc) -> IO Cleanup)
-> (CanvasSize -> Theme -> Doc) -> IO Cleanup
forall a b. (a -> b) -> a -> b
$ \canvasSize :: CanvasSize
canvasSize theme :: Theme
theme ->
let pblock :: Doc
pblock = Theme -> Block -> Doc
prettyBlock Theme
theme Block
block
(prows :: Int
prows, pcols :: Int
pcols) = Doc -> Index
PP.dimensions Doc
pblock
(mLeft :: Int
mLeft, mRight :: Int
mRight) = PresentationSettings -> Index
marginsOf PresentationSettings
pSettings
offsetRow :: Int
offsetRow = (CanvasSize -> Int
csRows CanvasSize
canvasSize Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
prows Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2)
offsetCol :: Int
offsetCol = ((CanvasSize -> Int
csCols CanvasSize
canvasSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
mLeft Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
mRight) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2) Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
pcols Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` 2)
spaces :: Trimmable Doc
spaces = Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Doc -> Trimmable Doc) -> Doc -> Trimmable Doc
forall a b. (a -> b) -> a -> b
$ Int -> Doc
PP.spaces Int
offsetCol in
[Doc] -> Doc
forall a. Monoid a => [a] -> a
mconcat (Int -> Doc -> [Doc]
forall a. Int -> a -> [a]
replicate (Int
offsetRow Int -> Int -> Int
forall a. Num a => a -> a -> a
- 3) Doc
PP.hardline) Doc -> Doc -> Doc
<$$>
Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent Trimmable Doc
spaces Trimmable Doc
spaces Doc
pblock
where
onlyImage :: Fragment -> Maybe Text
onlyImage (Fragment blocks :: [Block]
blocks)
| [Pandoc.Para para :: [Inline]
para] <- (Block -> Bool) -> [Block] -> [Block]
forall a. (a -> Bool) -> [a] -> [a]
filter Block -> Bool
isVisibleBlock [Block]
blocks
, [Pandoc.Image _ _ (target :: Text
target, _)] <- [Inline]
para =
Text -> Maybe Text
forall a. a -> Maybe a
Just Text
target
onlyImage (Fragment blocks :: [Block]
blocks)
| [Pandoc.Header _ _ _, Pandoc.Para para :: [Inline]
para] <- (Block -> Bool) -> [Block] -> [Block]
forall a. (a -> Bool) -> [a] -> [a]
filter Block -> Bool
isVisibleBlock [Block]
blocks
, [Pandoc.Image _ _ (target :: Text
target, _)] <- [Inline]
para =
Text -> Maybe Text
forall a. a -> Maybe a
Just Text
target
onlyImage _ = Maybe Text
forall a. Maybe a
Nothing
displayPresentationError :: Presentation -> String -> IO Cleanup
displayPresentationError :: Presentation -> String -> IO Cleanup
displayPresentationError pres :: Presentation
pres err :: String
err = Presentation -> (CanvasSize -> Theme -> Doc) -> IO Cleanup
displayWithBorders Presentation
pres ((CanvasSize -> Theme -> Doc) -> IO Cleanup)
-> (CanvasSize -> Theme -> Doc) -> IO Cleanup
forall a b. (a -> b) -> a -> b
$ \_ Theme {..} ->
Maybe Style -> Doc -> Doc
themed Maybe Style
themeStrong "Error occurred in the presentation:" Doc -> Doc -> Doc
<$$>
"" Doc -> Doc -> Doc
<$$>
(String -> Doc
PP.string String
err)
dumpPresentation :: Presentation -> IO ()
dumpPresentation :: Presentation -> Cleanup
dumpPresentation pres :: Presentation
pres =
let settings :: PresentationSettings
settings = Presentation -> PresentationSettings
pSettings Presentation
pres
theme :: Theme
theme = Theme -> Maybe Theme -> Theme
forall a. a -> Maybe a -> a
fromMaybe Theme
Theme.defaultTheme (PresentationSettings -> Maybe Theme
psTheme (PresentationSettings -> Maybe Theme)
-> PresentationSettings -> Maybe Theme
forall a b. (a -> b) -> a -> b
$ PresentationSettings
settings) in
Doc -> Cleanup
PP.putDoc (Doc -> Cleanup) -> Doc -> Cleanup
forall a b. (a -> b) -> a -> b
$ PresentationSettings -> Doc -> Doc
formatWith PresentationSettings
settings (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
[Doc] -> Doc
PP.vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
L.intersperse "----------" ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ do
Slide
slide <- Presentation -> [Slide]
pSlides Presentation
pres
Doc -> [Doc]
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc -> [Doc]) -> Doc -> [Doc]
forall a b. (a -> b) -> a -> b
$ case Slide
slide of
TitleSlide block :: Block
block -> "~~~title" Doc -> Doc -> Doc
<$$> Theme -> Block -> Doc
prettyBlock Theme
theme Block
block
ContentSlide fragments :: [Fragment]
fragments -> [Doc] -> Doc
PP.vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
L.intersperse "~~~frag" ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ do
Fragment
fragment <- [Fragment]
fragments
Doc -> [Doc]
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc -> [Doc]) -> Doc -> [Doc]
forall a b. (a -> b) -> a -> b
$ Theme -> Fragment -> Doc
prettyFragment Theme
theme Fragment
fragment
formatWith :: PresentationSettings -> PP.Doc -> PP.Doc
formatWith :: PresentationSettings -> Doc -> Doc
formatWith ps :: PresentationSettings
ps = Doc -> Doc
wrap (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc
indent
where
(marginLeft :: Int
marginLeft, marginRight :: Int
marginRight) = PresentationSettings -> Index
marginsOf PresentationSettings
ps
wrap :: Doc -> Doc
wrap = case (PresentationSettings -> Maybe Bool
psWrap PresentationSettings
ps, PresentationSettings -> Maybe (FlexibleNum Int)
psColumns PresentationSettings
ps) of
(Just True, Just (A.FlexibleNum col :: Int
col)) -> Maybe Int -> Doc -> Doc
PP.wrapAt (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
marginRight)
_ -> Doc -> Doc
forall a. a -> a
id
spaces :: Trimmable Doc
spaces = Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Doc -> Trimmable Doc) -> Doc -> Trimmable Doc
forall a b. (a -> b) -> a -> b
$ Int -> Doc
PP.spaces Int
marginLeft
indent :: Doc -> Doc
indent = Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent Trimmable Doc
spaces Trimmable Doc
spaces
prettyFragment :: Theme -> Fragment -> PP.Doc
prettyFragment :: Theme -> Fragment -> Doc
prettyFragment theme :: Theme
theme fragment :: Fragment
fragment@(Fragment blocks :: [Block]
blocks) =
Theme -> [Block] -> Doc
prettyBlocks Theme
theme [Block]
blocks Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
case Theme -> Fragment -> [Doc]
prettyReferences Theme
theme Fragment
fragment of
[] -> Doc
forall a. Monoid a => a
mempty
refs :: [Doc]
refs -> Doc
PP.hardline Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> [Doc] -> Doc
PP.vcat [Doc]
refs
prettyBlock :: Theme -> Pandoc.Block -> PP.Doc
prettyBlock :: Theme -> Block -> Doc
prettyBlock theme :: Theme
theme (Pandoc.Plain inlines :: [Inline]
inlines) = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
inlines
prettyBlock theme :: Theme
theme (Pandoc.Para inlines :: [Inline]
inlines) =
Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
inlines Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
PP.hardline
prettyBlock theme :: Theme
theme@Theme {..} (Pandoc.Header i :: Int
i _ inlines :: [Inline]
inlines) =
Maybe Style -> Doc -> Doc
themed Maybe Style
themeHeader (String -> Doc
PP.string (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
i '#') Doc -> Doc -> Doc
<+> Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
inlines) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
Doc
PP.hardline
prettyBlock theme :: Theme
theme (Pandoc.CodeBlock (_, classes :: [Text]
classes, _) txt :: Text
txt) =
Theme -> [Text] -> Text -> Doc
prettyCodeBlock Theme
theme [Text]
classes Text
txt
prettyBlock theme :: Theme
theme (Pandoc.BulletList bss :: [[Block]]
bss) = [Doc] -> Doc
PP.vcat
[ Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent
(Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Doc -> Trimmable Doc) -> Doc -> Trimmable Doc
forall a b. (a -> b) -> a -> b
$ Maybe Style -> Doc -> Doc
themed (Theme -> Maybe Style
themeBulletList Theme
theme) Doc
prefix)
(Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.Trimmable " ")
(Theme -> [Block] -> Doc
prettyBlocks Theme
theme' [Block]
bs)
| [Block]
bs <- [[Block]]
bss
] Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
PP.hardline
where
prefix :: Doc
prefix = " " Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
PP.string [Char
marker] Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> " "
marker :: Char
marker = case Text -> String
T.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Theme -> Maybe Text
themeBulletListMarkers Theme
theme of
Just (x :: Char
x : _) -> Char
x
_ -> '-'
theme' :: Theme
theme' = Theme
theme
{ themeBulletListMarkers :: Maybe Text
themeBulletListMarkers =
(\ls :: Text
ls -> Int -> Text -> Text
T.drop 1 Text
ls Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.take 1 Text
ls) (Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Theme -> Maybe Text
themeBulletListMarkers Theme
theme
}
prettyBlock theme :: Theme
theme@Theme {..} (Pandoc.OrderedList _ bss :: [[Block]]
bss) = [Doc] -> Doc
PP.vcat
[ Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent
(Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Doc -> Trimmable Doc) -> Doc -> Trimmable Doc
forall a b. (a -> b) -> a -> b
$ Maybe Style -> Doc -> Doc
themed Maybe Style
themeOrderedList (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Doc
PP.string String
prefix)
(Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.Trimmable " ")
(Theme -> [Block] -> Doc
prettyBlocks Theme
theme [Block]
bs)
| (prefix :: String
prefix, bs :: [Block]
bs) <- [String] -> [[Block]] -> [(String, [Block])]
forall a b. [a] -> [b] -> [(a, b)]
zip [String]
padded [[Block]]
bss
] Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
PP.hardline
where
padded :: [String]
padded = [String
n String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate (4 Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
n) ' ' | String
n <- [String]
numbers]
numbers :: [String]
numbers =
[ Int -> String
forall a. Show a => a -> String
show Int
i String -> ShowS
forall a. [a] -> [a] -> [a]
++ "."
| Int
i <- [1 .. [[Block]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Block]]
bss]
]
prettyBlock _theme :: Theme
_theme (Pandoc.RawBlock _ t :: Text
t) = Text -> Doc
PP.text Text
t Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
PP.hardline
prettyBlock _theme :: Theme
_theme Pandoc.HorizontalRule = "---"
prettyBlock theme :: Theme
theme@Theme {..} (Pandoc.BlockQuote bs :: [Block]
bs) =
let quote :: Trimmable Doc
quote = Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Maybe Style -> Doc -> Doc
themed Maybe Style
themeBlockQuote "> ") in
Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent Trimmable Doc
quote Trimmable Doc
quote (Theme -> [Block] -> Doc
prettyBlocks Theme
theme [Block]
bs)
prettyBlock theme :: Theme
theme@Theme {..} (Pandoc.DefinitionList terms :: [([Inline], [[Block]])]
terms) =
[Doc] -> Doc
PP.vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (([Inline], [[Block]]) -> Doc) -> [([Inline], [[Block]])] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ([Inline], [[Block]]) -> Doc
prettyDefinition [([Inline], [[Block]])]
terms
where
prettyDefinition :: ([Inline], [[Block]]) -> Doc
prettyDefinition (term :: [Inline]
term, definitions :: [[Block]]
definitions) =
Maybe Style -> Doc -> Doc
themed Maybe Style
themeDefinitionTerm (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
term) Doc -> Doc -> Doc
<$$>
Doc
PP.hardline Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> [Doc] -> Doc
PP.vcat
[ Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent
(Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Maybe Style -> Doc -> Doc
themed Maybe Style
themeDefinitionList ": "))
(Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.Trimmable " ") (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
Theme -> [Block] -> Doc
prettyBlocks Theme
theme ([Block] -> [Block]
Pandoc.plainToPara [Block]
definition)
| [Block]
definition <- [[Block]]
definitions
]
prettyBlock theme :: Theme
theme (Pandoc.Table caption :: [Inline]
caption aligns :: [Alignment]
aligns _ headers :: [[Block]]
headers rows :: [[[Block]]]
rows) =
Maybe Int -> Doc -> Doc
PP.wrapAt Maybe Int
forall a. Maybe a
Nothing (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
Theme -> Table -> Doc
prettyTable Theme
theme Table :: Doc -> [Alignment] -> [Doc] -> [[Doc]] -> Table
Table
{ tCaption :: Doc
tCaption = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
caption
, tAligns :: [Alignment]
tAligns = (Alignment -> Alignment) -> [Alignment] -> [Alignment]
forall a b. (a -> b) -> [a] -> [b]
map Alignment -> Alignment
align [Alignment]
aligns
, tHeaders :: [Doc]
tHeaders = ([Block] -> Doc) -> [[Block]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Theme -> [Block] -> Doc
prettyBlocks Theme
theme) [[Block]]
headers
, tRows :: [[Doc]]
tRows = ([[Block]] -> [Doc]) -> [[[Block]]] -> [[Doc]]
forall a b. (a -> b) -> [a] -> [b]
map (([Block] -> Doc) -> [[Block]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Theme -> [Block] -> Doc
prettyBlocks Theme
theme)) [[[Block]]]
rows
}
where
align :: Alignment -> Alignment
align Pandoc.AlignLeft = Alignment
PP.AlignLeft
align Pandoc.AlignCenter = Alignment
PP.AlignCenter
align Pandoc.AlignDefault = Alignment
PP.AlignLeft
align Pandoc.AlignRight = Alignment
PP.AlignRight
prettyBlock theme :: Theme
theme (Pandoc.Div _attrs :: Attr
_attrs blocks :: [Block]
blocks) = Theme -> [Block] -> Doc
prettyBlocks Theme
theme [Block]
blocks
prettyBlock _theme :: Theme
_theme Pandoc.Null = Doc
forall a. Monoid a => a
mempty
#if MIN_VERSION_pandoc(1,18,0)
prettyBlock theme :: Theme
theme@Theme {..} (Pandoc.LineBlock inliness :: [[Inline]]
inliness) =
let ind :: Trimmable Doc
ind = Doc -> Trimmable Doc
forall a. a -> Trimmable a
PP.NotTrimmable (Maybe Style -> Doc -> Doc
themed Maybe Style
themeLineBlock "| ") in
Maybe Int -> Doc -> Doc
PP.wrapAt Maybe Int
forall a. Maybe a
Nothing (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
Trimmable Doc -> Trimmable Doc -> Doc -> Doc
PP.indent Trimmable Doc
ind Trimmable Doc
ind (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
[Doc] -> Doc
PP.vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$
([Inline] -> Doc) -> [[Inline]] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Theme -> [Inline] -> Doc
prettyInlines Theme
theme) [[Inline]]
inliness
#endif
prettyBlocks :: Theme -> [Pandoc.Block] -> PP.Doc
prettyBlocks :: Theme -> [Block] -> Doc
prettyBlocks theme :: Theme
theme = [Doc] -> Doc
PP.vcat ([Doc] -> Doc) -> ([Block] -> [Doc]) -> [Block] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Block -> Doc) -> [Block] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Theme -> Block -> Doc
prettyBlock Theme
theme) ([Block] -> [Doc]) -> ([Block] -> [Block]) -> [Block] -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Block -> Bool) -> [Block] -> [Block]
forall a. (a -> Bool) -> [a] -> [a]
filter Block -> Bool
isVisibleBlock
prettyInline :: Theme -> Pandoc.Inline -> PP.Doc
prettyInline :: Theme -> Inline -> Doc
prettyInline _theme :: Theme
_theme Pandoc.Space = Doc
PP.space
prettyInline _theme :: Theme
_theme (Pandoc.Str str :: Text
str) = Text -> Doc
PP.text Text
str
prettyInline theme :: Theme
theme@Theme {..} (Pandoc.Emph inlines :: [Inline]
inlines) =
Maybe Style -> Doc -> Doc
themed Maybe Style
themeEmph (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
inlines
prettyInline theme :: Theme
theme@Theme {..} (Pandoc.Strong inlines :: [Inline]
inlines) =
Maybe Style -> Doc -> Doc
themed Maybe Style
themeStrong (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
inlines
prettyInline Theme {..} (Pandoc.Code _ txt :: Text
txt) =
Maybe Style -> Doc -> Doc
themed Maybe Style
themeCode (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
Text -> Doc
PP.text (" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
txt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> " ")
prettyInline theme :: Theme
theme@Theme {..} link :: Inline
link@(Pandoc.Link _attrs :: Attr
_attrs text :: [Inline]
text (target :: Text
target, _title :: Text
_title))
| Inline -> Bool
isReferenceLink Inline
link =
"[" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Maybe Style -> Doc -> Doc
themed Maybe Style
themeLinkText (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
text) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "]"
| Bool
otherwise =
"<" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Maybe Style -> Doc -> Doc
themed Maybe Style
themeLinkTarget (Text -> Doc
PP.text Text
target) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> ">"
prettyInline _theme :: Theme
_theme Pandoc.SoftBreak = Doc
PP.softline
prettyInline _theme :: Theme
_theme Pandoc.LineBreak = Doc
PP.hardline
prettyInline theme :: Theme
theme@Theme {..} (Pandoc.Strikeout t :: [Inline]
t) =
"~~" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Maybe Style -> Doc -> Doc
themed Maybe Style
themeStrikeout (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "~~"
prettyInline theme :: Theme
theme@Theme {..} (Pandoc.Quoted Pandoc.SingleQuote t :: [Inline]
t) =
"'" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Maybe Style -> Doc -> Doc
themed Maybe Style
themeQuoted (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "'"
prettyInline theme :: Theme
theme@Theme {..} (Pandoc.Quoted Pandoc.DoubleQuote t :: [Inline]
t) =
"'" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Maybe Style -> Doc -> Doc
themed Maybe Style
themeQuoted (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "'"
prettyInline Theme {..} (Pandoc.Math _ t :: Text
t) =
Maybe Style -> Doc -> Doc
themed Maybe Style
themeMath (Text -> Doc
PP.text Text
t)
prettyInline theme :: Theme
theme@Theme {..} (Pandoc.Image _attrs :: Attr
_attrs text :: [Inline]
text (target :: Text
target, _title :: Text
_title)) =
"![" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Maybe Style -> Doc -> Doc
themed Maybe Style
themeImageText (Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
text) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "](" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
Maybe Style -> Doc -> Doc
themed Maybe Style
themeImageTarget (Text -> Doc
PP.text Text
target) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> ")"
prettyInline theme :: Theme
theme (Pandoc.Cite _ t :: [Inline]
t) = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t
prettyInline theme :: Theme
theme (Pandoc.Span _ t :: [Inline]
t) = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t
prettyInline _theme :: Theme
_theme (Pandoc.RawInline _ t :: Text
t) = Text -> Doc
PP.text Text
t
prettyInline theme :: Theme
theme (Pandoc.Note t :: [Block]
t) = Theme -> [Block] -> Doc
prettyBlocks Theme
theme [Block]
t
prettyInline theme :: Theme
theme (Pandoc.Superscript t :: [Inline]
t) = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t
prettyInline theme :: Theme
theme (Pandoc.Subscript t :: [Inline]
t) = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t
prettyInline theme :: Theme
theme (Pandoc.SmallCaps t :: [Inline]
t) = Theme -> [Inline] -> Doc
prettyInlines Theme
theme [Inline]
t
prettyInlines :: Theme -> [Pandoc.Inline] -> PP.Doc
prettyInlines :: Theme -> [Inline] -> Doc
prettyInlines theme :: Theme
theme = [Doc] -> Doc
forall a. Monoid a => [a] -> a
mconcat ([Doc] -> Doc) -> ([Inline] -> [Doc]) -> [Inline] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inline -> Doc) -> [Inline] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Theme -> Inline -> Doc
prettyInline Theme
theme)
prettyReferences :: Theme -> Fragment -> [PP.Doc]
prettyReferences :: Theme -> Fragment -> [Doc]
prettyReferences theme :: Theme
theme@Theme {..} =
(Inline -> Doc) -> [Inline] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Inline -> Doc
prettyReference ([Inline] -> [Doc]) -> (Fragment -> [Inline]) -> Fragment -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> [Inline]
getReferences ([Block] -> [Inline])
-> (Fragment -> [Block]) -> Fragment -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fragment -> [Block]
unFragment
where
getReferences :: [Pandoc.Block] -> [Pandoc.Inline]
getReferences :: [Block] -> [Inline]
getReferences = (Inline -> Bool) -> [Inline] -> [Inline]
forall a. (a -> Bool) -> [a] -> [a]
filter Inline -> Bool
isReferenceLink ([Inline] -> [Inline])
-> ([Block] -> [Inline]) -> [Block] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> [Inline]
forall a b. (Data a, Data b) => a -> [b]
grecQ
prettyReference :: Pandoc.Inline -> PP.Doc
prettyReference :: Inline -> Doc
prettyReference (Pandoc.Link _attrs :: Attr
_attrs text :: [Inline]
text (target :: Text
target, title :: Text
title)) =
"[" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
Maybe Style -> Doc -> Doc
themed Maybe Style
themeLinkText (Theme -> [Inline] -> Doc
prettyInlines Theme
theme ([Inline] -> Doc) -> [Inline] -> Doc
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
Pandoc.newlineToSpace [Inline]
text) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
"](" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
Maybe Style -> Doc -> Doc
themed Maybe Style
themeLinkTarget (Text -> Doc
PP.text Text
target) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>
(if Text -> Bool
T.null Text
title
then Doc
forall a. Monoid a => a
mempty
else Doc
PP.space Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "\"" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Text -> Doc
PP.text Text
title Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> "\"")
Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> ")"
prettyReference _ = Doc
forall a. Monoid a => a
mempty
isReferenceLink :: Pandoc.Inline -> Bool
isReferenceLink :: Inline -> Bool
isReferenceLink (Pandoc.Link _attrs :: Attr
_attrs text :: [Inline]
text (target :: Text
target, _)) =
[Text -> Inline
Pandoc.Str Text
target] [Inline] -> [Inline] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Inline]
text
isReferenceLink _ = Bool
False
isVisibleBlock :: Pandoc.Block -> Bool
isVisibleBlock :: Block -> Bool
isVisibleBlock Pandoc.Null = Bool
False
isVisibleBlock (Pandoc.RawBlock (Pandoc.Format "html") t :: Text
t) =
Bool -> Bool
not ("<!--" Text -> Text -> Bool
`T.isPrefixOf` Text
t Bool -> Bool -> Bool
&& "-->" Text -> Text -> Bool
`T.isSuffixOf` Text
t)
isVisibleBlock _ = Bool
True