summaryrefslogtreecommitdiff
path: root/themes/blowfish/layouts/shortcodes/codeimporter.html
blob: 952c28a8df3fad519a1fc67f65efd0af278da77c (plain)
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
{{ $url := .Get "url" }}
{{ $type := .Get "type" }}
{{ $startLine := .Get "startLine" | default 1 | int }}
{{ $startLine = sub $startLine 1 }}
{{ $endLine := .Get "endLine" | default -1 | int }}
{{ $selectedLines := slice }}
{{ with resources.GetRemote (urls.Parse $url) }}
  {{ $lines := split .Content "\n" }}
  {{ $totalLine := $lines | len }}

  {{ if ne $endLine -1 }}
    {{ $endLine = math.Min $endLine $totalLine }}
  {{ else }}
    {{ $endLine = $totalLine }}
  {{ end }}
  
  {{ if gt $startLine $endLine }}
    {{ errorf "Code Importer Shortcode - startLine is greater than endLine" . }}
  {{ end }}

  {{ $selectedLines := first $endLine $lines }}
  {{ $selectedLines = after $startLine $selectedLines }}
  {{ $codeBlock := printf "```%s\n%s\n```" $type (delimit $selectedLines "\n") }}
  {{ $codeBlock | markdownify }}
{{ else }}
  {{ errorf "Code Importer Shortcode - Unable to get remote resource" . }}
{{ end }}