mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
22 hours ago 41d6a4f7933f3c1e1c6da48f85490c01e1e7fb6a
wip: add filter partial
6 files modified
1 files renamed
2 files added
219 ■■■■■ changed files
config/_default/params.toml 2 ●●● patch | view | raw | blame | history
layouts/_partials/filter.html 112 ●●●●● patch | view | raw | blame | history
layouts/_partials/func/debug-cli.html 5 ●●●● patch | view | raw | blame | history
layouts/_partials/func/hooks/collector-dump.html 13 ●●●●● patch | view | raw | blame | history
layouts/_partials/func/hooks/collector.html 4 ●●●● patch | view | raw | blame | history
layouts/_partials/hook.html 75 ●●●●● patch | view | raw | blame | history
layouts/_partials/hooks/article/test.html 1 ●●●● patch | view | raw | blame | history
layouts/baseof.html 5 ●●●● patch | view | raw | blame | history
layouts/single.html 2 ●●● patch | view | raw | blame | history
config/_default/params.toml
@@ -257,4 +257,4 @@
icon = "youtube" # font awesome brand icon name
[ananke.hooks]
verbosity = "debug" # debug, warning, error
verbosity = "debug" # debug, info, warning, error
layouts/_partials/filter.html
New file
@@ -0,0 +1,112 @@
{{- $input := . -}}
{{- $context := collections.Dictionary -}}
{{- $config := site.Params.ananke.hooks | compare.Default (collections.Dictionary) -}}
{{- $disableMessages := $config.disable_messages | compare.Default (collections.Slice) -}}
{{- $colourHook := "\033[38;2;170;153;255m" -}}
{{- $colourUnused := "\033[38;2;204;119;102m" -}}
{{- $colourReset := "\033[0m" -}}
{{- $hookSlug := "FILTER" -}}
{{- if reflect.IsMap . -}}
  {{- with collections.Index . "__ananke_hook_slug" -}}
    {{- $hookSlug = . -}}
  {{- end -}}
  {{- with collections.Index . "__ananke_hook_input" -}}
    {{- $input = . -}}
  {{- end -}}
{{- end -}}
{{- if reflect.IsMap $input -}}
  {{- $context = collections.Merge $context $input -}}
  {{- $context = collections.Merge $context (collections.Dictionary "type" "full") -}}
  {{- partials.Include "func/debug-cli.html" (collections.Dictionary
      "message" (fmt.Printf "%s>>> %s: %s%s" $colourHook $hookSlug $context.hook $colourReset)
  ) -}}
  {{- partials.Include "func/debug-cli.html" (collections.Dictionary
      "message" "hook is extended"
      "severity" "debug"
  ) -}}
{{- else -}}
  {{- $context = collections.Merge $context (collections.Dictionary
      "hook" $input
      "context" collections.Dictionary
      "type" "simple"
  ) -}}
  {{- partials.Include "func/debug-cli.html" (collections.Dictionary
      "message" (fmt.Printf "%s>>> %s: %s%s" $colourHook $hookSlug $context.hook $colourReset)
  ) -}}
  {{- partials.Include "func/debug-cli.html" (collections.Dictionary
      "message" "hook is simple"
      "severity" "debug"
  ) -}}
{{- end -}}
{{- partials.Include "func/hooks/collector.html" (collections.Dictionary
    "hook" $context.hook
    "context" $context
) -}}
{{- $loaded := false -}}
{{- $output := "" -}}
{{- $partialName := fmt.Printf "hooks/%s.html" $context.hook -}}
{{- $partialHook := fmt.Printf "_partials/%s" $partialName -}}
{{- partials.Include "func/debug-cli.html" (collections.Dictionary
    "message" (fmt.Printf "partial Name: %s" $partialName)
    "severity" "debug"
) -}}
{{- $cache := false -}}
{{- with $context.cache -}}
  {{- $cache = . -}}
{{- else -}}
  {{- with $context.cached -}}
    {{- $cache = . -}}
  {{- end -}}
{{- end -}}
{{- if templates.Exists $partialHook -}}
  {{- if compare.Eq true $cache -}}
    {{- $output = partials.IncludeCached $partialName $context.context $context.hook -}}
    {{- partials.Include "func/debug-cli.html" (collections.Dictionary
        "message" "included cached"
        "severity" "debug"
    ) -}}
    {{- $loaded = true -}}
  {{- else -}}
    {{- $output = partials.Include $partialName $context.context -}}
    {{- partials.Include "func/debug-cli.html" (collections.Dictionary
        "message" "included uncached"
        "severity" "debug"
    ) -}}
    {{- $loaded = true -}}
  {{- end -}}
{{- end -}}
{{- if compare.Eq $loaded false -}}
  {{- if not (collections.In $disableMessages "unused_hooks") -}}
    {{- partials.Include "func/debug-cli.html" (collections.Dictionary
        "message" (fmt.Printf "%s<<< %s: `%s` %sunused%s" $colourHook $hookSlug $context.hook $colourUnused $colourReset)
        "context" $input
        "severity" "info"
    ) -}}
  {{- end -}}
{{- else -}}
  {{- partials.Include "func/debug-cli.html" (collections.Dictionary
      "message" (fmt.Printf "%s<<< %s: %s done%s" $colourHook $hookSlug $context.hook $colourReset)
  ) -}}
{{- end -}}
{{- return $output -}}
layouts/_partials/func/debug-cli.html
File was renamed from layouts/_partials/func/hooks/debug-cli.html
@@ -47,13 +47,16 @@
{{- else if compare.Eq $severity "error" -}}
  {{- $colourLevel = $colourError -}}
  {{- $severityLabel = "!ERR" -}}
{{- else if compare.Eq $severity "debug" -}}
  {{- $colourLevel = $colourComment -}}
  {{- $severityLabel = "DEBG" -}}
{{- end -}}
{{- if collections.IsSet . "level" -}}
  {{- $level = (cast.ToInt .level) -}}
{{- end -}}
{{- if compare.Le $severity "error" -}}
{{- if compare.Eq $severity "error" -}}
  {{- fmt.Errorf (fmt.Printf "%s%s%s/%s%s %s%s%s" $colourAnanke $namespace $colourComment $colourLevel $severityLabel $colourComment .message $colourReset) -}}
{{- else -}}
  {{- fmt.Warnf (fmt.Printf "%s%s%s/%s%s %s%s%s" $colourAnanke $namespace $colourComment $colourLevel $severityLabel $colourComment .message $colourReset) -}}
