Dayvid86
Dayvid86

Reputation: 31

Create a script to rename help file topics ending in ".txt"

I imported text files to create a help but the problem is that the extension of the files is also put in the topic name. I need a script that renames the topic names ending with ".txt" to remove this part.

I tried to:

I don't know how to code with HelpNDoc, I don't know the Pascal language either!

How can I proceed?

Upvotes: 2

Views: 108

Answers (1)

help-info.de
help-info.de

Reputation: 7260

I assume you have imported some text files using File > Import > Import all known files in a specific folder. This is resulting in something like this. Please note the topic names at the end of the table of contents.

enter image description here

HelpNDoc's scripting system is a subset of the Object Pascal language. In addition to usual object Pascal code, HelpNDoc provides an extensive set of API methods which can be used to automate multiple tasks from project creation to control how documentation is generated.

You can use following script (at your own risk). Copy from here and save it to a *.pas file and load this into HelpNDoc's Script Editor (Tools > Script Editor). Some program lines for the output are left in the script for testing purposes only and can be deleted if needed.

//-----------------------------------------------------------------------------
// rename topic caption
// ----------------------------------------------------------------------------
// see also HelpNDoc API documentation for HndTopics
// https://www.helpndoc.com/documentation/html/HelpNDocAPImethods.html#HndTopics
// ----------------------------------------------------------------------------

var
  // current topic ID
  aTopicId, sCaptionOld, sCaptionNew: string;

function EndsWith(SubText, Text: string): Boolean;
var
  EndStr: string;
begin
  EndStr := Copy(Text, Length(Text) - Length(SubText) + 1, Length(SubText));
  Result := SameText(SubText, EndStr);
end;

begin
  try
    // get first topic
    aTopicId := HndTopics.GetTopicFirst();
    // loop through all topics
    while aTopicId <> '' do
    begin
      sCaptionOld := HndTopics.GetTopicCaption(aTopicId);
      
      if EndsWith('.txt', sCaptionOld) then
        begin
        sCaptionNew := sCaptionOld;   
        Delete (sCaptionNew, Length(sCaptionOld) - 3, 4);
        Print('Topic renamed.');        
        Print('Len: ' + IntToStr(Length(sCaptionOld)));
        Print('Old: ' + sCaptionOld);
        Print('New: ' + sCaptionNew);
        // the specific topic's caption
        // procedure SetTopicCaption(const aTopicId: string; const sNewCaption: string);
        HndTopics.SetTopicCaption(aTopicId, sCaptionNew);
        end
      else
        Print('Topic skipped.');
        
      // get next topic
      aTopicId := HndTopics.GetTopicNext(aTopicId);
    end

  finally
    Print('Rename done.');
  end;
end.

Upvotes: 2

Related Questions