def main(infile, outfile): def p(text, tail, attrib, children, trail): return remove_adjacent_space(text + ' '.join(children)) + '\n' def b(text, tail, attrib, children, trail): return '**' + text + ' '.join(children) + '**' + tail def i(text, tail, attrib, children, trail): return '_' + text + ' '.join(children) + '_' + tail def h1(text, tail, attrib, children, trail): return '# ' + text + ' '.join(children) + '\n' def h2(text, tail, attrib, children, trail): return '# ' + text + ' '.join(children) + '\n' def a(text, tail, attrib, children, trail): return '[' + text + ' '.join( children) + '](' + attrib['href'] + ')' + tail xd.xd(infile, outfile, output_format='txt', tails=True, p=p, b=b, i=i, h1=h1, h2=h2, a=a)
def test_abc_unicode_text(): def abc(text): return text xd.xd('test_data/abc.xml', 'test_data/tmp.txt', abc=abc, depth=0) with codecs.open('test_data/tmp.txt', 'r', encoding='utf-8') as f: eq_(u"åäö\n", f.read())
def test_identity_file(): def default_action(tag, text, tail, children, attrib): return xd.TagWithTail(tag, text, tail, *children, **attrib) original = __pretty_xmlfile('test_data/po.xml') xd.xd('test_data/po.xml', 'test_data/po_tmp.xml', depth=0, many_outputs=False, default_action=default_action) roundtrip = __pretty_xmlfile('test_data/po_tmp.xml') eq_(roundtrip, original)
def sentences(infile, outfile): def w(text): return text def sentence(children): return ' '.join(children) def text(children, title, url): return title + ' ' + url + '\n' + '\n'.join(children) + '\n\n' xd.xd(infile, outfile, w=w, sentence=sentence, text=text)
def test_abc_unicode_json(): def abc(text): return text xd.xd('test_data/abc.xml', 'test_data/tmp.json', output_format='json', many_outputs=False, depth=0, abc=abc) with codecs.open('test_data/tmp.json', 'r', encoding='utf-8') as f: eq_(u'åäö', json.load(f))
def test_abc_unicode_xml(): def abc(text): return xd.Tag('abc', text) xd.xd( 'test_data/abc.xml', 'test_data/tmp.xml', many_outputs=False, abc=abc, depth=0) original = __pretty_xmlfile('test_data/abc.xml') roundtrip = __pretty_xmlfile('test_data/tmp.xml') eq_(original, roundtrip)
def identity(infile, outfile, limit=None, depth=1, xml_root='root'): def default_action(tag, text, tail, attrs, children, _parents): return xd.TagWithTail(tag, text, tail, *children, **attrs) depth = int(depth) xd.xd(tails=True, **locals())
def po_str_file(actions, expected): xd.xd('test_data/po.xml', 'test_data/po_tmp.txt', actions) with codecs.open('test_data/po_tmp.txt', 'r', encoding='utf-8') as f: eq_(f.read(), expected)