module Text.Table.Helper (
addInlineLabel
, getTableType
, toTableType
, getAligns
, isHeaderPresent
, isHeaderPresent1
, removeConfigString
, toBlocks
, getAtr
, toAlign
, tableFromImageInline
, tableFromCodeBlock
) where
import Text.CSV (CSV)
import Data.List (isInfixOf)
import Text.Pandoc (readMarkdown, def, ReaderOptions, readerExtensions)
import Text.Pandoc.Extensions
import qualified Text.Pandoc.JSON as J
import Text.Table.Definition
import Text.Table.Builder
#if MIN_VERSION_pandoc(2,0,0)
import Text.Pandoc (runPure)
import Data.Text (pack)
#endif
addInlineLabel :: [J.Inline] -> J.Pandoc -> J.Pandoc
addInlineLabel i (J.Pandoc m [(J.Table _ as ds ts tss)]) = J.Pandoc m [(J.Table i as ds ts tss)]
addInlineLabel _ x = x
toBlocks :: J.Pandoc -> [J.Block]
toBlocks (J.Pandoc _ bs) = bs
toTableType1 :: String -> TableType
toTableType1 (x:ys) = case x of
's' -> Simple
'm' -> Multiline
'p' -> Pipe
_ -> Grid
toTableType1 [] = Grid
toTableType :: String -> TableType
toTableType s = case s of
"simple" -> Simple
"multiline" -> Multiline
"pipe" -> Pipe
_ -> Grid
getTableType :: [J.Inline] -> TableType
getTableType ((J.Str s):[]) = toTableType1 s
getTableType (_:is) = getTableType is
getTableType [] = Grid
isHeaderPresent :: [J.Inline] -> Bool
isHeaderPresent ((J.Str s):[]) = not $ "n" `isInfixOf` s
isHeaderPresent (_:is) = isHeaderPresent is
isHeaderPresent [] = True
isHeaderPresent1 :: String -> Bool
isHeaderPresent1 ('n':'o':[]) = False
isHeaderPresent1 _ = True
toAlign :: String -> [Align]
toAlign (x:ys) = case x of
'l' -> LeftAlign : toAlign ys
'L' -> LeftAlign : toAlign ys
'r' -> RightAlign : toAlign ys
'R' -> RightAlign : toAlign ys
'c' -> CenterAlign : toAlign ys
'C' -> CenterAlign : toAlign ys
'd' -> DefaultAlign : toAlign ys
'D' -> DefaultAlign : toAlign ys
_ -> [] ++ toAlign ys
toAlign [] = []
getAligns :: [J.Inline] -> [Align]
getAligns ((J.Str s):[]) = toAlign s
getAligns (_:is) = getAligns is
getAligns [] = []
removeConfigString :: [J.Inline] -> [J.Inline]
removeConfigString (_:[]) = []
removeConfigString (x:ys) = x : removeConfigString ys
removeConfigString [] = []
getAtr :: AtrName -> Atrs -> AtrValue
getAtr a ((at,v):_) | a == at = v
getAtr a (_:xs) = getAtr a xs
getAtr a [] = ""
ropt :: ReaderOptions
ropt = def
{
readerExtensions = pandocExtensions
}
tableFromImageInline :: [J.Inline] -> CSV -> J.Pandoc
tableFromImageInline l = addInlineLabel (removeConfigString l) .
readMarkdown' ropt .
toMarkdown (getTableType l) AfterTable .
mkTable "" (getAligns l) (isHeaderPresent l)
tableFromCodeBlock :: Atrs -> CSV -> J.Pandoc
tableFromCodeBlock as = readMarkdown' ropt .
toMarkdown (toTableType $ getAtr "type" as) AfterTable .
mkTable (getAtr "caption" as)
(toAlign $ getAtr "aligns" as)
(isHeaderPresent1 $ getAtr "header" as)
#if MIN_VERSION_pandoc(2,0,0)
readMarkdown' :: ReaderOptions -> String -> J.Pandoc
readMarkdown' o s = case parsed of
(Left _) -> J.Pandoc J.nullMeta []
(Right p) -> p
where parsed = runPure (readMarkdown o $ pack s)
#elif MIN_VERSION_pandoc(1,14,0)
readMarkdown' :: ReaderOptions -> String -> J.Pandoc
readMarkdown' o s = case read of
(Left _) -> J.Pandoc J.nullMeta []
(Right p) -> p
where read = readMarkdown o s
#else
readMarkdown' :: ReaderOptions -> String -> J.Pandoc
readMarkdown' = readMarkdown
#endif