将<q>和</q>标记更改为“在特定位置对”

时间:2012-04-22 01:53:23

标签: haskell filter pandoc

我使用工具链将markdown转换为HMTL5,使用Pandoc将其作为HTML内容插入WordPress的可视化编辑器。

在插入图片时,WordPress会将所谓的shortcode表格放入

[caption id="attachment_100" align="aligncenter" width="300" caption="This is an image caption"]

进入HTML文本。这不是真正的降价,而是由Pandoc解释,它将每个" ... "对转换为<q> ... </q>对,用于HTML输出。这在WordPress中无法正常工作。

我需要阻止 " ... "的转换,但只需要在明确定义的[caption ... ]方括号中出现的那些,这些方括号是由WordPress专门放入的,不能是与我投入的其他内容混淆。

我不太了解Pandoc APIHaskell来编写内联paseser /过滤器来从Pandoc处理中免除此文本片段。鉴于我对Pandoc和Haskell缺乏认识,到目前为止,我在pandoc邮件列表上收到的建议已经超出了我的想法。

我想过写一个Perl过滤器,但是因为非常好的理由而被强烈劝阻使用正则表达式。

我在这里要求找出是否有一种强大的方法可以对<q> ... </q>块中的" ... "块内的文本进行反向替换[caption ... ]块通过pandoc运行,作为后处理步骤。

有人可以建议我如何解决这个问题吗?

非常感谢。

1 个答案:

答案 0 :(得分:2)

你想要这样的东西吗?

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

这段代码读取名为“input.txt”的文件,执行您描述的替换并将结果打印到“output.txt”。

用以下内容替换当前的主要内容:

main = interact outsideCaption 

使其从stdin读取到stdout,例如:

[rothesay]Ygfijj: echo "testing <q> [caption<q></q>]" | ./test 
testing <q> [caption""]