Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Pandoc Convert

Description
This Macro converts between HTML and Markdown scripts using Pandoc.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Jun 18, 2018
Date Last Modified
Jun 18, 2018

Macro Code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;

// The 'text' parameter will contain the text from the:
//   - Current Clipboard when run by HotKey
//   - History Item when run from the History Menu
// The returned string will be:
//   - Placed directly on the Clipboard
//   - Ignored by ClipboardFusion if it is 'null'
public static class ClipboardFusionHelper
{
    // Change this variable to where the pandoc.exe is located on your machine
    private static readonly string PandocPath = @"C:\Program Files (x86)\Pandoc\pandoc.exe";
    private enum ConvertModeEnum
    {
        HTMLToMarkdown,
        MarkdownToHTML
    }

	public static string ProcessText(string text)
	{
		return Convert(text, ConvertModeEnum.HTMLToMarkdown);
	}
	
	private static string Convert(string source, ConvertModeEnum mode)
	{
        string args = "-r html -t markdown_strict";
        if(mode == ConvertModeEnum.MarkdownToHTML)
            args = "-r markdown_strict -t html";
            
        ProcessStartInfo info = new ProcessStartInfo(PandocPath, args);
        info.RedirectStandardOutput = true;
        info.RedirectStandardInput = true;

        Process process = new Process();
        process.StartInfo = info;
        info.UseShellExecute = false;
        process.Start();

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

        process.WaitForExit(2000);
        using (StreamReader reader = new StreamReader(process.StandardOutput.BaseStream))
            return reader.ReadToEnd();
	}
}