将Html或RTF转换为Markdown或Wiki兼容语法?

时间:2011-05-25 05:07:50

标签: c# .net api markdown

是否有.net api可以做到这一点?我看到Pandoc有一个我可以包装的独立exe,但是如果已经存在某些内容我宁愿不知道。有什么建议?

2 个答案:

答案 0 :(得分:25)

这是我用来包装pandoc的代码。到目前为止,我还没有看到任何其他不错的方法。

public string Convert(string source)
{
    string processName = @"C:\Program Files\Pandoc\bin\pandoc.exe";
    string args = String.Format(@"-r html -t mediawiki");

    ProcessStartInfo psi = new ProcessStartInfo(processName, args);

    psi.RedirectStandardOutput = true;
    psi.RedirectStandardInput = true;

    Process p = new Process();
    p.StartInfo = psi;
    psi.UseShellExecute = false;
    p.Start();

    string outputString = "";
    byte[] inputBuffer = Encoding.UTF8.GetBytes(source);
    p.StandardInput.BaseStream.Write(inputBuffer, 0, inputBuffer.Length);
    p.StandardInput.Close();

    p.WaitForExit(2000);
    using (System.IO.StreamReader sr = new System.IO.StreamReader(
                                           p.StandardOutput.BaseStream))
    {

        outputString = sr.ReadToEnd();
    }

    return outputString;
}

答案 1 :(得分:7)

我创建了一个库Html2Markdown。用法非常简单。

var markdown = new Converter().Convert(html);

其中html是您要转换的HTML的字符串表示形式。我积极支持它,并乐意接受贡献。

相关问题