def test_simple(self): # Test 1: Simple document using our own element objects body = el.Body([ el.Heading(['Toplevel heading'], level=1), el.Paragraph(['Introductory preamble']), el.Section([ el.Paragraph(['Some text']), el.Subsection([el.Paragraph(['More text'])], ordinal='1.1', title="First subsection") ], ordinal='1', title='First section'), el.Section([el.Paragraph(['Even more text'])], ordinal='2', title='Second section') ]) want = """ <body xmlns="http://www.w3.org/1999/xhtml" about="http://localhost:8000/res/base/basefile"> <h1>Toplevel heading</h1> <p>Introductory preamble</p> <div content="First section" about="http://localhost:8000/res/base/basefile#S1" property="dcterms:title" typeof="bibo:DocumentPart" class="section"> <span href="http://localhost:8000/res/base/basefile" rel="dcterms:isPartOf"></span> <span content="1" about="http://localhost:8000/res/base/basefile#S1" property="bibo:chapter"/> <p>Some text</p> <div content="First subsection" about="http://localhost:8000/res/base/basefile#S1.1" property="dcterms:title" typeof="bibo:DocumentPart" class="subsection"> <span href="http://localhost:8000/res/base/basefile#S1" rel="dcterms:isPartOf"></span> <span content="1.1" about="http://localhost:8000/res/base/basefile#S1.1" property="bibo:chapter"/> <p>More text</p> </div> </div> <div content="Second section" about="http://localhost:8000/res/base/basefile#S2" property="dcterms:title" typeof="bibo:DocumentPart" class="section"> <span href="http://localhost:8000/res/base/basefile" rel="dcterms:isPartOf"></span> <span content="2" about="http://localhost:8000/res/base/basefile#S2" property="bibo:chapter"/> <p>Even more text</p> </div> </body>""" self._test_asxhtml(want, body)
def test_custom(self): # test 4: define a CompoundElement subclass and override # as_xhtml class Preamble(el.CompoundElement): tagname = "div" classname = "preamble" def as_xhtml(self, uri, parent_uri=None): # a fairly complicated custom serialization that # inserts a new child node where before there was only # text, and so that text has to be moved from the # parent.text to child.tail E = ElementMaker(namespace="http://www.w3.org/1999/xhtml") element = super(Preamble, self).as_xhtml(uri) note = E('span', {'class': 'preamble-note'}, self.note + ": ") note.tail = element.text element.text = None element.insert(0, note) return element body = el.Body([ el.Heading(['Toplevel heading'], level=1), Preamble(['Introductory preamble'], note='Read this first'), el.Section([el.Paragraph(['Some text'])], ordinal='1', title='First section') ]) want = """ <body xmlns="http://www.w3.org/1999/xhtml" about="http://localhost:8000/res/base/basefile"> <h1>Toplevel heading</h1> <div class="preamble"><span class="preamble-note">Read this first: </span>Introductory preamble</div> <div content="First section" about="http://localhost:8000/res/base/basefile#S1" property="dcterms:title" typeof="bibo:DocumentPart" class="section"> <span href="http://localhost:8000/res/base/basefile" rel="dcterms:isPartOf"></span> <span content="1" about="http://localhost:8000/res/base/basefile#S1" property="bibo:chapter"/> <p>Some text</p> </div> </body> """ self._test_asxhtml(want, body)
def make_section(parser): chunk = parser.reader.next() title = chunk.split("\n")[0] s = elements.Section(title=title) return parser.make_children(s)
def test_nested(self): # Make sure nested sections have the expected dcterms:isPartOf # relation if they have a @about property (could be from a # .uri property or dynamically constructed like # SectionalElement.as_xhtml) class MySection(el.CompoundElement): tagname = "div" classname = "mysection" partrelation = Namespace(util.ns['schema']).isPartOf body = el.Body([ el.Section([ el.Paragraph(['Some text']), el.Link("txt", uri="http://ex.org/ext"), el.Subsection([ el.Paragraph(['More text']), el.Subsubsection([el.Paragraph(['Even more text'])], ordinal="1.1.1", title="First subsubsection") ], ordinal="1.1", title="First subsection") ], ordinal="1", title="First section"), MySection([el.Paragraph(['Even more text'])], uri="http://example.org/s2") ]) want = """ <body xmlns="http://www.w3.org/1999/xhtml" about="http://localhost:8000/res/base/basefile"> <div content="First section" property="dcterms:title" about="http://localhost:8000/res/base/basefile#S1" typeof="bibo:DocumentPart" class="section"> <span rel="dcterms:isPartOf" href="http://localhost:8000/res/base/basefile"/> <span content="1" about="http://localhost:8000/res/base/basefile#S1" property="bibo:chapter"/> <p>Some text</p> <a href="http://ex.org/ext">txt</a> <div content="First subsection" property="dcterms:title" about="http://localhost:8000/res/base/basefile#S1.1" typeof="bibo:DocumentPart" class="subsection"> <span rel="dcterms:isPartOf" href="http://localhost:8000/res/base/basefile#S1"/> <span content="1.1" about="http://localhost:8000/res/base/basefile#S1.1" property="bibo:chapter"/> <p>More text</p> <div content="First subsubsection" property="dcterms:title" about="http://localhost:8000/res/base/basefile#S1.1.1" typeof="bibo:DocumentPart" class="subsubsection"> <span rel="dcterms:isPartOf" href="http://localhost:8000/res/base/basefile#S1.1"/> <span content="1.1.1" about="http://localhost:8000/res/base/basefile#S1.1.1" property="bibo:chapter"/> <p>Even more text</p> </div> </div> </div> <div about="http://example.org/s2" class="mysection"> <span rel="schema:isPartOf" href="http://localhost:8000/res/base/basefile"/> <p>Even more text</p> </div> </body>""" self._test_asxhtml(want, body)
def test_meta(self): # test 3: use a mix of our own elements and html elements, # with meta + uri attached to some nodes g1 = Graph().parse(format='n3', data=""" @prefix bibo: <http://purl.org/ontology/bibo/> . @prefix dcterms: <http://purl.org/dc/terms/> . <http://localhost:8000/res/base/basefile#S1> a bibo:DocumentPart; dcterms:title "First section"; bibo:chapter "1" . """) g2 = Graph().parse(format='n3', data=""" @prefix bibo: <http://purl.org/ontology/bibo/> . @prefix dcterms: <http://purl.org/dc/terms/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://localhost:8000/res/base/basefile#S2> a bibo:DocumentPart; dcterms:title "Second section"; bibo:chapter "2"; dcterms:creator "Fred Bloggs"@en-GB; dcterms:issued "2013-05-10"^^xsd:date; owl:sameAs <http://example.org/s2> . <http://example.org/s2> dcterms:title "Same same but different" . <http://localhost:8000/res/base/unlrelated> dcterms:title "Unrelated document" . """) body = el.Body([ el.Heading(['Toplevel heading'], level=1), html.P(['Introductory preamble']), html.Div([ html.P(['Some text']), el.Subsection([el.Paragraph(['More text'])], ordinal='1.1', title="First subsection") ], uri='http://localhost:8000/res/base/basefile#S1', meta=g1), el.Section([el.Paragraph(['Even more text'])], uri='http://localhost:8000/res/base/basefile#S2', meta=g2) ]) want = """ <body xmlns="http://www.w3.org/1999/xhtml" about="http://localhost:8000/res/base/basefile"> <h1>Toplevel heading</h1> <p>Introductory preamble</p> <div about="http://localhost:8000/res/base/basefile#S1" content="First section" property="dcterms:title" typeof="bibo:DocumentPart"> <span href="http://localhost:8000/res/base/basefile" rel="dcterms:isPartOf"/> <span content="1" property="bibo:chapter" xml:lang=""/> <p>Some text</p> <div about="http://localhost:8000/res/base/basefile#S1.1" content="First subsection" property="dcterms:title" typeof="bibo:DocumentPart" class="subsection"> <span href="http://localhost:8000/res/base/basefile#S1" rel="dcterms:isPartOf"/> <span about="http://localhost:8000/res/base/basefile#S1.1" content="1.1" property="bibo:chapter"/> <p>More text</p> </div> </div> <div about="http://localhost:8000/res/base/basefile#S2" class="section" content="Second section" property="dcterms:title" typeof="bibo:DocumentPart"> <span href="http://localhost:8000/res/base/basefile" rel="dcterms:isPartOf"/> <span href="http://example.org/s2" rel="owl:sameAs"> <span content="Same same but different" property="dcterms:title" xml:lang=""/> </span> <span content="2" property="bibo:chapter" xml:lang=""/> <span content="2013-05-10" property="dcterms:issued" datatype="xsd:date"/> <span content="Fred Bloggs" property="dcterms:creator" xml:lang="en-GB"/> <p>Even more text</p> </div> </body>""" self._test_asxhtml(want, body)
def make_section(parser): (secnumber, title) = analyze_sectionstart(parser.reader.next()) s = elements.Section(ordinal=secnumber, title=title) return parser.make_children(s)