How can I create the nested object in the dict

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)

image

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!

Have you seen the available methods to generate: Lists of Content in Hugo | Hugo ?

On the above link the following is documented:

{{ range (.Pages.GroupByDate "2006-01").Reverse }}

That is the method one should use to create chronological Blog archives -as per your screenshot above-.

Hi @Alexandros, It’s my pleasure to get the help of the moderators.
The link is helpful, although I want to do the Markmap. But I can use these things to simplify the code.

And I have done, I put the results as following for those who need to insert the Markmap.

Markmap

what is markmap? markmap-github

site-nevagation with markmap

demo

By Modified Date

code

Table of Content with markmap

demo

You can move the mouse to the SVG(right-hand side) and use it as the TOC.

link

code

single.html
toc_markmap.js

more example about Lists of Content in Hugo