This repository has been archived on 2023-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
nord-for-org/web/dart/reorganize_html.dart
Lucien Cartier-Tilet 3c942b4b8f
Fixes bug introduced in e819866
Table of content was made unavailable regardless of whether one was
provided by the page or not. This commit fixes this issue by making an
unavailable TOC only if one is not provided by the web page, otherwise
the TOC will be available.
2020-05-09 14:45:26 +02:00

76 lines
2.0 KiB
Dart

import 'dart:html';
import './navbar.dart' show makeNavbar;
const image_header =
'https://phundrak.fra1.cdn.digitaloceanspaces.com/img/mahakala-monochrome.png';
Future<Element> makeHeader() async {
var header = Element.tag('header');
header
..append(Element.img()
..attributes['src'] = image_header
..attributes['alt'] = 'Logo'
..attributes['heigh'] = '150px'
..attributes['width'] = '150px')
..append(querySelector('h1'));
var subt = querySelector('.subtitle');
if (subt != null) {
header.append(subt);
}
return header;
}
Future<void> wrapTables() async {
for (var table in querySelectorAll('table')) {
var largetable = DivElement()..className = 'largetable';
table.before(largetable);
largetable.children.add(table);
}
}
// All images that are not nested inside a link will be linkified to themselves.
void linkifyImg() {
querySelectorAll('img').forEach((img) {
if (img.parent.tagName == 'P') {
final link = Element.a()..attributes['href'] = img.attributes['src'];
img.insertAdjacentElement('beforeBegin', link);
link.append(img);
}
});
}
Future<void> reorganizeHtml() async {
final content = querySelector('#content');
// Make navbar
await makeNavbar().then((navbar) {
querySelector('body').insertAdjacentElement('afterBegin', navbar);
});
// Make header
await makeHeader().then((header) {
content.insertAdjacentElement('beforeBegin', header);
final subtitle = querySelector('.subtitle');
if (subtitle != null) {
header.append(subtitle);
}
});
// wrap tables in container for better SCSS display
await wrapTables();
linkifyImg();
// Add correct class to TOC
final toc = (querySelector('#table-of-contents') ??
(Element.div()
..attributes['id'] = 'table-of-contents'
..innerText = "Table of Contents Unavailable"))
..classes.add('dropdown');
querySelector('#toc-drop').append(toc);
// Remove all <br> tags from HTML
querySelectorAll('br').forEach((br) => br.remove());
}