This repository has been archived on 2023-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
nord-for-org/web/dart/parse_sitemap.dart

72 lines
2.1 KiB
Dart
Raw Normal View History

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('Couldnt open $path');
}
return 'Error';
}
// Parse the list of elements and detect pages from this list
Map<String, String> detectPages(List<dom.Element> sitemap, [String prefix]) {
final links = <String, String>{};
for (var elem in sitemap) {
for (var kw in excluded_keywords) {
if (elem.outerHtml.contains(kw)) {
continue;
}
}
if (elem.innerHtml.startsWith('<a')) {
elem = elem.firstChild;
final url = elem.attributes['href'];
final text = elem.firstChild.text;
links[url] = (prefix == null) ? text : '$text ($prefix)';
} else {
final prefix = elem.firstChild.text;
final ul = elem.children[0].children;
links.addAll(detectPages(ul, 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 sleep(Duration time) async {
return Future.delayed(time);
}
Future<html.Element> getSitemap() async {
final sitemap = await parseSitemap();
final pages = <html.Element>[];
sitemap.forEach((url, name) {
final link = html.Element.li()
..classes.add('dropdown-item')
..append(html.Element.a()
..attributes['href'] = url
..innerText = name);
pages.add(link);
});
var drop_container;
do {
await sleep(Duration(seconds: 1));
drop_container = html.querySelector('#drop-page');
} while (drop_container != null);
pages.forEach((link) => drop_container.append(link));
}