File: validate_links.escript

package info (click to toggle)
erlang 1%3A25.2.3%2Bdfsg-1%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,972 kB
  • sloc: erlang: 1,440,803; xml: 473,412; ansic: 392,382; cpp: 164,287; makefile: 17,392; sh: 13,842; lisp: 9,675; java: 8,578; asm: 6,426; perl: 5,527; python: 5,469; javascript: 610; pascal: 126; sed: 72; php: 3
file content (370 lines) | stat: -rwxr-xr-x 15,698 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env escript
%% -*- erlang -*-
%%! +A 1 +SDio 1 +S 1 -mode minimal
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2020-2021. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%----------------------------------------------------------------------
%% File    : chunk.escript
%%
%% Created : 1 Nov 2018 by Kenneth Lundin <uabkeld@elxa31hr002>
%%
%% Trampoline to xml to chunk creation.
%%----------------------------------------------------------------------

-mode(compile).

main([ErlTop,Mod2App|Args]) ->
    Files = [parse_file(Arg) || Arg <- Args],
    M2A = parse_mod2app(Mod2App),
    put(top, ErlTop),
    put(exit_code, 0),
    CachedFiles = maps:from_list([{m2a,M2A}|Files]),
    [validate_links(File,CachedFiles) ||
        {{App,_},_} = File <- Files,
        lists:member(App,["edoc","wx","eunit","syntax_tools"]) =:= false],
    erlang:halt(get(exit_code)).

parse_file(Filename) ->
%    io:format("Parse: ~p~n",[Filename]),
    case xmerl_sax_parser:file(Filename, [skip_external_dtd,
                                          {event_fun,fun handle_event/3},
                                          {event_state,#{ markers => [],
                                                          funcs => [],
                                                          datatypes => []}}]) of
        {ok, Res, _} ->
%            [io:format("~p~n",[Res]) || maps:get(filename,Res) == {"erts","erl_tracer"}],
            {maps:get(filename,Res), Res}
    end.

handle_event(startDocument, _Line, State) ->
    {State,[]};
handle_event({startElement, _, Tag, _, Attr}, Line, {State, Stack}) ->
    NewState = event({startElement, Tag, [{A,V} || {_,_,A,V} <- Attr]}, Line, State, Stack),
    {NewState,[Tag | Stack]};
handle_event({endElement, _, Tag, _}, Line, {State, [Tag|NewStack]}) ->
    {event({endElement,Tag}, Line, State, NewStack), NewStack};
handle_event({characters, Chars}, Line, {State, Stack}) ->
    {event({characters,Chars}, Line, State, Stack), Stack};
handle_event(endDocument, _Line, {State,[]}) ->
    State;
handle_event(Ignore, _, S) when
      (not is_tuple(Ignore)) orelse (element(1,Ignore) =/= startElement),
      (not is_tuple(Ignore)) orelse (element(1,Ignore) =/= endElement),
      (not is_tuple(Ignore)) orelse (element(1,Ignore) =/= characters),
      Ignore =/= startDocument,
      Ignore =/= endDocument ->
    S;
handle_event(E,L,S) ->
    io:format("handle_event(~p,~p,~p) did not match~n",[E,L,S]),
    halt(1).

