~jurgis-pralgauskis/intro-to-code/trunk

« back to all changes in this revision

Viewing changes to intro-to-code-4web/intro2code/split_txt2exaples.py

  • Committer: jurgis
  • Date: 2009-03-11 23:19:24 UTC
  • Revision ID: jurgis@baltix-20090311231924-yut5x52kedu9ktd2
some progress experimenting with ajax -- now I get the new example text separately
to achieve this I added split_txt2examples.py which takes tutorial text or  CLI buffer and returns list of exaples, which consists of: intro, prompt, expected input  
and etc fields (interaction_output, extra_empty_lines, full_txt)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
import re
 
4
prompt_start = r'>>>|<<<|\?\?\?|>>\?'
 
5
prompt_other_lines_start =  r'==>|\.\.\.'
 
6
pattern = (r'^(?:%s).*?\n' % prompt_start) + r'(?:(?:%s).*?\n)*' % ( prompt_other_lines_start) + \
 
7
            r'(?:.+?\n)?' + r'\n*'
 
8
#~ pattern = r'^(?:%s).*?\n' % prompt_start 
 
9
 
 
10
print "pattern:", repr(pattern)
 
11
re_code_block = re.compile(r'(%s)' % pattern, re.DOTALL+re.MULTILINE)
 
12
re_prompt_start = re.compile(prompt_start)
 
13
 
 
14
#~ empty_space_atend = re.compile(r'\s+$', re.MULTILINE)
 
15
def structure_example(intro, code_block):
 
16
    #~ if intro.strip('\n\t ')  = '':
 
17
        #~ intro = '';
 
18
    full_txt = intro + code_block
 
19
    extra_empty_lines = 0
 
20
    try:
 
21
        while code_block[:-extra_empty_lines-1].endswith('\n'):
 
22
            extra_empty_lines += 1
 
23
    except KeyError:
 
24
        pass
 
25
    if extra_empty_lines:
 
26
            code_block = code_block[:-extra_empty_lines]
 
27
    
 
28
    prompt = code_block
 
29
    expected_input = ''
 
30
    interaction_output = ''
 
31
    
 
32
    if '==>' in code_block:
 
33
        pos = code_block.find('==>') 
 
34
        prompt, expected_input = code_block[:pos], code_block[pos:]
 
35
 
 
36
    #~ if not expected_input:
 
37
    code_lines = prompt.split('\n')
 
38
    for lineno, line in enumerate(code_lines):
 
39
        #~ if not re_prompt_start.match(line):
 
40
        if not line[:3] in ['>>>', '<<<', '???', '...', '>>?']:
 
41
            prompt = '\n'.join(code_lines[:lineno])
 
42
            interaction_output = '\n'.join(code_lines[lineno:])
 
43
            break
 
44
    #~ if expected_input and not expected_input.startswith('==>'):
 
45
        #~ expected_input = '==>' + expected_input
 
46
 
 
47
    
 
48
    return dict(
 
49
                intro=intro,
 
50
                prompt=prompt,
 
51
                expected_input=expected_input,
 
52
                interaction_output=interaction_output,
 
53
                extra_empty_lines=extra_empty_lines,
 
54
                full_txt=full_txt
 
55
            )
 
56
 
 
57
def txt2structured_examples(txt):
 
58
    examples = re_code_block.split(txt+'\n')
 
59
    #~ exampls_struct = [structure_example(intro, code_block)
 
60
    structured_examples = []
 
61
 
 
62
    for i, x in enumerate(examples):
 
63
        if i % 2 == 0:
 
64
            intro = x
 
65
        else:
 
66
            structured_examples.append( structure_example(intro, code_block=x) )
 
67
                
 
68
    return structured_examples
 
69
 
 
70
if __name__ == '__main__':
 
71
    import analyse_tutorial_text
 
72
 
 
73
    #~ txt = analyse_tutorial_text.init_text('../tutorials/py/intro.txt')
 
74
 
 
75
    txt = """
 
76
== Elementary Operations == 
 
77
 
 
78
First of all, computers must compute (count). try: 
 
79
>>> 2 + 5*10 
 
80
result : 52 
 
81
 
 
82
>>> x = 3 * 12 
 
83
result x: 36 
 
84
 
 
85
 
 
86
Also they can write :) 
 
87
>>> print 'Hi'  # or could also use double quotes: "
 
88
"""
 
89
    print 'txt:', txt
 
90
    from pprint import pprint
 
91
 
 
92
    examples = re_code_block.split(txt)
 
93
    structured_examples = txt2structured_examples(txt)
 
94
 
 
95
    print 'structured_examples'
 
96
    pprint (structured_examples)
 
97
    print 'len(structured_examples):', len(structured_examples)
 
98
    print 'len(examples):', len(examples)