UWP C# Call Ruby Kramdown

The example shows how c# calls kramdown to convert markdown file to html in application written for UWP(Universal Windows Platform).

Introduction

kramdown can convert markdown file to html, which is written in pure ruby.

The example shows how c# calls kramdown to convert markdown file to html in application written for UWP(Universal Windows Platform).

Background

This example is based on CLE. CLE supports multiple platforms, ruby language, and interaction between ruby and c#.

The sample is developed with c#, ruby 2.2, and kramdown-1.16.2.

1. Install CLE

2. Create new Windows Universal project

  • create new project

3. Set project parameters

  • Add reference, Star_csharp and Libstarcore

  • Add ruby share libraries

  • Add ruby libraries and kramdown files

Using the code

1. Initialize CLE

  • initialize CLE
StarCoreFactoryInit.Init(this);

StarCoreFactory starcore = StarCoreFactory.GetFactory();
StarServiceClass Service = (StarServiceClass)starcore._InitSimple("test", "123", 0, 0, null);
StarSrvGroupClass SrvGroup = (StarSrvGroupClass)Service._Get("_ServiceGroup");
    
  • define callback function
starcore._RegMsgCallBack_P(new StarMsgCallBackInterface(delegate (int ServiceGroupID, int uMes, object wParam, object lParam) {
    if (uMes == starcore._Getint("MSG_VSDISPMSG") || uMes == starcore._Getint("MSG_VSDISPLUAMSG") || uMes == starcore._Getint("MSG_DISPMSG") || uMes == starcore._Getint("MSG_DISPLUAMSG"))
    {
        Debug.WriteLine((string)wParam);
    }
    return null;
}));
    

2. Unzip ruby libraries and kramdown files to local folder

  • Unzip ruby libraries
try
{
    ZipFile.ExtractToDirectory(Package.Current.InstalledLocation.Path + "\\ruby.zip", ApplicationData.Current.LocalFolder.Path);
}
catch(Exception)
{

}
    
  • Unzip kramdown files
try
{
    ZipFile.ExtractToDirectory(Package.Current.InstalledLocation.Path + "\\kramdown-1.16.2.zip", ApplicationData.Current.LocalFolder.Path);
}
catch(Exception)
{

}
    

3. Initialize ruby and set ruby libraries searching path

  • initialize ruby
bool InitRawFlag = SrvGroup._InitRaw("ruby", Service);
dynamic ruby = Service._ImportRawContext("ruby", "", false, "");
  • ser ruby library search path
dynamic LOAD_PATH = ruby.LOAD_PATH;
LOAD_PATH.unshift(Package.Current.InstalledLocation.Path);
LOAD_PATH.unshift(ApplicationData.Current.LocalFolder.Path+ "\\ruby\\2.2.0");
LOAD_PATH.unshift(ApplicationData.Current.LocalFolder.Path + "\\kramdown-1.16.2\\lib");

4. Call kramdown

ruby.require("kramdown");
dynamic KramdownDocument = ruby.eval("Kramdown::Document");
dynamic resobj = KramdownDocument._New("", "", "# aaaaaa");
string res = resobj.method_missing("to_html");

Sample Download

History