2020-05-10 10:00:47 +00:00
|
|
|
|
import 'dart:html' as html show HttpRequest, Element, querySelector;
|
2020-05-05 12:49:26 +00:00
|
|
|
|
|
|
|
|
|
import 'package:html/parser.dart' show parse;
|
2020-05-10 10:00:47 +00:00
|
|
|
|
import 'package:html/dom.dart' as dom show Element;
|
2020-05-05 12:49:26 +00:00
|
|
|
|
|
2020-05-10 10:37:22 +00:00
|
|
|
|
final excluded_keywords = {'index', 'CONTRIBUTING', 'LICENSE', 'README'};
|
2020-05-05 14:48:09 +00:00
|
|
|
|
|
2020-05-05 12:49:26 +00:00
|
|
|
|
// Get the sitemap content
|
2020-05-10 10:00:47 +00:00
|
|
|
|
Future<String> fetchRemoteSitemap() async {
|
2020-05-05 12:49:26 +00:00
|
|
|
|
const path = 'sitemap.html';
|
|
|
|
|
try {
|
2020-05-10 10:00:47 +00:00
|
|
|
|
return await html.HttpRequest.getString(path);
|
2020-05-05 12:49:26 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
print('Couldn’t open $path');
|
|
|
|
|
}
|
|
|
|
|
return 'Error';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse the list of elements and detect pages from this list
|
2020-05-10 10:00:47 +00:00
|
|
|
|
Map<String, String> detectPages(List<dom.Element> sitemap, [String prefix]) {
|
2020-05-05 12:49:26 +00:00
|
|
|
|
final links = <String, String>{};
|
|
|
|
|
for (var elem in sitemap) {
|
2020-05-21 05:33:22 +00:00
|
|
|
|
for (var keyword in excluded_keywords) {
|
|
|
|
|
if (elem.outerHtml.contains(keyword)) {
|
2020-05-05 14:48:09 +00:00
|
|
|
|
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 {
|
2020-05-10 10:37:22 +00:00
|
|
|
|
prefix = (prefix == null)
|
2020-05-21 05:33:22 +00:00
|
|
|
|
? elem.firstChild.text.replaceAll('\n', '')
|
2020-05-10 10:37:22 +00:00
|
|
|
|
: '$prefix / ${elem.firstChild.text}';
|
2020-05-05 12:49:26 +00:00
|
|
|
|
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 {
|
2020-05-10 10:00:47 +00:00
|
|
|
|
final content = await fetchRemoteSitemap();
|
2020-05-05 12:49:26 +00:00
|
|
|
|
final sitemap = parse(content).getElementsByClassName('org-ul')[0].children;
|
|
|
|
|
return detectPages(sitemap);
|
|
|
|
|
}
|
2020-05-10 10:00:47 +00:00
|
|
|
|
|
|
|
|
|
Future sleep(Duration time) async {
|
|
|
|
|
return Future.delayed(time);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 05:33:22 +00:00
|
|
|
|
Future<void> createSitemap() async {
|
2020-05-10 10:00:47 +00:00
|
|
|
|
final sitemap = await parseSitemap();
|
2020-05-21 05:33:22 +00:00
|
|
|
|
final pages = html.querySelector('#drop-page');
|
2020-05-10 10:00:47 +00:00
|
|
|
|
sitemap.forEach((url, name) {
|
|
|
|
|
final link = html.Element.li()
|
|
|
|
|
..classes.add('dropdown-item')
|
|
|
|
|
..append(html.Element.a()
|
|
|
|
|
..attributes['href'] = url
|
|
|
|
|
..innerText = name);
|
2020-05-21 05:33:22 +00:00
|
|
|
|
pages.append(link);
|
2020-05-10 10:00:47 +00:00
|
|
|
|
});
|
|
|
|
|
}
|