-
-
Save bugsysailor/69b69bac59a4e5adfc24998089a21138 to your computer and use it in GitHub Desktop.
Pretty printing Hugo variables for debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.debugprint td { | |
padding-left: 4px; | |
padding-right: 4px; | |
} | |
.debugprint .key { | |
font-weight: bold; | |
} | |
.debugprint .type { | |
/* Hide Type columns in debugprint */ | |
/* display: none; */ | |
font-size: 0.9em; | |
font-family: monospace; | |
font-style: italic; | |
} | |
.debugprint .value { | |
font-family: monospace; | |
} | |
.debugprint .true { | |
color: green; | |
} | |
.debugprint .false { | |
color: red; | |
} | |
/* Don't touch the table headers */ | |
.debugprint th.key, | |
.debugprint th.type, | |
.debugprint th.value { | |
font-family: normal; | |
font-size: 1em; | |
font-style: normal; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Pretty Print for Debug --> | |
<!-- https://github.com/kaushalmodi/ox-hugo/blob/master/test/site/themes/bare_min/layouts/partials/debugprint.html --> | |
<!-- Usage examples: | |
{{/* partial "debugprint.html" .Params */}} | |
{{/* partial "debugprint.html" .Site */}} | |
{{/* partial "debugprint.html" .GitInfo */}} | |
{{/* partial "debugprint.html" .Resources */}} | |
{{/* partial "debugprint.html" .File */}} | |
--> | |
{{ $value := . }} | |
{{ $type := (printf "%T" $value) }} | |
{{ $typeIsBool := (eq "bool" $type) }} | |
{{ $typeIsNumber := (or (eq "int" $type) (eq "int64" $type) (eq "float64" $type)) }} | |
{{ $typeIsString := (eq "string" $type) }} | |
{{ $typeIsTime := (eq "time.Time" $type) }} | |
{{ $typeIsInterface := (eq "interface" $type) }} | |
{{ $typeIsSlice := (findRE "^([[][]]|.*TaxonomyList|output\\.Formats|resource\\.Resources)" $type) }} <!-- match ^[] --> | |
{{ $typeIsMap := (findRE "^(map[[].+[]]|.*SiteSocial)" $type) }} <!-- match ^map[*] --> | |
{{ $typeIsSiteInfo := (eq "*hugolib.SiteInfo" $type) }} | |
{{ $typeIsGitInfo := (findRE "^.*gitmap\\.GitInfo" $type) }} | |
{{ $typeIsOutputFormat := (eq "output.Format" $type) }} | |
{{ $typeIsResource := (findRE "^\\*resource" $type) }} | |
{{ $typeIsPage := (findRE "^\\*hugolib\\.Page" $type) }} | |
{{ $typeIsFileInfo := (eq "*hugolib.fileInfo" $type) }} | |
{{ $typeIsSpecial := (or $typeIsSiteInfo $typeIsGitInfo $typeIsOutputFormat $typeIsResource $typeIsPage $typeIsFileInfo) }} | |
{{ if $typeIsBool }} | |
{{ printf "<span class=\"debugprint\"><span class=\"%#v\">%#v</span></span>" $value $value | safeHTML }} | |
{{ else if (or $typeIsNumber $typeIsString) }} | |
<!-- Use %#v for strings so that their values always get double-quoted. | |
That is helpful to tell if a space char is within a string. --> | |
{{ printf "%#v" $value | safeHTML }} | |
{{ else if (or $typeIsTime $typeIsInterface) }} | |
{{ printf "%s" $value | safeHTML }} | |
{{ else if $typeIsSlice }} | |
{{ range $value }} | |
{{ partial "debugprint.html" . }} <!-- Recursive call FTW! --> | |
{{ end }} | |
{{ else if $typeIsMap }} | |
{{ if (gt (len $value) 0) }} | |
<table class="debugprint"> | |
<tr><th class="key">Key</th><th class="type">Type</th><th class="value">Value</th></tr> | |
{{ range $key1, $value1 := $value }} | |
{{ $type1 := (printf "%T" $value1) }} | |
{{ $type1IsTime := (eq "time.Time" $type1) }} | |
{{ if $type1IsTime }} | |
<!-- Print the date only if it is not at its initial value of Jan 1, 0001 --> | |
{{ if (ne "0001-01-01" ($value1.Format "2006-01-02")) }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"type\">%s</td><td class=\"value\">" $key1 $type1 | safeHTML }} | |
{{ partial "debugprint.html" $value1 }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
{{ else }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"type\">%s</td><td class=\"value\">" $key1 $type1 | safeHTML }} | |
{{ partial "debugprint.html" $value1 }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
{{ end }} | |
</table> | |
{{ end }} | |
{{ else if $typeIsSpecial }} | |
<div class="debugprint"> | |
{{ if $typeIsSiteInfo }} | |
{{ $siteVarNames := (slice "Title" "Author" "Social" "LastChange" "DisqusShortname" "GoogleAnalytics" "Copyright" "RSSLink" "LanguageCode" "BuildDrafts" "Params") }} | |
{{ $siteVarSymbols := (slice .Title .Author .Social .LastChange .DisqusShortname .GoogleAnalytics .Copyright .RSSLink .LanguageCode .BuildDrafts .Params ) }} | |
<table> | |
<tr><th class="key">SiteInfo Variable</th><th class="value">Value</th></tr> | |
{{ range $idx, $elem := $siteVarNames }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }} | |
{{ partial "debugprint.html" (index $siteVarSymbols $idx) }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
</table> | |
{{ else if $typeIsGitInfo }} | |
{{ $gitInfoVarNames := (slice "Hash" "AbbreviatedHash" "Subject" "AuthorName" "AuthorEmail" "AuthorDate") }} | |
{{ $gitInfoVarSymbols := (slice .Hash .AbbreviatedHash .Subject .AuthorName .AuthorEmail .AuthorDate ) }} | |
<table> | |
<tr><th class="key">GitInfo Variable</th><th class="value">Value</th></tr> | |
{{ range $idx, $elem := $gitInfoVarNames }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }} | |
{{ partial "debugprint.html" (index $gitInfoVarSymbols $idx) }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
</table> | |
{{ else if $typeIsOutputFormat }} | |
{{ $OutputFormatVarNames := (slice "Name" "MediaType" "Path" "BaseName" "Rel" "Protocol" "IsPlainText" "IsHTML" "NoUgly" "NotAlternative") }} | |
{{ $OutputFormatVarSymbols := (slice .Name .MediaType .Path .BaseName .Rel .Protocol .IsPlainText .IsHTML .NoUgly .NotAlternative ) }} | |
<table> | |
<tr><th class="key">OutputFormat Variable</th><th class="value">Value</th></tr> | |
{{ range $idx, $elem := $OutputFormatVarNames }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }} | |
{{ partial "debugprint.html" (index $OutputFormatVarSymbols $idx) }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
</table> | |
{{ else if $typeIsResource }} | |
{{ $ResourceVarNames := (slice "ResourceType" "Permalink" "RelPermalink" "AbsSourceFilename" "Name" "Title" "Params") }} | |
{{ $ResourceVarSymbols := (slice .ResourceType .Permalink .RelPermalink .AbsSourceFilename .Name .Title .Params ) }} | |
<table> | |
<tr><th class="key">Resource Variable</th><th class="value">Value</th></tr> | |
{{ range $idx, $elem := $ResourceVarNames }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }} | |
{{ partial "debugprint.html" (index $ResourceVarSymbols $idx) }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
</table> | |
<hr> | |
{{ else if $typeIsPage }} | |
{{ $PageVarNames := (slice "ResourceType" "Kind" "Draft" "Layout" "Permalink" "RelPermalink" "Name" "Title" "Params") }} | |
{{ $PageVarSymbols := (slice .ResourceType .Kind .Draft .Layout .Permalink .RelPermalink .Name .Title .Params ) }} | |
<table> | |
<tr><th class="key">Page Variable</th><th class="value">Value</th></tr> | |
{{ range $idx, $elem := $PageVarNames }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }} | |
{{ partial "debugprint.html" (index $PageVarSymbols $idx) }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
</table> | |
<hr> | |
{{ else if $typeIsFileInfo }} | |
{{ $FileInfoVarNames := (slice "UniqueID" "BaseFileName" "TranslationBaseName" "Lang" "Section" "LogicalName" "Dir" "Ext" "Path") }} | |
{{ $FileInfoVarSymbols := (slice .UniqueID .BaseFileName .TranslationBaseName .Lang .Section .LogicalName .Dir .Ext .Path ) }} | |
<table> | |
<tr><th class="key">FileInfo Variable</th><th class="value">Value</th></tr> | |
{{ range $idx, $elem := $FileInfoVarNames }} | |
{{ printf "<tr><td class=\"key\">%s</td><td class=\"value\">" $elem | safeHTML }} | |
{{ partial "debugprint.html" (index $FileInfoVarSymbols $idx) }} <!-- Recursive call FTW! --> | |
{{ printf "</td></tr>" | safeHTML }} | |
{{ end }} | |
</table> | |
{{ else }} | |
{{ printf "%#v" $value | safeHTML }} | |
{{ end }} | |
</div> | |
{{ else }} | |
{{ printf "%#v (<i>type:%s</i>)" $value $type | safeHTML }} | |
{{ end }} | |
<!-- Older, simpler version --> | |
<!-- <table class="debugprint"> --> | |
{{/* range $key1, $value1 := . */}} | |
{{/* printf "<tr><td>%#v</td><td>%#v</td></tr>" $key1 $value1 | safeHTML */}} | |
{{/* end */}} | |
<!-- </table> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment