-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathgh-codeblock.html
63 lines (50 loc) · 2.32 KB
/
gh-codeblock.html
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
{{ $apiBaseUrl := .Get "apiBaseUrl" | default "https://api.github.com/repos" }}
{{ $webBaseUrl := .Get "webBaseUrl" | default "https://github.com" }}
{{ $org := .Get "org" | default "SeleniumHQ" }}
{{ $repo := .Get "repo" | default "seleniumhq.github.io" }}
{{ $branch := .Get "branch" | default "trunk" }}
{{ $defaultBranchFromEnv := (getenv "SELENIUM_EXAMPLES_BRANCH") }}
{{ if $defaultBranchFromEnv }}
{{ $branch = $defaultBranchFromEnv }}
{{ end }}
{{ $defaultOrgFromEnv := (getenv "SELENIUM_EXAMPLES_ORG") }}
{{ if $defaultOrgFromEnv }}
{{ $org = $defaultOrgFromEnv }}
{{ end }}
{{ $defaultRepoFromEnv := (getenv "SELENIUM_EXAMPLES_REPO") }}
{{ if $defaultRepoFromEnv }}
{{ $repo = $defaultRepoFromEnv }}
{{ end }}
{{ $fullPath := .Get "path" }}
{{ $path := index (split $fullPath "#") 0 }}
{{ $apiUrl := printf "%s/%s/%s/contents%s?ref=%s" $apiBaseUrl $org $repo $path $branch }}
{{ $webUrl := printf "%s/%s/%s/blob/%s/%s" $webBaseUrl $org $repo $branch $fullPath }}
{{ $slicedPath := split $path "/" }}
{{ $language := index (last 1 (split (index (last 1 $slicedPath) 0) ".")) 0 }}
{{ $githubToken := (getenv "SELENIUM_CI_TOKEN") }}
{{ if $githubToken }}
{{ $toReplace := printf "://%s@" $githubToken }}
{{ $tokenInUrl := cond (eq $githubToken "") "://" $toReplace }}
{{ $apiUrlWithToken := replace $apiUrl "://" $tokenInUrl }}
{{ $apiResults := getJSON $apiUrlWithToken }}
{{ $content := base64Decode $apiResults.content }}
{{ $codeSnippet := $content }}
{{ $parsedApiUrl := urls.Parse $webUrl }}
{{ with $parsedApiUrl.Fragment }}
{{ $codeLines := split $parsedApiUrl.Fragment "-" }}
{{ $fromLine := sub (int (replace (index $codeLines 0) "L" "")) 1 }}
{{ $toLine := int (cond (eq (len $codeLines) 1) (replace (index $codeLines 0) "L" "") (replace (index $codeLines 1) "L" "")) }}
{{ $numOfLines := cond (eq (sub $toLine $fromLine) 0) 1 (sub $toLine $fromLine) }}
{{ $splitContent := split $content "\n" }}
{{ $codeSnippet = delimit (first $numOfLines (after $fromLine $splitContent)) "\n" }}
{{ end }}
{{ highlight $codeSnippet $language }}
<div class="text-end pb-2">
<a href="{{- $webUrl -}}" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
{{ else }}
{{ partial "github-content.html" }}
{{ end }}