Windows Desktop C# Call Ruby Kramdown

The example shows how to use kramdown to convert markdown file to html in apps developed with c# for windows desktop.

Introduction

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

The example shows how to use kramdown to convert markdown file to html in apps developed with c# for windows desktop.

Background

This example is based on CLE. CLE supports windows platform, ruby language, and interaction between ruby and delphi.

The sample is developed with virsual studio 2017, ruby 2.2(Installed with RubyInstaller), and kramdown-1.16.2.

1. Install CLE

2. Create c# project

  • create a windows classic desktop project

3. Add Star_csharp reference

  • Add star_csharp62 to the project

Using the code

1. Initialize CLE

  • Using Star_csharp
using Star_csharp;
    
  • Use the following code to initialize CLE
    
StarCoreFactory starcore = StarCoreFactory.GetFactory();
starcore._RegMsgCallBack_P((int ServiceGroupID, int uMes, Object wParam, Object lParam) => 
{
    if (uMes == starcore._Getint("MSG_DISPMSG") || uMes == starcore._Getint("MSG_DISPLUAMSG") || uMes == starcore._Getint("MSG_VSDISPMSG") || uMes == starcore._Getint("MSG_VSDISPLUAMSG"))
    {
        Debug.WriteLine((String)wParam);
    }
    return null;
});
StarServiceClass Service = starcore._InitSimple("test", "123", 0, 0, null);
StarSrvGroupClass SrvGroup = (StarSrvGroupClass)Service._Get("_ServiceGroup");
Service._CheckPassword(false);

2. Initialize ruby and set ruby libraries searching path

  • kramdown-1.16.2 library path must be set to ruby before load.
SrvGroup._InitRaw("ruby", Service);
dynamic varruby = Service._ImportRawContext("ruby", "", false, "");

dynamic LOAD_PATH = varruby.LOAD_PATH;
LOAD_PATH.unshift("C:\\Ruby22\\lib\\ruby\\gems\\2.2.0\\gems\\kramdown-1.16.2\\lib");

3. Call kramdown

varruby.require("kramdown");
dynamic KramdownDocument= varruby.eval("Kramdown::Document");
dynamic resobj = KramdownDocument._New("","","# aaaaaa");

dynamic Result  = resobj.method_missing("to_html");
string str = Result as string;

Sample Download

History