{{- $showhidden := .Get "showhidden"}}
{{- $style :=  .Get "style" | default "li" }}
{{- $depth :=  .Get "depth" | default 1 }}
{{- $withDescription :=  .Get "description" | default false }}
{{- $sortTerm :=  .Get "sort" | default "Weight" }}

{{- .Scratch.Set "current" .Page }}

{{- if (.Get "page")}}
	{{- with .Site.GetPage "section" (.Get "page") }}
		{{- $.Scratch.Set "current" . }}
	{{- end }}
{{- end }}

{{- $cpage := (.Scratch.Get "current") }}

<ul class="children children-{{$style}}">
	{{- .Scratch.Set "pages" $cpage.Pages }}
    {{- if $cpage.Sections}}
	    {{- .Scratch.Set "pages" ($cpage.Pages | union $cpage.Sections) }}
    {{- end}}
    {{- $pages := (.Scratch.Get "pages") }}

	{{- if eq $sortTerm "Weight"}}
		{{- template "childs" dict "menu" $pages.ByWeight "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
	{{- else if eq $sortTerm "Name"}}
		{{- template "childs" dict "menu" $pages.ByTitle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
	{{- else if eq $sortTerm "PublishDate"}}
		{{- template "childs" dict "menu" $pages.ByPublishDate "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
	{{- else if eq $sortTerm "Date"}}
		{{- template "childs" dict "menu" $pages.ByDate "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
	{{- else if eq $sortTerm "Length"}}
		{{- template "childs" dict "menu" $pages.ByLength "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
	{{- else}}
		{{- template "childs" dict "menu" $pages "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm}}
	{{end}}
</ul>

{{.Inner|safeHTML}}

{{ define "childs" }}
	{{- range .menu }}
		{{- if and .Params.hidden (not $.showhidden) }}
		{{- else}}

<span>
{{- if hasPrefix $.style "h"}}
	{{- $num := sub ( int (trim $.style "h") ) 1 }}
	{{- $numn := add $num $.count }}

	{{- (printf "<h%d>" $numn)|safeHTML}}
		<a href="{{.RelPermalink}}" >{{ .Title }}</a>
	{{- (printf "</h%d>" $numn)|safeHTML}}
{{- else}}
	{{- (printf "<%s>" $.style)|safeHTML}}
		<a href="{{.RelPermalink}}" >{{ .Title }}</a>
	{{- (printf "</%s>" $.style)|safeHTML}}
{{- end}}





			{{- if $.description}}
				{{- if .Description}}
<p>{{.Description}}</p>
				{{- else}}
<p>{{.Summary}}</p>
				{{- end}}
			{{- end}}
</span>


			{{- if lt $.count $.depth}}
{{- if eq $.style "li" }}
<ul>
{{- end}}
	{{- .Scratch.Set "pages" .Pages }}
    {{- if .Sections}}
	    {{- .Scratch.Set "pages" (.Pages | union .Sections) }}
    {{- end}}
    {{- $pages := (.Scratch.Get "pages") }}

	{{- if eq $.sortTerm "Weight"}}
		{{- template "childs" dict "menu" $pages.ByWeight  "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
	{{- else if eq $.sortTerm "Name"}}
		{{- template "childs" dict "menu" $pages.ByTitle  "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
	{{- else if eq $.sortTerm "PublishDate"}}
		{{- template "childs" dict "menu" $pages.ByPublishDate  "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
	{{- else if eq $.sortTerm "Date"}}
		{{- template "childs" dict "menu" $pages.ByDate  "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
	{{- else if eq $.sortTerm "Length"}}
		{{- template "childs" dict "menu" $pages.ByLength  "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
	{{- else}}
		{{- template "childs" dict "menu" $pages  "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm}}
	{{- end}}
{{- if eq $.style "li"}}
</ul>
{{- end }}
			{{- end }}

		{{- end }}
	{{- end }}
{{- end }}
