2021-11-21 00:22:06 +01:00
|
|
|
import imgkit
|
|
|
|
import sys
|
2021-11-21 02:26:45 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
from threading import Thread
|
2021-11-21 00:22:06 +01:00
|
|
|
|
|
|
|
from libsixel.encoder import Encoder, SIXEL_OPTFLAG_WIDTH, SIXEL_OPTFLAG_COLORS
|
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
HTML_WIDTH = 720
|
|
|
|
|
2021-11-21 02:26:45 +01:00
|
|
|
def preview(html, width, height, img, htmlWidth=HTML_WIDTH):
|
2021-11-21 00:22:06 +01:00
|
|
|
options = {
|
|
|
|
'encoding': 'utf-8',
|
|
|
|
'width': htmlWidth,
|
|
|
|
'height': int(htmlWidth/width*height),
|
|
|
|
'quiet': None,
|
2021-11-21 02:26:45 +01:00
|
|
|
'enable-local-file-access': None
|
2021-11-21 00:22:06 +01:00
|
|
|
}
|
2021-11-21 02:26:45 +01:00
|
|
|
#with NamedTemporaryFile(suffix='.png') as img:
|
|
|
|
def render():
|
2021-11-21 00:22:06 +01:00
|
|
|
with NamedTemporaryFile(suffix='.html') as page:
|
|
|
|
page.write(bytes(html, encoding='utf-8'))
|
2021-11-21 02:26:45 +01:00
|
|
|
with open('test.html', 'w') as test:
|
|
|
|
test.write(html)
|
2021-11-21 00:22:06 +01:00
|
|
|
page.flush()
|
|
|
|
imgkit.from_file(page.name, img.name, options=options)
|
2021-11-21 02:26:45 +01:00
|
|
|
th = Thread(target=render)
|
|
|
|
th.start()
|
|
|
|
#render()
|
|
|
|
|
|
|
|
def update(width, img):
|
|
|
|
sys.stdout.write('\x1b[1;1H')
|
|
|
|
sys.stdout.flush()
|
|
|
|
encoder = Encoder()
|
|
|
|
encoder.setopt(SIXEL_OPTFLAG_WIDTH, str(width))
|
|
|
|
encoder.setopt(SIXEL_OPTFLAG_COLORS, '256')
|
|
|
|
encoder.encode(img.name)
|
2021-11-21 00:22:06 +01:00
|
|
|
|
2021-11-21 02:26:45 +01:00
|
|
|
def previewCard(template, style, content, width, height, img, htmlWidth=HTML_WIDTH, frontSide=None):
|
|
|
|
if frontSide:
|
|
|
|
template = template.replace('{{FrontSide}}', frontSide)
|
2021-11-21 00:22:06 +01:00
|
|
|
for field in content:
|
|
|
|
|
|
|
|
# conditional sections
|
|
|
|
formated = ''
|
|
|
|
i = 0
|
|
|
|
remove = False
|
|
|
|
while i < len(template):
|
|
|
|
section = template[i:i + len(field) + 5]
|
|
|
|
if section in ('{{#'+field+'}}', '{{^'+field+'}}'):
|
|
|
|
i += len(field) + 5
|
|
|
|
if (section[2] == '#' and not content[field]) or (section[2] == '^' and content[field]):
|
|
|
|
remove = True
|
|
|
|
if section == '{{/'+field+'}}':
|
|
|
|
i += len(field) + 5
|
|
|
|
if remove:
|
|
|
|
remove = False
|
|
|
|
if not remove and i < len(template):
|
|
|
|
formated += template[i]
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
template = formated.replace('{{'+field+'}}', content[field])
|
|
|
|
|
|
|
|
template = '<style>'+style+'</style>'+template
|
2021-11-21 02:26:45 +01:00
|
|
|
preview(TEMPLATE.replace('{{CONTENT}}', template)
|
|
|
|
.replace('{{WD}}', os.getcwd()),
|
|
|
|
width, height, img, htmlWidth)
|
2021-11-21 00:22:06 +01:00
|
|
|
|
|
|
|
TEMPLATE_FILE = 'template.html'
|
|
|
|
with open(TEMPLATE_FILE) as templateFile:
|
|
|
|
TEMPLATE = templateFile.read()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
preview('<h1>Hello World!</h1>', 500, 500)
|
|
|
|
previewCard('<h1>{{Front}}</h1>',
|
|
|
|
'h1{color:cyan;}',
|
|
|
|
{
|
|
|
|
'Front': 'Hello World!',
|
|
|
|
'Back': 'From Python...'
|
|
|
|
},
|
|
|
|
500, 500)
|