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'), ' ')
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')
def test_select_by_id(): paragraph1 = Paragraph('A paragraph with an ID.', id='par') paragraph2 = Paragraph('A paragraph with another ID.', id='another') paragraph3 = Paragraph('A paragraph without ID.') selector1 = Paragraph.like(id='par') selector2 = Paragraph.like(id='another') selector3 = Paragraph assert selector1.match(paragraph1, stylesheet, document) assert selector2.match(paragraph2, stylesheet, document) assert selector3.match(paragraph1, stylesheet, document) assert selector3.match(paragraph2, stylesheet, document) assert selector3.match(paragraph3, stylesheet, document) assert not selector1.match(paragraph2, stylesheet, document) assert not selector1.match(paragraph3, stylesheet, document) assert not selector2.match(paragraph1, stylesheet, document) assert not selector2.match(paragraph3, stylesheet, document)
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, 'paragraph 2': paragraph2_selector, 'paragraph 4': paragraph4_selector, 'missing style': missing_selector, })
from rinoh.color import HexColor from rinoh.dimension import PT, CM from rinoh.document import DocumentTree, Document, FakeContainer from rinoh.flowable import GroupedFlowables, StaticGroupedFlowables from rinoh.font import FontWeight, FontSlant, FontWidth from rinoh.language import EN from rinoh.paragraph import Paragraph, ParagraphStyle from rinoh.text import StyledText, SingleStyledText from rinoh.style import StyleSheet, StyledMatcher, PARENT_STYLE 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') grouped1_selector = GroupedFlowables.like('grouped1') paragraph7_selector = grouped1_selector / Paragraph.like('paragraph7') 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, 'paragraph 2': paragraph2_selector, 'paragraph 4': paragraph4_selector, 'grouped 1': grouped1_selector,