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:
2020-05-10 12:00:47 +02:00
부모 2f297b6374
커밋 aa4600b588
3개의 변경된 파일33개의 추가작업 그리고 22개의 파일을 삭제

파일 보기

@@ -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();
});
} }

파일 보기

@@ -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)'

파일 보기

@@ -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('Couldn’t open $path'); print('Couldn’t open $path');
} }
@@ -17,7 +17,7 @@ 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) {
@@ -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));
}