module Text.Table.Tablify (
tablifyCsvLinks,
tablifyCsvLinksPure
) where
import Text.CSV (parseCSV, parseCSVFromFile)
import Text.Pandoc.JSON (Block(Para, CodeBlock), Inline(Image), toJSONFilter)
import Data.List (isSuffixOf)
import Text.Table.Helper
tablifyCsvLinks :: Block -> IO [Block]
#if MIN_VERSION_pandoc(1,16,0)
tablifyCsvLinks :: Block -> IO [Block]
tablifyCsvLinks (Para [(Image Attr
_ [Inline]
l (Text
f, Text
_))]) | [Char]
"csv" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` (Text -> [Char]
getString Text
f) = do
#else
tablifyCsvLinks (Para [(Image l (f, _))]) | "csv" `isSuffixOf` f = do
#endif
Either ParseError CSV
csv <- [Char] -> IO (Either ParseError CSV)
parseCSVFromFile ([Char] -> IO (Either ParseError CSV))
-> [Char] -> IO (Either ParseError CSV)
forall a b. (a -> b) -> a -> b
$ Text -> [Char]
getString Text
f
case Either ParseError CSV
csv of
(Left ParseError
_) -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return []
(Right CSV
xss) -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> IO [Block]) -> (CSV -> [Block]) -> CSV -> IO [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Pandoc -> [Block]
toBlocks (Pandoc -> [Block]) -> (CSV -> Pandoc) -> CSV -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Inline] -> CSV -> Pandoc
tableFromImageInline [Inline]
l (CSV -> IO [Block]) -> CSV -> IO [Block]
forall a b. (a -> b) -> a -> b
$
CSV
xss
tablifyCsvLinks b :: Block
b@(CodeBlock (Text
_, [Text]
cs, [(Text, Text)]
as) Text
s) | [Char]
"table" [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ((Text -> [Char]) -> [Text] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Text -> [Char]
getString [Text]
cs) = do
let file :: [Char]
file = [Char] -> Atrs -> [Char]
getAtr [Char]
"source" Atrs
as1
case [Char]
file of
[Char]
"" -> case [Char]
s1 of
[Char]
"" -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return [Block
b]
[Char]
_ -> case ([Char] -> [Char] -> Either ParseError CSV
parseCSV [Char]
"" [Char]
s1) of
(Left ParseError
_) -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return []
(Right CSV
xss) -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> IO [Block]) -> (CSV -> [Block]) -> CSV -> IO [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Pandoc -> [Block]
toBlocks (Pandoc -> [Block]) -> (CSV -> Pandoc) -> CSV -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Atrs -> CSV -> Pandoc
tableFromCodeBlock Atrs
as1 (CSV -> IO [Block]) -> CSV -> IO [Block]
forall a b. (a -> b) -> a -> b
$
CSV
xss
[Char]
_ -> do
Either ParseError CSV
csv <- [Char] -> IO (Either ParseError CSV)
parseCSVFromFile [Char]
file
case Either ParseError CSV
csv of
(Left ParseError
_) -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return []
(Right CSV
xss) -> [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> IO [Block]) -> (CSV -> [Block]) -> CSV -> IO [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Pandoc -> [Block]
toBlocks (Pandoc -> [Block]) -> (CSV -> Pandoc) -> CSV -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Atrs -> CSV -> Pandoc
tableFromCodeBlock Atrs
as1 (CSV -> IO [Block]) -> CSV -> IO [Block]
forall a b. (a -> b) -> a -> b
$
CSV
xss
where s1 :: [Char]
s1 = Text -> [Char]
getString Text
s
as1 :: Atrs
as1 = (((Text, Text) -> ([Char], [Char])) -> [(Text, Text)] -> Atrs
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> [Char]) -> (Text, Text) -> ([Char], [Char])
forall a b. (a -> b) -> (a, a) -> (b, b)
applyToTuple Text -> [Char]
getString) [(Text, Text)]
as )
tablifyCsvLinks Block
x = [Block] -> IO [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return [Block
x]
tablifyCsvLinksPure :: Block -> [Block]
tablifyCsvLinksPure :: Block -> [Block]
tablifyCsvLinksPure b :: Block
b@(CodeBlock (Text
_, [Text]
cs, [(Text, Text)]
as) Text
s) | [Char]
"table" [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ((Text -> [Char]) -> [Text] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Text -> [Char]
getString [Text]
cs) = do
case [Char]
s1 of
[Char]
"" -> Block -> [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return Block
b
[Char]
_ -> case ([Char] -> [Char] -> Either ParseError CSV
parseCSV [Char]
"" [Char]
s1) of
(Left ParseError
_) -> Block -> [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return Block
b
(Right CSV
xss) -> Pandoc -> [Block]
toBlocks (Pandoc -> [Block]) -> (CSV -> Pandoc) -> CSV -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Atrs -> CSV -> Pandoc
tableFromCodeBlock Atrs
as1 (CSV -> [Block]) -> CSV -> [Block]
forall a b. (a -> b) -> a -> b
$
CSV
xss
where s1 :: [Char]
s1 = Text -> [Char]
getString Text
s
as1 :: Atrs
as1 = (((Text, Text) -> ([Char], [Char])) -> [(Text, Text)] -> Atrs
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> [Char]) -> (Text, Text) -> ([Char], [Char])
forall a b. (a -> b) -> (a, a) -> (b, b)
applyToTuple Text -> [Char]
getString) [(Text, Text)]
as )
tablifyCsvLinksPure Block
x = Block -> [Block]
forall (m :: * -> *) a. Monad m => a -> m a
return Block
x