使用Pandoc在Haskell中生成降价表

时间:2018-12-14 04:23:32

标签: haskell pandoc

我正在尝试直接使用Pandoc AST创建表。我正在尝试几种方法。但是,似乎没有任何效果。我没有得到表输出,而是得到了[TABLE]。我既尝试了Pandoc构建器monad,也尝试使用原始构造器直接创建Table。我不确定在这里出了什么问题。

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ScopedTypeVariables #-}


module Main where

import Lib

import Data.Text (Text)
import qualified Data.Text as T

----------------------------------------
-- Pandoc imports

import Text.Pandoc
import Text.Pandoc.Builder
import Text.Pandoc.Options
import Text.Pandoc.Writers.Markdown


testTable :: IO ()
testTable = do
  let my_tab = simpleTable [plain "Column 1"] [[plain "hello there"], [plain "hello there 2"], [plain "hello there 2"]]
  let tab2 = Table [] [AlignDefault] [0.0] [[Plain [Str "Column",Space,Str "1"]]] [[[Plain [Str "Hello",Space,Str "there"]]] ,[[Plain [Str "Hello",Space,Str "there",Space,Str "2"]]] ,[[Plain [Str "Hello",Space,Str "there",Space,Str "3"]]]]

  let other_tab = simpleTable colHeaders [testRow, testRow]
  let thing = setTitle "Test report" $ doc $ para "Hey there" <> my_tab <> para "paragraph after table" <> singleton tab2 <> other_tab
  md' <- runIO (writeMarkdown def thing)
  case md' of
    Left _ -> print "Damn"
    Right md -> writeFile "my.md" (T.unpack md)



-- https://www.gwern.net/haskell/goodreadsToMarkdown.hs
colHeaders :: [Blocks]
colHeaders = map singleton [ Plain [Str "Title"]
                           , Plain [Str "Author"]
                           ]


testRow = map singleton [ Plain [Str "Col 1"]
                        , Plain [Str "Col 2"]
                        ]


main :: IO ()
main = do
  print "hello"
  testTable
  let headers = map (plain . text) ["foo", "bar"]
  let rows = map (map (plain . text)) [["1", "2"], ["3", "4"]]
--  md' <- runIO (writeMarkdown def (doc (simpleTable headers rows)))
  let caption' = text "Hello"
  md' <- runIO (writeMarkdown def (doc (table caption' [(AlignLeft, 1.0), (AlignLeft, 1.0)] headers rows)))
  case md' of
    Left _ -> print "Damn"
    Right md -> print md

1 个答案:

答案 0 :(得分:2)

Markdown默认情况下不支持表格。 Pandoc实现了多种输出表的方法,但这必须启用。最简单的方法是使用组成pandoc风格的Markdown的扩展集。

您可以代替writeMarkdown def thing来写

writeMarkdown (def{writerExtensions = pandocExtensions}) thing

现在您的程序使用pandoc的表扩展名之一生成表。