From 8f2704506c22e8dc3b4cc8590d972897370091b1 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 04 Mar 2010 13:29:39 +0000 Subject: clean up of save as HTML code --- diff --git a/NEWS b/NEWS index f3adb9d..210bd83 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,7 @@ o more complete support in non-Sugar environments o new (and improved) sample code o Logo code for project added to View Source + o save SVG block lets you generate SVG with Turtle Art * completed a major refactoring of the code o 90% smaller download bundle-size diff --git a/TurtleArtActivity.py b/TurtleArtActivity.py index b3b825a..e7f3387 100644 --- a/TurtleArtActivity.py +++ b/TurtleArtActivity.py @@ -1,6 +1,6 @@ #Copyright (c) 2007, Playful Invention Company -#Copyright (c) 2008-9, Walter Bender -#Copyright (c) 2009, Raul Gutierrez Segales +#Copyright (c) 2008-10, Walter Bender +#Copyright (c) 2009,10 Raul Gutierrez Segales #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal @@ -98,11 +98,11 @@ class TurtleArtActivity(activity.Activity): # write html out to datastore self.save_as_html.set_icon("htmlon") _logger.debug("saving html code") - # until we expose the option to choose, always embed images + # until we have URLs for datastore objects, always embed images embed_flag = True # grab code from stacks - html = save_html(self,self.tw,embed_flag) + html = save_html(self,self.tw, embed_flag) if len(html) == 0: return diff --git a/taexporthtml.py b/taexporthtml.py index e65c423..10fcb1f 100644 --- a/taexporthtml.py +++ b/taexporthtml.py @@ -18,8 +18,11 @@ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. -from sugar.activity import activity -from sugar.datastore import datastore +try: + from sugar.activity import activity + from sugar.datastore import datastore +except ImportError: + pass import os.path from talogo import get_pixbuf_from_journal from tautils import data_to_string, save_picture, image_to_base64, get_path @@ -29,41 +32,46 @@ def save_html(self, tw, embed_flag=True): self.embed_images = embed_flag - # dictionary defines the html wrappers around template elements - # start of block, end of block + # A dictionary to define the HTML wrappers around template elements self.html_glue = { - 'doctype': ("\n", ""), + 'doctype': "\n", 'html': ("\n", "\n"), - 'head': ("\n\n", \ - "\n"), - 'meta': ("\n", ""), + 'head': ("\n\n", "\n"), + 'meta': "\n", 'title': ("", "\n"), - 'style': ("\n"), + 'style': ("\n"), 'body': ("\n", "\n\n"), 'div': ("
\n", "
\n"), 'slide': ("\n\n"), 'h1': ("

", "

