Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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')])
Ejemplo n.º 3
0
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')])
Ejemplo n.º 4
0
def test_parse_selector():
    assert parse_selector("Paragraph") == Paragraph
    assert parse_selector("Paragraph / StyledText") == Paragraph / StyledText
    assert parse_selector("  Paragraph / StyledText") == Paragraph / StyledText
    assert parse_selector(" Paragraph /StyledText  ") == Paragraph / StyledText
    assert parse_selector("Paragraph('aa') / StyledText('bb')") \
               == Paragraph.like('aa') / StyledText.like('bb')
    assert parse_selector("Paragraph('aa') / StyledText") \
               == Paragraph.like('aa') / StyledText
    assert parse_selector("Paragraph('aa' ,meh=5) / StyledText") \
               == Paragraph.like('aa', meh=5) / StyledText
    assert parse_selector("Paragraph /    StyledText('bb ') ") \
               == Paragraph / StyledText.like('bb ')
    assert parse_selector("Paragraph('aa') / ... / StyledText(blip ='blop')") \
               == Paragraph.like('aa') / ... / StyledText.like(blip='blop')
    assert parse_selector("  'paragraph' / StyledText")\
               == SelectorByName('paragraph') / StyledText
    assert parse_selector("Paragraph('aa') / ... / 'some style'") \
               == Paragraph.like('aa') / ... / SelectorByName('some style')
Ejemplo n.º 5
0
def test_parse_selector():
    assert parse_selector("Paragraph") == Paragraph
    assert parse_selector("Paragraph / StyledText") == Paragraph / StyledText
    assert parse_selector("  Paragraph / StyledText") == Paragraph / StyledText
    assert parse_selector(" Paragraph /StyledText  ") == Paragraph / StyledText
    assert parse_selector("Paragraph('aa') / StyledText('bb')") \
               == Paragraph.like('aa') / StyledText.like('bb')
    assert parse_selector("Paragraph('aa') / StyledText") \
               == Paragraph.like('aa') / StyledText
    assert parse_selector("Paragraph('aa' ,meh=5) / StyledText") \
               == Paragraph.like('aa', meh=5) / StyledText
    assert parse_selector("Paragraph /    StyledText('bb ') ") \
               == Paragraph / StyledText.like('bb ')
    assert parse_selector("Paragraph('aa') / ... / StyledText(blip ='blop')") \
               == Paragraph.like('aa') / ... / StyledText.like(blip='blop')
    assert parse_selector("  'paragraph' / StyledText")\
               == SelectorByName('paragraph') / StyledText
    assert parse_selector("Paragraph('aa') / ... / 'some style'") \
               == Paragraph.like('aa') / ... / SelectorByName('some style')
Ejemplo n.º 6
0
def test_parse_class_selector():
    def helper(string):
        chars = CharIterator(string)
        return parse_class_selector(chars), ''.join(chars)

    assert helper('Paragraph') == (Paragraph, '')
    assert helper('Paragraph   ') == (Paragraph, '   ')
    assert helper('  Paragraph') == (Paragraph, '')
    assert helper('  Paragraph ') == (Paragraph, ' ')
    assert helper('Paragraph()') == (Paragraph.like(), '')
    assert helper('Paragraph(   )') == (Paragraph.like(), '')
    assert helper("Paragraph('style')") == (Paragraph.like('style'), '')
    assert helper("Paragraph('style', "
                  "meh=5)") == (Paragraph.like('style', meh=5), '')
    assert helper(" StyledText('style')  ") == (StyledText.like('style'), '  ')
Ejemplo n.º 7
0
def test_parse_class_selector():
    def helper(string):
        chars = CharIterator(string)
        return parse_class_selector(chars), ''.join(chars)

    assert helper('Paragraph') == (Paragraph, '')
    assert helper('Paragraph   ') == (Paragraph, '   ')
    assert helper('  Paragraph') == (Paragraph, '')
    assert helper('  Paragraph ') == (Paragraph, ' ')
    assert helper('Paragraph()') == (Paragraph.like(), '')
    assert helper('Paragraph(   )') == (Paragraph.like(), '')
    assert helper("Paragraph('style')") == (Paragraph.like('style'), '')
    assert helper("Paragraph('style', "
                  "meh=5)") == (Paragraph.like('style', meh=5), '')
    assert helper(" StyledText('style')  ") == (StyledText.like('style'), '  ')
Ejemplo n.º 8
0
def test_styledtext_from_string():
    assert StyledText.from_string("'one'") \
           == MixedStyledText([SingleStyledText('one')])
    assert StyledText.from_string("'on\\'e'") \
           == MixedStyledText([SingleStyledText("on'e")])
    assert StyledText.from_string("'one' 'two'") \
           == MixedStyledText([SingleStyledText('one'),
                               SingleStyledText('two')])
    assert StyledText.from_string("'one'(style1)") \
           == MixedStyledText([SingleStyledText('one', style='style1')])
    assert StyledText.from_string("'one' (style1)") \
           == MixedStyledText([SingleStyledText('one', style='style1')])
    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'") \
           == MixedStyledText([SingleStyledText('one\N{NO-BREAK SPACE}two')])
    assert StyledText.from_string("'one' '{gt}two'(style2)") \
           == MixedStyledText([SingleStyledText('one'),
                               SingleStyledText('>two', style='style2')])
Ejemplo n.º 9
0
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')
Ejemplo n.º 10
0
#
# Use of this source code is subject to the terms of the GNU Affero General
# Public License v3. See the LICENSE file or http://www.gnu.org/licenses/.

import pytest

from rinoh.attribute import Var
from rinoh.color import HexColor
from rinoh.dimension import PT, CM
from rinoh.document import DocumentTree, Document, FakeContainer
from rinoh.language import EN
from rinoh.paragraph import Paragraph
from rinoh.text import StyledText, SingleStyledText
from rinoh.style import StyleSheet, StyledMatcher

emphasis_selector = StyledText.like('emphasis')
emphasis2_selector = StyledText.like('emphasis2')
highlight_selector = StyledText.like('highlight')
highlight2_selector = StyledText.like('highlight2')
paragraph_selector = Paragraph
paragraph2_selector = Paragraph.like('paragraph2')
paragraph3_selector = Paragraph.like('paragraph3')
paragraph4_selector = Paragraph.like('paragraph4')
missing_selector = Paragraph.like('missing')

matcher = StyledMatcher({
    'emphasized text': emphasis_selector,
    'emphasized text 2': emphasis2_selector,
    'highlighted text': highlight_selector,
    'highlighted text 2': highlight2_selector,
    'paragraph': paragraph_selector,
Ejemplo n.º 11
0
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')])
Ejemplo n.º 12
0
#
# Copyright (c) Brecht Machiels.
#
# Use of this source code is subject to the terms of the GNU Affero General
# Public License v3. See the LICENSE file or http://www.gnu.org/licenses/.

import pytest

from rinoh.dimension import PT
from rinoh.document import DocumentTree, Document, FakeContainer
from rinoh.language import EN
from rinoh.paragraph import Paragraph
from rinoh.text import StyledText, SingleStyledText
from rinoh.style import StyleSheet, StyledMatcher, Specificity

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')