def test_template_configuration_file_base(): with TemporaryDirectory() as tmpdir: dir = Path(tmpdir) with (dir / 'a.rtt').open('w') as a: a.write(CONFIGURATION_A) with (dir / 'b.rtt').open('w') as b: b.write(CONFIGURATION_B) conf = TemplateConfigurationFile(b.name) assert conf.template == MyDocumentTemplate header_text = conf.get_variable(PageTemplate, 'header_text', Var('my_header_text')) expected_header_text = SingleStyledText('Configuration B Header') assert header_text == expected_header_text doc = create_document(conf) assert doc.get_option('a') is True assert doc.get_option('b') is False assert doc.get_option('c') is False part_template, = doc.part_templates part = part_template.document_part(doc, 'number') assert part.template_name == 'contents' page = part.new_page(1, new_chapter=False) assert page.template_name == 'contents_page' assert page.get_config_value('columns', doc) == 1 assert page.get_config_value('column_spacing', doc) == 1 * PT # the retrieved config values have a parent (Header/Footer), so we can't # use == but compare their repr (which doesn't include the parent) assert (repr(page.get_config_value('header_text', doc)) == repr(expected_header_text)) assert (repr(page.get_config_value( 'footer_text', doc)) == repr(Field(PAGE_NUMBER) + ' / ' + Field(NUMBER_OF_PAGES)))
def test_styledtext_from_string_field(): assert StyledText.from_string("'{SECTION_NUMBER(7)}' (style)") \ == Field(SECTION_NUMBER(7), style='style') assert StyledText.from_string("'abc {NUMBER_OF_PAGES}' (style)") \ == MixedStyledText([SingleStyledText('abc '), Field(NUMBER_OF_PAGES)], style='style') assert StyledText.from_string("'{PAGE_NUMBER}abc' (style)") \ == MixedStyledText([Field(PAGE_NUMBER), SingleStyledText('abc')], style='style') assert StyledText.from_string("'one{nbsp}two{SECTION_TITLE(1)}'") \ == MixedStyledText([SingleStyledText('one\N{NO-BREAK SPACE}two'), Field(SECTION_TITLE(1))]) assert StyledText.from_string("'one{PAGE_NUMBER}'(style1) 'moh'(style2)") \ == MixedStyledText([MixedStyledText([SingleStyledText('one'), Field(PAGE_NUMBER)], style='style1'), SingleStyledText('moh', style='style2')]) assert StyledText.from_string("'{SectionTitles.chapter}abc' (style)") \ == MixedStyledText([StringField(SectionTitles, 'chapter'), SingleStyledText('abc')], style='style') assert StyledText.from_string("'1{AdmonitionTitles.warning}2' (style)") \ == MixedStyledText([SingleStyledText('1'), StringField(AdmonitionTitles, 'warning'), SingleStyledText('2')], style='style') assert StyledText.from_string("'{UserStrings.my_string}abc' (style)") \ == MixedStyledText([StringField(UserStrings, 'my_string'), SingleStyledText('abc')], style='style')