{{- $siteSourceUrl := site.Params.siteSourceUrl -}}
{{- $submoduleLinks := hugo.Data.submodules -}}
{{- $hostData := hugo.Data.hosts -}}
{{- $lines := .lines -}}
{{- $bestLength := -1 -}}
{{- $bestUrl := printf "%s/code/%s" $siteSourceUrl .path -}}
{{- $bestPath := "" -}}
{{- $filePath := .path -}}
{{- range $path, $url := $submoduleLinks -}}
    {{- if and (le $bestLength (len $path)) (hasPrefix $filePath $path) -}}
        {{- $bestLength = len $path -}}
        {{- $bestPath = $path -}}
        {{- $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 $bestUrl $host -}}
            {{- with $data.linesSuffix -}}
                {{- $bestUrl = add $bestUrl (printf . (index $lines 0) (index $lines 1)) -}}
            {{- end -}}
        {{- end -}}
    {{- end -}}
{{- end -}}
{{- return (dict "url" $bestUrl "basePath" $bestPath) -}}
