Skip to content

Commit 28a8c8c

Browse files
authored
Merge pull request #154 from hughrun/branch_versions
bugfix versions and add picker
2 parents dc55d9f + b8fc1d6 commit 28a8c8c

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ jobs:
108108
109109
- name: Compile site
110110
run: |
111-
python generate.py
111+
python generate.py ${{ matrix.version }}
112112
113113
- uses: stefanzweifel/git-auto-commit-action@v5
114114
with:

generate.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
env.install_gettext_translations(i18n)
1616

1717

18-
def get_page_metadata(locale_slug, page):
18+
def get_page_metadata(locale_slug, page, version_slug=False):
1919
"""title/order etc for a page
2020
this is how the markdown file is composed:
2121
@@ -63,11 +63,15 @@ def get_page_metadata(locale_slug, page):
6363
else page.split("/")[-1]
6464
)
6565
path_dir = page.split("/")[-1].replace(".md", ".html")
66-
header_obj["path"] = f"/{locale_slug}{path_dir}"
66+
header_obj["path"] = (
67+
f"/{locale_slug}{path_dir}"
68+
if not version_slug
69+
else f"/{version_slug}/{locale_slug}{path_dir}"
70+
)
6771
return header_obj
6872

6973

70-
def get_site_data(locale_slug, locale_code, page):
74+
def get_site_data(locale_slug, locale_code, page, version_slug=False):
7175
"""this should be a file"""
7276
category_dirs = glob("content/*/")
7377
categories = []
@@ -80,14 +84,14 @@ def get_site_data(locale_slug, locale_code, page):
8084
f"locale/{locale_code}/{cat_dir}/*.md" if locale_slug else f"{cat_dir}/*.md"
8185
)
8286
for subcat in glob(location):
83-
subcategories.append(get_page_metadata(locale_slug, subcat))
87+
subcategories.append(get_page_metadata(locale_slug, subcat, version_slug))
8488
subcategories.sort(key=lambda v: v.get("Order", -1))
8589

8690
categories.append({**parsed, **{"subcategories": subcategories}})
8791
categories.sort(key=lambda v: v["order"])
8892
template_data = {"categories": categories}
8993

90-
template_data["headers"] = get_page_metadata(locale_slug, page)
94+
template_data["headers"] = get_page_metadata(locale_slug, page, version_slug)
9195

9296
return template_data
9397

@@ -120,6 +124,7 @@ def format_markdown(file_path):
120124

121125

122126
if __name__ == "__main__":
127+
# when we generate for older versions we need to change the page links
123128
version = sys.argv[1] if len(sys.argv) > 1 else False
124129
# iterate through each locale
125130
for locale in i18n.locales_metadata:
@@ -131,15 +136,13 @@ def format_markdown(file_path):
131136

132137
i18n.setLocale(locale["code"])
133138

134-
LOCALIZED_SITE_PATH = f"site/{version}/" if version else "site/"
139+
LOCALIZED_SITE_PATH = "site/"
135140
if locale["code"] != "en_US":
136141
paths = [
137142
["index.html", f"locale/{locale['code']}/content/index.md"],
138143
["page.html", f"locale/{locale['code']}/content/**/*.md"],
139144
]
140-
LOCALIZED_SITE_PATH = (
141-
f"site/{version}/{SLUG}" if version else f"site/{SLUG}"
142-
)
145+
LOCALIZED_SITE_PATH = f"site/{SLUG}"
143146

144147
# iterate through template types
145148
for path, content_paths in paths:
@@ -158,11 +161,17 @@ def format_markdown(file_path):
158161
with open(
159162
f"{LOCALIZED_SITE_PATH}{output_path}", "w+", encoding="utf-8"
160163
) as render_file:
161-
data = get_site_data(SLUG, locale["code"], content_path)
164+
data = get_site_data(SLUG, locale["code"], content_path, version)
162165
data["content"] = format_markdown(content_path)
163-
data["path"] = f"/{SLUG}{output_path}"
166+
data["path"] = (
167+
f"/{SLUG}{output_path}"
168+
if not version
169+
else f"/{version}/{SLUG}{output_path}"
170+
)
171+
versions = ["latest", "v0.7.5"]
164172
render_file.write(
165173
template.render(
174+
versions=versions,
166175
locale=locale,
167176
locales_metadata=i18n.locales_metadata,
168177
**data,

templates/layout.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@
7373
</header>
7474
<div class="columns">
7575
<nav class="menu column is-one-quarter mt-2">
76+
<div class="select">
77+
<select aria-label="{{ _("Previous versions") }}" id="version_selection">
78+
{% for option in versions %}
79+
<option value="{{ option }}" {% if option == version %}selected{% endif %}>{{ option }}</option>
80+
{% endfor %}
81+
</select>
82+
</div>
7683
<h2 class="menu-label"><a href="/{{ locale['slug'] }}">Welcome</a></h2>
7784
{% for category in categories %}
7885
<h2 class="menu-label">{{ category.title }}</h2>
@@ -160,6 +167,31 @@ <h3 class="title is-6">{{ _("Learn more") }}</h3>
160167
}
161168
})()
162169
</script>
170+
<script type="text/javascript">
171+
(function() {
172+
var versionSelector = document.getElementById("version_selection");
173+
versionSelector.onchange = function(event) {
174+
var current_version = "{{ version|safe }}";
175+
console.log(current_version)
176+
var current_location = window.location.pathname;
177+
var version_index = current_location.indexOf(current_version);
178+
var new_location = "/" + event.target.value;
179+
180+
console.log(current_location)
181+
console.log(new_location)
182+
183+
if (version_index) {
184+
new_location += current_location.slice(version_index + current_version.length) + "/";
185+
} else {
186+
console.log("no version_index")
187+
new_location += `/${current_location.slice(1)}`;
188+
}
189+
190+
window.location = new_location;
191+
}
192+
})()
193+
</script>
194+
163195

164196
</body>
165197
</html>

0 commit comments

Comments
 (0)