-
Notifications
You must be signed in to change notification settings - Fork 0
/
onmyoji.js
69 lines (62 loc) · 1.87 KB
/
onmyoji.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
console.clear();
console.debug('Script init');
// Get current url
pathname = window.location.pathname;
pathname = pathname == '/' ? '/index.html' : pathname;
console.debug('Pathname:', pathname);
// Show corresponding page path
page_path = '/onmyoji/pages' + pathname.replace(".html", ".page");
console.debug('Page_path:', page_path);
function load_doc (path) {
return new Promise((resolve) => {
//let abs_path = window.location.href + rel_path; // cant i use just the relative?
let request = new XMLHttpRequest ();
request.open ('GET', path, true);
request.overrideMimeType("text/html");
request.responseType = 'document';
request.send(null);
request.onload = () => {
resolve(request.responseXML);
}
});
}
function get_template(xml) {
page_settings = xml.getElementsByTagName("onmyoji-page")[0];
template = page_settings.getAttribute('template');
path = `/onmyoji/templates/${template}.template`;
return path
}
function get_fields(xml) {
let fields = {};
let tags = xml.getElementsByTagName("page-cut");
for(let i in tags) {
tag = tags[i];
if(typeof(tag)=='object') {
let key = tag.getAttribute('key');
fields[key] = tag.innerHTML;
}
};
console.log({fields:fields});
return fields
}
function process_template(template, fields) {
let raw = new XMLSerializer().serializeToString(template);
let processed_text = raw;
for(let key in fields) {
console.log(key);
search_for = '{{' + key + '}}';
processed_text = processed_text.replaceAll(search_for, fields[key]);
}
let renderized = processed_text;
return renderized
}
load_doc(page_path).then((page) => {
template_path = get_template(page);
console.debug('template_path:', template_path);
load_doc(template_path).then((template) => {
let fields = get_fields(page);
let renderized = process_template(template, fields);
console.log(renderized);
document.documentElement.innerHTML = renderized;
});
})