64 lines
2.0 KiB
Dart
64 lines
2.0 KiB
Dart
import 'dart:html' as html show HttpRequest, Element, querySelector;
|
||
|
||
import 'package:html/parser.dart' show parse;
|
||
import 'package:html/dom.dart' as dom show Element;
|
||
|
||
final excluded_keywords = {'index', 'CONTRIBUTING', 'LICENSE', 'README'};
|
||
|
||
// Get the sitemap content
|
||
Future<String> fetchRemoteSitemap() async {
|
||
const path = '/sitemap.html';
|
||
try {
|
||
return await html.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<dom.Element> t_sitemap,
|
||
[String t_prefix]) {
|
||
final links = <String, String>{};
|
||
for (var elem in t_sitemap) {
|
||
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;
|
||
}
|
||
links['/$url'] = (t_prefix == null) ? text : '$text ($t_prefix)';
|
||
} else {
|
||
t_prefix = (t_prefix == null)
|
||
? elem.firstChild.text.replaceAll('\n', '')
|
||
: '$t_prefix / ${elem.firstChild.text}';
|
||
final ul = elem.children[0].children;
|
||
links.addAll(detectPages(ul, t_prefix));
|
||
}
|
||
}
|
||
return 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 = parse(content).getElementsByClassName('org-ul')[0].children;
|
||
return detectPages(sitemap);
|
||
}
|
||
|
||
Future<void> createSitemap() async {
|
||
final sitemap = await parseSitemap();
|
||
final pages = html.querySelector('#drop-page');
|
||
sitemap.forEach((url, name) {
|
||
final link = html.Element.li()
|
||
..classes.add('dropdown-item')
|
||
..append(html.Element.a()
|
||
..attributes['href'] = url
|
||
..innerText = name);
|
||
pages.append(link);
|
||
});
|
||
}
|