\n"), 'table': ("\n", "
\n"), 'tr': ("\n", "\n"), - 'td': ("\n", \ - "\n\n"), - 'img': ("\"Image\"\n"), + 'td': ("\n", + "\n\n"), + 'img': ("\"Image\"\n"), 'img2': ("\"Image\"\n"), 'ul': ("\n", "
\n"), 'li': ("", "\n") } if self.embed_images == True: - # store images in-line as base64 - self.html_glue['img'] = ("\"Image\"\n") - self.html_glue['img2'] = ("\"Image\"\n") + self.html_glue['img'] = ("+\
+                                 \n") + self.html_glue['img2'] = ("\"Image\"\n") + """ + Transalate 'show' and 'template' blocks into HTML, ignoring most + turtle graphics. Saving as SVG will capture all of the graphical elements + of a page. + """ bs = tw.just_blocks() code = "" - self.imagecount = 0 # incremented for each image - slidecount = 0 # incremented for each template (slide) + self.imagecount = 0 + slidecount = 0 for b in bs: this_stack = "" data = walk_stack(self, tw, b) @@ -73,27 +81,26 @@ charset=UTF-8\">\n", ""), if type(d) == type((1,2)): (d,b) = d if type(d) is float or type(d) is int: - d = str(d) # convert floats and ints to strings - # transalate some Turtle Art blocks into HTML - # show and template blocks - # ignores most turtle graphics - if d == "show": # show bricks take one argument + d = str(d) + if d == "show" or d == "showaligned": show = 1 - elif d == "container": # containers hold a media block - show = 2 - elif show > 0: # process the argument to show or container - if show == 1: # could be media or a string - if d[0:8] == '#smedia_': # show media + elif show > 0: # Process the argument to show or container, + if show == 1: # which could be media or a string + if d[0:8] == '#smedia_': this_stack += add_image(self, tw, d) - elif d[0:8] == '#sdescr_': # show description + elif d[0:8] == '#sdescr_': this_stack += add_description(self, tw, d) - elif d[0:2] == '#s': # show a string + elif d[0:2] == '#s': this_stack += d[2:] show = 0 else: this_stack += d show = 0 - # process slide templates + + """ + The following mess is in order to process 'template' blocks, + a depreciated block type. + """ elif d == "t1x1": tp1 = 1 elif d == "t2x1": @@ -106,8 +113,8 @@ charset=UTF-8\">\n", ""), tp6 = 1 elif d == "t2x2": tp7 = 1 - elif tp3 > 0: # bullets - if tp3 == 8: # title comes first + elif tp3 > 0: # Bullets + if tp3 == 8: # The title comes first... tmp = self.html_glue['slide'][0] + \ str(slidecount) + \ self.html_glue['slide'][1] + \ @@ -118,7 +125,7 @@ charset=UTF-8\">\n", ""), self.html_glue['ul'][0] bullets = 6 slidecount += 1 - elif d[2:] != "": # process bullets + elif d[2:] != "": # then the bullets tmp = self.html_glue['li'][0] + d[2:] + \ self.html_glue['li'][1] this_stack += tmp @@ -129,7 +136,7 @@ charset=UTF-8\">\n", ""), self.html_glue['div'][1]) elif tp1 == 1 or tp2 == 1 or tp8 == 1 or\ tp6 == 1 or tp7 == 1: - # first time through, process title + # The first time through, process the title this_stack += (self.html_glue['slide'][0] + \ str(slidecount) + \ self.html_glue['slide'][1] + \ @@ -241,33 +248,38 @@ charset=UTF-8\">\n", ""), this_stack += (tmp + self.html_glue['table'][1] + \ self.html_glue['div'][1]) tp7 = 0 + """ + End of depreciated-block section + """ if len(data) > 0: code += this_stack """ - if no show or template blocks were present, we've got no slides, - so save a screendump instead + If no show or template blocks were present then we've got nothing, + so save a screen dump and project code instead. """ if slidecount == 0: if self.embed_images == True: imgdata = image_to_base64(save_picture(self.tw.canvas), tw.activity) else: - imgdata = os.path.join(self.load_save_folder, 'image.png') - self.tw.save_as_image(filepath) + imgdata = os.path.join(self.tw.load_save_folder, 'image') + self.tw.save_as_image(imgdata) code += (self.html_glue['img'][0] + imgdata + \ self.html_glue['img'][1]) code += self.html_glue['div'][0] - # get a json dump of the code code += data_to_string(tw.assemble_data_to_save(False, True)) code += self.html_glue['div'][1] - - code = self.html_glue['doctype'][0] + \ + if tw.running_sugar: + title = _("Turtle Art") + " " + tw.activity.metadata['title'] + else: + title = _("Turtle Art") + code = self.html_glue['doctype'] + \ self.html_glue['html'][0] + \ self.html_glue['head'][0] + \ - self.html_glue['meta'][0] + \ + self.html_glue['meta'] + \ self.html_glue['title'][0] + \ - _("Turtle Art") + " " + tw.activity.metadata['title'] + \ + title + \ self.html_glue['title'][1] + \ self.html_glue['style'][0] + \ self.html_glue['style'][1] + \ @@ -281,10 +293,8 @@ charset=UTF-8\">\n", ""), def walk_stack(self, tw, blk): top = tw.find_top_block(blk) if blk == top: - # only walk the stack if the block is the top block return tw.lc.run_blocks(top, tw.block_list.list, False) else: - # not top of stack, then return empty list return [] def add_image(self, tw, d): @@ -313,7 +323,6 @@ def add_image(self, tw, d): return "" def add_description(self, tw, d): - # show description if d[8:] != "None": try: dsobject = datastore.get(d[8:]) diff --git a/turtleart.py b/turtleart.py index 1de9d04..80a8494 100755 --- a/turtleart.py +++ b/turtleart.py @@ -29,8 +29,10 @@ import os.path import locale from gettext import gettext as _ from taconstants import OVERLAY_LAYER -from tautils import data_to_string, data_from_string +from tautils import data_to_string, data_from_string, get_save_name + from tawindow import TurtleArtWindow +from taexporthtml import save_html """ Make a path if it doesn't previously exist @@ -86,10 +88,14 @@ class TurtleMain(): menu.append(menu_items) menu_items.connect("activate", self._do_save_cb) menu_items.show() - menu_items = gtk.MenuItem(_("Save Picture")) + menu_items = gtk.MenuItem(_("Save as image")) menu.append(menu_items) menu_items.connect("activate", self._do_save_picture_cb) menu_items.show() + menu_items = gtk.MenuItem(_("Save as HTML")) + menu.append(menu_items) + menu_items.connect("activate", self._do_save_html_cb) + menu_items.show() activity_menu = gtk.MenuItem(_("File")) activity_menu.show() @@ -219,6 +225,16 @@ class TurtleMain(): def _do_save_picture_cb(self, widget): self.tw.save_as_image() + def _do_save_html_cb(self, widget): + html = save_html(self, self.tw, False) + if len(html) == 0: + return + filename, self.tw.load_save_folder = get_save_name('.html', + self.tw.load_save_folder, 'portfolio') + f = file(filename, "w") + f.write(html) + f.close() + def _do_resize_cb(self, widget, factor): if factor == -1: self.tw.block_scale = 2.0 -- cgit v0.9.1