Lucien Cartier-Tilet
f99d45e8ab
This commit removes the `html' package dependency, speeding up the compile time of the Dart code. It also simplifies the code and removes some unused code. For instance the sun, moon, and lightbulb icons is removed, the `Theme' class is removed, and the `switchTheme' function is now merged into the `setTheme' function. The `makeThemeItem' function has also had its second argument removed.
64 lines
2.0 KiB
Dart
64 lines
2.0 KiB
Dart
import 'dart:html' show HttpRequest, Element, querySelector;
|
||
|
||
final excluded_keywords = {'index', 'CONTRIBUTING', 'LICENSE', 'README'};
|
||
|
||
// Get the sitemap content
|
||
Future<String> fetchRemoteSitemap() async {
|
||
const path = '/sitemap.html';
|
||
try {
|
||
return await HttpRequest.getString(path);
|
||
} catch (e) {
|
||
print('Couldn’t open $path');
|
||
}
|
||
return 'Error';
|
||
}
|
||
|
||
// Parse the list of elements and detect pages from this list
|
||
Map<String, String> detectPages(List<Element> t_sitemap,
|
||
[String t_prefix, Map<String, String> t_links]) {
|
||
t_links ??= <String, String>{};
|
||
|
||
// parse each element in sitemap
|
||
for (var elem in t_sitemap) {
|
||
// if it’s a link
|
||
if (elem.innerHtml.startsWith('<a')) {
|
||
elem = elem.firstChild;
|
||
final url = elem.attributes['href'];
|
||
final text = elem.firstChild.text;
|
||
if (excluded_keywords.contains(text) ||
|
||
excluded_keywords.contains(url.substring(0, url.length - 5))) {
|
||
continue;
|
||
}
|
||
t_links['/$url'] = (t_prefix == null) ? text : '$text ($t_prefix)';
|
||
} else {
|
||
final prefix = (t_prefix == null)
|
||
? elem.firstChild.text.replaceAll('\n', '')
|
||
: '$t_prefix / ${elem.firstChild.text}'.replaceAll('\n', '');
|
||
t_links = detectPages(elem.children[0].children, prefix, t_links);
|
||
}
|
||
}
|
||
return t_links;
|
||
}
|
||
|
||
// This function returns a Map which contains all links to languages detected
|
||
// from the sitemap.
|
||
Future<Map<String, String>> parseSitemap() async {
|
||
final content = await fetchRemoteSitemap();
|
||
final sitemap = Element.ul()..innerHtml = content;
|
||
final sitemapNodes = sitemap.querySelector('ul').children;
|
||
return detectPages(sitemapNodes);
|
||
}
|
||
|
||
Future<void> createSitemap() async {
|
||
final sitemap = await parseSitemap();
|
||
final pages = querySelector('#drop-page');
|
||
sitemap.forEach((url, name) {
|
||
final link = Element.li()
|
||
..classes.add('dropdown-item')
|
||
..append(Element.a()
|
||
..attributes['href'] = url
|
||
..innerText = name);
|
||
pages.append(link);
|
||
});
|
||
}
|