Files
hinode/layouts/_shortcodes/command.html
2025-06-05 07:16:51 +02:00

77 lines
3.4 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.
*/}}
{{ $symbols := dict
"bash" (dict "prompt" "$" "contPrompt" ">" "contString" "\\")
"powershell" (dict "prompt" "PS>" "contPrompt" ">>" "contString" "`")
"sql" (dict "prompt" "sql>" "contPrompt" "->" "contString" "(con)")
}}
{{/* Initialize arguments */}}
{{- $args := partial "utilities/InitArgs.html" (dict "structure" "command" "args" .Params "named" .IsNamedParams "group" "shortcode") -}}
{{- if or $args.err $args.warnmsg -}}
{{- partial (cond $args.err "utilities/LogErr.html" "utilities/LogWarn.html") (dict
"partial" "shortcodes/command.html"
"msg" "Invalid arguments"
"details" ($args.errmsg | append $args.warnmsg)
"file" page.File
"position" .Position
)}}
{{- end -}}
{{/* Initialize local arguments */}}
{{- $prompt := $args.prompt | default (index (index $symbols $args.shell) "prompt") -}}
{{- $contPrompt := index (index $symbols $args.shell) "contPrompt" -}}
{{- $contString := index (index $symbols $args.shell) "contString" -}}
{{- $filter := "(out)" -}}
{{- $input := trim .Inner " \t\r\n" -}}
{{- if (and (eq $args.shell "bash") $args.host $args.user) -}}
{{- $prompt = printf "[%s@%s] %s" $args.user $args.host $prompt -}}
{{- end -}}
{{/* Main code */}}
{{ if not $args.err }}
{{- $lines := split $input "\n" -}}
{{- $prefix := "" -}}
{{- $prevLine := "" -}}
{{- $refined := "" -}}
{{- range $line := $lines -}}
{{- $line = trim $line " \t\r\n" -}}
{{- if hasPrefix $line $filter -}}
{{- $prefix = printf "%s<span></span>" $prefix -}}
{{- if eq $args.shell "sql" -}}
{{- $line = printf "--%s" (strings.TrimPrefix $filter $line) -}}
{{- else -}}
{{- $line = printf "#%s" (strings.TrimPrefix $filter $line) -}}
{{- end -}}
{{- else if (strings.HasSuffix $prevLine $contString) -}}
{{- $prefix = printf "%s<span data-prompt=\"%s\"></span>" $prefix $contPrompt -}}
{{- else -}}
{{- $prefix = printf "%s<span data-prompt=\"%s\"></span>" $prefix $prompt -}}
{{- end -}}
{{- $prevLine = $line -}}
{{- if (and (eq $args.shell "sql") (strings.HasSuffix $line $contString)) -}}
{{- $line = strings.TrimSuffix $contString $line -}}
{{- end -}}
{{- $refined = printf "%s\n%s" $refined $line -}}
{{- end -}}
{{- $refined := trim $refined "\r\n" -}}
{{- $output := (transform.Highlight $refined $args.shell | safeHTML) -}}
{{- $insert := printf "<span class=\"command-line-prompt\">%s</span><span class=\"line\">" $prefix -}}
{{- $output := (replace $output ("<span class=\"line\">" | safeHTML) $insert 1 | safeHTML) -}}
{{- if eq $args.shell "sql" -}}
{{- $output = (replace $output "<span class=\"c1\">--" "<span class=\"c1\">" | safeHTML) -}}
{{- else -}}
{{- $output = (replace $output "<span class=\"c1\">#" "<span class=\"c1\">" | safeHTML) -}}
{{- $output = (replace $output "<span class=\"c\">#" "<span class=\"c\">" | safeHTML) -}}
{{- end -}}
<div class="mb-3 syntax-highlight{{ with $args.class }} {{ . }}{{ end }}">
{{- $output -}}
</div>
{{ end }}