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')
def test_template_configuration_file(): with TemporaryDirectory() as tmpdir: dir = Path(tmpdir) with (dir / 'a.rtt').open('w') as a: a.write(CONFIGURATION_A) conf = TemplateConfigurationFile(a.name) assert conf.template == MyDocumentTemplate header_text = conf.get_variable(PageTemplate, 'header_text', Var('my_header_text')) expected_header_text = (Field(SECTION_NUMBER(1)) + ' ' + Field(SECTION_TITLE(1))) assert header_text == expected_header_text doc = create_document(conf) assert doc.get_option('a') is False assert doc.get_option('b') is False assert doc.get_option('c') is True 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)))
from rinoh.dimension import PERCENT from rinoh.float import InlineImage from rinoh.paper import A4 from rinoh.backend import pdf from rinoh.frontend.dita import DITAReader from rinoh.reference import Variable, SECTION_TITLE, PAGE_NUMBER from rinoh.text import Tab from rinohlib.templates.article import Article, ArticleOptions, TITLE from rinohlib.templates.book import Book, BookOptions from rinohlib.stylesheets.sphinx import stylesheet HEADER_TEXT = 'About RinohType' + Tab() + Tab() + Variable(SECTION_TITLE(1)) FOOTER_TEXT = ( InlineImage('company_logo.pdf', scale=0.7, baseline=31.5 * PERCENT) + Tab() + Tab() + 'page ' + Variable(PAGE_NUMBER)) OPTIONS = ArticleOptions(page_size=A4, table_of_contents=True) OPTIONS = ArticleOptions(page_size=A4, columns=2, table_of_contents=True, header_text=HEADER_TEXT, footer_text=FOOTER_TEXT, stylesheet=stylesheet, abstract_location=TITLE, show_date=False, show_author=False) if __name__ == '__main__': reader = DITAReader() for name in ('sequence', 'hierarchy'):