1#!/usr/bin/env escript
2%% -*- erlang -*-
3%% %CopyrightBegin%
4%%
5%% Copyright Ericsson AB 2010-2018. All Rights Reserved.
6%%
7%% Licensed under the Apache License, Version 2.0 (the "License");
8%% you may not use this file except in compliance with the License.
9%% You may obtain a copy of the License at
10%%
11%%     http://www.apache.org/licenses/LICENSE-2.0
12%%
13%% Unless required by applicable law or agreed to in writing, software
14%% distributed under the License is distributed on an "AS IS" BASIS,
15%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16%% See the License for the specific language governing permissions and
17%% limitations under the License.
18%%
19%% %CopyrightEnd%
20%%----------------------------------------------------------------------
21%% File    : github_link.escript
22%%
23%% Created : 12 Dec 2017 by Lukas Larsson
24%%----------------------------------------------------------------------
25
26main([In, Filename, Sha, Out]) ->
27    {ok, Bin} = file:read_file(In),
28
29    TagsToAnnotate = ["description", "func", "datatype", "section"],
30
31    Subs = subs(TagsToAnnotate, Filename, Sha, re:split(Bin,[$\n])),
32
33    file:write_file(Out, Subs).
34
35subs([], _, _, Bin) ->
36    lists:join("\n", Bin);
37subs([Pat|Pats], Fn, Sha, Bin) ->
38    subs(Pats, Fn, Sha, sub(Bin, Pat, Fn, Sha)).
39
40sub(Bin, Pat, Fn, Sha) ->
41    sub(Bin, Pat, Fn, Sha, 1).
42sub([], _Pat, _Fn, _Sha, _Cnt) ->
43    [];
44sub([H|T], Pat, Fn, Sha, Cnt) ->
45    %% We use the maint branch here, it is not as exact as the tag,
46    %% but it is the best we can do as github does not allow doing
47    %% pullrequests on anything but branches.
48    [re:replace(H,["<",Pat,">"],
49                    ["<",Pat," ghlink=\"maint/",Fn,"#L",
50                     integer_to_list(Cnt),"\">"],[{return,list}]) |
51     sub(T, Pat, Fn, Sha, Cnt+1)].
52