使用pypandoc将嵌套列表从JSON字符串转换为MD字符串



前言

我完全意识到,将JSON(复杂数据结构(转换为MD(标记语言(可能是一个定义不清的概念,因为它们无法表示相同的东西。无论如何,我认为它可以用于简单的结构,如嵌套列表。

关于pypandoc和这样的转换的文档很少,无论如何,我读过这篇文章,也有类似的问题,我想知道它是否可以实现,但我无法使它按预期工作。

MCVE

假设我想将一些简单的JSON嵌套列表转换为MD嵌套列表。这是我的MCVE

import json
import pypandoc
items = [
"Item A",
"Item B",
"Item C", ["Sub Item C.1", "Sub Item C.2", "Sub Item C.3"],
"Item D", ["Sub Item D.1", "Sub Item D.2"],
"Item E"
]
result = pypandoc.convert_text(json.dumps(items), to="json", format="md")

它显然返回了一个JSON字符串:

'{"pandoc-api-version":[1,22,2,1],"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"["},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"A"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"B"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"C:"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Str","c":"["},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Sub"},{"t":"Space"},{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"C.1"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Sub"},{"t":"Space"},{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"C.2"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Sub"},{"t":"Space"},{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"C.3"}]]},{"t":"Str","c":"],"},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"D"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Str","c":"["},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Sub"},{"t":"Space"},{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"D.1"}]]},{"t":"Str","c":","},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Sub"},{"t":"Space"},{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"D.2"}]]},{"t":"Str","c":"],"},{"t":"Space"},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"Item"},{"t":"Space"},{"t":"Str","c":"E"}]]},{"t":"Str","c":"]"}]}]}rn'

它似乎是需要编码的内容的内部表示,但现在是MD。

但我期待着这个输出:

- Item A
- Item B
- Item C
- Sub Item C.1
- Sub Item C.2
- Sub Item C.3
- Item D
- Sub Item D.1
- Sub Item D.2
- Item E

奇怪的是,开关toformat的混淆术语似乎表明了从MD到JSON的转换。

当按相反的顺序配置呼叫时,它失败得很惨:

result = pypandoc.convert_text(json.dumps(items), format="json", to="md")
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_7128/3771718373.py in <module>
----> 1 result = pypandoc.convert_text(json.dumps(items), format="json", to="md")
~anaconda3libsite-packagespypandoc__init__.py in convert_text(source, to, format, extra_args, encoding, outputfile, filters, verify_format, sandbox, cworkdir)
91     """
92     source = _as_unicode(source, encoding)
---> 93     return _convert_input(source, format, 'string', to, extra_args=extra_args,
94                           outputfile=outputfile, filters=filters,
95                           verify_format=verify_format, sandbox=sandbox,
~anaconda3libsite-packagespypandoc__init__.py in _convert_input(source, format, input_type, to, extra_args, outputfile, filters, verify_format, sandbox, cworkdir)
416     # check that pandoc returned successfully
417     if p.returncode != 0:
--> 418         raise RuntimeError(
419             'Pandoc died with exitcode "%s" during conversion: %s' % (p.returncode, stderr)
420         )
RuntimeError: Pandoc died with exitcode "64" during conversion: JSON parse error: Error in $: mempty

所以我想知道是否可以使用pypandoc将JSON转换为MD?

我应该如何处理第一个示例中返回的JSON,它是一个内部pandoc表示,之后确实可以转换为MD吗?我缺少什么来实现这种转换?

虽然没有直接回答这个问题,但似乎可以通过使用snakemd:来实现预期的输出

import snakemd
items = snakemd.MDList([
"Item A",
"Item B",
"Item C:", snakemd.MDList(["Sub Item C.1", "Sub Item C.2", "Sub Item C.3"]),
"Item D", snakemd.MDList(["Sub Item D.1", "Sub Item D.2"]),
"Item E"
])
items.render()

哪个返回:

- Item A
- Item B
- Item C:
- Sub Item C.1
- Sub Item C.2
- Sub Item C.3
- Item D
- Sub Item D.1
- Sub Item D.2
- Item E

但这并不能回答最初的问题。

相关内容

  • 没有找到相关文章

最新更新