Haskell 改变<;q>;及</q>;标签至“标签”;在特定位置配对

Haskell 改变<;q>;及</q>;标签至“标签”;在特定位置配对,haskell,filter,pandoc,Haskell,Filter,Pandoc,我正在使用一个工具链将markdown转换为HMTL5,用于作为HTML内容插入WordPress的可视化编辑器 当涉及到插入图像时,WordPress会将所谓的shortcode放入表单中 [caption id="attachment_100" align="aligncenter" width="300" caption="This is an image caption"] 转换为HTML文本。这不是真正的降价,而是由Pandoc解释的,它将每个“…”对翻译成对。这在WordPress

我正在使用一个工具链将markdown转换为HMTL5,用于作为HTML内容插入WordPress的可视化编辑器

当涉及到插入图像时,WordPress会将所谓的
shortcode
放入表单中

[caption id="attachment_100" align="aligncenter" width="300" caption="This is an image caption"]
转换为HTML文本。这不是真正的降价,而是由Pandoc解释的,它将每个
“…”
对翻译成
对。这在WordPress中无法正常工作

我需要防止转换
“…”
,但仅限于定义明确的
[标题…]
方括号内发生的转换,这些方括号由WordPress专门输入,不能与我输入的其他内容混淆

我对or的了解还不够,无法编写内联paseser/filter来免除这个文本片段的Pandoc处理。鉴于我对pandoc和Haskell缺乏了解,我在pandoc邮件列表上收到的建议到目前为止已经超出了我的想象

我曾想过编写一个Perl过滤器,但由于很好的理由,我被强烈劝阻不要使用regexp

我在这里问的是,是否有一种可靠的方法可以从
进行反向替换标记到
“…”
仅用于
[标题…]
块中的文本,作为后处理步骤,该文本已通过pandoc运行

有人能建议我怎么做吗


非常感谢。

你想要这样的东西吗

import Data.List
import System.IO

main = do
   inh  <- openFile "input.txt"  ReadMode
   outh <- openFile "output.txt" WriteMode
   str <- hGetContents inh
   hPutStrLn outh (outsideCaption str) 
   hClose inh
   hClose outh

outsideCaption::String->String
outsideCaption [] = []
outsideCaption str@(x:xs)
    | isPrefixOf "[caption" str = insideCaption str
    | otherwise                 = x:outsideCaption xs


insideCaption::String->String
insideCaption []       = []
insideCaption (']':xs) = ']':outsideCaption xs
insideCaption str@(x:xs)
    | (isPrefixOf "<q>"  str) = '\"':insideCaption (drop 3 str)
    | (isPrefixOf "</q>" str) = '\"':insideCaption (drop 4 str)
    |  otherwise              = x   :insideCaption         xs
使其从标准输入读取到标准输出,例如:

[rothesay]Ygfijj: echo "testing <q> [caption<q></q>]" | ./test 
testing <q> [caption""] 
[rothesay]Ygfijj:echo“测试[说明]”|/测试
测试[标题“”]

谢谢。我编译并运行了它,它运行正常!不过,我可以麻烦您做两个更改吗:(1)它是否可以就地运行,写入与输入相同的输出文件,或者以其他方式接受来自stdin和stdout的I/O?(2) 我意识到我还需要去掉一对标签。第一个出现在前面的
[标题
as
[阳离子
之前,第二个出现在第二个
]
as

之后,而不是第一个封闭的
]
之后。再次感谢。@chandra我添加了一个关于如何使用stdin/stdout的建议,您能为您提到的其他替换提供一个输入/输出示例吗?谢谢。我相信我现在可以读取和写入同一个文件。我无法在备注框中输入样本I/O。请看看这个。
[rothesay]Ygfijj: echo "testing <q> [caption<q></q>]" | ./test 
testing <q> [caption""]