summaryrefslogtreecommitdiff
path: root/docs/tipue/tipue_data.lpr
blob: 2910f24c9048b1db60d0b481e6cd23e491e6672f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
program tipue_data;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils;
  
type
  TContentParser = class(TObject)
  private
    FList: TStringList;
    index: integer;
    procedure   ParseCntFile(AFilename: string);
    procedure   WriteDataFile;
    function    ExtractDescription(AFilename: string): string;
  public
    constructor Create;
    destructor  Destroy; override;
    procedure   Run;
  end;
  
  
function tiNumToken(const AValue, AToken : string): integer;
var
  i, iCount : integer;
  lsValue : string;
begin
  Result := 0;
  if AValue = '' then
    Exit; //==>

  iCount := 0;
  lsValue := AValue;
  i := pos(AToken, lsValue);
  while i <> 0 do begin
    delete(lsValue, i, length(AToken));
    inc(iCount);
    i := pos(AToken, lsValue);
  end;
  Result := iCount + 1;
end;

function tiToken(const AValue, AToken: string; const APos: integer): string;
var
  i, iCount, iNumToken: integer;
  lsValue: string;
begin
  result := '';

  iNumToken := tiNumToken(AValue, AToken);
  if APos = 1 then begin
    if pos(AToken, AValue) = 0 then result := AValue
    else result := copy(AValue, 1, pos(AToken, AValue)-1);
    end
  else if (iNumToken < APos-1) or (APos<1) then begin
    result := '';
    end
  else begin

    { Remove leading blocks }
    iCount := 1;
    lsValue := AValue;
    i := pos(AToken, lsValue);
    while (i<>0) and (iCount<APos) do begin
      delete(lsValue, 1, i + length(AToken) - 1);
      inc(iCount);
      i := pos(AToken, lsValue);
    end;

    if (i=0) and (iCount=APos) then result := lsValue
    else if (i=0) and (iCount<>APos) then
      result := ''
    else
      result := copy(lsValue, 1, i-1);
  end;
end;

function tiGetTickCount: Cardinal;
begin
  Result := Cardinal(Trunc(Now * 24 * 60 * 60 * 1000));
end;


{ TContentParser }

procedure TContentParser.ParseCntFile(AFilename: string);
const
  cLine = 's[%d] = "%s^%s^%s^%s^0"';
var
  sl: TStringList;
  l: integer;
  s: string;
  prefix: string;
  desc: string;
begin
  s := '#';
  sl := TStringList.Create;
  try
    sl.LoadFromFile(AFilename);
    l := sl.IndexOf(':link tree');
    while (s <> '') or (s <> ':classes') do
    begin
//      writeln(' prosessing.... ', s);
      if (s = '') or (s =':classes') then
        break
      else if s[1] = '#' then
      begin
        delete(s, 1, 1);
        prefix := lowercase(tiToken(s, ' ', 1)) + PathDelim;
      end
      else
      begin
        desc := ExtractDescription(prefix + tiToken(trim(s), ' ', 2));
        FList.Add(Format(cLine, [
            index,                                // Array element number
            tiToken(trim(s), ' ', 1),             // Title
            prefix + tiToken(trim(s), ' ', 2),    // URL
            desc,                                 // Display Description
            desc                                  // Searchable Description
            ]));
        inc(index);
      end;

      if l+1 < sl.Count then
      begin
        inc(l);
        s := trim(sl[l]);
      end
      else
        break;
    end;  { while }
  finally
    sl.Free;
  end;
end;

procedure TContentParser.WriteDataFile;
begin
  FList.Insert(0, 'var s = new Array()');
  FList.Insert(1, '');
  FList.SaveToFile('tip_data.js');
end;

function TContentParser.ExtractDescription(AFilename: string): string;
var
  sl: TStringList;
  idx: integer;
  s: string;
begin
  sl := TStringList.Create;
  try
  sl.LoadFromFile('./' + AFilename);
  idx := sl.IndexOf('<h2>Declaration</h2>') -1;
  if idx >= 0 then
  begin
    s := Copy(sl[idx], 4, Length(sl[idx]) -8);
    Result := s;
  end
  else
    Result := '';
  except
      Result := '';
  end;
  sl.Free;
end;

constructor TContentParser.Create;
begin
  FList := TStringList.Create;
  index := 0;
end;

destructor TContentParser.Destroy;
begin
  FList.Free;
  inherited Destroy;
end;

procedure TContentParser.Run;
begin
  ParseCntFile('./corelib.cnt');
  ParseCntFile('./gui.cnt');
  WriteDataFile;
end;


var
  p: TContentParser;
  start: TDateTime;
begin
  p := TContentParser.Create;
  try
    start := Now;
    p.Run;
    writeln('Data processed in ', FormatDateTime('ss.zzz', Now - start), ' seconds.');
  finally
    p.Free;
  end;
end.