Debian Codegraph @

hack/doc-site/hugo/layouts/shortcodes/code.html

raw symbols SCIP
1 {{- /*
2 code: include a source file from the examples mount with syntax highlighting,
3 followed by a "Full source" link back to the file on GitHub.
4
5 Parameters:
6 file (required) Path relative to docs/examples, e.g. "customcodec/uint32.go".
7 lang Chroma lexer name. Defaults to "text".
8 options Chroma options passed through, e.g. "linenos=table,hl_lines=3-5".
9 lines "N-M" (1-based, inclusive) to show only a slice of the file.
10 When set, the GitHub source link gets a matching #L{N}-L{M} anchor.
11 Mutually exclusive with `region`.
12 region Name of a named region delimited by `// snippet:NAME` and
13 `// endsnippet:NAME` markers in the file. Marker lines are
14 stripped; captured lines are de-indented by the leading
15 whitespace of the first non-blank captured line.
16 Mutually exclusive with `lines`.
17 nolink Set to "true" to suppress the "Full source" footnote.
18 */ -}}
19 {{- $file := .Get "file" -}}
20 {{- if not $file -}}
21 {{- errorf "code shortcode at %s: missing required parameter %q" .Position "file" -}}
22 {{- end -}}
23 {{- $lang := .Get "lang" | default "text" -}}
24 {{- $opts := .Get "options" | default "" -}}
25 {{- $lines := .Get "lines" -}}
26 {{- $region := .Get "region" -}}
27 {{- $nolink := eq (.Get "nolink") "true" -}}
28 {{- if and $lines $region -}}
29 {{- errorf "code shortcode at %s: %q and %q are mutually exclusive" .Position "lines" "region" -}}
30 {{- end -}}
31 {{- $resourcePath := printf "examples/%s" $file -}}
32 {{- $resource := resources.Get $resourcePath -}}
33 {{- if not $resource -}}
34 {{- errorf "code shortcode at %s: file %q not found under assets/examples" .Position $file -}}
35 {{- else -}}
36 {{- $content := $resource.Content -}}
37 {{- $anchor := "" -}}
38 {{- with $lines -}}
39 {{- $parts := split . "-" -}}
40 {{- $startStr := index $parts 0 -}}
41 {{- $endStr := cond (gt (len $parts) 1) (index $parts 1) $startStr -}}
42 {{- $start := int $startStr -}}
43 {{- $end := int $endStr -}}
44 {{- $all := split $content "\n" -}}
45 {{- $count := add (sub $end $start) 1 -}}
46 {{- $content = delimit (first $count (after (sub $start 1) $all)) "\n" -}}
47 {{- $anchor = printf "#L%s-L%s" $startStr $endStr -}}
48 {{- end -}}
49 {{- with $region -}}
50 {{- $startMarker := printf "// snippet:%s" . -}}
51 {{- $endMarker := printf "// endsnippet:%s" . -}}
52 {{- $all := split $content "\n" -}}
53 {{- $captured := slice -}}
54 {{- $inRegion := false -}}
55 {{- $startLine := 0 -}}
56 {{- $endLine := 0 -}}
57 {{- range $i, $line := $all -}}
58 {{- if and $inRegion (strings.Contains $line $endMarker) -}}
59 {{- $inRegion = false -}}
60 {{- $endLine = add $i 1 -}}
61 {{- else if and (not $inRegion) (strings.Contains $line $startMarker) -}}
62 {{- $inRegion = true -}}
63 {{- $startLine = add $i 2 -}}
64 {{- else if $inRegion -}}
65 {{- $captured = $captured | append $line -}}
66 {{- end -}}
67 {{- end -}}
68 {{- if eq (len $captured) 0 -}}
69 {{- errorf "code shortcode at %s: region %q not found in %q" $.Position . $file -}}
70 {{- end -}}
71 {{- /* dedent: leading whitespace of first non-blank captured line */ -}}
72 {{- $prefix := "" -}}
73 {{- $found := false -}}
74 {{- range $captured -}}
75 {{- if and (not $found) (ne (trim . " \t") "") -}}
76 {{- $stripped := strings.TrimLeft " \t" . -}}
77 {{- $prefix = substr . 0 (sub (len .) (len $stripped)) -}}
78 {{- $found = true -}}
79 {{- end -}}
80 {{- end -}}
81 {{- $dedented := slice -}}
82 {{- range $captured -}}
83 {{- $dedented = $dedented | append (strings.TrimPrefix $prefix .) -}}
84 {{- end -}}
85 {{- /* drop a leading blank line (common when a blank line separates the snippet marker from a godoc-eligible symbol) */ -}}
86 {{- if and (gt (len $dedented) 0) (eq (trim (index $dedented 0) " \t") "") -}}
87 {{- $dedented = after 1 $dedented -}}
88 {{- end -}}
89 {{- /* drop a trailing blank line (common when endsnippet sits on its own line) */ -}}
90 {{- $n := len $dedented -}}
91 {{- if and (gt $n 0) (eq (trim (index $dedented (sub $n 1)) " \t") "") -}}
92 {{- $dedented = first (sub $n 1) $dedented -}}
93 {{- end -}}
94 {{- $content = delimit $dedented "\n" -}}
95 {{- $anchor = printf "#L%d-L%d" $startLine $endLine -}}
96 {{- end -}}
97 {{- /* strip trailing //nolint:... directives so suppressions in source don't leak into rendered snippets */ -}}
98 {{- $lineSet := split $content "\n" -}}
99 {{- $cleaned := slice -}}
100 {{- range $lineSet -}}
101 {{- $line := . -}}
102 {{- $stripped := replaceRE `\s*//\s*nolint:.*$` "" $line -}}
103 {{- if or (ne $stripped "") (eq (trim $line " \t") "") -}}
104 {{- $cleaned = $cleaned | append $stripped -}}
105 {{- end -}}
106 {{- end -}}
107 {{- $content = delimit $cleaned "\n" -}}
108 {{- highlight $content $lang $opts }}
109 {{- if not $nolink }}
110 {{- $repo := site.Params.sourceRepository | default "https://github.com/go-openapi/runtime" -}}
111 {{- $sourceURL := printf "%s/blob/master/docs/examples/%s%s" $repo $file $anchor }}
112
113 <p class="code-source"><em>Full source: <a href="{{ $sourceURL }}" target="_blank" rel="noopener"><code>docs/examples/{{ $file }}</code></a></em></p>
114 {{- end }}
115 {{- end -}}