aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_docgen/priv/bin/github_link.escript
blob: 1b36fca202fb218f3d0e51ec48e2d07dd936a4ae (plain) (blame)
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
#!/usr/bin/env escript
%% -*- erlang -*-
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2016. 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    : github_link.escript
%%
%% Created : 12 Dec 2017 by Lukas Larsson
%%----------------------------------------------------------------------

main([In, Filename, Sha, Out]) ->
    {ok, Bin} = file:read_file(In),

    TagsToAnnotate = ["description", "func", "datatype", "section"],

    Subs = subs(TagsToAnnotate, Filename, Sha, re:split(Bin,[$\n])),

    file:write_file(Out, Subs).

subs([], _, _, Bin) ->
    lists:join("\n", Bin);
subs([Pat|Pats], Fn, Sha, Bin) ->
    subs(Pats, Fn, Sha, sub(Bin, Pat, Fn, Sha)).

sub(Bin, Pat, Fn, Sha) ->
    sub(Bin, Pat, Fn, Sha, 1).
sub([], _Pat, _Fn, _Sha, _Cnt) ->
    [];
sub([H|T], Pat, Fn, Sha, Cnt) ->
    %% We use the maint branch here, it is not as exact as the tag,
    %% but it is the best we can do as github does not allow doing
    %% pullrequests on anything but branches.
    [re:replace(H,["<",Pat,">"],
                    ["<",Pat," ghlink=\"maint/",Fn,"#L",
                     integer_to_list(Cnt),"\">"],[{return,list}]) |
     sub(T, Pat, Fn, Sha, Cnt+1)].