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:
parent
300c1dbee6
commit
d692d30ff8
@ -1,6 +1,7 @@
|
||||
import 'dart:html' show HttpRequest;
|
||||
|
||||
import 'package:html/parser.dart' show parse;
|
||||
import 'package:html/dom.dart' show Element;
|
||||
|
||||
// Get the sitemap content
|
||||
Future<String> getSitemap() async {
|
||||
@ -13,26 +14,30 @@ Future<String> getSitemap() async {
|
||||
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
|
||||
// from the sitemap.
|
||||
Future<Map<String, String>> parseSitemap() async {
|
||||
var links = <String, String>{};
|
||||
await getSitemap().then((String content) {
|
||||
final sitemap = parse(content).getElementsByClassName('org-ul')[0].children;
|
||||
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;
|
||||
final content = await getSitemap();
|
||||
final sitemap = parse(content).getElementsByClassName('org-ul')[0].children;
|
||||
return detectPages(sitemap);
|
||||
}
|
||||
|
@ -15,10 +15,10 @@ Element makeIcon(List<String> classes, [String id]) {
|
||||
return icon;
|
||||
}
|
||||
|
||||
Future<Element> makeLanguages() async {
|
||||
// Languages
|
||||
var languages = Element.ul()
|
||||
..attributes['id'] = 'drop-lang'
|
||||
Future<Element> makePages() async {
|
||||
// Pages
|
||||
var pages = Element.ul()
|
||||
..attributes['id'] = 'drop-page'
|
||||
..classes.add('dropdown');
|
||||
await parseSitemap().then((final sitemap) => {
|
||||
sitemap.forEach((url, name) {
|
||||
@ -29,15 +29,15 @@ Future<Element> makeLanguages() async {
|
||||
Element.a()
|
||||
..attributes['href'] = url
|
||||
..innerText = name);
|
||||
languages.insertAdjacentElement('beforeEnd', link);
|
||||
pages.insertAdjacentElement('beforeEnd', link);
|
||||
})
|
||||
});
|
||||
return Element.li()
|
||||
..append(Element.a()
|
||||
..attributes['href'] = 'javascript:void(0)'
|
||||
..append(makeIcon(['fas', 'fa-language'])))
|
||||
..append(makeIcon(['fas', 'fa-flag'])))
|
||||
..classes.addAll(['nav-item', 'has-dropdown'])
|
||||
..insertAdjacentElement('beforeEnd', languages);
|
||||
..insertAdjacentElement('beforeEnd', pages);
|
||||
}
|
||||
|
||||
Future<Element> makeTocDropDown() async {
|
||||
@ -115,7 +115,7 @@ Future<Element> makeShare() async {
|
||||
|
||||
// Add a navbar atop of the HTML body, containing two buttons:
|
||||
// - 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 {
|
||||
var navbar_content = Element.ul()..classes.add('navbar-nav');
|
||||
|
||||
@ -128,8 +128,8 @@ Future<Element> makeNavbar() async {
|
||||
..attributes['href'] = '/'
|
||||
..append(makeIcon(['fas', 'fa-home']))));
|
||||
|
||||
// Add languages
|
||||
navbar_content.append(await makeLanguages());
|
||||
// Add pages
|
||||
navbar_content.append(await makePages());
|
||||
|
||||
// TOC icon
|
||||
navbar_content.append(await makeTocDropDown());
|
||||
|
@ -381,7 +381,7 @@ header {
|
||||
transform: translateX(-75%);
|
||||
}
|
||||
|
||||
#drop-lang {
|
||||
#drop-page {
|
||||
flex-direction: column;
|
||||
transform: translateX(-40%);
|
||||
li {
|
||||
|
Reference in New Issue
Block a user