layouts/_partials/func/hooks/collector-dump.html
@@ -1,8 +1,5 @@
{{- $scratch := page.Scratch.Get "dnb.hooks" -}}
{{- partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" "Hooks Collector") -}}
{{- partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" (printf "%#v" $scratch)) -}}
{{/*
{{ partials.Include "func/hooks/collector-dump.html" . }}
@todo must use stored instead of stashed and must be post processed.
*/}}
{{- $scratch := page.Store.Get "ananke.hooks" -}}
{{- partials.Include "func/debug-cli.html" (collections.Dictionary "message" "Hooks Collector" "severity" "debug") -}}
{{- range $index, $item := $scratch -}}
    {{- partials.Include "func/debug-cli.html" (collections.Dictionary "message" (printf "%d: %#v" $index $item) "severity" "debug") -}}
{{- end -}}
layouts/_partials/func/hooks/collector.html
@@ -1,6 +1,6 @@
{{ $scratch := page.Scratch.Get "ananke.hooks" }}
{{ $scratch := page.Store.Get "ananke.hooks" }}
{{ if eq $scratch "" }}
  {{ $scratch = slice }}
{{ end }}
{{- page.Scratch.Add "ananke.hooks" (slice .hook) -}}
{{- page.Store.Add "ananke.hooks" (slice .hook) -}}
layouts/_partials/hook.html
@@ -1,70 +1,5 @@
{{- $context := collections.Dictionary -}}
{{- $config := site.Params.ananke.hooks -}}
{{- $colourHook := "\033[38;2;170;153;255m" -}}
{{- $colourUnused := "\033[38;2;204;119;102m" -}}
{{- $colourReset := "\033[0m" -}}
{{- /* create context from extended or simple hook */ -}}
{{- if reflect.IsMap . -}}
  {{- /* extended use case with map */ -}}
  {{- $context = collections.Merge $context . }}
  {{- $context = collections.Merge $context (collections.Dictionary "type" "full") }}
  {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" (fmt.Printf "%s>>> HOOK: %s%s" $colourHook $context.hook $colourReset)) }}
  {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" "hook is extended") }}
  {{- /* extended use case */ -}}
  {{- $context = collections.Merge $context . }}
  {{- $context = collections.Merge $context (collections.Dictionary "type" "full") }}
{{- else -}}
  {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" (fmt.Printf "%s>>> HOOK: %s%s" $colourHook $context $colourReset)) }}
  {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" "hook is simple") }}
  {{- /* simple use case */ -}}
  {{- $context = collections.Merge
                  $context
                  (collections.Dictionary "hook" . "context" collections.Dictionary) -}}
  {{- $context = collections.Merge $context (collections.Dictionary "type" "simple") }}
{{- end -}}
{{/**
$context.hook = hook name
$context.cache = whether to cache the hook (optional, default: false, if used this is used as cache indicator)
$context.context = context of the hook (e.g. page, section, etc.)
$context.type = type of the hook (e.g. full, simple)
context is empty for simple hooks
**/}}
{{- /* add hook to collector */ -}}
{{- partials.Include "func/hooks/collector.html" (collections.Dictionary
    "hook" $context.hook
    "context" $context
) -}}
{{- $loaded := false -}}
{{- $partialName := fmt.Printf "hooks/%s.html" $context.hook -}}
{{- $partialHook := fmt.Printf "_partials/%s" $partialName -}}
{{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" (fmt.Printf "partial Name: %s" $partialName)) }}
{{- if templates.Exists $partialHook -}}
  {{- if compare.Eq true (.cached | compare.Default false) -}}
    {{- partials.IncludeCached $partialName $context.context -}}
    {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" "included") }}
    {{- $loaded = true -}}
  {{- else -}}
    {{- partials.Include $partialName $context.context -}}
    {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" "included uncached") }}
    {{- $loaded = true -}}
  {{- end -}}
{{- end -}}
{{- if compare.Eq $loaded false -}}
  {{- if not (collections.In $config.disable_messages "unused_hooks") -}}
    {{- partials.Include "func/hooks/debug-cli.html" (collections.Dictionary
        "message" (fmt.Printf "%s<<< HOOK `%s` %sunused%s" $colourHook $context.hook $colourUnused $colourReset)
        "context" .
        "severity" "info"
    ) -}}
  {{- end -}}
{{ else }}
  {{ partials.Include "func/hooks/debug-cli.html" (collections.Dictionary "message" (fmt.Printf "%s<<< HOOK: %s done%s" $colourHook $context.hook $colourReset)) }}
{{- end -}}
{{/* "A hook is just a filter that outputs immediately!" he said and moved on with his life. */}}
{{- partials.Include "filter.html" (collections.Dictionary
    "__ananke_hook_input" .
    "__ananke_hook_slug" "HOOK"
) -}}
layouts/_partials/hooks/article/test.html
New file
@@ -0,0 +1 @@
{{ return printf . "World" }}
layouts/baseof.html
@@ -78,4 +78,7 @@
    </main>
    {{ block "footer" . }}{{ partials.IncludeCached "site-footer.html" . }}{{ end }}
  </body>
</html>
</html>
{{- with (templates.Defer (dict "key" "hooks-collector")) -}}
  {{- partials.Include "func/hooks/collector-dump.html" . }}
{{ end -}}
layouts/single.html
@@ -18,7 +18,7 @@
  <article class="{{ $post_class }} flex-l {{ if $needs_aside }}mw8{{ else }}mw7{{ end }} center ph3 flex-wrap justify-between">
    <header class="mt4 w-100">
      {{- partials.Include "hook.html" ( dict "hook" "article/section-link"  "context" . ) -}}
      {{- partials.Include "hook.html" ( dict "hook" "article/section-link222"  "context" . ) -}}
      {{- partials.Include "filter.html" ( dict "hook" "article/test"  "context" "Hello %s" ) -}}
      {{- partials.IncludeCached "social/share.html" . . -}}
      <h1 class="f1 athelas mt3 mb1">
        {{- .Title -}}