summaryrefslogtreecommitdiff
path: root/sandbox/aahz/OO/OOdirectives.py
blob: e437f6fbfa4a800936aaf9d96a2241108dab9682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import os

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst.languages import en

registry = directives._directive_registry
registry['index'] = ('OOdirectives', 'index_directive')
registry['include-code'] = ('OOdirectives', 'include_code')
registry['include-output'] = ('OOdirectives', 'include_output')

en.directives['index'] = 'index'
en.directives['include-code'] = 'include-code'
en.directives['include-output'] = 'include-output'


class index_entry(nodes.General, nodes.Element): pass
class index_entry(nodes.Inline, nodes.TextElement): pass

def index_directive(name, arguments, options, content, lineno,
        content_offset, block_text, state, state_machine):
    #nodes = []
    #for entry in content:
    #    nodes.append(index_entry(entry, entry))
    #return nodes
    entries = '\n'.join(content)
    return [index_entry(entries,entries)]

index_directive.content = 1



def include_code(name, arguments, options, content, lineno,
        content_offset, block_text, state, state_machine):
    #obj = state_machine.document.attributes
    #print >> sys.stderr, obj
    #print >> sys.stderr, dir(obj)
    document = state_machine.document
    dirname = getDocDir(document)
    fname = os.path.join(dirname, arguments[0])
    code = open(fname).read()
    return [nodes.literal_block(code, code)]

include_code.arguments = (0, 1, 0)



def include_output(name, arguments, options, content, lineno,
        content_offset, block_text, state, state_machine):
    document = state_machine.document
    dirname = getDocDir(document)
    fname = os.path.join(dirname, arguments[0])
    cmd = os.environ['PYTHON'] + ' ' + fname
    f_input, f_output = os.popen4(cmd)
    output = f_output.read()
    f_output.close()
    return [nodes.literal_block(output, output)]

include_output.arguments = (0, 1, 0)


def getDocDir(document):
    source = document.current_source
    if source is None:
        return os.getcwd()
    else:
        dirname = os.path.dirname(os.path.abspath(source))
        if dirname is None:
            return os.getcwd()
        else:
            return dirname