In [21]:
from docutils.parsers import rst
from docutils.nodes import document
from docutils.utils import new_document
from docutils.frontend import OptionParser

In [11]:
document??

In [19]:
rst??

In [10]:
new_document?

In [22]:
settings = OptionParser(
    components=(rst.Parser,)
    ).get_default_values()

In [23]:
parser = rst.Parser()
document = new_document('.', settings)

In [19]:
infile = '../rest/puppet/puppet.rst'
infile = 'rest/babelfish/chameleon.rst'

In [3]:
!pwd


/home/jng/devel/lamlam/notebooks

In [24]:
parser.parse(open(infile).read(), document)

In [6]:
for x in document:
    print(x)
    for ii in x:
        print(ii)


<section ids="puppet" names="puppet"><title>Puppet</title><section ids="all-puppets" names="all\ puppets"><title>all puppets</title><paragraph>trust:</paragraph><block_quote><paragraph>I am a software puppet</paragraph></block_quote><paragraph>hardware:</paragraph><block_quote><paragraph>I am a software puppet</paragraph></block_quote><paragraph>are there other kinds of puppet?:</paragraph><block_quote><paragraph>yes</paragraph></block_quote><paragraph>see identity</paragraph><paragraph>see fun</paragraph><paragraph>see teach</paragraph><paragraph>see story</paragraph></section></section>
<title>Puppet</title>
<section ids="all-puppets" names="all\ puppets"><title>all puppets</title><paragraph>trust:</paragraph><block_quote><paragraph>I am a software puppet</paragraph></block_quote><paragraph>hardware:</paragraph><block_quote><paragraph>I am a software puppet</paragraph></block_quote><paragraph>are there other kinds of puppet?:</paragraph><block_quote><paragraph>yes</paragraph></block_quote><paragraph>see identity</paragraph><paragraph>see fun</paragraph><paragraph>see teach</paragraph><paragraph>see story</paragraph></section>

In [45]:
def dump(data, depth=0):

    for item in data:
        print(len(item.children))
        tag = item.tagname
        text = 
        print(' ' * depth, item.tagname, item.astext())

        for sub in item:
            if str(sub) != sub:
                dump(sub, depth+1)

In [59]:
document.walk?

In [54]:
[x.tagname for x in s.children]


Out[54]:
['title', 'section', 'section']

In [47]:
dump(document)


3
 section Chameleon

Change

Song

self 0 <3 infinity
0
  #text Chameleon
1
  title Change
1
  title Song
1
  paragraph self 0 <3 infinity

In [28]:
dump(document)
print()
dump(document[0])


 <section ids="chameleon" names="chameleon"><title>Chameleon</title><section ids="change" names="change"><title>Change</title></section><section ids="song" names="song"><title>Song</title><paragraph>self 0 <3 infinity</paragraph></section></section> <class 'docutils.nodes.section'>
  Chameleon <class 'docutils.nodes.Text'>
  <title>Change</title> <class 'docutils.nodes.title'>
  <title>Song</title> <class 'docutils.nodes.title'>
  <paragraph>self 0 <3 infinity</paragraph> <class 'docutils.nodes.paragraph'>

 <title>Chameleon</title> <class 'docutils.nodes.title'>
 <section ids="change" names="change"><title>Change</title></section> <class 'docutils.nodes.section'>
  Change <class 'docutils.nodes.Text'>
 <section ids="song" names="song"><title>Song</title><paragraph>self 0 <3 infinity</paragraph></section> <class 'docutils.nodes.section'>
  Song <class 'docutils.nodes.Text'>
  self 0 <3 infinity <class 'docutils.nodes.Text'>

In [20]:
cd ..


/home/jng/devel/lamlam

In [1]:
cd ~/devel/lamlam


/home/jng/devel/lamlam

In [13]:
run bootstrap.py rest/babelfish/chameleon.rst


code/babelfish/chameleon.py

In [3]:
run code/babelfish/chameleon.py


Cogging /home/jng/devel/lamlam/code/babelfish/chameleon.py
---- <section ids="chameleon" names <class 'docutils.nodes.section'>
section
SECTION
SUB <title>Chameleon</title>

------ Chameleon <class 'docutils.nodes.Text'>
text
SUB <section ids="change" names="change"><title>Change</title></section>

------ <title>Change</title> <class 'docutils.nodes.title'>
title
SUB <section ids="song" names="song"><title>Song</title><paragraph>self 0 <3 infinity</paragraph></section>

------ <title>Song</title> <class 'docutils.nodes.title'>
title
------ <paragraph>self 0 <3 infinity< <class 'docutils.nodes.paragraph'>
paragraph
imports: []
response: ['# section: content: Chameleon\n\nChange\n\nSong\n\nself 0 <3 infinity depth: 0', '# Text: content: Chameleon depth: 1', '# title: content: Change depth: 1', '# title: content: Song depth: 1', '# paragraph: content: self 0 <3 infinity depth: 1']
  (changed)

In [35]:
for xx in document[0]:
    print(xx.tagname, xx.astext())


title Chameleon
section Change
section Song

self 0 <3 infinity