File: PasDoc_Tipue.pas

package info (click to toggle)
pasdoc 0.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 33,536 kB
  • sloc: pascal: 28,894; javascript: 7,665; xml: 2,597; makefile: 519; sh: 417
file content (281 lines) | stat: -rw-r--r-- 10,343 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{
  Copyright 1998-2018 PasDoc developers.

  This file is part of "PasDoc".

  "PasDoc" is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  "PasDoc" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with "PasDoc"; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

  ----------------------------------------------------------------------------
}

{ @abstract(Helper unit for integrating tipue [http://www.tipue.com/]
  with pasdoc HTML output.) }
unit PasDoc_Tipue;

interface

uses PasDoc_Utils, PasDoc_Items;

{ Put this in <head> of every page with search button. }
function TipueSearchButtonHead: string;

{ Put this at a place where Tipue button should appear.
  It will make a form with search button.
  You will need to use Format to insert the localized word for "Search", e.g.:
  Format(TipueSearchButton, ['Search']) for English. }
function TipueSearchButton: string;

{ Adds some additional files to html documentation, needed for tipue engine.

  OutputPath is our output path, where html output must be placed.
  Must end with PathDelim.

  Units must be non-nil. It will be used to generate index data for tipue. }
procedure TipueAddFiles(Units: TPasUnits;
  const Introduction, Conclusion: TExternalItem;
  const AdditionalFiles: TExternalItemList;
  const Head, BodyBegin, BodyEnd: string;
  const LanguageCode: string;
  const OutputPath: string);

implementation

uses Classes, SysUtils;

function TipueSearchButtonHead: string;
begin
  { Note that this deliberately doesn't contain various Tipue JavaScript files.
    They are only needed on Tipue results page, and are included by tipue_results.html . }
  Result := '<link rel="stylesheet" type="text/css" href="tipuesearch/tipuesearch.css">' + LineEnding;
end;

function TipueSearchButton: string;
begin
  Result :=
    '<form class="search-form" action="tipue_results.html">' +
    //'<div class="search-input"><input type="text" name="q" id="tipue_search_input"></div>' +
    '<div class="search-input">'+
    '<input type="text" name="q" id="tipue_search_input" pattern=".{3,}" title="At least 3 characters" required>' +

    //<button type="submit" class="tipue_search_button"><div class="tipue_search_icon">&#9906;</div></button>'+

    { TODO: Add a value="Search" to <input type="button" ...>
      and hide it visually by one of the CSS tricks on
      http://stackoverflow.com/questions/12723937/remove-value-attribute-of-input-element-using-css-only }
    '<div class="search-button"><input type="button" id="tipue_search_button" onclick="this.form.submit();"></div>' +
    '</div></form>' + LineEnding +
    '<div style="clear: both"></div>' + LineEnding;
end;

procedure TipueAddFiles(Units: TPasUnits;
  const Introduction, Conclusion: TExternalItem;
  const AdditionalFiles: TExternalItemList;
  const Head, BodyBegin, BodyEnd: string;
  const LanguageCode: string;
  const OutputPath: string);

  procedure WriteTipueIndexData(const FileName: string);
  var
    OutFile: TextFile;
    NeedsLeadingComma: boolean;
    i: Integer;

    { Write one line of index data.
      See http://www.tipue.com/help/search/data/.
      Note: we try hard to *not* place a comma on the final newline,
      as google suggests that IE may have problems with it.

      For now ShortDescription is not used. It was useful
      with old tipue version, we keep calculating it as it may be useful again
      in the future. }
    procedure WriteIndexData(
      const Title, URL, ShortDescription, LongDescription: string);
    begin
      if NeedsLeadingComma then
        Write(OutFile, ',');
      Writeln(OutFile);
      Write(OutFile, '     {"title": "', Title,
        '", "text": "', LongDescription,
        '", "tags": "', { no tags for now? Or maybe use here ShortDescription? }
        '", "url": "', URL, '"}');
      NeedsLeadingComma := true;
    end;

    procedure WriteItemIndexData(Item: TBaseItem);

      function EscapeIndexEntry(const S: string): string;
      const
        { We want to avoid introducing special chars in JavaScript string,
          so \ and " and newline markers must be escaped. }
        ReplacementArray: array[0..3] of TCharReplacement = (
          (cChar: #10; sSpec: ' '),
          (cChar: #13; sSpec: ' '),
          (cChar: '"'; sSpec: '\"'),
          (cChar: '\'; sSpec: '\\')
        );
      begin
        { Strip HTML tags, as Tipue results printing assumes that text contains
          no HTML elements, otherwise they are inserted into page source
          and break HTML rendering. }
        Result := StripHtml(S);
        Result := StringReplaceChars(Result, ReplacementArray);
      end;

    var
      ShortDescription, LongDescription: string;
      EnumMember: TPasItem;
      i: Integer;
    begin
      { calculate ShortDescription }
      if Item is TPasItem then
        ShortDescription :=
          EscapeIndexEntry(TPasItem(Item).AbstractDescription) else
      if Item is TExternalItem then
        ShortDescription :=
          EscapeIndexEntry(TExternalItem(Item).ShortTitle) else
        ShortDescription := '';

      { calculate LongDescription.
        Note that LongDescription will not be shown to user anywhere
        (it will only be searched by tipue), so we don't care how
        things look here. We just glue some properties of Item together. }
      LongDescription :=
        EscapeIndexEntry(Item.DetailedDescription) +
        ' ' + EscapeIndexEntry(Item.Authors.Text);
      if Item is TPasItem then
        LongDescription := LongDescription +
          ' ' + EscapeIndexEntry(TPasItem(Item).Params.Text(' ', ' ')) +
          ' ' + EscapeIndexEntry(TPasItem(Item).Raises.Text(' ', ' '));
      if Item is TPasMethod then
        LongDescription := LongDescription +
          ' ' + EscapeIndexEntry(TPasMethod(Item).Returns);
      if Item is TPasEnum then
      begin
        for i := 0 to TPasEnum(Item).Members.Count - 1 do
        begin
          EnumMember := TPasEnum(Item).Members.PasItemAt[i];
          LongDescription := LongDescription +
            ' ' + EscapeIndexEntry(EnumMember.Name) +
            ' ' + EscapeIndexEntry(EnumMember.AbstractDescription) +
            ' ' + EscapeIndexEntry(EnumMember.DetailedDescription) +
            ' ' + EscapeIndexEntry(EnumMember.Authors.Text);
        end;
      end;
      if Item is TExternalItem then
      begin
        LongDescription := LongDescription +
          ' ' + EscapeIndexEntry(TExternalItem(Item).Title);
      end;

      WriteIndexData(Item.QualifiedName, Item.FullLink,
        ShortDescription, LongDescription);
    end;

    procedure WriteItemsIndexData(Items: TPasItems);
    var
      i: Integer;
    begin
      for i := 0 to Items.Count - 1 do
        WriteItemIndexData(Items.PasItemAt[i]);
    end;

    procedure WriteCIOsIndexData(CIOs: TPasItems);
    var
      i: Integer;
      CIO: TPasCIO;
    begin
      for i := 0 to CIOs.Count - 1 do
      begin
        CIO := CIOs[i] as TPasCIO;
        WriteItemIndexData(CIO);
        WriteItemsIndexData(CIO.Fields);
        WriteItemsIndexData(CIO.Methods);
        WriteItemsIndexData(CIO.Properties);
        WriteItemsIndexData(CIO.Types);
        if CIO.Cios.Count > 0 then
          WriteCIOsIndexData(CIO.Cios);
      end;
    end;

    procedure WriteUnitsIndexData(Units: TPasUnits);
    var
      i: Integer;
      U: TPasUnit;
    begin
      for i := 0 to Units.Count - 1 do
      begin
        U := Units[i] as TPasUnit;
        WriteItemIndexData(U);
        WriteCIOsIndexData(U.CIOs);
        WriteItemsIndexData(U.Constants);
        WriteItemsIndexData(U.FuncsProcs);
        WriteItemsIndexData(U.Types);
        WriteItemsIndexData(U.Variables);
      end;
    end;

  begin
    Assign(OutFile, FileName);
    Rewrite(OutFile);
    try
      Write(OutFile, 'var tipuesearch = {"pages": [');
      NeedsLeadingComma := false;

      if Introduction <> nil then
        WriteItemIndexData(Introduction);
      if Conclusion <> nil then
        WriteItemIndexData(Conclusion);
      if (AdditionalFiles <> nil) and (AdditionalFiles.Count > 0) then
      begin
        for i := 0 to AdditionalFiles.Count - 1 do
        begin
          WriteItemIndexData(AdditionalFiles.Get(i));
        end;
      end;
      WriteUnitsIndexData(Units);

      Writeln(OutFile, LineEnding + ']};');
    finally CloseFile(OutFile) end;
  end;

const
  TipueSearchCss: {$I tipuesearch.css.inc};
  TipueSearchScript: {$I tipuesearch.js.inc};
  TipueSearchSetScript:{$I tipuesearch_set.js.inc};
  JQueryScript: {$I jquery.min.js.inc};
  TipueSearchImage: {$I search.png.inc};
  TipueLoaderImage: {$I loader.gif.inc};
var
  TipueResultsPage: string;
begin
  CreateDir(OutputPath + 'tipuesearch');
  DataToFile(OutputPath + 'tipuesearch' + PathDelim + 'tipuesearch.css', TipueSearchCss);
  DataToFile(OutputPath + 'tipuesearch' + PathDelim + 'tipuesearch.js', TipueSearchScript);
  DataToFile(OutputPath + 'tipuesearch' + PathDelim + 'tipuesearch_set.js', TipueSearchSetScript);
  DataToFile(OutputPath + 'tipuesearch' + PathDelim + 'jquery.min.js', JQueryScript);

  TipueResultsPage := {$I tipue_results.html.inc};
  TipueResultsPage := StringReplace(TipueResultsPage, '###-PASDOC-HEAD-###', Head, []);
  TipueResultsPage := StringReplace(TipueResultsPage, '###-PASDOC-BODY-BEGIN-###', BodyBegin, []);
  TipueResultsPage := StringReplace(TipueResultsPage, '###-PASDOC-BODY-END-###', BodyEnd, []);
  TipueResultsPage := StringReplace(TipueResultsPage, '###-PASDOC-LANGUAGE-###', LanguageCode, []);
  StringToFile(OutputPath + 'tipue_results.html', TipueResultsPage);

  DataToFile(OutputPath + 'tipuesearch' + PathDelim + 'search.png', TipueSearchImage);
  DataToFile(OutputPath + 'tipuesearch' + PathDelim + 'loader.gif', TipueLoaderImage);
  WriteTipueIndexData(OutputPath + 'tipuesearch' + PathDelim + 'tipuesearch_content.js');
end;

end.