def test_save_should_use_python_format_if_marker_is_supplied_and_template_has_named_markers(): t = Templater(template='{{start}}<u>{{text}}</u>{{end}}', marker=regexp_marker) t.save('test.html', marker='[--{}--]') result = read_file_and_delete('test.html') expected = '[--start--]<u>[--text--]</u>[--end--]\n' assert expected == result
def test_Templater_save_should_save_template_as_a_raw_file_with_markers(): processed_template = [None, '<b>', None, '</b><u>', None, '</u>', None] t = Templater(template=processed_template) t.save('test.html', marker='|||') result = read_file_and_delete('test.html') expected = '|||<b>|||</b><u>|||</u>|||\n' assert expected == result
def test_Templater_save_should_save_template_as_a_raw_file_with_markers(): processed_template = [None, '<b>', None, '</b><u>', None, '</u>', None] t = Templater(template=processed_template) t.save('test.html', marker='|||') result = read_file_and_delete('test.html') expected = '|||<b>|||</b><u>|||</u>|||\n' assert expected == result
def test_save_should_use_NAMED_MARKER_if_template_has_named_markers_and_no_marker_supplied(): t = Templater(template='{{one}}<u>{{two}}</u>{{three}}', marker=regexp_marker) t.save('test.html') result = read_file_and_delete('test.html') named_markers = [NAMED_MARKER.format(header) for header in t._headers] expected = t.join(named_markers) + '\n' assert expected == result
def test_passing_headers_with_different_size_from_self_headers_should_raise_AttributeError(): t = Templater(template='{{one}}<u>{{two}}</u>{{three}}', marker=regexp_marker) try: t.save('test.html', headers=list('abcde')) except AttributeError: pass else: unlink('test.html') raise 'AttributeError not raised!'
def test_save_should_use_headers_instead_of_self_headers_if_supplied(): t = Templater(template='{{one}}<u>{{two}}</u>{{three}}', marker=regexp_marker) t.save('test.html', headers=list('abc')) result_1 = read_file_and_delete('test.html') named_markers = [NAMED_MARKER.format(header) for header in list('abc')] expected_1 = t.join(named_markers) + '\n' assert expected_1 == result_1 t.save('test.html', marker='[--{}--]', headers=list('abc')) result_2 = read_file_and_delete('test.html') expected_2 = '[--a--]<u>[--b--]</u>[--c--]' + '\n' assert expected_2 == result_2
def test_save_should_use_headers_instead_of_self_headers_if_supplied(): t = Templater(template='{{one}}<u>{{two}}</u>{{three}}', marker=regexp_marker) t.save('test.html', headers=list('abc')) result_1 = read_file_and_delete('test.html') named_markers = [NAMED_MARKER.format(header) for header in list('abc')] expected_1 = t.join(named_markers) + '\n' assert expected_1 == result_1 t.save('test.html', marker='[--{}--]', headers=list('abc')) result_2 = read_file_and_delete('test.html') expected_2 = '[--a--]<u>[--b--]</u>[--c--]' + '\n' assert expected_2 == result_2
#!/usr/bin/env python # coding: utf-8 from os import unlink from templater import Templater t = Templater() t.learn('<b>spam</b>') t.learn('<b>eggs</b>') t.learn('<b>ham</b>') t.save('my-little-template.html', marker='|||') # will put a `\n` in the EOF t.dump('my-template.tpl') print(t.parse('<b>parsing using first template object</b>')) t2 = Templater.open('my-little-template.html', marker='|||') # it removes `\n`/`\r\n` in the end of file before creating template definition print(t2.parse('<b>parsing using second template object</b>')) t3 = Templater.load('my-template.tpl') print(t3.parse('<b>parsing using third template object</b>')) # 'my-little-template.html' will have the template string with blanks filled by # '|||' # 'my-template.tpl' will have the pickle of Templater object # Removing files: unlink('my-little-template.html') unlink('my-template.tpl')
#!/usr/bin/env python # coding: utf-8 from os import unlink from templater import Templater t = Templater() t.learn('<b>spam</b>') t.learn('<b>eggs</b>') t.learn('<b>ham</b>') t.save('my-little-template.html', marker='|||') # will put a `\n` in the EOF t.dump('my-template.tpl') print t.parse('<b>parsing using first template object</b>') t2 = Templater.open('my-little-template.html', marker='|||') # it removes `\n`/`\r\n` in the end of file before creating template definition print t2.parse('<b>parsing using second template object</b>') t3 = Templater.load('my-template.tpl') print t3.parse('<b>parsing using third template object</b>') # 'my-little-template.html' will have the template string with blanks filled by # '|||' # 'my-template.tpl' will have the pickle of Templater object # Removing files: unlink('my-little-template.html') unlink('my-template.tpl')
#!/usr/bin/env python # coding: utf-8 from re import compile as re_compile from templater import Templater regexp_marker = re_compile(r'{{([a-zA-Z0-9_-]*)}}') # match ''{{var}}'' template = Templater('{{first-var}}<b>{{second-var}}</b>{{third-var}}', marker=regexp_marker) # regexp marker also works for Templater.open to specify named markers result = template.parse('This <b> is </b> a test.') # returns a dict print(result) template.save('template-with-named-markers.html', marker='{{{{{}}}}}')
def test_save_should_use_marker_if_supplied_and_template_hasnt_named_markers(): t = Templater(template='+<u>+</u>+', marker='+') t.save('test.html', marker='%%') result = read_file_and_delete('test.html') expected = '%%<u>%%</u>%%\n' assert expected == result
def test_save_should_use_self_marker_if_no_marker_supplied(): t = Templater(template='+<u>+</u>+', marker='+') t.save('test.html') result = read_file_and_delete('test.html') expected = '+<u>+</u>+\n' assert expected == result
def test_save_should_use_marker_if_supplied_and_template_hasnt_named_markers(): t = Templater(template='+<u>+</u>+', marker='+') t.save('test.html', marker='%%') result = read_file_and_delete('test.html') expected = '%%<u>%%</u>%%\n' assert expected == result
def test_save_should_use_self_marker_if_no_marker_supplied(): t = Templater(template='+<u>+</u>+', marker='+') t.save('test.html') result = read_file_and_delete('test.html') expected = '+<u>+</u>+\n' assert expected == result
#!/usr/bin/env python # coding: utf-8 from re import compile as re_compile from templater import Templater regexp_marker = re_compile(r'{{([a-zA-Z0-9_-]*)}}') # match ''{{var}}'' template = Templater('{{first-var}}<b>{{second-var}}</b>{{third-var}}', marker=regexp_marker) # regexp marker also works for Templater.open to specify named markers result = template.parse('This <b> is </b> a test.') # returns a dict print result template.save('template-with-named-markers.html', marker='{{{{{}}}}}')