2
import sys, xml.sax.saxutils, base64
3
from gourmet.gdebug import *
4
from gourmet.gglobals import *
5
from xml_exporter import XmlExporter
7
class rec_to_xml (XmlExporter):
8
"""A vastly simplified recipe XML exporter.
10
The previous XML format was written as a format designed for
11
itself (its design predated gourmet). This one is really written
12
as a simple way to save, load and exchange Gourmet recipes. As a
13
result, the code is much simpler, and the format should be quicker
14
to write and more convenient for exchanging single recipes.
16
This implementation actually uses the DOM to ensure correctness.
19
doc_element = 'gourmetDoc'
23
def write_head (self):
24
self.rec_el = self.create_element_with_attrs("recipe",{'id':self.r.id})
25
self.top_element.appendChild(self.rec_el)
27
def write_attr (self, attr, text):
28
self.rec_el.appendChild(self.create_text_element(attr,text))
30
def write_text (self, attr, text):
31
self.rec_el.appendChild(
32
self.create_text_element(attr,text)
35
def write_image (self, image):
36
image_el = self.create_element_with_attrs('image',{'format':'jpeg'})
38
self.xmlDoc.createCDATASection(base64.b64encode(image))
40
self.rec_el.appendChild(image_el)
42
def handle_italic (self, chunk): return '<i>'+chunk+'</i>'
43
def handle_bold (self, chunk): return '<b>'+chunk+'</b>'
44
def handle_underline (self, chunk): return '<u>'+chunk+'</u>'
46
def write_foot (self):
47
if self.i_created_this_document:
48
self.xmlDoc.writexml(self.ofi, newl = '\n', addindent = "\t",
51
def write_inghead (self):
52
self.inglist_el = self.xmlDoc.createElement('ingredient-list')
53
self.top_inglist = self.inglist_el # because groups will let us nest...
54
self.rec_el.appendChild(self.inglist_el)
56
def write_ingref (self, amount=1, unit=None, item=None, refid=None, optional=False):
57
self.inglist_el.appendChild(
58
self.create_text_element('ingref',
65
def write_ing (self, amount=1, unit=None, item=None, key=None, optional=False):
67
ing_el = self.create_element_with_attrs('ingredient',{'optional':'yes'})
69
ing_el = self.create_element_with_attrs('ingredient',{})
70
self.inglist_el.appendChild(ing_el)
73
self.create_text_element('amount',amount)
77
self.create_text_element('unit',unit)
81
self.create_text_element('item',item)
85
self.create_text_element('key',key)
88
def write_grouphead (self, name):
89
group_el = self.xmlDoc.createElement('inggroup')
91
self.create_text_element('groupname',name)
93
self.inglist_el = group_el
95
def write_groupfoot (self):
96
self.top_inglist.appendChild(self.inglist_el)
97
self.inglist_el = self.top_inglist
100
class recipe_table_to_xml (exporter.ExporterMultirec, XmlExporter):
101
doc_element = 'gourmetDoc'
104
def __init__ (self, rd, recipe_table, out, one_file=True, progress_func=None, change_units=False,
107
exporter.ExporterMultirec.__init__(
108
self, rd, recipe_table, out, one_file=True, ext='xml', exporter=rec_to_xml,
109
progress_func=progress_func,
110
exporter_kwargs={'change_units':change_units,
112
'xmlDoc':self.xmlDoc,
113
# This order is now in our DTD so we'd
114
# better make it solid.
115
'attr_order':('title',
116
'category','cuisine',
119
'preptime','cooktime',
122
'order':['attr','image','ings','text'],
127
def write_footer (self, *args):
128
self.xmlDoc.writexml(self.ofi, newl = '\n', addindent = "\t",
132
return xml.sax.saxutils.quoteattr(xml.sax.saxutils.escape(str))