def test_paragraph(self): source = """\ This is a one-line paragraph. """ expected_tree = [ParagraphState('This is a one-line paragraph.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_bold(self): source = """\ Paragraph with **emphasized text**. """ expected_tree = [ ParagraphState('Paragraph with <b>emphasized text</b>.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_diagram_and_paragraph(self): source = """\ Short diagram. Following paragraph. """ expected_tree = [DiagramState('Short diagram.\n'), ParagraphState('Following paragraph.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_heading2(self): source = """\ ## Title # Some text. """ expected_tree = [ParsingState('Title', Styles.Heading2), ParagraphState('Some text.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_bold(self): source = """\ Paragraph with **emphasized text**. """ expected_tree = [ ParagraphState('Paragraph with <b>emphasized text</b>.') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_links(self): source = """\ Paragraph with [example link][link]. [link]: http://example.com """ expected_tree = [ParagraphState( 'Paragraph with <a href="http://example.com">example link</a>.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_two_paragraphs(self): source = """\ This is a paragraph. This is another. """ expected_tree = [ParagraphState('This is a paragraph.'), ParagraphState('This is another.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_heading2(self): source = """\ ## Title # Some text. """ expected_tree = [ ParsingState('Title', Styles.Heading2), ParagraphState('Some text.') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_diagram_and_paragraph(self): source = """\ Short diagram. Following paragraph. """ expected_tree = [ DiagramState('Short diagram.\n'), ParagraphState('Following paragraph.') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_two_links(self): source = """\ Now [two][first] [links][second]. [first]: http://example.com [second]: http://bogus.com """ expected_tree = [ParagraphState( 'Now <a href="http://example.com">two</a> ' '<a href="http://bogus.com">links</a>.')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_numbered(self): source = """\ Some text. 1. First 2. Second """ expected_tree = [ParagraphState('Some text.'), BulletedState('First', bullet='1'), BulletedState('Second', bullet='2')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_two_paragraphs(self): source = """\ This is a paragraph. This is another. """ expected_tree = [ ParagraphState('This is a paragraph.'), ParagraphState('This is another.') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_two_links(self): source = """\ Now [two][first] [links][second]. [first]: http://example.com [second]: http://bogus.com """ expected_tree = [ ParagraphState('Now <a href="http://example.com">two</a> ' '<a href="http://bogus.com">links</a>.') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_links(self): source = """\ Paragraph with [example link][link]. [link]: http://example.com """ expected_tree = [ ParagraphState( 'Paragraph with <a href="http://example.com">example link</a>.' ) ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_diagram(self): source = """\ Indent of four spaces makes a diagram. Indent is removed. """ expected_diagram = """\ Indent of four spaces makes a diagram. Indent is removed. """ expected_tree = [DiagramState(expected_diagram)] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_numbered(self): source = """\ Some text. 1. First 2. Second """ expected_tree = [ ParagraphState('Some text.'), BulletedState('First', bullet='1'), BulletedState('Second', bullet='2') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_numbered_multiple_lines(self): source = """\ Some text. 1. First item with multiple lines. 2. Second item. """ expected_tree = [ ParagraphState('Some text.'), BulletedState('First item with multiple lines.', bullet='1'), BulletedState('Second item.', bullet='2')] tree = parse(source) self.assertEqual(expected_tree, tree)
def test_numbered_multiple_lines(self): source = """\ Some text. 1. First item with multiple lines. 2. Second item. """ expected_tree = [ ParagraphState('Some text.'), BulletedState('First item with multiple lines.', bullet='1'), BulletedState('Second item.', bullet='2') ] tree = parse(source) self.assertEqual(expected_tree, tree)
def go(): doc = SimpleDocTemplate("moonside.pdf") styles = getSampleStyleSheet() paragraph_style = styles[Styles.Normal] list_style = ListStyle('default_list', bulletFontSize=paragraph_style.fontSize, bulletFormat='%s.') story = [] f = open('rules.md') group = [] bulleted = [] first_bullet = None states = parse(f.read()) for state in states: if state.style == Styles.Diagram: flowable = Diagram(state.text) else: flowable = Paragraph(state.text, styles[state.style]) if state.style.startswith(Styles.Heading): group.append(flowable) elif state.bullet: bulleted.append(flowable) first_bullet = first_bullet or state.bullet else: if bulleted: story.append(ListFlowable(bulleted, style=list_style, start=first_bullet)) bulleted = [] first_bullet = None story.append(Spacer(1, 0.055*inch)) if not group: story.append(flowable) else: group.append(flowable) story.append(KeepTogether(group)) group = [] story.append(Spacer(1, 0.055*inch)) if bulleted: story.append(ListFlowable(bulleted, style=list_style, start=first_bullet)) doc.build(story, onFirstPage=firstPage)
def go(): doc = SimpleDocTemplate("docs/donimoes.pdf") styles = getSampleStyleSheet() paragraph_style = styles[Styles.Normal] list_style = ListStyle('default_list', bulletFontSize=paragraph_style.fontSize, bulletFormat='%s.') story = [] f = open('docs/rules.md') group = [] bulleted = [] first_bullet = None states = parse(f.read()) for state in states: if state.style == Styles.Diagram: flowable = Diagram(state.text) else: flowable = Paragraph(state.text, styles[state.style]) if state.style.startswith(Styles.Heading): group.append(flowable) elif state.bullet: bulleted.append(flowable) first_bullet = first_bullet or state.bullet else: if bulleted: story.append( ListFlowable(bulleted, style=list_style, start=first_bullet)) bulleted = [] first_bullet = None story.append(Spacer(1, 0.055 * inch)) if not group: story.append(flowable) else: group.append(flowable) story.append(KeepTogether(group)) group = [] story.append(Spacer(1, 0.055 * inch)) if bulleted: story.append( ListFlowable(bulleted, style=list_style, start=first_bullet)) doc.build(story, onFirstPage=firstPage)
def main(): with open('rules.md', 'rU') as f: rules_text = f.read() states = parse(rules_text) scores = '' heading = '' for state in states: if state.style == Styles.Diagram and heading.startswith('Problem'): n = heading.split(' ')[-1] board = Board.create(state.text) analysis = BoardAnalysis(board) if board.hasMatch(): n += '(matches)' print(n + '. ' + ', '.join(analysis.solution).upper()) score = BoardAnalysis.calculate_score(analysis.get_values()) scores += '{}. {}x{} {} ({} nodes)\n'.format(n, board.width, board.height, score, analysis.graph_size) if state.style.startswith(Styles.Heading): heading = state.text print(scores)