%% Get the filename of document
event({startElement, Tag, _Attr}, _Line, #{ type := Type } = State, _) when
      (Tag =:= "module") and (Type =:= "erlref");
      (Tag =:= "lib") and (Type =:= "cref");
      (Tag =:= "com") and (Type =:= "comref");
      (Tag =:= "file") and (Type =:= "fileref");
      (Tag =:= "app") and (Type =:= "appref");
      (Tag =:= "file") and (Type =:= "chapter") ->
    maps:put(filename,undefined,State);
event({characters,ModuleName},{Path,_,_},
      #{ type := "chapter", filename := undefined } = State, _) ->
    maps:put(filename,{path2app(Path),filename:rootname(ModuleName)}, State);
event({characters,ModuleName},{Path,_,_},
      #{ type := "appref", filename := undefined } = State, _) ->
    maps:put(filename,{path2app(Path),ModuleName ++ "_app"}, State);
event({characters,ModuleName},{Path,_,_},#{ filename := undefined } = State, _) ->
    maps:put(filename,{path2app(Path),ModuleName}, State);

%% Get the see* attributes
event({startElement, "see" ++ _ = What, Attr}, Line, State, [Parent|_])
%% Links are not rendered when emitted in <name>, but the edoc docs still has links here
%% So we ignore those...
  when Parent =/= "name" ->
    maps:put(What, [{Line,Attr}|maps:get(What, State, [])], State);

%% Get all the marker attributes
event({startElement, "marker", [{"id",Id}]}, _Line, State, _) ->
    maps:put(markers, [Id|maps:get(markers, State, [])], State);
event({startElement, "title", []}, _Line, State, _) ->
    error = maps:find(characters,State),
    maps:put(characters,[],State);
event({endElement, "title"}, _Line, State, _) ->
    Id = string:lowercase(
           re:replace(maps:get(characters,State),"[?: /()\"\r\n]","-",[global,{return,list}])),
    NewState = maps:put(markers, [Id|maps:get(markers, State, [])], State),
    maps:remove(characters,NewState);
event({startElement, "nametext", []}, _Line, State, _) ->
    error = maps:find(characters,State),
    maps:put(characters,[],State);
event({endElement, "nametext"}, _Line, State, _) ->
    Id = re:replace(maps:get(characters,State),"\\(.*","",[global,{return,list},dotall]),
    NewState = maps:put(markers, [Id|maps:get(markers, State, [])], State),
    maps:remove(characters,NewState);
event({startElement, "datatype_title", []}, _Line, State, _) ->
    error = maps:find(characters,State),
    maps:put(characters,[],State);
event({endElement, "datatype_title"}, _Line, State, _) ->
    Id = lists:flatten(maps:get(characters,State)),
    NewState = maps:put(markers, [Id|maps:get(markers, State, [])], State),
    maps:remove(characters,NewState);
event({startElement, "description", _}, _Line, State, _) ->
    maps:put(markers, ["description"|maps:get(markers, State, [])], State);

%% Get all func->name markers in erlref
event({startElement, "name", Attr}, _Line, #{ type := "erlref" } = State, ["func" | _]) ->
    AnchorState =
        case proplists:get_value("anchor",Attr) of
            undefined -> State;
            Anchor -> maps:put(markers, [Anchor|maps:get(markers, State, [])], State)
        end,
    case {proplists:get_value("name",Attr),proplists:get_value("arity",Attr)} of
        {undefined,undefined} ->
            %% We parse the function name from the content,
            %% so have to capture characters
            error = maps:find(characters,AnchorState),
            maps:put(characters,[],AnchorState);
        {Func,Arity} when Func =/= undefined, Arity =/= undefined ->
            maps:put(funcs, [{Func,Arity}|maps:get(funcs,AnchorState,[])], AnchorState)
    end;
event({endElement,"name"}, _Line, #{ characters := Chars} = State, ["func"|_]) ->
    FAs = docgen_xml_to_chunk:func_to_tuple(Chars),
    NewFuncs =
        lists:foldl(
          fun({F,A},Acc) ->
                  [{F,integer_to_list(A)} | Acc]
          end, maps:get(funcs,State,[]), FAs),
    maps:remove(characters, maps:put(funcs, NewFuncs, State));

%% Get all datatype->name markers in erlref
event({startElement, "name", Attr}, _Line, #{ type := "erlref" } = State, ["datatype" | _]) ->
    case {proplists:get_value("name",Attr),proplists:get_value("arity",Attr)} of
        {undefined,undefined} ->
            %% We parse the datatype name from the content,
            %% so have to capture characters
            error = maps:find(characters,State),
            maps:put(characters,[],State);
        {Name,Arity} when Name =/= undefined, Arity =:= undefined ->
            maps:put(datatypes, [Name|maps:get(datatypes,State,[])], State)
    end;
event({endElement,"name"}, _Line, #{ characters := Chars} = State, ["datatype"|_]) ->
    FAs = docgen_xml_to_chunk:func_to_tuple(Chars),
    NewFuncs =
        lists:foldl(
          fun({F,_A},Acc) -> [F | Acc] end, maps:get(datatypes,State,[]), FAs),
    maps:remove(characters, maps:put(datatypes, NewFuncs, State));

%% Capture all characters
event({characters,Chars},_Line, #{ characters := Acc } = State, _) ->
    maps:put(characters,[Acc,Chars],State);

%% Get the type of document
event({startElement, What, _Attr}, _, State, _) when
      What =:= "erlref";
      What =:= "appref";
      What =:= "comref";
      What =:= "cref";
      What =:= "fileref";
      What =:= "chapter" ->
    maps:put(type,What,State);
event({startElement, What, _Attr}, {Path,Filename,_}, State, _) when
      What =:= "book";
      What =:= "application";
      What =:= "internal";
      What =:= "part" ->
    State#{ filename => {path2app(Path),filename:rootname(Filename)}, type => What };
event(_Event, _Line, State, _) ->
    State.

parse_mod2app(Filename) ->
%    io:format("Parse: ~p~n",[Filename]),
    Event =
        fun({startElement,_,"module",_,[{_,_,"name",Name}]}, _, {undefined, Acc}) ->
                {Name,Acc};
           ({characters,Chrs}, _, {Name, Acc}) when Name =/= undefined ->
                {undefined,[{Name,Chrs}|Acc]};
           (_, _, State) ->
                State
        end,
    case xmerl_sax_parser:file(Filename, [skip_external_dtd,
                                          {event_fun,Event},
                                          {event_state,{undefined,[]}}]) of
        {ok, {_,Res}, _} ->
            maps:from_list(Res)
    end.

validate_links({Filename, Links}, CachedFiles) ->
    %% io:format("~s ~p~n",[Links]),
    lists:foreach(
      fun({LinkType, TypeLinks}) ->
              lists:foreach(
                fun({Line,Link}) ->
                        validate_link(Filename, LinkType, Line, Link, CachedFiles)
                end, TypeLinks)
      end, maps:to_list(maps:filter(fun(Key,_) -> not is_atom(Key) end,Links))).
validate_link(Filename, LinkType, Line, [{"marker",Marker}], CachedFiles) ->
    validate_link(Filename, LinkType, Line, Marker, CachedFiles);
validate_link(Filename, "seemfa", Line, Link, CachedFiles) ->
    case string:find(Link,"#") of
        nomatch ->
            fail(Line, "Invalid link in seemfa. "
                 "Must contains a '#'.");
        _ ->
            {App,Mod,Anchor} = ParsedLink = parse_link(Filename, maps:new(), Link),
            case string:lexemes(Anchor,"/") of
                [Func, Arity] ->
                    try list_to_integer(Arity) of
                        _ ->
                            MF = App ++ ":" ++ Mod ++ "#" ++ Func,
                            case maps:find({App,Mod},CachedFiles) of
                                error ->
                                    fail(Line, "Could not find ~ts in ~ts", [Mod, App]);
                                {ok, ModInfo} ->
                                    Funcs = maps:get(funcs,ModInfo),
                                    case lists:member({Func,Arity},Funcs) of
                                        true ->
                                            validate_type(Line, "seemfa",
                                                          read_link(Line, ParsedLink, CachedFiles));
                                        false ->
                                            fail(Line, "Could not find documentation for ~s when "
                                                 "resolving link",[MF  ++ "/" ++ Arity])
                                    end
                            end
                    catch _:_ ->
                            fail(Line, "Invalid arity for seemfa. "
                                 "Must end in a number")
                    end;
                _ ->
                    fail(Line, "Invalid anchor for seemfa. "
                         "Must contain a '/'.")
            end
    end;
validate_link(Filename, LinkType = "seetype", Line, Link, CachedFiles) ->
    {App,Mod,Type} = ParsedLink = parse_link(Filename, maps:get(m2a,CachedFiles), Link),
    Types = maps:get(datatypes,maps:get({App,Mod},CachedFiles)),
    case lists:member(Type, Types) of
        false ->
            fail(Line, "Could not find documentation for ~s when "
                 "resolving link",[App ++ ":" ++ Mod ++ "#" ++ Type]);
        _ ->
            validate_type(Line,LinkType,read_link(Line, ParsedLink, CachedFiles))
    end;
validate_link({"jinterface","jinterface_users_guide"},"seefile",_, _, _) ->
    %% Skip links to java documentation
    ok;
validate_link(Filename, LinkType, Line, Link, CachedFiles) ->
    ParsedLink = parse_link(Filename, maps:new(), Link),
    TargetInfo = read_link(Line, ParsedLink, CachedFiles),
    validate_type(Line,LinkType,TargetInfo),
    validate_marker(Line,ParsedLink,TargetInfo).

parse_link({SelfApp,Filename},Mod2App,Link) ->
    {AppMod, Marker} =
        case string:split(Link, "#") of
            [Link] ->
                {Link, ""};
            [AppMod0,Marker0] ->
                {AppMod0, Marker0}
        end,
    {App,Mod} =
        case string:split(AppMod, ":") of
            [""] ->
                {SelfApp,Filename};
            [AppMod] ->
                {maps:get(AppMod,Mod2App,SelfApp),AppMod};
            [App0,Mod0] ->
                {App0,Mod0}
        end,
    {App,Mod,Marker}.

read_link(_Line, {_App,"index",_}, _Cache) ->
    #{type => "index", markers => [] };
read_link(Line, {App,Mod,_}, Cache) ->
    case filename:extension(Mod) of
        Ext when Ext =:= ".svg"; Ext =:= ".png" ->
            #{type => "fileref", markers => [] };
        _ ->
            case maps:find({App,Mod}, Cache) of
                {ok, Info } ->
                    Info;
                error ->
                    %% fail(Line, "Could not find: ~p~p~n~p~n",
                    %%   [App,Mod,lists:sort(maps:keys(Cache))]),
                    fail(Line, "Could not find: ~s:~s~n",[App,Mod]),
                    halt(1)
            end
    end.

validate_type(_Line, "seeerl",#{ type := "erlref" }) ->
    ok;
validate_type(_Line, "seemfa", #{ type := "erlref"}) ->
    ok;
validate_type(_Line, "seetype", #{ type := "erlref"}) ->
    ok;
validate_type(_Line, "seeguide", #{ type := "chapter"}) ->
    ok;
validate_type(_Line, "seeguide", #{ type := "index"}) ->
    ok;
validate_type(_Line, "seefile", #{ type := "fileref"}) ->
    ok;
validate_type(_Line, "seeapp", #{ type := "appref"}) ->
    ok;
validate_type(_Line, "seeapp", #{ type := "index"}) ->
    ok;
validate_type(_Line, "seecom", #{ type := "comref"}) ->
    ok;
validate_type(_Line, "seecref", #{ type := "cref"}) ->
    ok;
validate_type(Line, From, #{ type := To }) ->
    fail(Line, "Invalid <~s> pointing to a <~s> file",[From,To]).

validate_marker(_Line, {_,_,[]}, _) ->
    ok;
validate_marker(Line, {_,_,Marker}, #{ filename := {App,Name}, markers := Markers }) ->
    case lists:any(fun(M) -> string:equal(Marker,M,true) end,Markers) of
        true ->
            ok;
        false ->
            StringMarkers = lists:join(", ",Markers),
            fail(Line, "Could not find marker <~s> in ~s:~s~n  Available: ~s",
                 [Marker,App,Name,StringMarkers])
    end.

path2app(Path) ->
    case lists:reverse(string:lexemes(Path,"/")) of
        ["xml","doc",App|_] ->
            App;
        [System,"xml","doc","system"|_] ->
            "system/" ++ System
    end.

%% getFileName("erts",Mod) ->
%%     filename:join([get(top),"erts","doc","xml",Mod++".xml"]);
%% getFileName("system/"++System,Mod) ->
%%     filename:join([get(top),"system","doc",System,"..","xml",System,Mod++".xml"]);
%% getFileName(App,Mod) ->
%%     filename:join([get(top),"lib",App,"doc","src","..","xml",Mod++".xml"]).

fail(Line, Slogan) ->
    fail(Line, Slogan,[]).
fail({Path, Filename, Line}, Slogan, SloganArgs) ->
    Format = lists:flatten(
               io_lib:format("~s:~p ~s~n",
                             [filename:join(Path,Filename),Line,Slogan])),
    io:format(Format, SloganArgs),
    put(exit_code, 1).