Sitemap query now fully asynchronous

Sitemap generation is now independent from the navbar generation, and
it will be added to the navbar only once the navbar has been created.

Also, better style for code.
This commit is contained in:
Lucien Cartier-Tilet 2020-05-10 12:00:47 +02:00
parent 2f297b6374
commit aa4600b588
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 33 additions and 22 deletions

View File

@ -1,9 +1,9 @@
import './reorganize_html.dart' show reorganizeHtml; import './reorganize_html.dart' show reorganizeHtml;
import './theme.dart' show enableThemeChanger, setTheme; import './theme.dart' show enableThemeChanger, setTheme;
import './parse_sitemap.dart' show getSitemap;
Future<void> main() async { Future<void> main() async {
await setTheme(); await setTheme();
await reorganizeHtml().then((_) { await reorganizeHtml().then((_) => enableThemeChanger());
enableThemeChanger(); await getSitemap();
});
} }

View File

@ -28,18 +28,6 @@ Future<Element> makePages() async {
var pages = Element.ul() var pages = Element.ul()
..attributes['id'] = 'drop-page' ..attributes['id'] = 'drop-page'
..classes.add('dropdown'); ..classes.add('dropdown');
await parseSitemap().then((final sitemap) => {
sitemap.forEach((url, name) {
final link = Element.li()
..classes.add('dropdown-item')
..insertAdjacentElement(
'afterBegin',
Element.a()
..attributes['href'] = url
..innerText = name);
pages.insertAdjacentElement('beforeEnd', link);
})
});
return Element.li() return Element.li()
..append(Element.a() ..append(Element.a()
..attributes['href'] = 'javascript:void(0)' ..attributes['href'] = 'javascript:void(0)'

View File

@ -1,15 +1,15 @@
import 'dart:html' show HttpRequest; import 'dart:html' as html show HttpRequest, Element, querySelector;
import 'package:html/parser.dart' show parse; import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart' show Element; import 'package:html/dom.dart' as dom show Element;
final excluded_keywords = ['index', 'CONTRIBUTING', 'LICENSE', 'README']; final excluded_keywords = ['index', 'CONTRIBUTING', 'LICENSE', 'README'];
// Get the sitemap content // Get the sitemap content
Future<String> getSitemap() async { Future<String> fetchRemoteSitemap() async {
const path = 'sitemap.html'; const path = 'sitemap.html';
try { try {
return await HttpRequest.getString(path); return await html.HttpRequest.getString(path);
} catch (e) { } catch (e) {
print('Couldnt open $path'); print('Couldnt open $path');
} }
@ -17,10 +17,10 @@ Future<String> getSitemap() async {
} }
// Parse the list of elements and detect pages from this list // Parse the list of elements and detect pages from this list
Map<String, String> detectPages(List<Element> sitemap, [String prefix]) { Map<String, String> detectPages(List<dom.Element> sitemap, [String prefix]) {
final links = <String, String>{}; final links = <String, String>{};
for (var elem in sitemap) { for (var elem in sitemap) {
for(var kw in excluded_keywords) { for (var kw in excluded_keywords) {
if (elem.outerHtml.contains(kw)) { if (elem.outerHtml.contains(kw)) {
continue; continue;
} }
@ -42,7 +42,30 @@ Map<String, String> detectPages(List<Element> sitemap, [String prefix]) {
// This function returns a Map which contains all links to languages detected // This function returns a Map which contains all links to languages detected
// from the sitemap. // from the sitemap.
Future<Map<String, String>> parseSitemap() async { Future<Map<String, String>> parseSitemap() async {
final content = await getSitemap(); final content = await fetchRemoteSitemap();
final sitemap = parse(content).getElementsByClassName('org-ul')[0].children; final sitemap = parse(content).getElementsByClassName('org-ul')[0].children;
return detectPages(sitemap); 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));
}