From c0c2b83059642c315bdf1cf03793674853da089f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sat, 8 Feb 2025 16:05:12 +0100 Subject: [PATCH] link: support linkable output formats #850 #1008 --- .../partials/_relearn/decoratedLink.gotmpl | 86 ++++++++++--------- layouts/partials/menu.html | 2 +- layouts/partials/topbar/button/markdown.html | 3 +- layouts/partials/topbar/button/next.html | 6 +- layouts/partials/topbar/button/prev.html | 6 +- layouts/partials/topbar/button/print.html | 3 +- layouts/partials/topbar/button/source.html | 3 +- layouts/partials/topbar/func/button.html | 45 +++++++--- layouts/partials/version.txt | 2 +- 9 files changed, 97 insertions(+), 59 deletions(-) diff --git a/layouts/partials/_relearn/decoratedLink.gotmpl b/layouts/partials/_relearn/decoratedLink.gotmpl index 882fb3cddd..666fe8d91b 100644 --- a/layouts/partials/_relearn/decoratedLink.gotmpl +++ b/layouts/partials/_relearn/decoratedLink.gotmpl @@ -1,57 +1,65 @@ -{{- $href := partial "permalink.gotmpl" (dict "to" .linkObject) }} -{{- $silent := .silent | default false }} -{{- $hideFilepath := .hideFilepath | default false }} {{- $u := urls.Parse .url }} +{{- $hideFilepath := .hideFilepath | default false }} +{{- $type := printf "%T" .linkObject }} +{{- $isPage := eq $type "*hugolib.pageState" }} +{{- $href := partial "permalink.gotmpl" (dict "to" .linkObject) }} +{{- if $isPage }} + {{- $format := $u.Query.Get "format" }} + {{- if $format }} + {{- $of := .linkObject.OutputFormats.Get $format }} + {{- if $of }} + {{- $href = partial "permalink.gotmpl" (dict "to" $of) }} + {{- else }} + {{- $msg := printf "output format %q not found" $of }} + {{- partial "partials/inline/print-error" (dict "url" .url "page" .page "param" .param "msg" $msg "linkObject" .linkObject "hideFilepath" $hideFilepath) }} + {{- end }} + {{- end }} +{{- end }} {{- with $u.RawQuery }} {{- $href = printf "%s?%s" $href . }} {{- end }} {{- with $u.Fragment }} {{- $href = printf "%s#%s" $href . }} - {{- if not $silent }} - {{- partial "inline/validate-fragment.html" (dict "origPage" $.page "page" $.linkObject "parsedURL" $u "url" $.url "param" $.param "hideFilepath" $hideFilepath) }} + {{- if $isPage }} + {{- partial "inline/validate-fragment.html" (dict "page" $.page "linkObject" $.linkObject "parsedURL" $u "url" $.url "param" $.param "hideFilepath" $hideFilepath) }} {{- end }} {{- end }} +{{- $href = replaceRE `&lang=\w*\b` "" $href }} +{{- $href = replaceRE `\?lang=\w*&\b` "?" $href }} +{{- $href = replaceRE `\?lang=\w*\b` "" $href }} +{{- $href = replaceRE `&format=\w*\b` "" $href }} +{{- $href = replaceRE `\?format=\w*&\b` "?" $href }} +{{- $href = replaceRE `\?format=\w*\b` "" $href }} {{- return $href }} - {{- define "partials/inline/validate-fragment.html" }} - {{- /* - Validates the fragment portion of a link destination. - */}} - - {{- /* Initialize. */}} - {{- $errorLevel := .errorLevel }} - {{- $origPage := .origPage }} - {{- $p := .page }} - {{- $url := .url }} {{- $u := .parsedURL }} - {{- $param := .param }} - {{- $hideFilepath := .hideFilepath }} - - {{- /* Validate. */}} {{- with $u.Fragment }} - {{- if $p.Fragments.Identifiers.Contains . }} - {{- if gt ($p.Fragments.Identifiers.Count .) 1 }} - {{- $filepath := "[virtual file]" }}{{ with and $origPage $origPage.File $origPage.File.Filename }}{{ $filepath = . }}{{ end }} + {{- if $.linkObject.Fragments.Identifiers.Contains . }} + {{- if gt ($.linkObject.Fragments.Identifiers.Count .) 1 }} + {{- $filepath := "[virtual file]" }}{{ with and $.page $.page.File $.page.File.Filename }}{{ $filepath = . }}{{ end }} {{- $msg := printf "%q: duplicate heading ID %q found" $filepath . }} - {{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" $url "page" $origPage "param" $param "msg" $msg) }} + {{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" $.url "page" $.page "param" $.param "msg" $msg) }} {{- end }} {{- else }} - {{- /* Determine target path for warning and error message. */}} - {{- $targetPath := "" }} - {{- with $p.File }} - {{- $targetPath = .Path }} - {{- else }} - {{- $targetPath = .Path }} - {{- end }} - {{- $filepath := "[virtual file]" }}{{ with and $origPage $origPage.File $origPage.File.Filename }}{{ $filepath = . }}{{ end }} {{- $msg := printf "heading ID %q not found" . }} - {{- if not $hideFilepath }} - {{- $msg = printf "%q: %s" $filepath $msg }} - {{- end }} - {{- if or $hideFilepath (ne $origPage $p) }} - {{- $msg = printf "%s in %q" $msg $targetPath }} - {{- end }} - {{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" $url "page" $origPage "param" $param "msg" $msg) }} + {{- partial "partials/inline/print-error" (dict "url" $.url "page" $.page "param" $.param "msg" $msg "linkObject" $.linkObject "hideFilepath" $.hideFilepath) }} {{- end }} {{- end }} -{{- end }} \ No newline at end of file +{{- end }} +{{- define "partials/inline/print-error" }} + {{- $targetpath := "" }} + {{- with .linkObject.File }} + {{- $targetpath = .Path }} + {{- else }} + {{- $targetpath = .Path }} + {{- end }} + {{- $filepath := "[virtual file]" }}{{ with and .page .page.File .page.File.Filename }}{{ $filepath = . }}{{ end }} + {{- $msg := .msg }} + {{- if not .hideFilepath }} + {{- $msg = printf "%q: %s" $filepath $msg }} + {{- end }} + {{- if or .hideFilepath (ne .page .linkObject) }} + {{- $msg = printf "%s in %q" $msg $targetpath }} + {{- end }} + {{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" .url "page" .page "param" .param "msg" $msg) }} +{{- end }} diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html index 659b25edc1..7b1ccf74b0 100644 --- a/layouts/partials/menu.html +++ b/layouts/partials/menu.html @@ -174,7 +174,7 @@ {{- $url = .Params.menuPageRef }} {{- $linkObject := partial "_relearn/refObject.gotmpl" (dict "page" . "objectRef" .Params.menuPageRef) }} {{- if $linkObject }} - {{- $url = partial "_relearn/decoratedLink.gotmpl" (dict "url" .Params.menuPageRef "page" . "linkObject" $linkObject "param" "link" "silent" true) }} + {{- $url = partial "_relearn/decoratedLink.gotmpl" (dict "url" .Params.menuPageRef "page" . "linkObject" $linkObject "param" "link") }} {{- end }} {{- else if .Params.menuUrl }} {{- $isCrosslink = true }} diff --git a/layouts/partials/topbar/button/markdown.html b/layouts/partials/topbar/button/markdown.html index b583262919..ec452acdd9 100644 --- a/layouts/partials/topbar/button/markdown.html +++ b/layouts/partials/topbar/button/markdown.html @@ -8,10 +8,11 @@ {{- $show = not (partial "_relearn/pageIsSpecial.gotmpl" .) }} {{- end }} {{- if $show }} + {{- $href := printf "%s?format=markdown" .Path }} {{- partial "topbar/func/button.html" (dict "page" . "class" "topbar-button-markdown" - "href" $format.RelPermalink + "href" $href "icon" "fa-fw fab fa-markdown" "onwidths" $onwidths "onwidthm" $onwidthm diff --git a/layouts/partials/topbar/button/next.html b/layouts/partials/topbar/button/next.html index 1be84e0036..4870465842 100644 --- a/layouts/partials/topbar/button/next.html +++ b/layouts/partials/topbar/button/next.html @@ -10,10 +10,14 @@ {{- end }} {{- $next := partialCached "_relearn/pageNext.gotmpl" . .Path }} {{- $nextTitle := partial "title.gotmpl" (dict "page" $next "outputFormat" "html") }} + {{- $href := "" }} + {{- with (and $next $next.Path) }} + {{- $href = . }} + {{- end }} {{- partial "topbar/func/button.html" (dict "page" . "class" "topbar-button-next" - "href" (partial "permalink.gotmpl" (dict "to" $next)) + "href" $href "icon" "chevron-right" "onwidths" $onwidths "onwidthm" $onwidthm diff --git a/layouts/partials/topbar/button/prev.html b/layouts/partials/topbar/button/prev.html index d06fd03ebe..c7bb20a562 100644 --- a/layouts/partials/topbar/button/prev.html +++ b/layouts/partials/topbar/button/prev.html @@ -10,10 +10,14 @@ {{- end }} {{- $prev := partialCached "_relearn/pagePrev.gotmpl" . .Path }} {{- $prevTitle := partial "title.gotmpl" (dict "page" $prev "outputFormat" "html") }} + {{- $href := "" }} + {{- with (and $prev $prev.Path) }} + {{- $href = . }} + {{- end }} {{- partial "topbar/func/button.html" (dict "page" . "class" "topbar-button-prev" - "href" (partial "permalink.gotmpl" (dict "to" $prev)) + "href" $href "icon" "chevron-left" "onwidths" $onwidths "onwidthm" $onwidthm diff --git a/layouts/partials/topbar/button/print.html b/layouts/partials/topbar/button/print.html index c95fbc9446..6466e2c75e 100644 --- a/layouts/partials/topbar/button/print.html +++ b/layouts/partials/topbar/button/print.html @@ -8,10 +8,11 @@ {{- $show = not (partial "_relearn/pageIsSpecial.gotmpl" .) }} {{- end }} {{- if $show }} + {{- $href := printf "%s?format=print" .Path }} {{- partial "topbar/func/button.html" (dict "page" . "class" "topbar-button-print" - "href" $format.RelPermalink + "href" $href "icon" "print" "onwidths" $onwidths "onwidthm" $onwidthm diff --git a/layouts/partials/topbar/button/source.html b/layouts/partials/topbar/button/source.html index a488dd1de8..966cc8d9d1 100644 --- a/layouts/partials/topbar/button/source.html +++ b/layouts/partials/topbar/button/source.html @@ -8,10 +8,11 @@ {{- $show = and (not (partial "_relearn/pageIsSpecial.gotmpl" .)) .File .File.Filename }} {{- end }} {{- if $show }} + {{- $href := printf "%s?format=source" .Path }} {{- partial "topbar/func/button.html" (dict "page" . "class" "topbar-button-source" - "href" $format.RelPermalink + "href" $href "icon" "code" "onwidths" $onwidths "onwidthm" $onwidthm diff --git a/layouts/partials/topbar/func/button.html b/layouts/partials/topbar/func/button.html index 9c3a5d60ac..a0fd71acef 100644 --- a/layouts/partials/topbar/func/button.html +++ b/layouts/partials/topbar/func/button.html @@ -5,8 +5,15 @@ {{- $onwidths := cond (isset . "onwidths") .onwidths "show" }} {{- $onwidthm := cond (isset . "onwidthm") .onwidthm "show" }} {{- $onwidthl := cond (isset . "onwidthl") .onwidthl "show" }} -{{- $target := .target | default "" }} +{{- $attributes := .attributes | default dict }} {{- $type := .type | default "" }} +{{- $title := (trim .title " ") | default "" }} +{{- $hint := (trim .hint " ") | default "" }} +{{- $icon := (trim .icon " ") | default "" }} +{{- if and $icon (not (findRE ".*?\\bfa-\\w.*?" $icon)) }} + {{- $icon = printf "fa-fw fas fa-%s" $icon }} +{{- end }} +{{- $content := .content }} {{- $isDisabled := not $href }} {{- $isButton := false }} {{- if or (not $href) (strings.HasPrefix $href "javascript:") }} @@ -15,26 +22,38 @@ {{- if not $type }} {{- $type = "button" }} {{- end }} -{{- else if and (eq (len $target) 0) (or (strings.HasPrefix $href "http://") (strings.HasPrefix $href "https://") ) }} - {{- $target = "_blank" }} - {{- if isset $page.Site.Params "externallinktarget" }} - {{- $target = $page.Site.Params.externalLinkTarget }} +{{- else }} + {{- $u := urls.Parse .href }} + {{- if $u.IsAbs }} + {{- partialCached "_relearn/urlExists.gotmpl" (dict "url" .href "page" $page "type" "link") $u.String }} + {{- else }} + {{- $linkObject := partial "_relearn/linkObject.gotmpl" (dict "url" .href "page" $page) }} + {{- if $linkObject }} + {{- $href = partial "_relearn/decoratedLink.gotmpl" (dict "url" .href "page" $page "linkObject" $linkObject "param" "link") }} + {{- else }} + {{- $filepath := "[virtual file]" }}{{ with and $page $page.File $page.File.Filename }}{{ $filepath = . }}{{ end }} + {{- $msg := printf "%q: link '%s' is not a page or a resource" $filepath .href }} + {{- partial "_relearn/urlErrorReport.gotmpl" (dict "url" .href "page" $page "param" "link" "msg" $msg) }} + {{- end }} {{- end }} + {{- $attributes = merge $attributes (partial "_relearn/linkAttributes.gotmpl" (dict "url" $href "page" $page "attributes" (dict "class" "topbar-control" "title" $hint))) -}} {{- end }} -{{- $title := (trim .title " ") | default "" }} -{{- $hint := (trim .hint " ") | default "" }} -{{- $icon := (trim .icon " ") | default "" }} -{{- if and $icon (not (findRE ".*?\\bfa-\\w.*?" $icon)) }} - {{- $icon = printf "fa-fw fas fa-%s" $icon }} -{{- end }} -{{- $content := .content }}
{{- if $isDisabled -}} {{- else if $isButton -}}