def get_interlinear(model): if not model.interlinear.strip(): return '', 'monospace' interlinear = model.interlinear format = model.il_format if format == 'leipzig': il = InterlinearText() try: return il.do_text(interlinear), format except InterlinearError: format = 'monospace' return '<pre>%s</pre>' % escape(interlinear), format
def run(self): self.assert_has_content() text = '\n'.join(self.content) try: il = InterlinearText() html = il.do_text(text) except InterlinearError as e: raise self.error(e.args[0]) text_nodes, messages = self.state.inline_text(text, self.lineno) node = self.interlinear(text, '', *text_nodes) node.line = self.content_offset + 1 return [node] + messages
def visit_interlinear(self, node): il = InterlinearText() text = il.do_text(node.astext()) self.body.append(text) node.children = []
try: il = InterlinearText() html = il.do_text(text) except InterlinearError as e: raise self.error(e.args[0]) text_nodes, messages = self.state.inline_text(text, self.lineno) node = self.interlinear(text, '', *text_nodes) node.line = self.content_offset + 1 return [node] + messages if __name__ == '__main__': from docutils.core import publish_parts from pprint import pprint il = InterlinearText() directives.register_directive('interlinear', InterlinearDirective) def test(text): parts = publish_parts(source=text, writer=CALSHTMLWriter()) return parts['fragment'] goodteststring = """ .. interlinear:: x a z b y 0 "foo ba"
def setUp(self): self.il = InterlinearText()
class TestInterlinearText(unittest.TestCase): def setUp(self): self.il = InterlinearText() def test_set_final(self): text = """asd ghg-jhjlk-jkljl A B-C-D "dfdd jgjj hjkhjk jkhjkhjkh" """.strip().split('\n') ilb = self.il.InterlinearBlock() ilb, lines = self.il._check_final(ilb, text) self.assertEqual(ilb.final, '"dfdd jgjj hjkhjk jkhjkhjkh"') self.assertEqual(len(text)-1, len(lines)) def test_set_no_final(self): text = '''asd ghg-jhjlk-jkljl A B-C-D'''.strip().split('\n') ilb = self.il.InterlinearBlock() ilb, lines = self.il._check_final(ilb, text) self.assertEqual(ilb.final, '') self.assertEqual(len(text), len(lines)) def test_empty(self): text = '' with self.assertRaises(InterlinearError) as error: self.il.do_text(text) self.assertEqual(error.exception, InterlinearErrorMsg.empty_block) def test_single_line(self): text = "'g jhgjhgjh'" expected = "<p>'g jhgjhgjh'</p>\n" result = self.il.do_text(text) self.assertEqual(expected, result) def test_single_simple_block(self): text = """asd ghg-jhjlk-jkljl sdsds rtyy-ry A B-C-D E F-G """ expected = """<div class="interlinear"> <div class="word"> <div class="line">asd</div> <div class="line">A</div> </div> <div class="word"> <div class="line">ghg-jhjlk-jkljl</div> <div class="line">B-C-D</div> </div> <div class="word"> <div class="line">sdsds</div> <div class="line">E</div> </div> <div class="word"> <div class="line">rtyy-ry</div> <div class="line">F-G</div> </div> </div> """ result = self.il.do_text(text) self.assertEqual(expected, result) def test_single_wrong_token_block(self): text = """a b c\n1 2""" with self.assertRaises(InterlinearError) as e: result = self.il.do_text(text) self.assertEqual(e.exception, InterlinearErrorMsg.wrong_number_of_tokens) text = """a b c\n1 2 3 4""" with self.assertRaises(InterlinearError) as e: result = self.il.do_text(text) self.assertEqual(e.exception, InterlinearErrorMsg.wrong_number_of_tokens) def test_trailing_line(self): text = """asd ghg-jhjlk-jkljl A B-C-D "dfdd jgjj hjkhjk jkhjkhjkh" """ expected = """<div class="interlinear"> <div class="word"> <div class="line">asd</div> <div class="line">A</div> </div> <div class="word"> <div class="line">ghg-jhjlk-jkljl</div> <div class="line">B-C-D</div> </div> </div> <p>"dfdd jgjj hjkhjk jkhjkhjkh"</p> """ result = self.il.do_text(text) self.assertEqual(expected, result) def test_single_complex_block(self): text = """asd ghg-jhjlk-jkljl A B-C-D "dfdd jgjj hjkhjk jkhjkhjkh" """ expected = """<div class="interlinear"> <div class="word"> <div class="line">asd</div> <div class="line">A</div> </div> <div class="word"> <div class="line">ghg-jhjlk-jkljl</div> <div class="line">B-C-D</div> </div> <p>"dfdd jgjj hjkhjk jkhjkhjkh"</p> </div> """ result = self.il.do_text(text) self.assertEqual(expected, result) def test_double_complex_block(self): text = """asd ghg-jhjlk-jkljl sdsds rtyy-ry A B-C-D E F-G "dfdd jgjj hjkhjk jkhjkhjkh" 1 2-3-4 5 6-7 A B-C-D E F-G "blbl ghghgh fifi yaa" """ expected = """<div class="interlinear"> <div class="word"> <div class="line">asd</div> <div class="line">A</div> </div> <div class="word"> <div class="line">ghg-jhjlk-jkljl</div> <div class="line">B-C-D</div> </div> <div class="word"> <div class="line">sdsds</div> <div class="line">E</div> </div> <div class="word"> <div class="line">rtyy-ry</div> <div class="line">F-G</div> </div> <p>"dfdd jgjj hjkhjk jkhjkhjkh"</p> </div> <div class="interlinear"> <div class="word"> <div class="line">1</div> <div class="line">A</div> </div> <div class="word"> <div class="line">2-3-4</div> <div class="line">B-C-D</div> </div> <div class="word"> <div class="line">5</div> <div class="line">E</div> </div> <div class="word"> <div class="line">6-7</div> <div class="line">F-G</div> </div> <p>"blbl ghghgh fifi yaa"</p> </div> """ result = self.il.do_text(text) self.assertEqual(expected, result)