Files
hinode/layouts/shortcodes/docs.html
2025-01-01 07:08:10 +01:00

93 lines
3.7 KiB
HTML

<!--
Copyright © 2022 - 2025 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
This source code adapts the original embedded shortcode as maintained by the The Bootstrap Authors. It introduces
the following modifications:
- Added validation of shortcode arguments
- Modified the layout
The original source code is available on:
https://github.com/twbs/bootstrap/blob/main/site/layouts/shortcodes/scss-docs.html
Copyright (c) 2011-2023 The Bootstrap Authors. Licensed under The MIT License (MIT).
-->
{{ $error := false -}}
<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "docs" "args" .Params) }}
{{ errorf "Invalid arguments: %s" .Position -}}
{{ $error = true }}
{{ end }}
<!-- Initialize arguments -->
{{- $name := .Get "name" | default "" -}}
{{- $basePath := .Site.Params.docs.basePath -}}
{{- $file := .Get "file" | default "" -}}
{{- if hasPrefix $file "./" -}}
{{- $file = path.Clean $file -}}
{{- else -}}
{{- $file = path.Join $basePath (path.Clean $file) -}}
{{- end -}}
{{- $extension := strings.TrimLeft "." (path.Ext $file) }}
{{- $capture_start := "" -}}
{{- $capture_end := "" -}}
{{- $id := .Get "id" | default (printf "docs-collapse-%d" .Ordinal) -}}
{{- $supportedExtensions := slice "js" "scss" "toml" -}}
{{- if in $supportedExtensions $extension -}}
{{- if eq $extension "toml" }}
{{- $capture_start = printf "# toml-docs-start %s" $name -}}
{{- $capture_end = printf "# toml-docs-end %s" $name -}}
{{- else -}}
{{- $capture_start = printf "// %s-docs-start %s" $extension $name -}}
{{- $capture_end = printf "// %s-docs-end %s" $extension $name -}}
{{- end -}}
{{- else -}}
{{- errorf "File format not supported (line %s): %s" .Position $file -}}
{{- end -}}
{{- $show := true -}}
{{ if isset .Params "show" }}{{ $show = partial "utilities/CastBool.html" (.Get "show") }}{{ end -}}
{{- $full := true -}}
{{ if isset .Params "full" }}{{ $full = partial "utilities/CastBool.html" (.Get "full") }}{{ end -}}
{{- $class := .Get "class" | default "" -}}
<!-- Main code -->
{{- if not $error -}}
{{- /* Force-check if the file exists */ -}}
{{ $tmp := os.Stat $file }}
{{- $regex := printf `%s((?:.|\n)*)%s` $capture_start $capture_end -}}
{{- $match := findRE $regex (readFile $file) -}}
{{- $match = index $match 0 -}}
{{- if not $match -}}
{{- errorf "%s: %q: Got no matches for name=%q in file=%q!" .Position .Name $name $file -}}
{{- end -}}
{{- $match = replace $match $capture_start "" -}}
{{- $match = replace $match $capture_end "" -}}
<ul class="nav nav-tabs{{ with $class }} {{ . }}{{ end }}">
<li class="nav-item">
<a class="nav-link active font-monospace"
href="#body-{{ $id }}"
aria-current="page"
data-bs-toggle="collapse"
data-bs-target=".multi-{{ $id }}"
aria-expanded="false"
aria-controls="body-{{ $id }} footer-{{ $id }}">
<small>{{ if $full }}{{ strings.TrimPrefix "/" (strings.TrimPrefix $basePath $file) }}{{ else }}{{ path.Base $file }}{{ end }}</small>
</a>
</li>
</ul>
<div class="border-start border-end border-bottom mb-3">
<div class="collapse multi-{{ $id }}{{ if $show }} show{{ end }} syntax-highlight" id="body-{{ $id }}">
{{- highlight (trim $match "\r\n") $extension "" -}}
</div>
<div class="collapse multi-{{ $id }}{{ if not $show }} show{{ end }} p-3" id="footer-{{ $id }}"><i>...</i></div>
</div>
{{- end -}}