【问题标题】:Convert Html or RTF to Markdown or Wiki Compatible syntax? [closed]将 Html 或 RTF 转换为 Markdown 或 Wiki 兼容语法? [关闭]
【发布时间】:2015-11-12 03:14:47
【问题描述】:

有没有 .net api 可以做到这一点?我看到Pandoc 有一个我可以打包的独立 exe,但如果那里已经有东西我宁愿不要。有什么建议么?

【问题讨论】:

    标签: c# .net api markdown


    【解决方案1】:

    这是我用来包装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;
    }
    

    【讨论】:

    • 两个小改动:“ASCIIEncoding.UTF8”应该是“Encoding.UTF8”,可以将“Thread.Sleep(2000)”替换为“p.WaitForExit(2000)”,会返回如果进程越早退出,则越早。
    • 为什么是string args = String.Format(@"-r html -t mediawiki"); 而不仅仅是string args = "-r html -t mediawiki";?我缺少 String.Format 的任何预期副作用吗?
    【解决方案2】:

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

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

    html 是您希望转换的 HTML 的字符串表示形式。我积极支持它并乐于接受贡献。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多