1#!/usr/bin/env python3
2import os
3
4
5def process_index_rst(path):
6    #  print(path)
7    with open(path, 'r') as fp:
8        data = fp.read()
9
10    data = data.split('\n')
11
12    last_line = ""
13    title_tmp = ""
14
15    for line in data:
16        line = line.strip()
17
18        if not line:
19            continue
20
21        if line.startswith('---'):
22            title_tmp = last_line.strip()
23
24        elif line.startswith('.. lv_example::'):
25            name = line.replace('.. lv_example::', '').strip()
26            yield name, title_tmp
27
28        last_line = line
29
30
31h1 = {
32    "get_started": "Get Started",
33    "styles": "Styles",
34    "anim": "Animations",
35    "event": "Events",
36    "layouts": "Layouts",
37    "scroll": "Scrolling",
38    "widgets": "Widgets"
39}
40
41widgets = {
42    "obj": "Base Widget",
43    "animimg": "Animation Image",
44    "arc": "Arc",
45    "bar": "Bar",
46    "button": "Button",
47    "buttonmatrix": "Button Matrix",
48    "calendar": "Calendar",
49    "canvas": "Canvas",
50    "chart": "Chart",
51    "checkbox": "Checkbox",
52    "dropdown": "Dropdown",
53    "image": "Image",
54    "imagebutton": "Image Button",
55    "keyboard": "Keyboard",
56    "label": "Label",
57    "led": "LED",
58    "line": "Line",
59    "list": "List",
60    "lottie": "Lottie",
61    "menu": "Menu",
62    "msgbox": "Message Box",
63    "roller": "Roller",
64    "scale":"Scale",
65    "slider": "Slider",
66    "span": "Span",
67    "spinbox": "Spinbox",
68    "spinner": "Spinner",
69    "switch": "Switch",
70    "table": "Table",
71    "tabview": "Tabview",
72    "textarea": "Textarea",
73    "tileview": "Tileview",
74    "win": "Window",
75}
76
77HEADING = '='
78CHAPTER = '#'
79SECTION = '*'
80SUBSECTION = '='
81SUBSUBSECTION = '-'
82
83
84def write_header(h_num, text, f):
85    text = text.strip()
86    if h_num == 0:
87        f.write(header_defs[h_num] * len(text))
88        f.write('\n')
89
90    f.write(text + '\n')
91    f.write(header_defs[h_num] * len(text))
92    f.write('\n\n')
93
94
95# This is the order that Sphinx uses for the headings/titles. 0 is the
96# largest and 4 is the smallest. If this order is not kept in the reST files
97# Sphinx will complain
98header_defs = {
99    0: HEADING,
100    1: CHAPTER,
101    2: SECTION,
102    3: SUBSECTION,
103    4: SUBSUBSECTION
104}
105
106layouts = {
107    "flex": "Flex",
108    "grid": "Grid",
109}
110
111
112def print_item(path, lvl, d, fout):
113    for k in d:
114        v = d[k]
115        if k.startswith(path + "/lv_example_"):
116            write_header(lvl, v, fout)
117            fout.write(f".. lv_example:: {k}\n")
118            fout.write("\n")
119
120
121def exec(temp_directory):
122    output_path = os.path.join(temp_directory, 'examples.rst')
123
124    paths = ["../examples/", "../demos/"]
125    fout = open(output_path, "w")
126    filelist = []
127
128    for path in paths:
129        for root, dirs, files in os.walk(path):
130            for f in files:
131                # append the file name to the list
132                filelist.append(os.path.join(root, f))
133
134    filelist = [fi for fi in filelist if fi.endswith("index.rst")]
135
136    d_all = {}
137    # print all the file names
138    for fn in filelist:
139        d_all.update(dict(tuple(item for item in process_index_rst(fn))))
140
141    # fout.write("```eval_rst\n")
142    # fout.write(":github_url: |github_link_base|/examples.md\n")
143    # fout.write("```\n")
144    # fout.write("\n")
145
146    fout.write('.. _examples:\n\n')
147    write_header(0, 'Examples', fout)
148
149    for h in h1:
150        write_header(1, h1[h], fout)
151
152        if h == "widgets":
153            for w in widgets:
154                write_header(2, widgets[w], fout)
155                print_item(h + "/" + w, 3, d_all, fout)
156        elif h == "layouts":
157            for l in layouts:
158                write_header(2, layouts[l], fout)
159                print_item(h + "/" + l, 3, d_all, fout)
160        else:
161            print_item(h, 2, d_all, fout)
162
163        fout.write("")
164