I’m trying to create a tree list that shows all articles by date, but I’m having trouble creating nested Dict, and if this is possible, I think I can do the last part by myself.
Please, allow me to use the Python code to explain the logic I want.
explanation with python code
dict_date = {}
for article_name, (yy, mm, dd) in [('post-013-1', (2021, 1, 13)), ('post-013-2', (2021, 1, 13)),
('article-xxx', (2020, 12, 25))]:
if yy not in dict_date:
dict_date[yy] = {} # create a new empty dict # How to do that?
if mm not in dict_date[yy]:
dict_date[yy][mm] = [] # create a new empty list # How to do that?
dict_date[yy][mm].append(article_name) # How to do that?
html_text = "<ul>"
for yy, dict_mm in dict_date.items():
html_text += f"<li>{yy}<ul>"
for mm, article_list in dict_mm.items():
html_text += f"<li>{mm}<ul>"
for article_name in article_list:
html_text += f"<li>{article_name}</li>"
html_text += '</ul></li>'
html_text += '</ul></li>'
html_text += '</ul>'
print(html_text)
with open('test.temp.html', 'w', encoding='utf-8') as f:
f.write(html_text)
My question is the three How to do that
above.
I’ve looked at Scratch and dict in the documentation, but they don’t seem to help. Am I missing something?
The following code is what I’m planning to do in Hugo now.
{{ $tree_date := dict }}
{{- range .Site.RegularPages -}}
{{ if .Date }}
{{ $cur_date := dateFormat "2006-01-02" .Date }}
{{ $cur_date_yy_mm_dd := split $cur_date "-" }}
{{ $yy := index $cur_date_yy_mm_dd 0 }}
{{ $mm := index $cur_date_yy_mm_dd 1 }}
{{ $dd := index $cur_date_yy_mm_dd 2 }}
{{ if not (in $tree_date $yy) }}
{{ end }}
{{ end }}
{{- end -}}
I plan to have Hugo only produce the dict and then feed it to javascript to complete the final act, but of course, it would be best if it could all be done by Hugo.
Or, if you have already implemented this(tree list of dates), you can share it with me. Thank you very much!