-
Notifications
You must be signed in to change notification settings - Fork 0
/
datablad.py
109 lines (90 loc) · 4.79 KB
/
datablad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# yattag.py
#
import re
import codecs
from lxml import objectify
from yattag import Doc, indent
def get_products(file_path):
tree = objectify.parse(file_path)
root = tree.getroot()
products = {}
for x in root.findall('g_otemp'):
prodcode = x.prodid.text
products[prodcode] = x
return products
def make_html(prod_dict):
for prodcode, prod in prod_dict.items():
doc, tag, text = Doc().tagtext()
doc.asis('<!DOCTYPE html>')
with tag('html', lang='eng'):
with tag('head'):
doc.asis('<meta charset="UTF-8" />')
doc.asis('<link rel="stylesheet" type="text/css" href="style.css">')
doc.asis('<meta name="viewport" content="width=device-width">')
with tag('title'):
text(prod.prodid.text)
with tag('body', klass='main'):
with tag('header', klass='main-header'):
doc.stag('img', klass='logo', src='logo.jpg', alt='Villa Import AS')
with tag('h1', klass='prodname'):
text(prod.desc.text)
with tag('div', klass='content'):
with tag('div', klass='primary-content group'):
with tag('div', klass='product-picture'):
doc.stag('img', src='http://www.villaimport.no/images/produktbilder/Full%20Size/'+ prod.prodid.text + '.jpg', alt=prod.desc.text)
with tag('div', klass='product-details'):
with tag('div', klass='basic-info'):
with tag('h2'):
text('Produktinformasjon')
with tag('ul', klass='field'):
with tag('li'):
text('Varenummer: ' + prod.prodid.text)
with tag('li'):
text('Produktgruppe: ' + prod.prodgroupex.text)
with tag('li'):
text('Pakning: ' + re.sub('.000', '',
prod.weight.text) + ' ' + prod.weightunit.text)
with tag('div', klass='ingredients'):
with tag('h2'):
text('Ingredienser')
with tag('p', klass='field'):
text(prod.prodnote.text)
with tag('div', klass='secondary-content'):
with tag('div', klass='energy'):
with tag('h2'):
text('Næringsinnehold Pr. 100 gram.')
with tag('ul', klass='field'):
for value in re.split('\n', prod.technote.text):
if value.lstrip() != "":
with tag('li'):
text(value.lstrip())
with tag('div', klass='allergens'):
with tag('h2'):
text('Allergener')
with tag('ul', klass='field'):
for word in set(re.split('[\[\]/{}.,() ]+', prod.prodnote.text)):
if word.isupper():
with tag('li'):
text(word.capitalize())
with tag('div', klass='storage'):
with tag('h2'):
text('Oppbevaring')
with tag('ul', klass='field'):
for word in re.findall(r'[A-Z][^A-Z]*', prod.annenote.text):
with tag('li'):
text(word)
with tag('footer', klass='main-footer'):
with tag('p'):
doc.asis(
'''<a href="www.villaimport.no">www.villaimport.no</a> ◦
<a href="tel:+4723229999">+47 23229999</a> ◦
<a href="mailto:mail@villaimport.no">mail@villaimport.no</a>''')
with tag('p'):
doc.asis('© Villa Import AS - 2016')
with codecs.open(prodcode + '.html', 'w', 'utf-8') as file:
file.write(indent(doc.getvalue()))
xmlfile = input('Select a Catalog File: ');
make_html(get_products(xmlfile))