Browse Source

Extract code groups into a partial and display them for entire files and raw files.

donations
Danila Fedorin 11 months ago
parent
commit
5d0920cb6d
  1. 4
      themes/vanilla/assets/scss/code.scss
  2. 12
      themes/vanilla/layouts/partials/geturl.html
  3. 3
      themes/vanilla/layouts/partials/group.html
  4. 1
      themes/vanilla/layouts/partials/highlightgroup.html
  5. 3
      themes/vanilla/layouts/shortcodes/codeblock.html
  6. 23
      themes/vanilla/layouts/shortcodes/codelines.html
  7. 3
      themes/vanilla/layouts/shortcodes/rawblock.html

4
themes/vanilla/assets/scss/code.scss

@ -15,6 +15,10 @@ $code-color-comment: grey;
}
}
.highlight-group pre {
margin: 0;
}
code {
font-family: $font-code;
background-color: $code-color;

12
themes/vanilla/layouts/partials/geturl.html

@ -0,0 +1,12 @@
{{ $scratch := .scratch }}
{{ $scratch.Set "bestLength" -1 }}
{{ $scratch.Set "bestUrl" (printf "https://dev.danilafe.com/Web-Projects/blog-static/src/branch/master/code/%s" .path) }}
{{ $filePath := .path }}
{{ range $module, $props := .submoduleLinks }}
{{ $path := index $props "path" }}
{{ $bestLength := $scratch.Get "bestLength" }}
{{ if and (le $bestLength (len $path)) (hasPrefix $filePath $path) }}
{{ $scratch.Set "bestLength" (len $path) }}
{{ $scratch.Set "bestUrl" (printf "%s%s" (index $props "url") (strings.TrimPrefix $path $filePath)) }}
{{ end }}
{{ end }}

3
themes/vanilla/layouts/partials/group.html

@ -0,0 +1,3 @@
<div class="highlight-group">
<div class="highlight-label">From <a href="{{ .url }}">{{ path.Base .path }}</a>{{ .comment }}</div>{{ .content }}
</div>

1
themes/vanilla/layouts/partials/highlightgroup.html

@ -0,0 +1 @@
{{ partial "group.html" (dict "url" .url "path" .path "comment" .comment "content" (highlight .code .language .opts)) }}

3
themes/vanilla/layouts/shortcodes/codeblock.html

@ -1 +1,2 @@
{{ highlight (readFile (printf "code/%s" (.Get 1))) (.Get 0) "" }}
{{ partial "geturl.html" (dict "scratch" .Scratch "path" (.Get 1) "submoduleLinks" .Site.Params.submoduleLinks) }}
{{ partial "highlightgroup.html" (dict "url" (.Scratch.Get "bestUrl") "path" (.Get 1) "comment" ", entire file" "code" (readFile (printf "code/%s" (.Get 1))) "language" (.Get 0) "opts" "linenos=table") }}

23
themes/vanilla/layouts/shortcodes/codelines.html

@ -13,20 +13,11 @@
{{ .Scratch.Set "opts" "" }}
{{ end }}
{{ .Scratch.Set "bestLength" -1 }}
{{ .Scratch.Set "bestUrl" (printf "https://dev.danilafe.com/Web-Projects/blog-static/src/branch/master/code/%s" (.Get 1)) }}
{{ $filePath := (.Get 1) }}
{{ $scratch := .Scratch }}
{{ range $module, $props := .Site.Params.submoduleLinks }}
{{ $path := index $props "path" }}
{{ $bestLength := $scratch.Get "bestLength" }}
{{ if and (le $bestLength (len $path)) (hasPrefix $filePath $path) }}
{{ $scratch.Set "bestLength" (len $path) }}
{{ $scratch.Set "bestUrl" (printf "%s%s" (index $props "url") (strings.TrimPrefix $path $filePath)) }}
{{ end }}
{{ partial "geturl.html" (dict "scratch" .Scratch "path" (.Get 1) "submoduleLinks" .Site.Params.submoduleLinks) }}
{{ if eq (.Get 2) (.Get 3) }}
{{ .Scratch.Set "comment" (printf ", line %d" (.Get 2)) }}
{{ else }}
{{ .Scratch.Set "comment" (printf ", lines %d through %d" (.Get 2) (.Get 3)) }}
{{ end }}
<div class="highlight-group">
<div class="highlight-label">From <a href="{{ .Scratch.Get "bestUrl" }}">{{ path.Base (.Get 1) }}</a>,
{{ if eq (.Get 2) (.Get 3) }}line {{ .Get 2 }}{{ else }} lines {{ .Get 2 }} through {{ .Get 3 }}{{ end }}</div>
{{ highlight (delimit $lines "\n") (.Get 0) (printf "linenos=table,linenostart=%d%s" (.Get 2) (.Scratch.Get "opts")) }}
</div>
{{ partial "highlightgroup.html" (dict "url" (.Scratch.Get "bestUrl") "path" (.Get 1) "comment" (.Scratch.Get "comment") "code" (delimit $lines "\n") "language" (.Get 0) "opts" (printf "linenos=table,linenostart=%d%s" (.Get 2) (.Scratch.Get "opts"))) }}

3
themes/vanilla/layouts/shortcodes/rawblock.html

@ -1 +1,2 @@
<pre><code>{{ readFile (printf "code/%s" (.Get 0)) }}</code></pre>
{{ partial "geturl.html" (dict "scratch" .Scratch "path" (.Get 0) "submoduleLinks" .Site.Params.submoduleLinks) }}
{{ partial "group.html" (dict "url" (.Scratch.Get "bestUrl") "path" (.Get 0) "comment" ", entire file" "content" (safeHTML (printf "<pre><code>%s</code></pre>" (readFile (printf "code/%s" (.Get 0)))))) }}

Loading…
Cancel
Save