Java Call Ruby Kramdown

The example shows how java calls kramdown to convert markdown file to html.

Introduction

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

The example shows how java calls kramdown to convert markdown file to html.

Background

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

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

1. Install CLE

2. Install ruby and kramdown

  • skip

Using the code

1. Initialize CLE

  • initialize CLE
final StarCoreFactory starcore= StarCoreFactory.GetFactory();
StarServiceClass Service=starcore._InitSimple("test","123",0,0);
StarSrvGroupClass SrvGroup = (StarSrvGroupClass)Service._Get("_ServiceGroup");
Service._CheckPassword(false);
    
  • define callback function
starcore._RegMsgCallBack_P(new StarMsgCallBackInterface(){
    public Object Invoke(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"))
        {
            String Str;
            Str = (String)wParam;
            System.out.println(Str);
        }
        return null;
    }
});
    

2. Initialize ruby and set ruby libraries searching path

  • initialize ruby
starcore._SetScript("ruby","", "-v 2.2.0");
SrvGroup._InitRaw("ruby",Service);
StarObjectClass ruby = Service._ImportRawContext("ruby","",false,"");
  • ser ruby library search path
StarObjectClass LOAD_PATH = (StarObjectClass)ruby._R("LOAD_PATH");
LOAD_PATH._Call("unshift", "C:\\Ruby22\\lib\\ruby\\gems\\2.2.0\\gems\\kramdown-1.16.2\\lib");

3. Call kramdown

ruby._Call("require","kramdown");
StarObjectClass KramdownDocument = (StarObjectClass)ruby._Call("eval","Kramdown::Document");
StarObjectClass resobj = KramdownDocument._New("","","# aaaaaa");
Object res = resobj._Call("method_missing","to_html");
System.out.println(res);

4. Compile and Run

  • compile : javac -classpath .;c:\srplab\libs\starcore.jar java_call_ruby_kramdown.java

  • run : java -classpath .;c:\srplab\libs\starcore.jar java_call_ruby_kramdown

  • You can also add “starcore.jar” to environment variable “CLASSPATH”

5. Using SRPWatch to capture output from cle

  • output

Sample Download

History