selfprivacy.org/_vendor/github.com/google/docsy/layouts/shortcodes/readfile.html

42 lines
1.5 KiB
HTML
Raw Normal View History

2024-04-21 23:04:16 +00:00
{{/* Store ordinal, to be retrieved by parent element */}}
{{ if ge hugo.Version "0.93.0" }}
{{ .Page.Store.Add "Ordinal" 1 }}
{{ end }}
{{/* Handle the "file" named parameter or a single unnamed parameter as the file
path */}}
{{ if .IsNamedParams }}
{{ $.Scratch.Set "fparameter" ( .Get "file" ) }}
{{ else }}
{{ $.Scratch.Set "fparameter" ( .Get 0 ) }}
{{ end }}
{{/* If the first character is "/", the path is absolute from the site's
`baseURL`. Otherwise, construct an absolute path using the current directory */}}
{{ if eq (.Scratch.Get "fparameter" | printf "%.1s") "/" }}
{{ $.Scratch.Set "filepath" ($.Scratch.Get "fparameter") }}
{{ else }}
{{ $.Scratch.Set "filepath" "/" }}
{{ $.Scratch.Add "filepath" $.Page.File.Dir }}
{{ $.Scratch.Add "filepath" ($.Scratch.Get "fparameter") }}
{{ end }}
2024-04-25 22:15:39 +00:00
{{/* If the file exists, read it and highlight it if it's code.
Throw a compile error or print an error on the page if the file is not found */}}
2024-04-21 23:04:16 +00:00
{{ if fileExists ($.Scratch.Get "filepath") }}
{{ if eq (.Get "code") "true" }}
{{- highlight ($.Scratch.Get "filepath" | readFile | htmlUnescape |
safeHTML ) (.Get "lang") "" -}}
{{ else }}
2024-04-25 22:15:39 +00:00
{{- $.Scratch.Get "filepath" | os.ReadFile | .Page.RenderString | safeHTML -}}
2024-04-21 23:04:16 +00:00
{{ end }}
2024-04-25 22:15:39 +00:00
{{ else if eq (.Get "draft") "true" }}
2024-04-21 23:04:16 +00:00
2024-04-25 22:15:39 +00:00
<p style="color: #D74848"><b><i>The file <code>{{ $.Scratch.Get "filepath" }}</code> was not found.</i></b></p>
2024-04-21 23:04:16 +00:00
2024-04-25 22:15:39 +00:00
{{ else }}{{- errorf "Shortcode %q: file %q not found at %s" .Name ($.Scratch.Get "filepath") .Position -}}{{ end }}