RPGs3_card_insanity/style.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-09-10 00:52:30 +02:00
var md = window.markdownit();
var context = {
2019-10-06 16:03:07 +02:00
'xml' : {},
2019-10-14 21:40:50 +02:00
'yaml' : jsyaml.load(document.getElementById("yaml_data").innerHTML),
2019-09-10 00:52:30 +02:00
'alert' : alert,
};
2019-09-09 19:29:27 +02:00
2019-10-06 16:03:07 +02:00
// read xml data
var xml_data_items = document.getElementsByClassName("xml_data")
for (var i=0; i < xml_data_items.length; i++) {
var key = xml_data_items[i].id.substr(9);
var val = xml_data_items[i].innerHTML;
context.xml[key] = val;
}
2019-10-06 21:43:37 +02:00
var env = new nunjucks.Environment([], {
2019-09-11 05:09:42 +02:00
autoescape: true,
trimBlocks: true,
lstripBlocks: true,
2019-09-10 00:52:30 +02:00
});
env.addFilter('markdown', function(str) {
return md.render(str);
});
env.addFilter('cull_whitespace', function(str) {
return str.split(/\s+/).join(' ').trim();
});
env.addFilter('any', function(iterable) {
for (var index = 0; index < iterable.length; index++) {
if (iterable[index]) return true;
}
return false;
});
env.addFilter('all', function(iterable) {
for (var index = 0; index < iterable.length; index++) {
if (!iterable[index]) return false;
}
return true;
});
2019-10-14 21:40:50 +02:00
env.addFilter('startswith', function(string, match) {
return string.slice(0, match.length) === match;
});
env.addFilter('tail', function(sequence) {
2019-10-15 21:20:37 +02:00
return sequence.slice(1);
});
env.addFilter('split', function(string, delimiter) {
return string.split(delimiter);
});
env.addFilter('is_number', function(string) {
return !isNaN(string);
});
env.addFilter('is_string', function(string) {
return String(string) === string;
2019-10-14 21:40:50 +02:00
});
2019-09-09 19:29:27 +02:00
2019-10-06 21:43:37 +02:00
try {
rendered = env.renderString(jinja_template, context);
} catch(err) {
rendered = "<pre style=\"font-size:0.8em; width:100%; border-radius:5px; padding:2mm; box-sizing: border-box; border: 2px solid black; white-space: normal; background-color:#ff8888;\">" + err + "</pre>";
}
//console.log(rendered);
2019-09-10 17:54:18 +02:00
document.write(rendered);