{-
The MIT License (MIT)

Copyright (c) 2015 Wasif Hasan Baig <pr.wasif@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-}

{- |
   Module      : Text.Table.Tablify
   Copyright   : Copyright (C) 2015 Wasif Hasan Baig
   License     : MIT

   Maintainer  : Venkateswara Rao Mandela <venkat.mandela@gmail.com>
   Stability   : alpha

Function for operating on Pandoc fenced code blocks or image blocks
with the ".table" attribute and converting them into Pandoc tables.
-}

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)
-- Local imports
import Text.Table.Helper

tablifyCsvLinks :: Block -> IO [Block]
-- variant 1: Referencing CSV file in Image Links
#if MIN_VERSION_pandoc(1,16,0)
-- Image Attr [Inline] Target -- ^ Image:  alt text (list of inlines), target
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
-- Image [Inline] Target -- ^ Image:  alt text (list of inlines), target
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
-- variant 2 and 3: Fenced Code Blocks
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
      -- variant 2: Referencing CSV file in Fenced Code Blocks
      [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
      -- variant 3:Including CSV content inside Fenced Code Blocks
      [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 )
-- Return input unchanged in case of no match
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