Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/taexporthtml.py
blob: 2dac7e6f7484d5c265c299377911261709ed37ea (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#Copyright (c) 2008-9, Walter Bender

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import pygtk
pygtk.require('2.0')
import gtk
import os.path
from tautils import data_to_string, save_picture, image_to_base64, get_path
from gettext import gettext as _
from cgi import escape


def save_html(self, tw, embed_flag=True):
    """ Either: Save canvas and code or pictures to HTML """
    self.embed_images = embed_flag

    # A dictionary to define the HTML wrappers around template elements
    self.html_glue = {
        'doctype': '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ' + \
             'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n',
        'html': ('<html>\n", "</html>\n'),
        'html_svg': ('<html xmlns="http://www.w3.org/1999/xhtml">\n',
                     '</html>\n'),
        'head': ('<head>\n<!-- Created by Turtle Art -->\n', '</head>\n'),
        'meta': '<meta http-equiv="content-type" content="text/html; ' + \
            'charset=UTF-8"/>\n',
        'title': ('<title>', '</title>\n'),
        'style': ('<style type="text/css">\n<!--\n', '-->\n</style>\n'),
        'body': ('<body>\n', '\n</body>\n'),
        'div': ('<div>\n', '</div>\n'),
        'slide': ('\n<a name="slide', '"></a>\n'),
        'h1': ('<h1>', '</h1>\n'),
        'table': ('<table cellpadding="10\'>\n', '</table>\n'),
        'tr': ('<tr>\n', '</tr>\n'),
        'td': ('<td valign="top" width="400" height="300">\n',
               '\n</td>\n'),
        'img': ('<img width="400" height="300" alt="Image" ' + \
                'src="file://"', '".png" />\n'),
        'img2': ('<img alt="Image" src="image"', '".png" />\n'),
        'img3': ('<img alt="Image" src="file://"', '"" />\n'),
        'ul': ('<table>\n', '</table>\n'),
        'li': ('<tr><td>', '</td></tr>\n')}

    comment = '<!--\n\<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"' + \
        ' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [\n\
    <!ENTITY ns_svg "http://www.w3.org/2000/svg">\n\
    <!ENTITY ns_xlink "http://www.w3.org/1999/xlink">\n\
]>\n\
-->\n'
    if self.embed_images == True:
        self.html_glue['img'] = ('<img width="400" height="300" alt=" + \
                                 ""Image" src="data:image/png;base64,\n"',
                                 '" "/>\n')
        self.html_glue['img2'] = ('<img alt="Image" src="data:image/png;" + \
                                  "base64,\n"', '" "/>\n')

    """
    If there are saved_pictures, put them into a .html; otherwise, save a
    screendump and the turtle project code.
    """
    code = ''
    if len(tw.saved_pictures) > 0:
        for i, p in enumerate(tw.saved_pictures):
            code += self.html_glue['slide'][0] + str(i)
            code += self.html_glue['slide'][1] + \
                    self.html_glue['div'][0] + \
                    self.html_glue['h1'][0]
            if self.embed_images == True:
                f = open(p, 'r')
                imgdata = f.read()
                f.close()
                if p.endswith(('.svg')):
                    tmp = imgdata
                else:
                    pixbuf = gtk.gdk.pixbuf_new_from_file(p)
                    imgdata = image_to_base64(pixbuf,
                        get_path(tw.activity, 'instance'))
                    tmp = self.html_glue['img2'][0]
                    tmp += imgdata
                    tmp += self.html_glue['img2'][1]
            else:
                if p.endswith(('.svg')):
                    f = open(p, 'r')
                    imgdata = f.read()
                    f.close()
                    tmp = imgdata
                else:
                    tmp = self.html_glue['img3'][0]
                    tmp += p
                    tmp += self.html_glue['img3'][1]
            code += tmp + \
                    self.html_glue['h1'][1] + \
                    self.html_glue['div'][1]
    else:
        if self.embed_images == True:
            imgdata = image_to_base64(save_picture(self.tw.canvas),
                                      get_path(tw.activity, 'instance'))
        else:
            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]
        code += escape(data_to_string(tw.assemble_data_to_save(False, True)))
        code += self.html_glue['div'][1]

    if tw.running_sugar:
        title = _('Turtle Art') + ' ' + tw.activity.metadata['title']
    else:
        title = _('Turtle Art')

    header = self.html_glue['doctype'] + \
            self.html_glue['html'][0]
    style = self.html_glue['style'][0] + \
            self.html_glue['style'][1]
    if len(tw.saved_pictures) > 0:
        if tw.saved_pictures[0].endswith(('.svg')):
            header = self.html_glue['html_svg'][0]
            style = comment

    code = header + \
           self.html_glue['head'][0] + \
           self.html_glue['meta'] + \
           self.html_glue['title'][0] + \
           title + \
           self.html_glue['title'][1] + \
           style + \
           self.html_glue['head'][1] + \
           self.html_glue['body'][0] + \
           code + \
           self.html_glue['body'][1] + \
           self.html_glue['html'][1]
    return code