Files
hinode/layouts/shortcodes/command.html
2024-01-01 12:32:39 +01:00

84 lines
3.3 KiB
HTML

<!--
Copyright © 2024 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.
-->
{{ $error := false -}}
<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "command" "args" .Params) }}
{{ errorf "Invalid arguments: %s" .Position -}}
{{ $error = true }}
{{ end }}
<!-- Initialize arguments -->
{{- $host := .Get "host" | default "" -}}
{{- $user := .Get "user" | default "" -}}
{{- $prompt := .Get "prompt" | default "" -}}
{{- $filter := "(out)" -}}
{{- $input := trim .Inner " \t\r\n" -}}
{{- $class := .Get "class" | default "" -}}
{{- $shell := lower (.Get "shell") | default "bash" -}}
{{- $continuationPrompt := ">" -}}
{{- $continuationStr := "\\" -}}
{{- if eq $shell "powershell" -}}
{{- if not $prompt }}{{ $prompt = "PS>" }}{{ end -}}
{{- $continuationPrompt = ">>" -}}
{{- $continuationStr = "`" -}}
{{- else if eq $shell "sql" -}}
{{- if not $prompt }}{{ $prompt = "sql>" }}{{ end -}}
{{- $continuationPrompt = "->" -}}
{{- $continuationStr = "(con)" -}}
{{- else -}}
{{- $shell = "bash" -}}
{{- if not $prompt }}{{ $prompt = "$" }}{{ end -}}
{{- if (and ($host) ($user)) -}}
{{- $prompt = printf "[%s@%s] %s" $user $host $prompt -}}
{{- end -}}
{{- end -}}
<!-- Main code -->
{{ if not $error }}
{{- $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 $shell "sql" -}}
{{- $line = printf "--%s" (strings.TrimPrefix $filter $line) -}}
{{- else -}}
{{- $line = printf "#%s" (strings.TrimPrefix $filter $line) -}}
{{- end -}}
{{- else if (strings.HasSuffix $prevLine $continuationStr) -}}
{{- $prefix = printf "%s<span data-prompt=\"%s\"></span>" $prefix $continuationPrompt -}}
{{- else -}}
{{- $prefix = printf "%s<span data-prompt=\"%s\"></span>" $prefix $prompt -}}
{{- end -}}
{{- $prevLine = $line -}}
{{- if (and (eq $shell "sql") (strings.HasSuffix $line $continuationStr)) -}}
{{- $line = strings.TrimSuffix $continuationStr $line -}}
{{- end -}}
{{- $refined = printf "%s\n%s" $refined $line -}}
{{- end -}}
{{- $refined := trim $refined "\r\n" -}}
{{- $output := (transform.Highlight $refined $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 $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 $class }} {{ . }}{{ end }}">
{{- $output -}}
</div>
{{ end }}