From 5d0920cb6d81390c1d234280ded7fa35e2f9aeda Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 17 Jan 2021 18:23:43 -0800 Subject: [PATCH] Extract code groups into a partial and display them for entire files and raw files. --- themes/vanilla/assets/scss/code.scss | 4 ++++ themes/vanilla/layouts/partials/geturl.html | 12 ++++++++++ themes/vanilla/layouts/partials/group.html | 3 +++ .../layouts/partials/highlightgroup.html | 1 + .../vanilla/layouts/shortcodes/codeblock.html | 3 ++- .../vanilla/layouts/shortcodes/codelines.html | 23 ++++++------------- .../vanilla/layouts/shortcodes/rawblock.html | 3 ++- 7 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 themes/vanilla/layouts/partials/geturl.html create mode 100644 themes/vanilla/layouts/partials/group.html create mode 100644 themes/vanilla/layouts/partials/highlightgroup.html diff --git a/themes/vanilla/assets/scss/code.scss b/themes/vanilla/assets/scss/code.scss index 058a767..21d91ff 100644 --- a/themes/vanilla/assets/scss/code.scss +++ b/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; diff --git a/themes/vanilla/layouts/partials/geturl.html b/themes/vanilla/layouts/partials/geturl.html new file mode 100644 index 0000000..2101a73 --- /dev/null +++ b/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 }} diff --git a/themes/vanilla/layouts/partials/group.html b/themes/vanilla/layouts/partials/group.html new file mode 100644 index 0000000..f17e544 --- /dev/null +++ b/themes/vanilla/layouts/partials/group.html @@ -0,0 +1,3 @@ +
+
From {{ path.Base .path }}{{ .comment }}
{{ .content }} +
diff --git a/themes/vanilla/layouts/partials/highlightgroup.html b/themes/vanilla/layouts/partials/highlightgroup.html new file mode 100644 index 0000000..6208044 --- /dev/null +++ b/themes/vanilla/layouts/partials/highlightgroup.html @@ -0,0 +1 @@ +{{ partial "group.html" (dict "url" .url "path" .path "comment" .comment "content" (highlight .code .language .opts)) }} diff --git a/themes/vanilla/layouts/shortcodes/codeblock.html b/themes/vanilla/layouts/shortcodes/codeblock.html index 3982d6b..77224aa 100644 --- a/themes/vanilla/layouts/shortcodes/codeblock.html +++ b/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") }} diff --git a/themes/vanilla/layouts/shortcodes/codelines.html b/themes/vanilla/layouts/shortcodes/codelines.html index a09bbe9..c6ac4f1 100644 --- a/themes/vanilla/layouts/shortcodes/codelines.html +++ b/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)) }} +{{ 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 }} -{{ end }} -
-
From {{ path.Base (.Get 1) }}, - {{ if eq (.Get 2) (.Get 3) }}line {{ .Get 2 }}{{ else }} lines {{ .Get 2 }} through {{ .Get 3 }}{{ end }}
- {{ highlight (delimit $lines "\n") (.Get 0) (printf "linenos=table,linenostart=%d%s" (.Get 2) (.Scratch.Get "opts")) }} -
+{{ 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"))) }} diff --git a/themes/vanilla/layouts/shortcodes/rawblock.html b/themes/vanilla/layouts/shortcodes/rawblock.html index 3352ce0..51cf51a 100644 --- a/themes/vanilla/layouts/shortcodes/rawblock.html +++ b/themes/vanilla/layouts/shortcodes/rawblock.html @@ -1 +1,2 @@ -
{{ readFile (printf "code/%s" (.Get 0)) }}
+{{ 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 "
%s
" (readFile (printf "code/%s" (.Get 0)))))) }}