readfile.html 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. {{/* Store ordinal, to be retrieved by parent element */}}
  2. {{ if ge hugo.Version "0.93.0" }}
  3. {{ .Page.Store.Add "Ordinal" 1 }}
  4. {{ end }}
  5. {{/* Handle the "file" named parameter or a single unnamed parameter as the file
  6. path */}}
  7. {{ if .IsNamedParams }}
  8. {{ $.Scratch.Set "fparameter" ( .Get "file" ) }}
  9. {{ else }}
  10. {{ $.Scratch.Set "fparameter" ( .Get 0 ) }}
  11. {{ end }}
  12. {{/* If the first character is "/", the path is absolute from the site's
  13. `baseURL`. Otherwise, construct an absolute path using the current directory */}}
  14. {{ if eq (.Scratch.Get "fparameter" | printf "%.1s") "/" }}
  15. {{ $.Scratch.Set "filepath" ($.Scratch.Get "fparameter") }}
  16. {{ else }}
  17. {{ $.Scratch.Set "filepath" "/" }}
  18. {{ $.Scratch.Add "filepath" $.Page.File.Dir }}
  19. {{ $.Scratch.Add "filepath" ($.Scratch.Get "fparameter") }}
  20. {{ end }}
  21. {{/* If the file exists, read it and highlight it if it's code.
  22. Throw a compile error or print an error on the page if the file is not found */}}
  23. {{ if fileExists ($.Scratch.Get "filepath") }}
  24. {{ if eq (.Get "code") "true" }}
  25. {{- highlight ($.Scratch.Get "filepath" | readFile | htmlUnescape |
  26. safeHTML ) (.Get "lang") "" -}}
  27. {{ else }}
  28. {{- $.Scratch.Get "filepath" | os.ReadFile | .Page.RenderString | safeHTML -}}
  29. {{ end }}
  30. {{ else if eq (.Get "draft") "true" }}
  31. <p style="color: #D74848"><b><i>The file <code>{{ $.Scratch.Get "filepath" }}</code> was not found.</i></b></p>
  32. {{ else }}{{- errorf "Shortcode %q: file %q not found at %s" .Name ($.Scratch.Get "filepath") .Position -}}{{ end }}