Delphi Call Kramdown With Ruby

The example shows how to use kramdown to convert markdown file to html in apps developed with delphi.

Introduction

Delphi is widely used programming language and kramdown is used by Jekyll to parse the markdown file. 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 delphi.

Background

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

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

1. Install CLE

2. Create delphi project

  • create a vcl form project

3. Add starcore.pas to the project

  • Add starcore.pas to the project

    “starcore.pas” is open source file written in pascal, which encapsulates the interface functions of CLE.

    The code

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,starcore;
    

4. Use variant access methods and properties of objects

  • variant

    For easy access to the object’s methods and properties. The CLE object is encapsulated as a variant variable. In this way, you can handle the cle object to the same access delphi object. For example,

var
  varruby : variant;
  LOAD_PATH : variant;
  ...
  
begin
  varruby := SRPOBJECT_TOVARIANT(ruby,true);  
  LOAD_PATH := varruby.LOAD_PATH;
  LOAD_PATH.unshift('C:\Ruby22\lib\ruby\gems\2.2.0\gems\kramdown-1.16.2\lib');  
    
  • Please refer to the programmer’s guide for more details.

Using the code

1. Initialize CLE

  • Callback function
function MsgCallBack( ServiceGroupID:VS_ULONG; uMsg:VS_ULONG; wParam:VS_UWORD; lParam:VS_UWORD; IsProcessed:PVS_BOOL; Para:VS_UWORD ) : VS_UWORD; cdecl;
var
   str : string;
begin
   if ( uMsg = MSG_VSDISPLUAMSG) or (uMsg = MSG_VSDISPMSG ) or (uMsg = MSG_DISPMSG ) or (uMsg = MSG_DISPLUAMSG ) then
   begin
        str := TOVS_STRING(PVS_CHAR(wParam));
        if not (Length(str) = 0 ) then
        begin
            Form1.Memo1.Lines.Add(str);
        end;
   end;
   Result := 0;
end;
    
  • Use the following code to initialize CLE
    
var
  SRPControlInterface : Pointer;
  BasicSRPInterface : Pointer;
  ServiceID : VS_UUID;
  CLEInterface : Pointer;
  
begin
  
  StarCore_Init('libstarcore.dll');
  VSCore_RegisterCallBackInfo(@MsgCallBack,0);
  VSCore_Init( TRUE, TRUE, '', 0, '', 0,nil);
	SRPControlInterface := VSCore_QueryControlInterface();
	BasicSRPInterface := SRPControl_QueryBasicInterface(SRPControlInterface,0);
  INIT_UUID( ServiceID );
  if( SRPBasic_CreateService( BasicSRPInterface, '','test',@ServiceID,'123',0,0,0,0,0,0 ) = false ) then
     exit;
  CLEInterface := SRPBasic_GetSRPInterface(BasicSRPInterface,'test','root','123');
  (*--must set SRP_CLEInterface after getting the service interface--*)
  SRP_CLEInterface := CLEInterface;  

2. Initialize ruby and set ruby libraries searching path

  • kramdown-1.16.2 library path must be set to ruby before load.
  SRPControl_SetScriptInterface(SRPControlInterface,'ruby','', '-v 2.2.0');
  SRPBasic_InitRaw(BasicSRPInterface,'ruby',CLEInterface);
  ruby := SRPI_ImportRawContext(CLEInterface,'ruby','',false,NULL);
  varruby := SRPOBJECT_TOVARIANT(ruby,true);

  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');
  KramdownDocument := varruby.eval('Kramdown::Document');
  Memo1.Lines.Add(KramdownDocument);
  resobj := KramdownDocument.create('# aaaaaa');

  Result := resobj.method_missing('to_html');
  Memo1.Lines.Add(Result);   

Sample Download

History