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_referencetext_from_string(): assert ReferenceText.from_string("'{NUMBER}'") == ReferenceField('number') assert ReferenceText.from_string("'Chapter {NUMBER}\t{title}'") \ == MixedStyledText([SingleStyledText('Chapter '), ReferenceField('number'), Tab(), ReferenceField('title')]) assert ReferenceText.from_string("'{bull} '(style1)" "'{nbsp}{TITLE}\t{PaGE}'(style2)") \ == MixedStyledText( [SingleStyledText('\N{BULLET} ', style='style1'), MixedStyledText([SingleStyledText('\N{NO-BREAK SPACE}'), ReferenceField('title'), Tab(), ReferenceField('page')], style='style2')])
def generate_indices(self, docnames): def index_flowables(content): for section, entries in content: yield IndexLabel(str(section)) for (name, subtype, docname, anchor, _, _, _) in entries: target_ids = ([anchor] if anchor else None) entry_name = SingleStyledText(name, style='domain') yield IndexEntry(entry_name, level=2 if subtype == 2 else 1, target_ids=target_ids) indices_config = self.config.rinoh_domain_indices if indices_config: for domain in self.env.domains.values(): for indexcls in domain.indices: indexname = '%s-%s' % (domain.name, indexcls.name) if isinstance(indices_config, list): if indexname not in indices_config: continue content, collapsed = indexcls(domain).generate(docnames) if not content: continue index_section_label = str(indexcls.localname) yield IndexSection(SingleStyledText(index_section_label), index_flowables(content))
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_inline_image(): assert StyledText.from_string("IMAGE('images/image.pdf')") \ == InlineImage('images/image.pdf') assert StyledText.from_string("image('images/image.pdf', scale = 2.3)") \ == InlineImage('images/image.pdf', scale=2.3) assert StyledText.from_string("iMAGe('images/image.pdf' ,baseline=10 %)") \ == InlineImage('images/image.pdf', baseline=10*PERCENT) assert StyledText.from_string("Image('space image.pdf',baseline=10 %, " " scale=0.8 , width= 5 in, rotate=45)") \ == InlineImage('space image.pdf', scale=0.8, baseline=10*PERCENT, width=5*INCH, rotate=45) assert StyledText.from_string("'Text with an ' (a) IMAGE('image.png') " "' in the middle.'(b)") \ == MixedStyledText([SingleStyledText('Text with an ', style='a'), InlineImage('image.png'), SingleStyledText(' in the middle.', style='b')])
def index_flowables(content): for section, entries in content: yield IndexLabel(str(section)) for (name, subtype, docname, anchor, _, _, _) in entries: target_ids = ([anchor] if anchor else None) entry_name = SingleStyledText(name, style='domain') yield IndexEntry(entry_name, level=2 if subtype == 2 else 1, target_ids=target_ids)
def test_numberformat_from_string(): assert NumberFormat.from_string('none') == None assert NumberFormat.from_string('number') == NumberFormat.NUMBER assert NumberFormat.from_string('lowercase character') \ == NumberFormat.LOWERCASE_CHARACTER assert NumberFormat.from_string('uppercase CHARACTER') \ == NumberFormat.UPPERCASE_CHARACTER assert NumberFormat.from_string('LOWERCASE ROMAN') \ == NumberFormat.LOWERCASE_ROMAN assert NumberFormat.from_string('uppercase roman') \ == NumberFormat.UPPERCASE_ROMAN assert NumberFormat.from_string('sYMBOl') == NumberFormat.SYMBOL assert NumberFormat.from_string("'1'") == SingleStyledText('1') assert NumberFormat.from_string("'2' (strong)") \ == SingleStyledText('2', style='strong') assert NumberFormat.from_string("'3' (strong) 'A'") \ == MixedStyledText([SingleStyledText('3', style='strong'), SingleStyledText('A')]) with pytest.raises(ValueError): NumberFormat.from_string('Character') with pytest.raises(ValueError): NumberFormat.from_string('roMAN')
ssheet2 = StyleSheet('ssheet2', base=ssheet1, matcher=matcher2) ssheet2.variables['font-size'] = 20 * PT ssheet2.variables['text-align'] = 'right' ssheet2.variables['indent-first'] = 0.5 * CM ssheet2('highlighted text', font_slant='italic') ssheet2('paragraph', space_below=10 * PT, indent_first=Var('indent-first'), font_width='condensed') ssheet2('paragraph 3', base='paragraph 2', margin_left=1 * PT) ssheet2('paragraph 4', padding_right=2 * PT) highlighted = SingleStyledText('highlighed', style='highlight') highlighted2 = SingleStyledText('highlighed 2', style='highlight2') emphasized = SingleStyledText('emphasized', style='emphasis') emphasized2 = SingleStyledText('emphasized 2', style='emphasis2') paragraph = Paragraph('A paragraph with ' + emphasized + ' text.') paragraph2 = Paragraph('A second paragraph with ' + emphasized2 + ' text.', style='paragraph2') emphasized3 = SingleStyledText('emphasized 3', style='emphasis2') paragraph3 = Paragraph('A third paragraph with ' + emphasized3 + ' text.', style='paragraph3') paragraph4 = Paragraph('A fourth paragraph.', style='paragraph4') paragraph5 = Paragraph( 'A paragraph for which no style is present in the ' 'style sheet', style='missing') paragraph6 = Paragraph('A sixth paragraph with ' + highlighted + ' text.')
def field_spans(self, container): callback = lambda item: self.warn_unknown_reference_id(item, container) text = self.citation.bibliography.cite(self.citation, callback) field_text = SingleStyledText(text) field_text.parent = self.parent return field_text.spans()
def __init__(self, terms): label = SingleStyledText("Index Terms \N{EM DASH} ", BOLD_ITALIC_STYLE) text = ", ".join(sorted(terms)) + "." text = text.capitalize() return super().__init__(label + text, style=styles['abstract'])
def __init__(self, text): label = SingleStyledText("Abstract \N{EM DASH} ", BOLD_ITALIC_STYLE) return super().__init__(label + text, style=styles['abstract'])
def test_styledtext_from_string(): assert StyledText.from_string("'one'") == SingleStyledText('one') assert StyledText.from_string("'on\\'e'") == SingleStyledText("on'e") assert StyledText.from_string("'''with\nnewline'''") \ == SingleStyledText('with\nnewline') assert StyledText.from_string("'one' 'two'") \ == MixedStyledText([SingleStyledText('one'), SingleStyledText('two')]) assert StyledText.from_string("'one''two'") \ == MixedStyledText([SingleStyledText('one'), SingleStyledText('two')]) assert StyledText.from_string("'one'\n'two'") \ == MixedStyledText([SingleStyledText('one'), SingleStyledText('two')]) assert StyledText.from_string("'one'(style1)") \ == SingleStyledText('one', style='style1') assert StyledText.from_string("'one' (style1)") \ == SingleStyledText('one', style='style1') assert StyledText.from_string("'one'(style 1)") \ == SingleStyledText('one', style='style 1') assert StyledText.from_string("'one'(style 1)") \ == SingleStyledText('one', style='style 1') assert StyledText.from_string("'one'( style 1 )") \ == SingleStyledText('one', style='style 1') assert StyledText.from_string("'one'(style1) 'two'") \ == MixedStyledText([SingleStyledText('one', style='style1'), SingleStyledText('two')]) assert StyledText.from_string("'one'\t(style1) 'two'(style2)") \ == MixedStyledText([SingleStyledText('one', style='style1'), SingleStyledText('two', style='style2')]) assert StyledText.from_string("'one{nbsp}two'") \ == SingleStyledText('one\N{NO-BREAK SPACE}two') assert StyledText.from_string("'one' '{gt}two'(style2)") \ == MixedStyledText([SingleStyledText('one'), SingleStyledText('>two', style='style2')])
def test_get_style(): assert emphasized.get_style('font_weight', container) == FontWeight.MEDIUM assert emphasized.get_style('font_width', container) == FontWidth.CONDENSED assert emphasized.get_style('font_slant', container) == FontSlant.ITALIC assert emphasized2.get_style('font_weight', container) == FontWeight.MEDIUM assert emphasized2.get_style('font_width', container) == FontWidth.NORMAL assert emphasized2.get_style('font_slant', container) == FontSlant.OBLIQUE assert emphasized3.get_style('font_weight', container) == FontWeight.MEDIUM assert emphasized3.get_style('font_width', container) == FontWidth.NORMAL assert emphasized3.get_style('font_slant', container) == FontSlant.OBLIQUE assert highlighted2.get_style('font_color', container) == HexColor('00f') assert highlighted2.get_style('font_size', container) == 12 * PT assert highlighted2.get_style('font_slant', container) == FontSlant.ITALIC assert paragraph.get_style('margin_left', container) == 0 assert paragraph.get_style('margin_right', container) == 55 * PT assert paragraph.get_style('space_above', container) == 5 * PT assert paragraph.get_style('space_below', container) == 10 * PT assert paragraph.get_style('font_weight', container) == FontWeight.MEDIUM assert paragraph.get_style('font_width', container) == FontWidth.CONDENSED assert paragraph.get_style('font_slant', container) == FontSlant.UPRIGHT assert paragraph.get_style('font_size', container) == 8 * PT assert paragraph2.get_style('margin_left', container) == 0 assert paragraph2.get_style('margin_right', container) == 0 assert paragraph2.get_style('space_above', container) == 0 assert paragraph2.get_style('space_below', container) == 0 assert paragraph2.get_style('padding_top', container) == 0 assert paragraph2.get_style('padding_bottom', container) == 3 * PT assert paragraph2.get_style('font_weight', container) == FontWeight.MEDIUM assert paragraph2.get_style('font_width', container) == FontWidth.NORMAL assert paragraph2.get_style('font_slant', container) == FontSlant.UPRIGHT assert paragraph2.get_style('font_size', container) == 10 * PT assert paragraph3.get_style('margin_left', container) == 1 * PT assert paragraph3.get_style('margin_right', container) == 0 assert paragraph3.get_style('space_above', container) == 0 assert paragraph3.get_style('space_below', container) == 0 assert paragraph3.get_style('padding_top', container) == 0 assert paragraph3.get_style('padding_bottom', container) == 3 * PT assert paragraph3.get_style('font_weight', container) == FontWeight.MEDIUM assert paragraph3.get_style('font_width', container) == FontWidth.NORMAL assert paragraph3.get_style('font_slant', container) == FontSlant.UPRIGHT assert paragraph3.get_style('font_size', container) == 10 * PT # from 'paragraph' in ssheet1 assert paragraph4.get_style('margin_right', container) == 55 * PT assert paragraph4.get_style('space_above', container) == 5 * PT assert paragraph4.get_style('font_size', container) == 8 * PT # from 'paragraph' in ssheet2 assert paragraph4.get_style('space_below', container) == 10 * PT assert paragraph4.get_style('font_width', container) == FontWidth.CONDENSED # from 'paragraph 4' in sheet1 assert paragraph4.get_style('padding_top', container) == 5 * PT # from 'paragraph 4' in sheet2 assert paragraph4.get_style('padding_right', container) == 2 * PT assert paragraph5.get_style('font_slant', container) == FontSlant.UPRIGHT before_p4 = paragraph4.get_style('before', container) assert before_p4 == SingleStyledText('before p4', source=ssheet1) after_p4 = paragraph4.get_style('after', container) assert after_p4 == SingleStyledText('after p4', source=ssheet2) assert grouped1.get_style('padding_top', container) == 3 * PT assert paragraph7.get_style('margin_left', container) == 7 * PT assert paragraph7.get_style('padding_top', container) == 3 * PT
ssheet1('emphasized text', font_slant='italic', font_size=Var('font-size')) ssheet1('emphasized text 2', base='emphasized text', font_slant='oblique') ssheet1('highlighted text', font_color=HexColor('00f')) ssheet1('highlighted text 2', base='highlighted text', font_size=12 * PT) ssheet1('paragraph', margin_right=55 * PT, space_above=5 * PT, text_align=Var('text-align'), font_size=8 * PT, font_color=Var('font-color'), indent_first=2 * PT) ssheet1('paragraph 2', padding_bottom=3 * PT) ssheet1('paragraph 4', base='paragraph', padding_top=5 * PT, before=SingleStyledText('before p4')) ssheet1('grouped 1', padding_top=3 * PT) matcher2 = StyledMatcher({ 'paragraph 3': paragraph3_selector, 'paragraph 7': paragraph7_selector, }) ssheet2 = StyleSheet('ssheet2', base=ssheet1, matcher=matcher2) ssheet2.variables['font-size'] = 20 * PT ssheet2.variables['text-align'] = 'right' ssheet2.variables['indent-first'] = 0.5 * CM ssheet2('highlighted text', font_slant='italic') ssheet2('paragraph',
emphasis_selector = StyledText.like('emphasis') paragraph_selector = Paragraph matcher = StyledMatcher({ 'emphasized text': emphasis_selector, 'paragraph': paragraph_selector, }) ssheet1 = StyleSheet('base', matcher) ssheet1('emphasized text', font_slant='italic') ssheet1('paragraph', space_above=5 * PT) ssheet2 = StyleSheet('test', base=ssheet1) ssheet2('paragraph', space_below=10 * PT) emphasized = SingleStyledText('emphasized', style='emphasis') paragraph = Paragraph('A paragraph with ' + emphasized + ' text.') doctree = DocumentTree([paragraph]) document = Document(doctree, ssheet2, EN) container = FakeContainer(document) def test_style(): style1 = ssheet1['emphasized text'] assert style1.font_width == 'normal' assert style1.font_slant == 'italic' style2 = ssheet1['paragraph'] assert style2.space_above == 5 * PT