2020-05-05 12:49:26 +00:00
|
|
|
|
import 'dart:html' show HttpRequest;
|
|
|
|
|
|
|
|
|
|
import 'package:html/parser.dart' show parse;
|
|
|
|
|
import 'package:html/dom.dart' show Element;
|
|
|
|
|
|
2020-05-05 14:48:09 +00:00
|
|
|
|
final excluded_keywords = ['index', 'CONTRIBUTING', 'LICENSE', 'README'];
|
|
|
|
|
|
2020-05-05 12:49:26 +00:00
|
|
|
|
// Get the sitemap content
|
|
|
|
|
Future<String> getSitemap() 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> sitemap, [String prefix]) {
|
|
|
|
|
final links = <String, String>{};
|
|
|
|
|
for (var elem in sitemap) {
|
2020-05-05 14:48:09 +00:00
|
|
|
|
for(var kw in excluded_keywords) {
|
|
|
|
|
if (elem.outerHtml.contains(kw)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (elem.innerHtml.startsWith('<a')) {
|
2020-05-05 12:49:26 +00:00
|
|
|
|
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 getSitemap();
|
|
|
|
|
final sitemap = parse(content).getElementsByClassName('org-ul')[0].children;
|
|
|
|
|
return detectPages(sitemap);
|
|
|
|
|
}
|