pandoc-placetable 0.1.2 → 0.2
raw patch · 3 files changed
+35/−13 lines, 3 files
Files
- README.md +7/−2
- pandoc-placetable.cabal +1/−1
- pandoc-placetable.hs +27/−10
README.md view
@@ -2,8 +2,8 @@ A Pandoc filter that replaces [fenced code blocks](http://pandoc.org/README.html#fenced-code-blocks) (that have the class `table`) with tables generated from CSV. The CSV is read from the code block-and from an optional external CSV file and concatenated. There's an option to enable parsing of-inline markdown.+and from an optional external CSV file (or URL) and concatenated. There's an option to enable+parsing of inline markdown. Some usage examples: @@ -34,6 +34,8 @@ - **file**: The path or URL to the CSV file that is appended to the code block's content - **header**: If set to `yes`, then the first row of data is interpreted as the table headers. - **aligns**: For each column, one letter (L, R or C) that specifies the column's alignment.+- **widths**: For each column, a number specifying its width as a fraction of the page width,+ e.g. `widths="0.5 0.2 0.3"`. - **inlinemarkdown**: If set to yes, interprets the caption and CSV fields as markdown. This requires pandoc-placetable to be compiled with the `inlineMarkdown` flag (see below). - **caption**: The table caption.@@ -42,6 +44,9 @@ tab and `\s` for a space. - **quotechar**: A one-character string that may be used in the CSV to quote fields containing special characters, defaults to `"`.++If there is an `id` set (e.g. `{.table #my-id}`), the table will be wrapped in a `div` with+that `id` so it can be referenced. ## Installation
pandoc-placetable.cabal view
@@ -1,5 +1,5 @@ Name: pandoc-placetable-Version: 0.1.2+Version: 0.2 Build-Type: Simple Synopsis: Pandoc filter to include CSV files Description: A Pandoc filter that replaces code blocks (that have the class `table`)
pandoc-placetable.hs view
@@ -13,7 +13,14 @@ import System.Environment (getArgs) import Text.Pandoc.JSON import Text.Pandoc.Definition-import Text.Pandoc.Builder (Inlines, Blocks, toList, fromList, table, para, str)+import Text.Pandoc.Builder ( Inlines+ , Blocks+ , toList+ , fromList+ , table+ , plain+ , str+ , divWith ) import qualified Data.ByteString.Lazy.Char8 as C @@ -43,11 +50,12 @@ httpConduitManager = newManager tlsManagerSettings placeTable :: Block -> IO [Block]-placeTable (CodeBlock (_, cls, kvs) txt) | "table" `elem` cls = do+placeTable (CodeBlock (ident, cls, kvs) txt) | "table" `elem` cls = do csv <- find "file" (return "") getCsv let header = find "header" False (== "yes") let inlinemd = find "inlinemarkdown" False (== "yes") let aligns = find "aligns" (repeat AlignDefault) (map toAlign)+ let widths = find "widths" [] (map read . words) let capt = find "caption" "" id let qc = find "quotechar" '"' head let sep = find "delimiter" ',' $ \d ->@@ -63,7 +71,10 @@ let s = if isSuffixOf "\n" s' then s' else s' ++ "\n"- return $ toList $ csvToTable header inlinemd aligns capt qc sep s+ let csvTable = csvToTable header inlinemd aligns widths capt qc sep s+ return $ toList $ if null ident+ then csvTable+ else divWith (ident,cls,[]) csvTable where find key def extract = case lookup key kvs of Just x -> extract x@@ -84,18 +95,19 @@ -- | Convert a CSV String to a Pandoc Table simpleCsvToTable :: String -> Blocks-simpleCsvToTable s = csvToTable False False (repeat AlignDefault) mempty '"' ',' s+simpleCsvToTable s = csvToTable False False (repeat AlignDefault) [] mempty '"' ',' s -- | Convert a bunch of options and a CSV String to a Pandoc Table csvToTable :: Bool -- ^ interpret first row as headers -> Bool -- ^ interpret as inline markdown (needs inlineMarkdown compile flag) -> [Alignment] -- ^ table column alignments+ -> [Double] -- ^ table column widths -> String -- ^ table caption -> Char -- ^ csv quotation character like " -> Char -- ^ csv field separator like , -> String -- ^ csv string to parse -> Blocks-csvToTable header inlinemd aligns caption qc sep s =+csvToTable header inlinemd aligns widths caption qc sep s = table (strToInlines caption) cellspecs (map strToBlocks headers) $ (map . map) strToBlocks rows where@@ -106,8 +118,13 @@ (headers, rows) = if header && length rows' > 0 then (head rows', tail rows') else ([], rows')- cols = if null rows' then 0 else length $ head rows'- cellspecs = zip aligns $ replicate cols 0+ nrCols = if null rows'+ then 0+ else length $ head rows'+ widths' = if length widths == nrCols+ then widths+ else replicate nrCols 0+ cellspecs = zip aligns widths' #if defined(INLINE_MARKDOWN) strToInlines s =@@ -128,10 +145,10 @@ then case readMarkdown def s of Right (Pandoc _ bs) -> fromList bs- Left e -> para $ str $ show e+ Left e -> plain $ str $ show e else- para $ str s+ plain $ str s #else strToInlines s = str s- strToBlocks s = para $ str s+ strToBlocks s = plain $ str s #endif