Make nunjucks rendering and jinja2 rendering the same

This commit is contained in:
Peder Bergebakken Sundt 2019-10-06 22:13:01 +02:00
parent fccf2d214d
commit a31e50eab1
5 changed files with 103 additions and 35 deletions

View File

@ -1,37 +1,45 @@
CARDS := $(wildcard cards/*.yaml)
CARDS_DESTS := $(patsubst cards/%.yaml,cards/build/%.html,$(CARDS))
CARDS_YAML := $(wildcard cards/*.yaml)
CARDS_XML := $(wildcard cards/*.xml)
CARDS := $(CARDS_YAML) $(CARDS_XML)
CARDS_DESTS_YAML := $(patsubst cards/%.yaml,cards/build/%.html,$(CARDS))
CARDS_DESTS_XML := $(patsubst cards/%.xml,cards/build/%.x.html,$(CARDS))
CARDS_DESTS := $(CARDS_DESTS_YAML) $(CARDS_DESTS_XML)
.PHONY: all
all: build/test_card.html build/test_card_rendered.html $(CARDS_DESTS) cards/build/all.html
all: build/test_card.html build/test_card_rendered.html cards/build/all.html
.PHONY: cards
cards: $(CARDS_DESTS) cards/build/all.html
.PHONY: style
style: build/style.xsl
.PHONY: dev
dev:
git ls-files | entr bash -c "make build/test_card.html build/test_card_rendered.html"
git ls-files | entr bash -c "make all"
.PHONY: dev_all
dev_all:
git ls-files | entr bash -c "make all"
dev_cards:
git ls-files | entr bash -c "make cards"
.PHONY: clean
clean:
rm -v build/* cards/build/*
build/test_card.html: build/card.xml build/style.xsl
build/test_card.html: build/test_card.xml build/style.xsl
xsltproc \
-o build/test_card.html \
build/style.xsl \
build/card.xml
build/test_card.xml
define PYTHON_MAKE_JINJA2_RENDERED_CARD
from jinja2 import Environment, FileSystemLoader
from markdown import markdown
import yaml, sys, glob
import xmltodict
with open("build/style.css") as f: css_data = f.read()
with open(sys.argv[2], "w") as f:
e = Environment(trim_blocks=True, lstrip_blocks=True, loader=FileSystemLoader(''))
@ -41,24 +49,47 @@ with open(sys.argv[2], "w") as f:
"any":any,
"all":all,
"cull_whitespace":(lambda x: " ".join(x.split())) })
for filename in ([sys.argv[1]] if sys.argv[1] != "ALL" else sorted(glob.glob("cards/*.yaml"))):
for filename in ([sys.argv[1]] if sys.argv[1] != "ALL" else sorted(glob.glob("cards/*.yaml") + glob.glob("cards/*.xml"))):
if sys.argv[1] == "ALL": f.write(f"\n\n<h1>{filename}</h1>\n")
with open(filename) as f2: data = f2.read()
with open(filename) as f2:
if filename.endswith("yaml"):
yaml_data = yaml.load(f2.read())
xml_data = {}
elif filename.endswith("xml"):
xml = xmltodict.parse(f2.read())
xml_data = xml.get("ability_card", {})
yaml_data = yaml.load(xml_data.get("yaml_data", ""))
if xml_data.get("image"):
yaml_data["figures"] = [{
'name': xml_data["image"] if (xml_data["image"][:4] == "http") else xml_data["image"].split(":", 1)[1],
'type': "url" if (xml_data["image"][:4] == "http") else xml_data["image"].split(":", 1)[0],
'scale': 1.0,
'flip_x': False,
'flip_y': False,
'offset': [0.0, 0.0],
'opacity': 1,
'rotation': 0,
}] + yaml_data.get("figures", [])
try:
data = e.get_template('style.html.j2').render(card=yaml.load(data), xml={})
data = e.get_template('style.html.j2').render(card=yaml_data, xml=xml_data)
except Exception as ex:
data = f"<pre style=\"color:red;\">{ex}</pre>"
f.write("\n" + data)
endef
export PYTHON_MAKE_JINJA2_RENDERED_CARD
build/test_card_rendered.html: test_card.yaml style.html.j2 build/style.css templates/card_header.html.j2
python3 -c "$$PYTHON_MAKE_JINJA2_RENDERED_CARD" "test_card.yaml" "build/test_card_rendered.html"
build/test_card_rendered.html: build/test_card.xml style.html.j2 build/style.css templates/card_header.html.j2
python3 -c "$$PYTHON_MAKE_JINJA2_RENDERED_CARD" "build/test_card.xml" "build/test_card_rendered.html"
export PYTHON_MAKE_JINJA2_RENDERED_CARD
cards/build/%.html: cards/%.yaml style.html.j2 build/style.css templates/card_header.html.j2
python3 -c "$$PYTHON_MAKE_JINJA2_RENDERED_CARD" $< $@
export PYTHON_MAKE_JINJA2_RENDERED_CARD
cards/build/%.x.html: cards/%.xml style.html.j2 build/style.css templates/card_header.html.j2
python3 -c "$$PYTHON_MAKE_JINJA2_RENDERED_CARD" $< $@
export PYTHON_MAKE_JINJA2_RENDERED_CARD
cards/build/all.html: $(CARDS) style.html.j2 build/style.css templates/card_header.html.j2
python3 -c "$$PYTHON_MAKE_JINJA2_RENDERED_CARD" ALL $@
@ -68,14 +99,14 @@ define PYTHON_MAKE_CARD_XML
from jinja2 import Environment, FileSystemLoader
with open("test_card.yaml") as f: data = f.read()
with open("test_card.xml") as f: xml_data = f.read()
with open("build/card.xml", "w") as f:
with open("build/test_card.xml", "w") as f:
f.write(Environment(
loader=FileSystemLoader('templates'))
.get_template('card.xml.j2')
.render(data=data, xml_data=xml_data))
endef
export PYTHON_MAKE_CARD_XML
build/card.xml: test_card.yaml test_card.xml templates/card.xml.j2
build/test_card.xml: test_card.yaml test_card.xml templates/card.xml.j2
python3 -c "$$PYTHON_MAKE_CARD_XML"

31
cards/100.xml Normal file
View File

@ -0,0 +1,31 @@
<ability_card>
<yaml_data>
style: white
</yaml_data>
<name>Thundering applause</name>
<description>spis meg huehuehue</description>
<image>img:piuy/chicken</image>
<cp></cp>
<range></range>
<duration></duration>
<power></power>
<difficulty></difficulty>
<symbol></symbol>
<playcost></playcost>
<component x="2" y="2" db_entry="🧩 Set Ability Name"/>
<component x="2" y="3" db_entry="🧩 Set Description"/>
<component x="4" y="2" db_entry="🧩 Set Image"/>
<component x="3" y="1" db_entry="🧩 CP Cost"/>
<component x="2" y="1" db_entry="🧩 Range"/>
<component x="4" y="1" db_entry="🧩 Duration"/>
<component x="5" y="1" db_entry="🧩 Power"/>
<component x="6" y="1" db_entry="🧩 Difficulty"/>
<component x="6" y="2" db_entry="🧩 Symbols"/>
<component x="7" y="1" db_entry="🧩 Play Cost"/>
<component x="5" y="3" db_entry="⚔️ Learn Ability"/>
</ability_card>

View File

@ -85,7 +85,7 @@
<div class="fjomp_card{{" " + card.style if card.style}}">
<header>
{{ card.title }}
{{ card.title or xml.name }}
</header>
{% if card.icon %}
<div class="icon">
@ -97,6 +97,10 @@
{{ named_icon_to_emoji(card.icon) }}
{% endif %}
</div>
{% elif xml.symbol %}
<div class="icon">
{{ xml.symbol }}
</div>
{% endif %}
<figure>
{% for figure in card.figures %}
@ -167,6 +171,8 @@
{% endif %}
{% if card.description %}
<center>{{ card.description | markdown | safe }}</center>
{% elif xml.description %}
<center>{{ xml.description | markdown | safe }}</center>
{% endif %}
{% if card.steps %}
<ul>

View File

@ -17,23 +17,23 @@ 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,
}];
// set the figure by the
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,
}].concat(context.card.figures);
}
var env = new nunjucks.Environment([], {
autoescape: true,
@ -64,5 +64,5 @@ try {
} 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);
//console.log(rendered);
document.write(rendered);

View File

@ -2,8 +2,8 @@
{% if is_local %}
<meta http-equiv="refresh" content="1.0">
{#
<meta http-equiv="refresh" content="1.0">
<script type="text/javascript" src="http://livejs.com/live.js"></script>
#}
{% endif %}