diff --git a/layouts/partials/linerangestr.html b/layouts/partials/linerangestr.html new file mode 100644 index 0000000..f42b5e6 --- /dev/null +++ b/layouts/partials/linerangestr.html @@ -0,0 +1,9 @@ +{{ if eq .from .to }} +{{ .scratch.Set "comment" (printf ", line %d" .from) }} +{{ else }} +{{ if eq .from 0 }} +{{ .scratch.Set "comment" (printf ", around line %d" .to) }} +{{ else }} +{{ .scratch.Set "comment" (printf ", lines %d through %d" .from .to) }} +{{ end }} +{{ end }} diff --git a/layouts/shortcodes/codelines.html b/layouts/shortcodes/codelines.html index c6ac4f1..70c5ed6 100644 --- a/layouts/shortcodes/codelines.html +++ b/layouts/shortcodes/codelines.html @@ -14,10 +14,6 @@ {{ end }} {{ partial "geturl.html" (dict "scratch" .Scratch "path" (.Get 1) "submoduleLinks" .Site.Params.submoduleLinks) }} +{{ partial "linerangestr.html" (dict "scratch" .Scratch "from" (.Get 2) "to" (.Get 3)) }} -{{ if eq (.Get 2) (.Get 3) }} -{{ .Scratch.Set "comment" (printf ", line %d" (.Get 2)) }} -{{ else }} -{{ .Scratch.Set "comment" (printf ", lines %d through %d" (.Get 2) (.Get 3)) }} -{{ end }} {{ partial "highlightgroup.html" (dict "url" (.Scratch.Get "bestUrl") "path" (.Get 1) "comment" (.Scratch.Get "comment") "code" (delimit $lines "\n") "language" (.Get 0) "opts" (printf "linenos=table,linenostart=%d%s" (.Get 2) (.Scratch.Get "opts"))) }} diff --git a/layouts/shortcodes/githubsnippet.html b/layouts/shortcodes/githubsnippet.html new file mode 100644 index 0000000..4629313 --- /dev/null +++ b/layouts/shortcodes/githubsnippet.html @@ -0,0 +1,10 @@ +{{- $repo := .Get 0 -}} +{{- $commit := .Get 1 -}} +{{- $file := .Get 2 -}} +{{- $lang := .Get 3 -}} +{{- $line := .Get 4 -}} +{{- $lines := split (trim .Inner "\n") "\n" -}} + +{{- $url := printf "https://github.com/%s/blob/%s/%s#L%d" $repo $commit $file $line -}} +{{- partial "linerangestr.html" (dict "scratch" .Scratch "from" 0 "to" $line) -}} +{{- partial "highlightgroup.html" (dict "url" $url "path" $file "comment" (.Scratch.Get "comment") "code" (trim .Inner "\n") "language" $lang) -}}