Search code examples
pascalscripthelpndoc

HelpNDoc 8.0.0.187 - Insert Snippet per Pascal Script into the current Topic position - how to?


I tried to save a Snippet to a TMemoryStream in the hope, I could insert the stream on the current topic. The stream is in binary form - I saw it, as I write the stream to my hard disk. Here is a code stripe of my code base for HelpNDoc Pascal-Engine. The parameter "content" stands for the Current HTML Topic text:

function getCustomHintBoxCode(content: String): String;
var _idx: Integer;

var _txt: String ;
var _tmp: String ;
var _str: String ;

var _arr: THndLibraryItemsInfoArray;
var _inf: THndLibraryItemsInfo;

const _hintbox = 'hintbox';
const _snippet = 'snippet'; 

begin
  _arr := HndLibraryItems.GetItemList([7]);  // 7 = Snippets
  _str := content;
  for _idx := 0 to Length(_arr) - 1 do
  begin
    _inf := _arr[ _idx ];
    _tmp := Trim( Copy(_inf.Caption,Length(_snippet)+2,64));
    if (LowerCase(Copy(_inf.Caption,1,7)) = _snippet) then
    begin
      HndLibraryItems.GetItemContent(_inf.id).SaveToFile('E:\out.tmp');
      showmessage('0: ' + _tmp);
    end;
  end;
  result := _str; 
end;

Is there a way, to save the stream directly to the current position of the existing topic ? Or: can the stream be saved a HTML or Text ?


Solution

  • I solved the problem in this multiple way:

    1. I have add a Variable to the Items-Library witht the name "hintbox_1"

    2. I set the placeholder string to "pophtml"

    3. I have added a new Variable: "enum -> HintBox=001 | HintBox=002 | ...

    4. I create 001.html file(s) in the sub-directory ".\helpers"

    5. I change the Pascal-Template "topics.pas.htm" file, so it fit my needs. the string "HintBox1=" would be cut, and rest "001" String, I added ".html", so I get: "001.html".

    6. let's compile the Project, give me the text of file 001.html into the current topic position, by replace text.

    My helper function shows so:

    // ------------------------------------------------------------------------
    // this is the root path of the HelpNDoc documentation
    // files. Some sub-folders will be include later ...
    // ------------------------------------------------------------------------ 
    const BASE_PATH = 'E:\HelpNDoc\mozilla\';
    
    // ------------------------------------------------------------------------
    // @brief  Get the variable, that is set in the content text (editor), and
    //         give back the contents of the variable that was defined in the
    //         template settings.
    //
    // @param  content - String: Body text for current topic.         
    //
    // @return String, that would be append to the current position of compiler
    //         processor. It replace the "hintbox" variable with files that will
    //         be involved as external files.
    //
    // @author Jens Kallup
    // ------------------------------------------------------------------------
    function getCustomHintBoxCode(content: String): String;
    var _idx: Integer;
    var _len: Integer;
    
    var _txt: String ;
    var _tmp: String ;
    var _str: String ;
    
    var _arr: THndLibraryItemsInfoArray;
    var _inf: THndLibraryItemsInfo;
    
    var _lst: TStringList;
    
    const _hintbox = 'hintbox';
    begin
      _arr := HndLibraryItems.GetItemList([5]);
      _str := content;
      for _idx := 0 to Length(_arr) - 1 do
      begin
        _inf := _arr[ _idx ];
        _tmp := Trim( Copy(_inf.Caption,Length(_hintbox)+2,64));
        if (LowerCase(Copy(_inf.Caption,1,7)) = _hintbox) then
        begin
          _tmp := HndLibraryItems .GetItemContentAsText (_inf.id);
          _txt := HndGeneratorInfo.GetCustomSettingValue(_tmp);
          _txt := StringReplace(_txt,'HintBox=','',[rfReplaceAll]);
          _txt := _txt + '.html';
          _lst := TStringList.Create;
          try
            try
              _lst.LoadFromFile(BASE_PATH + 'helpers\' + _txt);
            except
              on E: Exception do begin
                ShowMessage('Error occur during read the file: ' + #13#10 +
                BASE_PATH + 'helpers\' + _txt);
              end;
            end;
            if _lst.Count > 0 then
            begin
              _str := StringReplace(
              _str,                // old content
              _tmp,                // mark
              _lst.Text,           // new text
              [rfReplaceAll]);
            end; 
          finally
            _lst.Clear;
            _lst.Free;
          end;
        end;
      end;
      result := _str; 
    end;
    

    This bring me the advantage, to set tiny variable name into the Content-Editor instead the big fat boxes like Snippets, ... You would love it :-)

    I hope this can be usefull for any others. The code comes as beerware. So, please be fair, and use the code with a little note of this article.