packages feed

pandoc-csv2table (empty) → 1.0.0

raw patch · 8 files changed

+773/−0 lines, 8 filesdep +basedep +csvdep +pandocsetup-changed

Dependencies added: base, csv, pandoc, pandoc-csv2table, pandoc-types, text

Files

+ LICENSE view
@@ -0,0 +1,22 @@+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.
+ README.md view
@@ -0,0 +1,70 @@+# pandoc-csv2table-filter++A Pandoc filter that replaces image links having *.csv extension with+[Pandoc Table Markdown][1].++![A CSV file rendered to Markdown and PDF][png]++## Usage++In your markdown, include the csv file as shown below.++> \!\[This text will become the table caption\](table.csv)++You can use Pandoc Markdown in the CSV file.+It will be parsed by the Pandoc Markdown Reader before insertion into the+document.++See [example.md][example-md] and the rendered [pdf][example-pdf] version in+the Examples folder for more details on usage.++## Configuration String++A configuration string lets you specify++-   Type of the table+-   Column alignments+-   Whether to treat the first line of the CSV file as header or not++It is included right before the closing square bracket **without any space in+between**, as shown in the example below.++> \!\[Another table. mylrcd](table.csv)++`mylrcd` is the configuration string.+This will be rendered as a **m**ultiline table with a header with first column+**l**eft-aligned, second **r**ight-aligned, third **c**enter-aligned, and the+fourth one having **d**efault alignment.+***The config string will be removed from the caption after being processed.***++The config string can contain following letters:++-   **`s`** for **s**imple table+-   **`m`** for **m**ultiline table+-   **`g`** for **g**rid table+-   **`p`** for **p**ipe table+-   **`y`** (from **y**es) when you want the first row of CSV file to be the+    header.+-   **`n`** (from **n**o) when you want to omit the header.+-   **`l`** for **l**eft alignment of the column+-   **`r`** for **r**ight alignment of the column+-   **`c`** for **c**center alignment of the column+-   **`d`** for **d**efault alignment of the column++You can specify `l` `r` `c` `d` for each column in a series.+The extra letters will be ignored if they exceed the number of columns in the+CSV file.++## License++Copyright &copy; 2015 [Wasif Hasan Baig](https://twitter.com/_wbaig)++Source code is released under the Terms and Conditions of [MIT License](http://opensource.org/licenses/MIT).++Please refer to the [License file][license] in the project's root directory.++[png]: https://raw.githubusercontent.com/baig/pandoc-csv2table-filter/master/Examples/demo.png+[license]: https://raw.githubusercontent.com/baig/pandoc-csv2table-filter/master/LICENSE+[example-md]: https://raw.githubusercontent.com/baig/pandoc-csv2table-filter/master/Examples/example.md+[example-pdf]: https://github.com/baig/pandoc-csv2table-filter/blob/master/Examples/example.pdf+[1]: http://pandoc.org/README.html#tables
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ csv2table.hs view
@@ -0,0 +1,95 @@+#!/usr/bin/env runhaskell++{-+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.+-}++{-|+  A Pandoc filter that replaces image or fenced code blocks and replaces them+  with rendered pandoc markdown tables.+  +  Image Links should have a "csv" extension.+  Include the csv file in markdown as+  +  > ![This is the table caption.](table.csv)+  +  Instead of image links, you can use fenced code blocks to reference an+  external CSV file using the "source" attribute.+  +  > ```{.table caption="This is the **caption**" source="table.csv"}+  > ```++  You can also omit the source attribute and include the contents of the CSV+  inside the code block directly.+  +  > ```{.table caption="This is the **caption**"}+  > Fruit, Quantity, Price+  > apples, 15, 3.24+  > oranges, 12, 2.22+  > ```+  +  You can include Pandoc Markdown in the CSV file. It will be parsed+  by the Pandoc Markdown Reader when the table is inserted in the document.+  +  For a detailed explanation of usage and options, see <https://github.com/baig/pandoc-csv2table-filter/blob/master/README.md README>+  at project's source directory.+-}++import Text.CSV         (parseCSV, parseCSVFromFile)+import Data.List        (isSuffixOf)+import Text.Pandoc.JSON (Block(Para, CodeBlock), Inline(Image), toJSONFilter)+-- Local imports+import Text.Table.Helper++main :: IO ()+main = toJSONFilter tablifyCsvLinks++tablifyCsvLinks :: Block -> IO [Block]+tablifyCsvLinks (Para [(Image l (f, _))]) | "csv" `isSuffixOf` f = do+    csv <- parseCSVFromFile f+    case csv of+        (Left _)    -> return []+        (Right xss) -> return .+                       toBlocks .+                       tableFromImageInline l $+                       xss+tablifyCsvLinks b@(CodeBlock (_, cs, as) s) | "table" `elem` cs = do+    let file = getAtr "source" as+    case file of+      "" -> case s of+              "" -> return [b]+              _  -> case (parseCSV "" s) of+                      (Left _)    -> return []+                      (Right xss) -> return .+                                     toBlocks .+                                     tableFromCodeBlock as $+                                     xss+      _  -> do+              csv <- parseCSVFromFile file+              case csv of+                (Left _)    -> return []+                (Right xss) -> return .+                               toBlocks .+                               tableFromCodeBlock as $+                               xss+tablifyCsvLinks x = return [x]
+ pandoc-csv2table.cabal view
@@ -0,0 +1,77 @@+Name:                pandoc-csv2table+Version:             1.0.0+Synopsis:            Convert CSV to Pandoc Table Markdown+Description:         A Pandoc filter that replaces image inline or fenced code+                     blocks with pandoc markdown tables.+                     .+                     Image links must have a "csv" extension.+                     Include the csv file in markdown as+                     .+                     > ![This is the table caption.](table.csv)+                     .+                     You can also use fenced code blocks to reference an+                     external CSV file using the "source" attribute.+                     .+                     > ```{.table caption="This is the **caption**" source="table.csv"}+                     > ```+                     .+                     You can include the CSV contents inside fenced code blocks+                     and omit the source attribute.+                     .+                     > ```{.table caption="This is the **caption**"}+                     > Fruit, Quantity, Price+                     > apples, 15, 3.24+                     > oranges, 12, 2.22+                     > ```+                     .+                     CSV contents will be parsed by the pandoc markdown reader.+                     .+                     You can see a rendered PDF file with tables generated from+                     CSV files at <https://github.com/baig/pandoc-csv2table-filter/blob/master/Examples/example.pdf Example>.+                     .+                     For more information, see <https://github.com/baig/pandoc-csv2table-filter/blob/master/README.md README>+                     at project's source repository.++Homepage:            https://github.com/baig/pandoc-csv2table-filter+Bug-Reports:         https://github.com/baig/pandoc-csv2table-filter/issues+License:             MIT+License-File:        LICENSE+Author:              Wasif Hasan Baig <pr.wasif@gmail.com>+Maintainer:          Wasif Hasan Baig <pr.wasif@gmail.com>+Copyright:           (c) 2015 Wasif Hasan Baig+Stability:           alpha+Category:            Text+Build-Type:          Simple+Extra-Source-Files:  README.md+Cabal-Version:       >=1.10+Source-repository    head+  type:              git+  location:          git://github.com/baig/pandoc-csv2table-filter.git++Library+  Build-Depends:     base >=4.8 && <4.9,+                     csv >= 0.1.2,+                     text >= 0.11 && < 1.3,+                     pandoc >= 1.13.0.0,+                     pandoc-types >= 1.12.0.0+  Hs-Source-Dirs:    src+  Exposed-Modules:   Text.Table.Definition,+                     Text.Table.Builder,+                     Text.Table.Helper+  Buildable:         True+  Default-Language:  Haskell2010++Executable pandoc-csv2table+  Build-Depends:     base >=4.8 && <4.9,+                     csv >= 0.1.2,+                     pandoc >= 1.13.0.0,+                     pandoc-types >= 1.12.0.0,+                     pandoc-csv2table >= 1.0.0+  Hs-Source-Dirs:    .+  Main-Is:           csv2table.hs+  Buildable:         True+  Default-Language:  Haskell2010+  +  +  +  
+ src/Text/Table/Builder.hs view
@@ -0,0 +1,245 @@+{-+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.Builder+   Copyright   : Copyright (C) 2015 Wasif Hasan Baig+   License     : MIT++   Maintainer  : Wasif Hasan Baig <pr.wasif@gmail.com>+   Stability   : alpha++Functions for building Tables and converting them to markdown.+-}++module Text.Table.Builder (+      mkTable+    , toMarkdown+    , CaptionPos (..)+    , Atrs+) where++import Data.List+import Data.Text (pack, unpack, justifyLeft, justifyRight, center)++-- Local import+import Text.Table.Definition++-- | Table Builder Functions+--   Helper functions to create a Table from the parsed CSV file (a list of+--   list of Strings).++-- | Converts Lines to Cell.+mkCell :: Align -> Lines -> [Cell]+mkCell a xs = map (Cell (span xs) 0 a) liness+            where+              span   = maximum . map (length . lines)+              liness = map lines xs++-- | Converts a list of Lines to a list of Cells.+mkCells :: [Align] -> [Lines] -> [[Cell]]+mkCells as xss = map (addCellAligns as .+                      addCellWidths columnWidths .+                      mkCell DefaultAlign) xss+               where+                 lines1       = map . map $ lines+                 calcWidths   = map . map . map $ (+2) . length+                 columnWidths = map maximum .+                                map concat  .+                                transpose   .+                                calcWidths  .+                                lines1      $+                                xss++addCellAligns :: [Align] -> [Cell] -> [Cell]+addCellAligns (a:as) (c:cs) = updateCellAlign a c  : addCellAligns as cs+addCellAligns []     (c:cs) = c : addCellAligns [] cs+addCellAligns (a:as) []     = []+addCellAligns []     []     = []++updateCellAlign :: Align -> Cell -> Cell+updateCellAlign a (Cell s w _ xs) = Cell s w a xs++addCellWidths :: [Width] -> [Cell] -> [Cell]+addCellWidths (w:ws) (c:cs) = updateCellWidth w c : addCellWidths ws cs+addCellWidths []     (c:cs) = c : addCellWidths [] cs+addCellWidths (w:ws) []     = []+addCellWidths []     []     = []++updateCellWidth :: Width -> Cell -> Cell+updateCellWidth w (Cell s _ a xs) = Cell s w a xs++mkRows :: [Align] -> [Lines] -> [Row]+mkRows as = map Row . mkCells as++mkColumns :: [Align] -> Row -> [Column]+mkColumns as (Row cs) = columnify as cs+                      where+                        columnify (a:as) ((Cell _ w _ _):cs) = Column w a  : columnify as cs+                        columnify []     ((Cell _ w _ _):cs) = Column w DefaultAlign : columnify [] cs+                        columnify (a:as) []                  = []+                        columnify []     []                  = []++mkTable :: Caption -> [Align] -> Bool -> [Lines] -> Table+mkTable c as h xss = case h of+                       True  -> Table c columns mkHeader $ tail $ mkRows as csv+                       False -> Table c columns NoHeader $ mkRows as csv+                   where+                     csv      = filter (/=[""]) xss+                     columns  = mkColumns as . head . mkRows as $ csv+                     mkHeader = Header $ head $ mkRows as csv++insertRowSeparator :: TableType -> [Column] -> Lines -> Lines+insertRowSeparator (Multiline) cs xs = intersperse "\n" xs+insertRowSeparator (Grid)      cs xs = intersperse (separator cs) xs+                                     where+                                       separator ((Column w _):cs) = "+" ++ replicate (w+2) '-' ++ separator cs+                                       separator []     = "+\n"+insertRowSeparator _           _  _  = []+                                                 +mkTableBorder :: TableType -> [Column] -> String+mkTableBorder (Multiline) ((Column w _):[]) = replicate w '-' ++ "\n"+mkTableBorder (Multiline) ((Column w _):cs) = replicate (w+1) '-' ++ mkTableBorder Multiline cs+mkTableBorder (Grid)      []                = "+\n"+mkTableBorder (Grid)      ((Column w _):cs) = "+" ++ replicate (w+2) '-' ++ mkTableBorder Grid cs+mkTableBorder _           _                 = ""++mkHeaderRowSeparator :: TableType -> [Column] -> String+mkHeaderRowSeparator (Grid) ((Column w _):cs) = "+" ++ replicate (w+2) '=' +++                                                mkHeaderRowSeparator Grid cs+mkHeaderRowSeparator (Pipe) ((Column w a):cs) = (let sep = "|" ++ replicate (w+2) '-' +                                                 in case a of+                                                     LeftAlign   -> "|:" ++ (drop 2 sep)+                                                     RightAlign  -> reverse $ ":" ++ (drop 1 . reverse $ sep)+                                                     CenterAlign -> ("|:" ++) $ drop 2 $ reverse $ ":" ++ (drop 1 . reverse $ sep)+                                                     _           -> reverse . drop 1. reverse $ sep ) +++                                                mkHeaderRowSeparator Pipe cs+mkHeaderRowSeparator (Grid) []                = "+\n"+mkHeaderRowSeparator (Pipe) []                = "|\n"+mkHeaderRowSeparator t      ((Column w _):[]) = replicate w '-' ++ "\n"+mkHeaderRowSeparator t      ((Column w _):cs) = replicate w '-' ++ " " +++                                                mkHeaderRowSeparator t cs++-- | Add members to pad cell content based on span+padCell :: Cell -> Cell+padCell (Cell s w a xs) = Cell s w a (xs ++ padList)+                        where+                          padList  = replicate padByNum (take w $ repeat ' ')+                          padByNum = s - length xs++-- | Expects padded cells+alignCellStrings :: TableType -> Cell -> Lines+alignCellStrings (Grid) (Cell _ w a cs) = map (alignText w LeftAlign) cs+alignCellStrings _      (Cell _ w a cs) = map (alignText w a) cs++cellToLines :: TableType -> Cell -> Lines+cellToLines t = alignCellStrings t . padCell++flatten :: [Lines] -> String+flatten = concatMap (++"\n") . map concat++addGutter :: Gutter -> Lines -> Lines+addGutter g (x:xs) = x : map ((replicate g ' ')++) xs++alignText :: Width -> Align -> String -> String+alignText w (LeftAlign)    = unpack . justifyLeft   w ' ' . pack+alignText w (RightAlign)   = unpack . justifyRight  w ' ' . pack+alignText w (CenterAlign)  = unpack . center        w ' ' . pack+alignText w (DefaultAlign) = unpack . justifyLeft   w ' ' . pack++row2Md :: TableType -> Row -> String+row2Md (Grid) (Row cs) = flatten $ transpose $ appendPipes $ map (cellToLines Grid) cs+row2Md (Pipe) (Row cs) = flatten $ transpose $ appendPipes $ map (cellToLines Pipe) cs+row2Md t      (Row cs) = flatten $ map (addGutter 1) $ transpose $ map (cellToLines t) cs++appendPipes :: [Lines] -> [Lines]+appendPipes (xs:[])  = [map (++" |") xs]+appendPipes (xs:xss) = map (("| "++) . (++" | ")) xs : (map (map (++" | ")) xss)++addCaption :: CaptionPos -> Caption -> String -> String+addCaption _             [] s = s+addCaption (BeforeTable) c  s = "Table: " ++ c ++ "\n\n" ++ s+addCaption (AfterTable)  c  s = s ++ "\nTable: " ++ c++toMarkdown :: TableType -> CaptionPos -> Table -> String+toMarkdown (Simple)    = toSimpleMd+toMarkdown (Multiline) = toMultilineMd+toMarkdown (Grid)      = toGridMd+toMarkdown (Pipe)      = toPipeMd++toMultilineMd :: CaptionPos -> Table -> String+toMultilineMd l (Table c cs (Header h) rs) = addCaption l c $+                                             mkTableBorder Multiline cs +++                                             row2Md Multiline h +++                                             mkHeaderRowSeparator Multiline cs +++                                             (concatMap (++"") $+                                              insertRowSeparator Multiline cs $+                                              map (row2Md Multiline) rs) +++                                             mkTableBorder Multiline cs+toMultilineMd l (Table c cs (NoHeader) rs) = addCaption l c $+                                             mkHeaderRowSeparator Multiline cs +++                                             (concatMap (++"") $+                                              insertRowSeparator Multiline cs $+                                              map (row2Md Multiline) rs) +++                                             mkHeaderRowSeparator Multiline cs++toGridMd :: CaptionPos -> Table -> String+toGridMd l (Table c cs (Header h) rs) = addCaption l c $+                                        mkTableBorder Grid cs +++                                        row2Md Grid h +++                                        mkHeaderRowSeparator Grid cs +++                                        (concatMap (++"") $+                                         insertRowSeparator Grid cs $+                                         map (row2Md Grid) rs) +++                                        mkTableBorder Grid cs+toGridMd l (Table c cs (NoHeader) rs) = addCaption l c $+                                        mkTableBorder Grid cs +++                                        (concatMap (++"") $+                                         insertRowSeparator Grid cs $+                                         map (row2Md Grid) rs) +++                                        mkTableBorder Grid cs++toPipeMd :: CaptionPos -> Table -> String+toPipeMd l (Table c cs (Header h) rs) = addCaption l c $+                                        row2Md Pipe h +++                                        mkHeaderRowSeparator Pipe cs +++                                        (concatMap (++"") $+                                         map (row2Md Pipe) rs)+toPipeMd l (Table c cs (NoHeader) rs) = addCaption l c $+                                        mkHeaderRowSeparator Pipe cs +++                                        (concatMap (++"") $+                                         map (row2Md Pipe) rs)++toSimpleMd :: CaptionPos -> Table -> String+toSimpleMd l (Table c cs (Header h) rs) = addCaption l c $+                                          row2Md Simple h +++                                          mkHeaderRowSeparator Simple cs +++                                          (concatMap (++"") $+                                           map (row2Md Simple) rs) +++                                          mkHeaderRowSeparator Simple cs+toSimpleMd l (Table c cs (NoHeader) rs) = addCaption l c $+                                          mkHeaderRowSeparator Simple cs +++                                          (concatMap (++"") $+                                           map (row2Md Simple) rs) +++                                          mkHeaderRowSeparator Simple cs
+ src/Text/Table/Definition.hs view
@@ -0,0 +1,116 @@+{-+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.Definition+   Copyright   : Copyright (C) 2015 Wasif Hasan Baig+   License     : MIT++   Maintainer  : Wasif Hasan Baig <pr.wasif@gmail.com>+   Stability   : alpha++Definition of 'Table' data structure for internal representation.+-}++module Text.Table.Definition (+      TableType (..)+    , CaptionPos (..)+    , Align (..)+    , Cell (..)+    , Row (..)+    , Column (..)+    , Header (..)+    , Table (..)+    , Span+    , Width+    , Gutter+    , Lines+    , Caption+    , AtrName+    , AtrValue+    , Atrs+) where++-- Type synonyms+type Span      = Int+type Width     = Int+type Gutter    = Int+type Lines     = [String]+type Caption   = String+type AtrName   = String+type AtrValue  = String+type Atrs      = [(AtrName, AtrValue)]++-- Data Definitions++-- | Type of the 'Table'.+data TableType = Simple    -- Simple Table+               | Multiline -- Multiline Table+               | Grid      -- Grid Table+               | Pipe      -- Pipe Table+               deriving (Eq, Show)++-- | Position of the caption.+data CaptionPos = BeforeTable -- Insert caption before table markdown.+                | AfterTable  -- Insert caption after table markdown.+                deriving (Show)++-- | Alignment of a Column in the Table.+--   Not all TableTypes support column alignments.+data Align = LeftAlign    -- Left Align+           | RightAlign   -- Right Align+           | CenterAlign  -- Center Align+           | DefaultAlign -- Default Align+           deriving (Show)++-- | A cell in a table has column span, cell width, cell alignment and the+--   number of lines.+--   +--     * __Span:__  Number of lines spanned by the cell.+--     * __Width:__ Width of the column this cell is contained inside+--     * __Align:__ Alignment of the content inside the cells+--     * __Lines:__ A list of strings where each string represents a line+data Cell = Cell Span Width Align Lines+            deriving (Show)+            +-- | A Row contains a list of Cells.+data Row = Row [Cell]+           deriving (Show)++-- | A Column contain information about its width and alignment.+--   +--     * __Width:__  Character length of the widest 'Cell' in a 'Column'.+--     * __Align:__  Alignment of the cells inside this column+data Column = Column Width Align+              deriving (Show)++-- | A Header contains a Row if present, otherwise NoHeader.+data Header = Header Row+            | NoHeader+            deriving (Show)++-- | A Table has a caption, information about each column's width and+--   alignment, either a header with a row or no header, and a series of rows.+data Table = Table Caption [Column] Header [Row]+             deriving (Show)
+ src/Text/Table/Helper.hs view
@@ -0,0 +1,146 @@+{-+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.Helper+   Copyright   : Copyright (C) 2015 Wasif Hasan Baig+   License     : MIT++   Maintainer  : Wasif Hasan Baig <pr.wasif@gmail.com>+   Stability   : alpha++This helper module exports functions extract values from Pandoc AST and +build Pandoc Document from CSV.+-}++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)+import qualified Text.Pandoc.JSON as J+-- Local imports+import Text.Table.Definition+import Text.Table.Builder++-- Helper functions to manipulate the Pandoc Document and parse the +-- Configuration String.++-- | Add Inline from Image into Table as the caption+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++-- | Extracts Blocks from Pandoc Document+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++-- | Whether to treat first line of CSV as a header or not.+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 []     = []++-- | Parse Config String for alignment information+getAligns :: [J.Inline] -> [Align]+getAligns ((J.Str s):[]) = toAlign s+getAligns (_:is)         = getAligns is+getAligns []             = []++-- | Remove Str Inline from caption+removeConfigString :: [J.Inline] -> [J.Inline]+removeConfigString (_:[]) = []+removeConfigString (x:ys) = x : removeConfigString ys+removeConfigString []     = []++-- | Get value of attribute+getAtr :: AtrName -> Atrs -> AtrValue+getAtr a ((at,v):_) | a == at = v+getAtr a (_:xs)               = getAtr a xs+getAtr a []                   = ""++-- | Make Pandoc Table from Image Inline +tableFromImageInline :: [J.Inline] -> CSV -> J.Pandoc+tableFromImageInline l = addInlineLabel (removeConfigString l) .+                         readMarkdown def .+                         toMarkdown (getTableType l) AfterTable .+                         mkTable "" (getAligns l) (isHeaderPresent l)++-- | Make Pandoc Table from Code Block +tableFromCodeBlock :: Atrs -> CSV -> J.Pandoc+tableFromCodeBlock as = readMarkdown def .+                        toMarkdown (toTableType $ getAtr "type" as) AfterTable .+                        mkTable (getAtr "caption" as)+                                (toAlign $ getAtr "aligns" as)+                                (isHeaderPresent1 $ getAtr "header" as)