def test_case2(self): """Test simple attribute.""" data = self.template % '<img alt="The beach" src="beach.jpg" />' doc = XHTMLFile(string=data) messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'The beach'), )])
def test_case1(self): """Test element content.""" data = self.template % '<p>hello little world</p>' doc = XHTMLFile(string=data) messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'hello little world'), )])
def test_case1(self): """Test element content.""" data = self.template % '<p>hello little world</p>' doc = XHTMLFile(string=data) messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'hello little world'),)])
def test_case2(self): """Test simple attribute.""" data = self.template % '<img alt="The beach" src="beach.jpg" />' doc = XHTMLFile(string=data) messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'The beach'),)])
def test_case3(self): """Test complex attribute.""" data = self.template % ('<input type="text" name="id" />\n' '<input type="submit" value="Change" />') doc = XHTMLFile(string=data) messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'Change'),)])
def test_case3(self): """Test complex attribute.""" data = self.template % ('<input type="text" name="id" />\n' '<input type="submit" value="Change" />') doc = XHTMLFile(string=data) messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'Change'), )])
def test_form(self): """Test complex attribute.""" # The document doc = XHTMLFile(string='<form xmlns="http://www.w3.org/1999/xhtml">\n' ' <input type="text" name="id" />\n' ' <input type="submit" value="Change" />\n' '</form>') messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'Change'), )])
def test_inline(self): doc = XHTMLFile(string='<p xmlns="http://www.w3.org/1999/xhtml">' 'Hi <b>everybody, </b><i>how are you ? </i>' '</p>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'Hi '), (START_FORMAT, 1), (TEXT, u'everybody, '), (END_FORMAT, 1), (START_FORMAT, 2), (TEXT, u'how are you ? '), (END_FORMAT, 2))] self.assertEqual(messages, expected)
def test_form(self): """Test complex attribute.""" # The document doc = XHTMLFile(string= '<form xmlns="http://www.w3.org/1999/xhtml">\n' ' <input type="text" name="id" />\n' ' <input type="submit" value="Change" />\n' '</form>') messages = [unit[0] for unit in doc.get_units()] self.assertEqual(messages, [((TEXT, u'Change'),)])
def test_inline(self): doc = XHTMLFile(string= '<p xmlns="http://www.w3.org/1999/xhtml">' 'Hi <b>everybody, </b><i>how are you ? </i>' '</p>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'Hi '), (START_FORMAT, 1), (TEXT, u'everybody, '), (END_FORMAT, 1), (START_FORMAT, 2), (TEXT, u'how are you ? '), (END_FORMAT, 2))] self.assertEqual(messages, expected)
def test_case5(self): """Test translation of an element content""" po = POFile(string= 'msgctxt "img[alt]"\n' 'msgid "The beach"\n' 'msgstr "La playa"') xhtml = XHTMLFile(string= self.template % '<img alt="The beach" src="beach.jpg" />') html = xhtml.translate(po) xhtml = XHTMLFile(string=html) messages = [unit[0] for unit in xhtml.get_units()] self.assertEqual(messages, [((TEXT, u'La playa'),)])
def _make_file(self, name, filename, mimetype, body, default_language): from webpage import WebPage if type(name) is not str: raise TypeError, 'expected string, got %s' % repr(name) # Web Pages are first class citizens if mimetype == 'text/html': body = tidy_html(body) class_id = 'webpage' elif mimetype == 'application/xhtml+xml': class_id = 'webpage' else: class_id = mimetype cls = self.database.get_resource_class(class_id) # Special case: web pages kw = {'filename': filename, 'data': body} if issubclass(cls, WebPage): kk, kk, language = FileName.decode(filename) if language is None: text = XHTMLFile(string=body).to_text() language = guess_language(text) or default_language kw['data'] = {language: body} return self.make_resource(name, cls, **kw)
def test_case4(self): """Test translation of an element content""" string = ( 'msgctxt "paragraph"\n' 'msgid "hello world"\n' 'msgstr "hola mundo"\n') p = POFile(string=string) string = self.template % '<p>hello world</p>' source = XHTMLFile(string=string) string = source.translate(p) xhtml = XHTMLFile(string=string) messages = [unit[0] for unit in xhtml.get_units()] self.assertEqual(messages, [((TEXT, u'hola mundo'),)])
def test_random(self): """Test element content.""" # The document doc = XHTMLFile(string='<body xmlns="http://www.w3.org/1999/xhtml">\n' ' <p>this <em>word</em> is nice</p>\n' ' <a href="/"><img src="logo.png" /></a>\n' ' <p><em>hello world</em></p><br/>' ' bye <em>J. David Ibanez Palomar</em>\n' '</body>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'this '), (START_FORMAT, 1), (TEXT, u'word'), (END_FORMAT, 1), (TEXT, u' is nice')), ((TEXT, u'hello world'), ), ((TEXT, u'bye '), (START_FORMAT, 6), (TEXT, u'J. David Ibanez Palomar'), (END_FORMAT, 6))] self.assertEqual(messages, expected)
def get_namespace(self, resource, context): source = resource.get_resource('/ui/wiki/help.txt') source = source.to_str() overrides = dict(resource.overrides) overrides['stylesheet'] = '' html = publish_string(source, writer_name='html', settings_overrides=overrides) document = XHTMLFile(string=html) events = document.get_body().get_content_elements() # Now remove some magic to make the help work like a wiki page source = source.split('.. XXX SPLIT HERE')[0] return { 'help_source': source, 'help_html': events, }
def test_random(self): """Test element content.""" # The document doc = XHTMLFile(string= '<body xmlns="http://www.w3.org/1999/xhtml">\n' ' <p>this <em>word</em> is nice</p>\n' ' <a href="/"><img src="logo.png" /></a>\n' ' <p><em>hello world</em></p><br/>' ' bye <em>J. David Ibanez Palomar</em>\n' '</body>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'this '), (START_FORMAT, 1), (TEXT, u'word'), (END_FORMAT, 1), (TEXT, u' is nice')), ((TEXT, u'hello world'),), ((TEXT, u'bye '), (START_FORMAT, 6), (TEXT, u'J. David Ibanez Palomar'), (END_FORMAT, 6))] self.assertEqual(messages, expected)
def test_paragraph(self): """Test formatted paragraph""" doc = XHTMLFile(string= '<p xmlns="http://www.w3.org/1999/xhtml">\n' 'The Mozilla project maintains <em>choice</em> and\n' '<em>innovation</em> on the Internet. Developing the\n' 'acclaimed, <em>open source</em>, <b>Mozilla 1.6</b>.\n' '</p>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'The Mozilla project maintains '), (START_FORMAT, 1), (TEXT, u'choice'), (END_FORMAT, 1), (TEXT, u' and '), (START_FORMAT, 2), (TEXT, u'innovation'), (END_FORMAT, 2), (TEXT, u' on the Internet.')), ((TEXT, u'Developing the acclaimed, '), (START_FORMAT, 3), (TEXT, u'open source'), (END_FORMAT, 3), (TEXT, u', '), (START_FORMAT, 4), (TEXT, u'Mozilla 1.6'), (END_FORMAT, 4), (TEXT, u'.'))] self.assertEqual(messages, expected)
def test_paragraph(self): """Test formatted paragraph""" doc = XHTMLFile( string='<p xmlns="http://www.w3.org/1999/xhtml">\n' 'The Mozilla project maintains <em>choice</em> and\n' '<em>innovation</em> on the Internet. Developing the\n' 'acclaimed, <em>open source</em>, <b>Mozilla 1.6</b>.\n' '</p>') messages = [unit[0] for unit in doc.get_units()] expected = [ ((TEXT, u'The Mozilla project maintains '), (START_FORMAT, 1), (TEXT, u'choice'), (END_FORMAT, 1), (TEXT, u' and '), (START_FORMAT, 2), (TEXT, u'innovation'), (END_FORMAT, 2), (TEXT, u' on the Internet.')), ((TEXT, u'Developing the acclaimed, '), (START_FORMAT, 3), (TEXT, u'open source'), (END_FORMAT, 3), (TEXT, u', '), (START_FORMAT, 4), (TEXT, u'Mozilla 1.6'), (END_FORMAT, 4), (TEXT, u'.')) ] self.assertEqual(messages, expected)
def test_table(self): doc = XHTMLFile(string='<table xmlns="http://www.w3.org/1999/xhtml">\n' ' <tr>\n' ' <th>Title</th>\n' ' <th>Size</th>\n' ' </tr>\n' ' <tr>\n' ' <td>The good, the bad and the ugly</td>\n' ' <td>looong</td>\n' ' </tr>\n' ' <tr>\n' ' <td>Love story</td>\n' ' <td>even longer</td>\n' ' </tr>\n' '</table>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'Title'), ), ((TEXT, u'Size'), ), ((TEXT, u'The good, the bad and the ugly'), ), ((TEXT, u'looong'), ), ((TEXT, u'Love story'), ), ((TEXT, u'even longer'), )] self.assertEqual(messages, expected)
def filter(path, mimetype, body): # HTML if mimetype == "text/html": source = XHTMLFile(string=body) target = XHTMLFile() elem = get_element(source.events, "div", **{"class": "body"}) if not elem: print "E", path return None elements = elem.get_content_elements() elements = rewrite_uris(elements, rewrite) elements = list(elements) target.set_body(elements) return target.to_str() # Skip elif mimetype in skip: return None # Keep elif mimetype in keep: return body # Unknown else: print "X", path, mimetype return body
def test_table(self): doc = XHTMLFile(string= '<table xmlns="http://www.w3.org/1999/xhtml">\n' ' <tr>\n' ' <th>Title</th>\n' ' <th>Size</th>\n' ' </tr>\n' ' <tr>\n' ' <td>The good, the bad and the ugly</td>\n' ' <td>looong</td>\n' ' </tr>\n' ' <tr>\n' ' <td>Love story</td>\n' ' <td>even longer</td>\n' ' </tr>\n' '</table>') messages = [unit[0] for unit in doc.get_units()] expected = [((TEXT, u'Title'),), ((TEXT, u'Size'),), ((TEXT, u'The good, the bad and the ugly'),), ((TEXT, u'looong'),), ((TEXT, u'Love story'),), ((TEXT, u'even longer'),)] self.assertEqual(messages, expected)
def filter(path, mimetype, body): # HTML if mimetype == 'text/html': source = XHTMLFile(string=body) target = XHTMLFile() elem = get_element(source.events, 'div', **{'class': 'body'}) if not elem: print 'E', path return None elements = elem.get_content_elements() elements = rewrite_uris(elements, rewrite) elements = list(elements) target.set_body(elements) return target.to_str() # Skip elif mimetype in skip: return None # Keep elif mimetype in keep: return body # Unknown else: print 'X', path, mimetype return body
def test_case5(self): """Test translation of an element content""" po = POFile(string='msgctxt "img[alt]"\n' 'msgid "The beach"\n' 'msgstr "La playa"') xhtml = XHTMLFile(string=self.template % '<img alt="The beach" src="beach.jpg" />') html = xhtml.translate(po) xhtml = XHTMLFile(string=html) messages = [unit[0] for unit in xhtml.get_units()] self.assertEqual(messages, [((TEXT, u'La playa'), )])
def test_case4(self): """Test translation of an element content""" string = ('msgctxt "paragraph"\n' 'msgid "hello world"\n' 'msgstr "hola mundo"\n') p = POFile(string=string) string = self.template % '<p>hello world</p>' source = XHTMLFile(string=string) string = source.translate(p) xhtml = XHTMLFile(string=string) messages = [unit[0] for unit in xhtml.get_units()] self.assertEqual(messages, [((TEXT, u'hola mundo'), )])
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # Import from Standard Library from datetime import date, timedelta # Import from itools from itools.datatypes import Date from itools.stl import stl from itools.html import XHTMLFile # Template to display full day events default_template_fd = XHTMLFile(string=""" <td class="color${event/cal}" xmlns="http://www.w3.org/1999/xhtml"> <a href="${event/url}" class="${event/STATUS}">${event/SUMMARY}</a> </td>""") # Template to display events with timetables default_template = XHTMLFile(string=""" <td xmlns="http://www.w3.org/1999/xhtml" xmlns:stl="http://www.hforge.org/xml-namespaces/stl" colspan="${cell/colspan}" rowspan="${cell/rowspan}" valign="top" class="color${cell/content/cal}"> <a stl:if="cell/newurl" class="add_event" href="${cell/newurl}"> <img width="16" height="16" src="${add_icon}" /> </a><br/> <a href="${cell/content/url}" class="${cell/content/STATUS}"> <span class="summary">${cell/content/SUMMARY}</span> </a> </td>""")