sách gpt4 ăn đã đi

html - Pandoc 是否能够向任何元素注入(inject)任意 HTML 属性?

In lại 作者:太空狗 更新时间:2023-10-29 13:15:57 25 4
mua khóa gpt4 giày nike

因此代码块可以使用 fenced_code_blocks 扩展定义 HTML 属性:

~~~~ {#mycode .haskell .numberLines startFrom="100"}
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

是否可以以某种方式将上述语法用于常规文本 block ?例如,我想转换以下 Markdown 文本:

# My header

~~~ {.text}
This is regular text. This is regular text.
~~~

~~~ {.quote}
> This is the first level of quoting.
>
> > This is nested blockquote.
>
> Back to the first level.
~~~

~~~ {data-id=test-123}
+ Red
+ Green
+ Blue
~~~

Như thế này:

My header


This is regular text. This is regular text.



This is the first level of quoting.



This is nested blockquote.



Back to the first level.




  • Red

  • Green

  • Blue


如果 Pandoc 本身没有这样的支持,是否可以在 Lua 中创建一个自定义编写器来支持?

biên tập查看sample.lua自定义编写器,有人知道第 35 行的“属性表”是什么吗?以及如何将这些属性传递给特定的 Pandoc 元素?此外,我在上面寻找的功能与 header_extension 非常相似扩展除了它适用于所有元素,而不仅仅是标题。

câu trả lời hay nhất

Pandoc's filters让您对文档的 Pandoc 内部表示进行操作。可以有一个执行不同转换的过滤器链。我将分享两个应该有所帮助的说明性过滤器示例。

Markdown 代码块

Pandoc 中的代码块通常用于嵌入来自编程语言的源代码列表,但在这里我们试图提取主体并将其解释为 Markdown 。而不是使用输入文档中的类,如 chữquote ,让我们使用通用的 as-markdown类(class)。 Pandoc 将自动生成适当的标签。

# My header

~~~ {.as-markdown}
This is regular text. This is regular text.
~~~

~~~ {.as-markdown}
> This is the first level of quoting.
>
> > This is nested blockquote.
>
> Back to the first level.
~~~

~~~ {.as-markdown data-id=test-123}
+ Red
+ Green
+ Blue
~~~

~~~ haskell
main :: IO ()
~~~

确保代码块没有 as-markdown类像往常一样解释,我包括了一个haskell代码块。这是过滤器的实现:

#!/usr/bin/env runhaskell
import Text.Pandoc.Definition (Pandoc(..), Block(..), Format(..))
import Text.Pandoc.Error (handleError)
import Text.Pandoc.JSON (toJSONFilter)
import Text.Pandoc.Options (def)
import Text.Pandoc.Readers.Markdown (readMarkdown)

asMarkdown :: String -> [Block]
asMarkdown contents =
case handleError $ readMarkdown def contents of
Pandoc _ blocks -> blocks

-- | Unwrap each CodeBlock with the "as-markdown" class, interpreting
-- its contents as Markdown.
markdownCodeBlock :: Maybe Format -> Block -> IO [Block]
markdownCodeBlock _ cb@(CodeBlock (_id, classes, _namevals) contents) =
if "as-markdown" `elem` classes then
return $ asMarkdown contents
khác
return [cb]
markdownCodeBlock _ x = return [x]

main :: IO ()
main = toJSONFilter markdownCodeBlock

正在运行 pandoc --filter markdown-code-block.hs index.mdsản xuất:

My header


This is regular text. This is regular text.



This is the first level of quoting.



This is nested blockquote.



Back to the first level.




  • Red

  • Green

  • Blue


main :: IO ()

快到了!唯一不太正确的部分是 HTML 属性。

来自代码块元数据的自定义 HTML 属性

以下过滤器应该可以帮助您入门。它使用 web-script 转换代码块类到 HTML 当目标格式为 html 时标记或 html5 .

#!/usr/bin/env runhaskell
import Text.Pandoc.Builder
import Text.Pandoc.JSON

webFormats :: [String]
webFormats =
[ "html"
, "html5"
]

script :: String -> Block
script src = Para $ toList $ rawInline "html" ("")

injectScript :: Maybe Format -> Block -> IO Block
injectScript (Just (Format format)) cb@(CodeBlock (_id, classes, _namevals) contents) =
if "web-script" `elem` classes then
if format `elem` webFormats then
return $ script contents
khác
return Null
khác
return cb
injectScript _ x = return x

main :: IO ()
main = toJSONFilter injectScript

data-id=test-123在你的最后一个区 block 中会通过 _namevals的键值对类型为 [(String, String)] .您需要做的就是重构 kịch bản支持 HTML 属性的任意标签和键值对,并根据这些输入指定要生成的 HTML。要查看输入文档的本地表示,请运行 pandoc -t native index.md .

[Header 1 ("my-header",[],[]) [Str "My",Space,Str "header"]
,CodeBlock ("",["as-markdown"],[]) "This is regular text. This is regular text."
,CodeBlock ("",["as-markdown"],[]) "> This is the first level of quoting.\n>\n> > This is nested blockquote.\n>\n> Back to the first level."
,CodeBlock ("",["as-markdown"],[("data-id","test-123")]) "+ Red\n+ Green\n+ Blue"
,Para [Str "To",Space,Str "ensure",Space,Str "regular",Space,Str "code",Space,Str "blocks",Space,Str "work",Space,Str "as",Space,Str "usual."]
,CodeBlock ("",["haskell"],[]) "main :: IO ()"]

如果您想试用这些示例中的任何一个,它们都在我的 pandoc-experiments 中。存储库。

关于html - Pandoc 是否能够向任何元素注入(inject)任意 HTML 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20200605/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress