Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 8f2704506c22e8dc3b4cc8590d972897370091b1

clean up of save as HTML code
NEWS
(1 / 0)
  
1919 o more complete support in non-Sugar environments
2020 o new (and improved) sample code
2121 o Logo code for project added to View Source
22 o save SVG block lets you generate SVG with Turtle Art
2223
2324* completed a major refactoring of the code
2425 o 90% smaller download bundle-size
  
11#Copyright (c) 2007, Playful Invention Company
2#Copyright (c) 2008-9, Walter Bender
3#Copyright (c) 2009, Raul Gutierrez Segales
2#Copyright (c) 2008-10, Walter Bender
3#Copyright (c) 2009,10 Raul Gutierrez Segales
44
55#Permission is hereby granted, free of charge, to any person obtaining a copy
66#of this software and associated documentation files (the "Software"), to deal
9898 # write html out to datastore
9999 self.save_as_html.set_icon("htmlon")
100100 _logger.debug("saving html code")
101 # until we expose the option to choose, always embed images
101 # until we have URLs for datastore objects, always embed images
102102 embed_flag = True
103103
104104 # grab code from stacks
105 html = save_html(self,self.tw,embed_flag)
105 html = save_html(self,self.tw, embed_flag)
106106 if len(html) == 0:
107107 return
108108
  
1818#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919#THE SOFTWARE.
2020
21from sugar.activity import activity
22from sugar.datastore import datastore
21try:
22 from sugar.activity import activity
23 from sugar.datastore import datastore
24except ImportError:
25 pass
2326import os.path
2427from talogo import get_pixbuf_from_journal
2528from tautils import data_to_string, save_picture, image_to_base64, get_path
3232
3333 self.embed_images = embed_flag
3434
35 # dictionary defines the html wrappers around template elements
36 # start of block, end of block
35 # A dictionary to define the HTML wrappers around template elements
3736 self.html_glue = {
38 'doctype': ("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 \
39Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n", ""),
37 'doctype': "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "+\
38 "Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n",
4039 'html': ("<html>\n", "</html>\n"),
41 'head': ("<head>\n<!-- Created by Turtle Art -->\n", \
42 "</head>\n"),
43 'meta': ("<meta http-equiv=\"content-type\" content=\"text/html; \
44charset=UTF-8\">\n", ""),
40 'head': ("<head>\n<!-- Created by Turtle Art -->\n", "</head>\n"),
41 'meta': "<meta http-equiv=\"content-type\" content=\"text/html; "+\
42 "charset=UTF-8\">\n",
4543 'title': ("<title>", "</title>\n"),
46 'style': ("<style type=\"text/css\">\n<!--\n","-->\n</style>\n"),
44 'style': ("<style type=\"text/css\">\n<!--\n", "-->\n</style>\n"),
4745 'body': ("<body>\n", "\n</body>\n"),
4846 'div': ("<div>\n", "</div>\n"),
4947 'slide': ("\n<a name=\"slide", "\"></a>\n"),
5048 'h1': ("<h1>", "</h1>\n"),
5149 'table': ("<table cellpadding=\"10\">\n", "</table>\n"),
5250 'tr': ("<tr>\n", "</tr>\n"),
53 'td': ("<td valign=\"top\" width=\"400\" height=\"300\">\n", \
54 "\n</td>\n"),
55 'img': ("<img width=\"400\" height=\"300\" alt=\"Image\" src=\"image",\
56 ".png\" />\n"),
51 'td': ("<td valign=\"top\" width=\"400\" height=\"300\">\n",
52 "\n</td>\n"),
53 'img': ("<img width=\"400\" height=\"300\" alt=\"Image\" "+\
54 "src=\"file://", ".png\" />\n"),
5755 'img2': ("<img alt=\"Image\" src=\"image", ".png\" />\n"),
5856 'ul': ("<table>\n", "</table>\n"),
5957 'li': ("<tr><td>", "</td></tr>\n") }
6058
6159 if self.embed_images == True:
62 # store images in-line as base64
63 self.html_glue['img'] = ("<img width=\"400\" height=\"300\" alt=\"Image\" src=\"data:image/png;base64,\n", " \"/>\n")
64 self.html_glue['img2'] = ("<img alt=\"Image\" src=\"data:image/png;base64,\n", " \"/>\n")
60 self.html_glue['img'] = ("<img width=\"400\" height=\"300\" alt="+\
61 "\"Image\" src=\"data:image/png;base64,\n",
62 " \"/>\n")
63 self.html_glue['img2'] = ("<img alt=\"Image\" src=\"data:image/png;"+\
64 "base64,\n", " \"/>\n")
6565
66 """
67 Transalate 'show' and 'template' blocks into HTML, ignoring most
68 turtle graphics. Saving as SVG will capture all of the graphical elements
69 of a page.
70 """
6671 bs = tw.just_blocks()
6772 code = ""
68 self.imagecount = 0 # incremented for each image
69 slidecount = 0 # incremented for each template (slide)
73 self.imagecount = 0
74 slidecount = 0
7075 for b in bs:
7176 this_stack = ""
7277 data = walk_stack(self, tw, b)
8181 if type(d) == type((1,2)):
8282 (d,b) = d
8383 if type(d) is float or type(d) is int:
84 d = str(d) # convert floats and ints to strings
85 # transalate some Turtle Art blocks into HTML
86 # show and template blocks
87 # ignores most turtle graphics
88 if d == "show": # show bricks take one argument
84 d = str(d)
85 if d == "show" or d == "showaligned":
8986 show = 1
90 elif d == "container": # containers hold a media block
91 show = 2
92 elif show > 0: # process the argument to show or container
93 if show == 1: # could be media or a string
94 if d[0:8] == '#smedia_': # show media
87 elif show > 0: # Process the argument to show or container,
88 if show == 1: # which could be media or a string
89 if d[0:8] == '#smedia_':
9590 this_stack += add_image(self, tw, d)
96 elif d[0:8] == '#sdescr_': # show description
91 elif d[0:8] == '#sdescr_':
9792 this_stack += add_description(self, tw, d)
98 elif d[0:2] == '#s': # show a string
93 elif d[0:2] == '#s':
9994 this_stack += d[2:]
10095 show = 0
10196 else:
10297 this_stack += d
10398 show = 0
104 # process slide templates
99
100 """
101 The following mess is in order to process 'template' blocks,
102 a depreciated block type.
103 """
105104 elif d == "t1x1":
106105 tp1 = 1
107106 elif d == "t2x1":
113113 tp6 = 1
114114 elif d == "t2x2":
115115 tp7 = 1
116 elif tp3 > 0: # bullets
117 if tp3 == 8: # title comes first
116 elif tp3 > 0: # Bullets
117 if tp3 == 8: # The title comes first...
118118 tmp = self.html_glue['slide'][0] + \
119119 str(slidecount) + \
120120 self.html_glue['slide'][1] + \
125125 self.html_glue['ul'][0]
126126 bullets = 6
127127 slidecount += 1
128 elif d[2:] != "": # process bullets
128 elif d[2:] != "": # then the bullets
129129 tmp = self.html_glue['li'][0] + d[2:] + \
130130 self.html_glue['li'][1]
131131 this_stack += tmp
136136 self.html_glue['div'][1])
137137 elif tp1 == 1 or tp2 == 1 or tp8 == 1 or\
138138 tp6 == 1 or tp7 == 1:
139 # first time through, process title
139 # The first time through, process the title
140140 this_stack += (self.html_glue['slide'][0] + \
141141 str(slidecount) + \
142142 self.html_glue['slide'][1] + \
248248 this_stack += (tmp + self.html_glue['table'][1] + \
249249 self.html_glue['div'][1])
250250 tp7 = 0
251 """
252 End of depreciated-block section
253 """
251254
252255 if len(data) > 0:
253256 code += this_stack
254257
255258 """
256 if no show or template blocks were present, we've got no slides,
257 so save a screendump instead
259 If no show or template blocks were present then we've got nothing,
260 so save a screen dump and project code instead.
258261 """
259262 if slidecount == 0:
260263 if self.embed_images == True:
261264 imgdata = image_to_base64(save_picture(self.tw.canvas), tw.activity)
262265 else:
263 imgdata = os.path.join(self.load_save_folder, 'image.png')
264 self.tw.save_as_image(filepath)
266 imgdata = os.path.join(self.tw.load_save_folder, 'image')
267 self.tw.save_as_image(imgdata)
265268 code += (self.html_glue['img'][0] + imgdata + \
266269 self.html_glue['img'][1])
267270 code += self.html_glue['div'][0]
268 # get a json dump of the code
269271 code += data_to_string(tw.assemble_data_to_save(False, True))
270272 code += self.html_glue['div'][1]
271
272 code = self.html_glue['doctype'][0] + \
273 if tw.running_sugar:
274 title = _("Turtle Art") + " " + tw.activity.metadata['title']
275 else:
276 title = _("Turtle Art")
277 code = self.html_glue['doctype'] + \
273278 self.html_glue['html'][0] + \
274279 self.html_glue['head'][0] + \
275 self.html_glue['meta'][0] + \
280 self.html_glue['meta'] + \
276281 self.html_glue['title'][0] + \
277 _("Turtle Art") + " " + tw.activity.metadata['title'] + \
282 title + \
278283 self.html_glue['title'][1] + \
279284 self.html_glue['style'][0] + \
280285 self.html_glue['style'][1] + \
293293def walk_stack(self, tw, blk):
294294 top = tw.find_top_block(blk)
295295 if blk == top:
296 # only walk the stack if the block is the top block
297296 return tw.lc.run_blocks(top, tw.block_list.list, False)
298297 else:
299 # not top of stack, then return empty list
300298 return []
301299
302300def add_image(self, tw, d):
323323 return ""
324324
325325def add_description(self, tw, d):
326 # show description
327326 if d[8:] != "None":
328327 try:
329328 dsobject = datastore.get(d[8:])
turtleart.py
(18 / 2)
  
2929import locale
3030from gettext import gettext as _
3131from taconstants import OVERLAY_LAYER
32from tautils import data_to_string, data_from_string
32from tautils import data_to_string, data_from_string, get_save_name
33
3334from tawindow import TurtleArtWindow
35from taexporthtml import save_html
3436
3537"""
3638Make a path if it doesn't previously exist
8888 menu.append(menu_items)
8989 menu_items.connect("activate", self._do_save_cb)
9090 menu_items.show()
91 menu_items = gtk.MenuItem(_("Save Picture"))
91 menu_items = gtk.MenuItem(_("Save as image"))
9292 menu.append(menu_items)
9393 menu_items.connect("activate", self._do_save_picture_cb)
9494 menu_items.show()
95 menu_items = gtk.MenuItem(_("Save as HTML"))
96 menu.append(menu_items)
97 menu_items.connect("activate", self._do_save_html_cb)
98 menu_items.show()
9599
96100 activity_menu = gtk.MenuItem(_("File"))
97101 activity_menu.show()
224224
225225 def _do_save_picture_cb(self, widget):
226226 self.tw.save_as_image()
227
228 def _do_save_html_cb(self, widget):
229 html = save_html(self, self.tw, False)
230 if len(html) == 0:
231 return
232 filename, self.tw.load_save_folder = get_save_name('.html',
233 self.tw.load_save_folder, 'portfolio')
234 f = file(filename, "w")
235 f.write(html)
236 f.close()
227237
228238 def _do_resize_cb(self, widget, factor):
229239 if factor == -1: