如何使用 Pandoc 从降价文件中获取 YAML 元数据? [Haskell]
How to get YAML metadata from a markdown file using Pandoc? [Haskell]
我想获取元数据和内容。
鉴于此降价文本:
---
title: The Title
---
just some content
并且这个函数提取Meta
:
extractMeta :: Pandoc -> Meta
extractMeta (Pandoc meta _) = meta
我总是得到
Meta {unMeta = fromList []}
作为回应。
元数据块似乎包含在其他地方:
Pandoc (Meta {unMeta = fromList []}) [HorizontalRule,Para [Str "title:",Space,Str "Something",SoftBreak,Str "id:",Space,Str "123123",SoftBreak,Str "---"],Para [Str "hi",Space,Str "this",Space,Str "is",Space,Str "the",Space,Str "first",Space,Str "blog",Space,Str "post"]]
有什么方法可以让 Pandoc 使用其 Haskell API 解析 YAML 元数据?
谢谢。
完整代码:
module Main where
import Data.Text (pack, Text)
import Text.Pandoc (def, readMarkdown, runIO, ReaderOptions (readerStandalone))
import Data.Functor ((<&>))
readAndPack :: FilePath -> IO Text
readAndPack = (<$>) pack . readFile . (++) "posts/"
main :: IO ()
main = do
md <- readAndPack "binary.markdown"
pandoc <- runIO $ readMarkdown def { readerStandalone = True } md
print pandoc
问题是:默认情况下不启用 YAML 元数据。你需要 Ext_yaml_metadata_block
才能得到你想要的。这是按预期解析 YAML 元数据的完整代码。
module Main where
import Data.Text (pack, Text)
import Text.Pandoc (def, readMarkdown, runIO, ReaderOptions (readerStandalone))
import Text.Pandoc.Extensions
import Text.Pandoc.Options
import Data.Functor ((<&>))
readAndPack :: FilePath -> IO Text
readAndPack = (<$>) pack . readFile . (++) "posts/"
main :: IO ()
main = do
md <- readAndPack "binary.markdown"
pandoc <- runIO $
readMarkdown
(def {
readerStandalone = True,
readerExtensions =
enableExtension Ext_yaml_metadata_block (readerExtensions def) } )
md
print pandoc
这是输出:
Right (Pandoc (Meta {unMeta = fromList [("title",MetaInlines [Str "The",Space,Str "Title"])]}) [Para [Str "just",Space,Str "some",Space,Str "content"]])
我想获取元数据和内容。
鉴于此降价文本:
---
title: The Title
---
just some content
并且这个函数提取Meta
:
extractMeta :: Pandoc -> Meta
extractMeta (Pandoc meta _) = meta
我总是得到
Meta {unMeta = fromList []}
作为回应。
元数据块似乎包含在其他地方:
Pandoc (Meta {unMeta = fromList []}) [HorizontalRule,Para [Str "title:",Space,Str "Something",SoftBreak,Str "id:",Space,Str "123123",SoftBreak,Str "---"],Para [Str "hi",Space,Str "this",Space,Str "is",Space,Str "the",Space,Str "first",Space,Str "blog",Space,Str "post"]]
有什么方法可以让 Pandoc 使用其 Haskell API 解析 YAML 元数据?
谢谢。
完整代码:
module Main where
import Data.Text (pack, Text)
import Text.Pandoc (def, readMarkdown, runIO, ReaderOptions (readerStandalone))
import Data.Functor ((<&>))
readAndPack :: FilePath -> IO Text
readAndPack = (<$>) pack . readFile . (++) "posts/"
main :: IO ()
main = do
md <- readAndPack "binary.markdown"
pandoc <- runIO $ readMarkdown def { readerStandalone = True } md
print pandoc
问题是:默认情况下不启用 YAML 元数据。你需要 Ext_yaml_metadata_block
才能得到你想要的。这是按预期解析 YAML 元数据的完整代码。
module Main where
import Data.Text (pack, Text)
import Text.Pandoc (def, readMarkdown, runIO, ReaderOptions (readerStandalone))
import Text.Pandoc.Extensions
import Text.Pandoc.Options
import Data.Functor ((<&>))
readAndPack :: FilePath -> IO Text
readAndPack = (<$>) pack . readFile . (++) "posts/"
main :: IO ()
main = do
md <- readAndPack "binary.markdown"
pandoc <- runIO $
readMarkdown
(def {
readerStandalone = True,
readerExtensions =
enableExtension Ext_yaml_metadata_block (readerExtensions def) } )
md
print pandoc
这是输出:
Right (Pandoc (Meta {unMeta = fromList [("title",MetaInlines [Str "The",Space,Str "Title"])]}) [Para [Str "just",Space,Str "some",Space,Str "content"]])