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

72 lines
1.9 KiB
Dart
Raw Normal View History

import 'dart:html';
import './navbar.dart' show makeNavbar;
const image_header = '/img/icon.png';
2020-05-05 17:20:28 +00:00
Future<Element> makeHeader() async {
var header = Element.tag('header');
header
..append(Element.img()
2020-05-05 17:20:28 +00:00
..attributes['src'] = image_header
..attributes['alt'] = 'Logo'
..attributes['heigh'] = '150px'
..attributes['width'] = '150px')
..append(querySelector('h1'));
2020-05-26 00:14:13 +00:00
final subt = header.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.
2020-05-10 10:37:22 +00:00
Future<void> linkifyImg() async {
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 {
// Make navbar
2020-05-10 10:37:22 +00:00
final navbar = await makeNavbar();
// Make header
2020-05-10 10:37:22 +00:00
final header = await makeHeader();
// wrap tables in container for better SCSS display
await wrapTables();
2020-05-10 10:37:22 +00:00
// Make images not linking somewhere link to themselves
await linkifyImg();
// Add navbar to page
querySelector('body').insertAdjacentElement('afterBegin', navbar);
// Add headet to page
querySelector('#content').insertAdjacentElement('beforeBegin', header);
// Add correct class to TOC
final toc = (querySelector('#table-of-contents') ??
(Element.div()
..attributes['id'] = 'table-of-contents'
2020-05-10 10:37:22 +00:00
..innerText = 'Table of Contents Unavailable'))
..classes.add('dropdown');
2020-05-10 10:37:22 +00:00
navbar.querySelector('#toc-drop').append(toc);
// Remove all <br> tags from HTML
querySelectorAll('br').forEach((br) => br.remove());
}