program tidyhelp;

{ Crude interface to TidyLib's embedded documentation. }


{$IFDEF FPC}{$H+}{$MODE OBJFPC}{$ENDIF}

uses tidy_h, tidytree;


function GetMaxOptLength(doc:pTidyDoc):LongInt;
var
L:Longint;
id:TidyOptionID;
begin
  Result:=0;
  for id :=low(TidyOptionId) to high(TidyOptionId) do begin
    L:=length(string(tidyOptGetName(tidyGetOption(doc, id))));
    if ( L > Result ) then Result:=L;
  end;
end;

function rpad(const s:string; len:longint):string;
begin
  Result:=s;
  while ( length(result) < len ) do Result:=Result+' ';
end;


procedure WrapText(const src:pChar; width:LongInt);
var
  p1, p2:pChar;
  tmp:ansistring;
const
  Breakers=[#9, #32, ',', '.'];
begin
  WriteLn(StringOfChar('-', width));
  tmp:=src;
  EntsToChars(tmp);
  tmp:=tmp+#0;
  p1:=@tmp[1];
  repeat
  if ( length(p1) < width ) then begin
    WriteLn(' ', p1);
    BREAK;
  end;
  p2:=@p1[width];
  while ( p2 > p1 ) and ( not ( p2^ in  Breakers) ) do dec(p2);
  if ( p2^ in  Breakers) then begin
    WriteLn(' ', copy(p1, 1, p2-p1));
    if (p2^=#32) then inc(p2)
  end;
  p1:=p2;
  until False;
  WriteLn(StringOfChar('-', width));
end;


var
  doc:pTidyDoc;
  opt:pTidyOption;
  cat:TidyConfigCategory=TidyMarkup;
  iter:pTidyIterator;
  i:LongInt;
  s:shortstring;
  L, W:LongInt;
  e:LongInt;
  name:ansistring;
const
  WIDTH=50;
begin
  WriteLn;
  doc:=tidyCreate;
  W:=GetMaxOptLength(doc);

  repeat
    WriteLn('Choose category:');
    WriteLn('  1. Markup        2. Diagnostics      3. Pretty-Printing');
    WriteLn('  4. Encoding      5. Miscellaneous    6. Quit');
    Write('?>');
    read(input, s);
    if ( s = '' ) then S:='0';
    while ( length(s) <> 1 ) or ( not ( s[1] in ['1'..'6'] ) ) do begin
      WriteLn('Please enter a number from 1 to 6');
      Write('?>');
      ReadLn(input, s);
      if ( s = '' ) then S:='0'
    end;

    case S[1] of
      '1':cat:=TidyMarkup;
      '2':cat:=TidyDiagnostics;
      '3':cat:=TidyPrettyPrint;
      '4':cat:=TidyEncoding;
      '5':cat:=TidyMiscellaneous;
      '6': HALT;
    end;
    WriteLn('Choose option:');

    iter:=tidyGetOptionList(doc);
    i:=0;
    repeat
      opt:=tidyGetNextOption(doc, @iter);
      if ( opt = nil ) then BREAK;
      if ( tidyOptGetCategory(opt) = cat ) then begin
        inc(i);
        Write(i:2, ': ', rpad(tidyOptGetName(opt), W));
        if ( ( i mod 2 ) = 0 ) then WriteLn;
      end;
    until FALSE;
    if ( ( i mod 2 ) <> 0 ) then WriteLn;
    Write('?>');
    ReadLn(s);
    ReadLn(s); // why do i need this twice ???
    e:=0;
    val(s, L, e);
    while ( e <> 0 ) or ( L < 1 ) or ( L > i ) do begin
      WriteLn('Please enter a number between 1 and ', i);
      Write('?>');
      ReadLn(s);
      val(s, L, e);
    end;
    iter:=tidyGetOptionList(doc);
    i:=0;
    repeat
      opt:=tidyGetNextOption(doc, @iter);
      if ( opt = nil ) then BREAK;
      if ( tidyOptGetCategory(opt) = cat ) then begin
        inc(i);
        if ( i = L ) then begin
          WriteLn;
          name:=tidyOptGetName(opt);
          WriteLn(StringOfChar('-', WIDTH));
          WriteLn( StringOfChar( #32, ( WIDTH div 2 ) - ( length(name) div 2 )), name );
          WrapText(tidyOptGetDoc(doc, opt), WIDTH);
          WriteLn;
          BREAK;
        end;
      end;
    until False;
  until False;
  tidyRelease(doc);
end.



Get CurlPas and TidyPas at SourceForge.net. Fast, secure and Free Open Source software downloads