pandoc-placetable 0.1.0 → 0.1.1
raw patch · 3 files changed
+48/−37 lines, 3 files
Files
- README.md +11/−8
- pandoc-placetable.cabal +3/−3
- pandoc-placetable.hs +34/−26
README.md view
@@ -12,20 +12,26 @@ ```{.table} some,values even,"with spaces"- "with the inlineMarkdown flag enabled","we _can_ write **Markdown** here" ``` + ```{.table inlinemarkdown=yes}+ "when compiled with the inlineMarkdown flag","we _can_ write **Markdown** here"+ ```+ ```{.table file="foo.csv" header=yes} Fruit,Quantity,Price ``` - ```{.table file="foo.csv" header=yes aligns=LRCRR caption="my caption" delimiter="," quotechar="\"" }+ ```{.table file="foo.csv" header=yes aligns=LRCRR inlinemarkdown=yes+ caption="my **caption**" delimiter="," quotechar="\"" } ``` All attributes are optional and are specified as follows: - file: The path 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.+- 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. - delimiter: A one-character string used in the CSV to separate fields, defaults to `,`. For characters not allowed standing alone in Pandoc markdown attributes, use `\t` for a@@ -33,17 +39,14 @@ - quotechar: A one-character string that may be used in the CSV to quote fields containing special characters, defaults to `"`. -When compiled with the `inlineMarkdown` flag, the CSV and the caption may contain markdown that will-be interpreted. The CSV may even contain multiline markdown in a cell (the field need to be quoted,-of course). Note that the flag causes Pandoc to be required as a dependency so it will take a while-to build.- ## Installation cabal update cabal install pandoc-placetable -or with the `inlineMarkdown` flag (see above):+When compiled with the `inlineMarkdown` flag, the `inlinemarkdown=yes`option is available to+have CSV and the caption be interpreted as markdown. Note that the flag causes Pandoc to be+required as a dependency so it will take a while to build. cabal install -f inlineMarkdown pandoc-placetable
pandoc-placetable.cabal view
@@ -1,5 +1,5 @@ Name: pandoc-placetable-Version: 0.1.0+Version: 0.1.1 Build-Type: Simple Synopsis: Pandoc filter to include CSV files Description: A Pandoc filter that replaces code blocks (that have the class `table`)@@ -22,8 +22,8 @@ Flag inlineMarkdown Default: False- Description: require entire Pandoc as a dependency and use it to compile- inline markdown in the CSV and caption+ Description: Require entire Pandoc as a dependency so it can be used to compile+ inline markdown in the CSV and caption if desired. Executable pandoc-placetable Main-Is: pandoc-placetable.hs
pandoc-placetable.hs view
@@ -28,6 +28,9 @@ let header = case lookup "header" kvs of Just "yes" -> True _ -> False+ let inlinemd = case lookup "inlinemarkdown" kvs of+ Just "yes" -> True+ _ -> False let toAlign c = case toUpper c of 'L' -> AlignLeft 'R' -> AlignRight@@ -37,8 +40,8 @@ Just as -> map toAlign as Nothing -> repeat AlignDefault let capt = case lookup "caption" kvs of- Just c -> strToInlines c- Nothing -> mempty+ Just c -> c+ Nothing -> "" let qc = case lookup "quotechar" kvs of Just q -> head q Nothing -> '"'@@ -56,24 +59,26 @@ let s = if isSuffixOf "\n" s' then s' else s' ++ "\n"- return $ toList $ csvToTable header aligns capt qc sep s+ return $ toList $ csvToTable header inlinemd aligns capt qc sep s placeTable a = return [a] -- | Convert a CSV String to a Pandoc Table simpleCsvToTable :: String -> Blocks-simpleCsvToTable s = csvToTable 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- -> Inlines -- ^ table caption+ -> String -- ^ table caption -> Char -- ^ csv quotation character like " -> Char -- ^ csv field separator like , -> String -- ^ csv string to parse -> Blocks-csvToTable header aligns caption qc sep s =- table caption cellspecs (map strToBlocks headers) $ (map . map) strToBlocks rows+csvToTable header inlinemd aligns caption qc sep s =+ table (strToInlines caption) cellspecs (map strToBlocks headers)+ $ (map . map) strToBlocks rows where exc = S.fromString qc sep s rows' = case exception exc of@@ -85,26 +90,29 @@ cols = if null rows' then 0 else length $ head rows' cellspecs = zip aligns $ replicate cols 0 --strToInlines :: String -> Inlines-strToInlines s = #if defined(INLINE_MARKDOWN)- case readMarkdown def s of- Right (Pandoc _ bs) -> fromList $ extractIns $ head bs- Left e -> str $ show e- where- extractIns (Para ins) = ins- extractIns _ = []-#else- str s-#endif+ strToInlines s =+ if inlinemd+ then+ -- strip newlines and wrap s in a header so only inline syntax is parsed+ let s' = "# " ++ (concat $ lines s)+ extractIns (Header _ _ ins) = ins+ extractIns _ = []+ in case readMarkdown def s' of+ Right (Pandoc _ bs) -> fromList $ extractIns $ head bs+ Left e -> str $ show e+ else+ str s -strToBlocks :: String -> Blocks-strToBlocks s =-#if defined(INLINE_MARKDOWN)- case readMarkdown def s of- Right (Pandoc _ bs) -> fromList bs- Left e -> para $ str $ show e+ strToBlocks s =+ if inlinemd+ then+ case readMarkdown def s of+ Right (Pandoc _ bs) -> fromList bs+ Left e -> para $ str $ show e+ else+ para $ str s #else- para $ str s+ strToInlines s = str s+ strToBlocks s = para $ str s #endif