From df22cb2b87cf9bc1a9da372e17d4c4eedfc3efff Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 9 Jun 2024 18:50:00 -0700 Subject: [PATCH] Use data from hosts.json to pull in link anchors Signed-off-by: Danila Fedorin --- layouts/partials/geturl.html | 22 +++++++++++++++++----- layouts/shortcodes/codelines.html | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/layouts/partials/geturl.html b/layouts/partials/geturl.html index 75cb30e..9b3f04c 100644 --- a/layouts/partials/geturl.html +++ b/layouts/partials/geturl.html @@ -1,15 +1,27 @@ {{- $siteSourceUrl := site.Params.siteSourceUrl -}} {{- $submoduleLinks := site.Data.submodules -}} +{{- $hostData := site.Data.hosts -}} {{- $scratch := .scratch -}} +{{- $lines := .lines -}} {{- $scratch.Set "bestLength" -1 -}} {{- $scratch.Set "bestUrl" (printf "%s/code/%s" $siteSourceUrl .path) -}} {{- $scratch.Set "bestPath" "" -}} {{- $filePath := .path -}} {{- range $path, $url := $submoduleLinks -}} -{{- $bestLength := $scratch.Get "bestLength" -}} -{{- if and (le $bestLength (len $path)) (hasPrefix $filePath $path) -}} -{{- $scratch.Set "bestLength" (len $path) -}} -{{- $scratch.Set "bestPath" $path -}} -{{- $scratch.Set "bestUrl" (printf "%s%s" $url (strings.TrimPrefix $path $filePath)) -}} + {{- $bestLength := $scratch.Get "bestLength" -}} + {{- if and (le $bestLength (len $path)) (hasPrefix $filePath $path) -}} + {{- $scratch.Set "bestLength" (len $path) -}} + {{- $scratch.Set "bestPath" $path -}} + {{- $scratch.Set "bestUrl" (printf "%s%s" $url (strings.TrimPrefix $path $filePath)) -}} + {{- end -}} {{- end -}} +{{- if .lines -}} +{{- /* If the user provided a line range, see if we can tack it on to the end of the link. */ -}} + {{- range $host, $data := $hostData -}} + {{- if hasPrefix ($scratch.Get "bestUrl") $host -}} + {{- with $data.linesSuffix -}} + {{- $scratch.Add "bestUrl" (printf . (index $lines 0) (index $lines 1)) -}} + {{- end -}} + {{- end -}} + {{- end -}} {{- end -}} diff --git a/layouts/shortcodes/codelines.html b/layouts/shortcodes/codelines.html index 90d127b..e0d805c 100644 --- a/layouts/shortcodes/codelines.html +++ b/layouts/shortcodes/codelines.html @@ -18,7 +18,7 @@ {{- $scratch.Set "hidden" (.Get 5) -}} {{- end -}} -{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1)) -}} +{{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1) "lines" (slice (.Get 2) (.Get 3))) -}} {{- partial "linerangestr.html" (dict "scratch" $scratch "from" (.Get 2) "to" (.Get 3)) -}} {{- with ($scratch.Get "hidden") -}}