Add SVG option for icons
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
ul {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
li {
|
||||||
|
list-style-type: none;
|
||||||
|
display: inline-block;
|
||||||
|
width: 10em;
|
||||||
|
height: 10em;
|
||||||
|
padding: 0;
|
||||||
|
margin: 1em 0.3em;
|
||||||
|
overflow: hidden;
|
||||||
|
border: solid 1px black;
|
||||||
|
border-radius: 0.2em;
|
||||||
|
}
|
||||||
|
li:hover {
|
||||||
|
border-color: #888;
|
||||||
|
background-color: #ddf;
|
||||||
|
}
|
||||||
|
|
||||||
|
li h1 {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 1em;
|
||||||
|
line-height: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
display: block;
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 6.8em;
|
||||||
|
width: auto;
|
||||||
|
height: 6.8em;
|
||||||
|
margin: 0 auto;
|
||||||
|
|
||||||
|
/*filter: invert(1) opacity(0.8);*/
|
||||||
|
filter: opacity(0.8);
|
||||||
|
}
|
||||||
|
|
||||||
|
body >h1,
|
||||||
|
form {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<link rel="stylesheet" href="svg.css">
|
||||||
|
|
||||||
|
#macro(form)
|
||||||
|
<form action="svg" method="post" enctype="multipart/form-data">
|
||||||
|
<h3>Upload SVG:</h3>
|
||||||
|
<p>
|
||||||
|
<input type="file" name="file" accept="image/svg+xml"><br/>
|
||||||
|
<input type="text" name="name" placeholder="name">
|
||||||
|
<input type="submit">
|
||||||
|
|
||||||
|
#if($uploaded)
|
||||||
|
<i>Upload was successfull!</i>
|
||||||
|
#end
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
#end
|
||||||
|
|
||||||
|
<a href="../" style="opacity:0.6;">Return to cardlist</a>
|
||||||
|
|
||||||
|
#form()
|
||||||
|
|
||||||
|
<h1>Available SVGs:</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
#foreach($name in $svgs)
|
||||||
|
<li><!--todo: add "copy to clipboard" feature-->
|
||||||
|
<h1>$name</h1>
|
||||||
|
<img src="/svg/${escape_html($name)}.svg">
|
||||||
|
</li>
|
||||||
|
#end
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
#form()
|
||||||
|
|
||||||
|
<a href="../" style="opacity:0.6;">Return to cardlist</a>
|
||||||
@@ -3,6 +3,7 @@ import sys, os, airspeed, glob
|
|||||||
from sanic import Sanic, response
|
from sanic import Sanic, response
|
||||||
from common import withTemplate, withResource
|
from common import withTemplate, withResource
|
||||||
import card
|
import card
|
||||||
|
import svg
|
||||||
import config
|
import config
|
||||||
|
|
||||||
app = Sanic()
|
app = Sanic()
|
||||||
@@ -39,26 +40,53 @@ async def show_cards(request, template={}):
|
|||||||
@app.post('/cards/preview')
|
@app.post('/cards/preview')
|
||||||
@withTemplate("cards/card.vm")
|
@withTemplate("cards/card.vm")
|
||||||
async def preview_card(request, template={}):
|
async def preview_card(request, template={}):
|
||||||
cards = [card.Card()]
|
cards = [card.from_form(request.form)]
|
||||||
|
if "save" in request.form:
|
||||||
for key, val in request.form.items():
|
card.to_file(cards[0])
|
||||||
if not val[0]: continue
|
was_saved = True
|
||||||
if type(getattr(card.Card, key)) in (tuple, list):
|
if "delete" in request.form:
|
||||||
setattr(cards[0], key, val)
|
card.del_file(cards[0])
|
||||||
else:
|
was_deleted = True
|
||||||
setattr(cards[0], key, val[0])
|
|
||||||
|
|
||||||
return response.html(template["card.vm"].merge(locals()))
|
return response.html(template["card.vm"].merge(locals()))
|
||||||
|
|
||||||
#add static files:
|
@app.get("/cards/svg")
|
||||||
|
@withTemplate("cards/svg.vm")
|
||||||
|
async def svg_list(request, template={}):
|
||||||
|
svgs = svg.list_all()
|
||||||
|
svgs.sort()
|
||||||
|
return response.html(template["svg.vm"].merge(locals()))
|
||||||
|
|
||||||
|
@app.post("/cards/svg")
|
||||||
|
@withTemplate("cards/svg.vm")
|
||||||
|
async def svg_add(request, template={}):
|
||||||
|
file = request.files.get("file")
|
||||||
|
name = request.form.get("name") or file.name
|
||||||
|
if name[-4:] == ".svg": name = name[:-4]
|
||||||
|
|
||||||
|
svg.store(name, file.body)
|
||||||
|
|
||||||
|
uploaded = True
|
||||||
|
svgs = svg.list_all()
|
||||||
|
return response.html(template["svg.vm"].merge(locals()))
|
||||||
|
|
||||||
|
#add static resources:
|
||||||
for i in glob.iglob(os.path.join(config.resourcedir, "**","*"), recursive=True):
|
for i in glob.iglob(os.path.join(config.resourcedir, "**","*"), recursive=True):
|
||||||
if i.split(".")[-1] in ("html", "css", "js"):
|
filetype = i.split('.')[-1]
|
||||||
|
if filetype in ("html", "css", "js"):
|
||||||
i = os.path.relpath(i, config.resourcedir)
|
i = os.path.relpath(i, config.resourcedir)
|
||||||
print(i)
|
print("Adding static resource", repr(i))
|
||||||
|
if filetype == "js": filetype = "javascript"
|
||||||
|
|
||||||
@app.get(f"/{i}")
|
@app.get(f"/{i}")
|
||||||
@withResource(i)
|
@withResource(i)
|
||||||
async def card_style(request, file={}):
|
async def card_style(request, file={}):
|
||||||
return response.text(tuple(file.values())[0], headers={"Content-Type": f"text/{i.split('.')[-1]}"})
|
file = tuple(file.values())[0]
|
||||||
|
return response.text(file, headers={"Content-Type": f"text/{filetype}"})
|
||||||
|
|
||||||
|
#add svgs:
|
||||||
|
@app.get(f"/svg/<name>.svg")
|
||||||
|
async def get_svg(request, name):
|
||||||
|
return response.text(svg.get(name), headers={"Content-Type": "image/svg+xml"})
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000)
|
app.run(host="0.0.0.0", port=8000)
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import glob, os
|
||||||
|
import config
|
||||||
|
from common import memoize, listify_output
|
||||||
|
|
||||||
|
@listify_output
|
||||||
|
def list_all():
|
||||||
|
for i in glob.glob(os.path.join(config.svgdir, "*.svg")):
|
||||||
|
yield os.path.basename(i)[:-4]
|
||||||
|
|
||||||
|
|
||||||
|
@memoize
|
||||||
|
def get(name):
|
||||||
|
with open(os.path.join(config.svgdir, f"{name}.svg"), "r") as f:
|
||||||
|
return f.read()
|
||||||
|
|
||||||
|
def store(name, data):
|
||||||
|
with open(os.path.join(config.svgdir, f"{name}.svg"), "wb" if type(data) is bytes else "w") as f:
|
||||||
|
return f.write(data)
|
||||||
Reference in New Issue
Block a user