ios Call Ruby Kramdown

The example shows how to use kramdown to convert markdown file to html in apps developed with object-c/c++ for iphone.

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 object-c/c++ for iphone.

Background

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

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

1. Install CLE

2. Create new xcode project

  • create a single view application

3. Set project parameters

  • Set preprocessor macros, add “ENV_IOS”

  • Set “header search paths” and “library search paths”

  • Change file “ViewController.m” to “ViewController.mm”

  • Add link binary “libiconv.tbd”, etc

  • Add ruby libraries

  • Add kramdown-1.16.2.

Using the code

1. Initialize CLE

  • add “vsopenapi.h”
#include "vsopenapi.h"
    
  • define callback function
    
static class ClassOfSRPInterface *SRPInterface;

static VS_UWORD MsgCallBack( VS_ULONG ServiceGroupID, VS_ULONG uMsg, VS_UWORD wParam, VS_UWORD lParam, VS_BOOL *IsProcessed, VS_UWORD Para )
{
    switch( uMsg ){
        case MSG_VSDISPMSG :
        case MSG_VSDISPLUAMSG :
            printf("[core]%s\n",(VS_CHAR *)wParam);
            break;
        case MSG_DISPMSG :
        case MSG_DISPLUAMSG :
            printf("%s\n",(VS_CHAR *)wParam);
            break;
    }
    return 0;
}

2. Initialize cle,ruby and set ruby libraries searching path

  • initialize cle
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    const char* destDir = [documentsDirectory UTF8String];
    VS_BOOL Result = StarCore_Init((VS_CHAR *)destDir);
    
    VSCoreLib_InitRuby();
    
    VS_CORESIMPLECONTEXT Context;
    
    SRPInterface = VSCoreLib_InitSimple(&Context,"test","123",0,0,MsgCallBack,0,NULL);
    SRPInterface ->CheckPassword(VS_FALSE);
  • initialize ruby
    NSString *respaths = [[NSBundle mainBundle] resourcePath];
    const VS_CHAR *res_cpath = [respaths UTF8String];
    
    class ClassOfBasicSRPInterface *BasicSRPInterface;
    
    BasicSRPInterface = SRPInterface ->GetBasicInterface();
    BasicSRPInterface ->InitRaw("ruby", SRPInterface);
  • ser ruby library search path
    void *ruby = SRPInterface -> ImportRawContext("ruby", "", VS_FALSE, "");
    void *LOAD_PATH = (void *)SRPInterface -> ScriptGetObject(ruby,"LOAD_PATH", NULL);
    
    char pathbuf[512];
    sprintf(pathbuf,"%s/ruby/2.2.0",res_cpath);
    SRPInterface->ScriptCall(LOAD_PATH,NULL, "unshift", "(s)",pathbuf);
    
    sprintf(pathbuf,"%s/kramdown-1.16.2/lib",res_cpath);
    SRPInterface->ScriptCall(LOAD_PATH,NULL, "unshift", "(s)",pathbuf);

3. Call kramdown

    SRPInterface->ScriptCall(ruby,NULL, "require", "(s)","kramdown");
    void *KramdownDocument = (void *)SRPInterface->ScriptCall(ruby,NULL, "eval", "(s)o","Kramdown::Document");
    
    void *resobj = (void *)SRPInterface ->IMallocObjectL2(SRPInterface->GetIDEx(KramdownDocument),"s","# aaaaaa");
    char *html = (char *)SRPInterface->ScriptCall(resobj,NULL, "method_missing", "(s)s","to_html");

Sample Download

History