Now all pages in sitemap are detected

Also made the code not language-related so it can be used as a backend
for all my org-generated websites.
This commit is contained in:
Lucien Cartier-Tilet 2020-05-05 11:50:28 +02:00
parent 300c1dbee6
commit d692d30ff8
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 35 additions and 30 deletions

View File

@ -1,6 +1,7 @@
import 'dart:html' show HttpRequest; import 'dart:html' show HttpRequest;
import 'package:html/parser.dart' show parse; import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart' show Element;
// Get the sitemap content // Get the sitemap content
Future<String> getSitemap() async { Future<String> getSitemap() async {
@ -13,26 +14,30 @@ Future<String> getSitemap() async {
return 'Error'; 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) {
if (elem.outerHtml.contains('index')) {
continue;
} else 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 // 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 {
var links = <String, String>{}; final content = await getSitemap();
await getSitemap().then((String content) { final sitemap = parse(content).getElementsByClassName('org-ul')[0].children;
final sitemap = parse(content).getElementsByClassName('org-ul')[0].children; return detectPages(sitemap);
for (var elem in sitemap) {
// TODO: make this recursive so prefixes in nested folders can be added to
// each other
if (elem.innerHtml.startsWith('<a')) {
elem = elem.firstChild;
final text = elem.firstChild.text;
final url = elem.attributes['href'];
if (!url.contains('index')) {
links[url] = text;
}
} else {
print('Sitemap folder:\n${elem.innerHtml}');
}
}
});
return links;
} }

View File

@ -15,10 +15,10 @@ Element makeIcon(List<String> classes, [String id]) {
return icon; return icon;
} }
Future<Element> makeLanguages() async { Future<Element> makePages() async {
// Languages // Pages
var languages = Element.ul() var pages = Element.ul()
..attributes['id'] = 'drop-lang' ..attributes['id'] = 'drop-page'
..classes.add('dropdown'); ..classes.add('dropdown');
await parseSitemap().then((final sitemap) => { await parseSitemap().then((final sitemap) => {
sitemap.forEach((url, name) { sitemap.forEach((url, name) {
@ -29,15 +29,15 @@ Future<Element> makeLanguages() async {
Element.a() Element.a()
..attributes['href'] = url ..attributes['href'] = url
..innerText = name); ..innerText = name);
languages.insertAdjacentElement('beforeEnd', link); 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)'
..append(makeIcon(['fas', 'fa-language']))) ..append(makeIcon(['fas', 'fa-flag'])))
..classes.addAll(['nav-item', 'has-dropdown']) ..classes.addAll(['nav-item', 'has-dropdown'])
..insertAdjacentElement('beforeEnd', languages); ..insertAdjacentElement('beforeEnd', pages);
} }
Future<Element> makeTocDropDown() async { Future<Element> makeTocDropDown() async {
@ -115,7 +115,7 @@ Future<Element> makeShare() async {
// Add a navbar atop of the HTML body, containing two buttons: // Add a navbar atop of the HTML body, containing two buttons:
// - One back to home // - One back to home
// - A dropdown to each language detected in the sitemap // - A dropdown to each page detected in the sitemap
Future<Element> makeNavbar() async { Future<Element> makeNavbar() async {
var navbar_content = Element.ul()..classes.add('navbar-nav'); var navbar_content = Element.ul()..classes.add('navbar-nav');
@ -128,8 +128,8 @@ Future<Element> makeNavbar() async {
..attributes['href'] = '/' ..attributes['href'] = '/'
..append(makeIcon(['fas', 'fa-home'])))); ..append(makeIcon(['fas', 'fa-home']))));
// Add languages // Add pages
navbar_content.append(await makeLanguages()); navbar_content.append(await makePages());
// TOC icon // TOC icon
navbar_content.append(await makeTocDropDown()); navbar_content.append(await makeTocDropDown());

View File

@ -381,7 +381,7 @@ header {
transform: translateX(-75%); transform: translateX(-75%);
} }
#drop-lang { #drop-page {
flex-direction: column; flex-direction: column;
transform: translateX(-40%); transform: translateX(-40%);
li { li {