RPGs3_card_insanity/style.js

69 lines
2.2 KiB
JavaScript

var md = window.markdownit();
var context = {
'xml' : {},
'card' : jsyaml.load(document.getElementById("yaml_data").innerHTML),
'alert' : alert,
};
// 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;
}
function is_set(asd) {
return !(typeof asd === 'undefined' || asd.length === 0);
}
if (!is_set(context.card.title) && context.xml.name ) context.card.title = context.xml.name;
if (!is_set(context.card.icon) && context.xml.symbol ) context.card.icon = context.xml.symbol;
if (!is_set(context.card.description) && context.xml.description) context.card.description = context.xml.description;
if (!is_set(context.card.figures) && context.xml.image ) context.card.figures = [{
'name': (context.xml.image.substr(0, 4) == "http")
? context.xml.image
: context.xml.image.split(":").slice(1).join(":"),
'type': (context.xml.image.substr(0, 4) == "http")
? "url"
: context.xml.image.split(":")[0],
'scale': 1.0,
'flip_x': false,
'flip_y': false,
'offset': [0.0, 0.0],
'opacity': 1,
'rotation': 0,
}];
var env = new nunjucks.Environment([], {
autoescape: true,
trimBlocks: true,
lstripBlocks: true,
});
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;
});
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);
document.write(rendered);