Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7cf8fff4e4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 82 lines (70 sloc) 2.608 kb
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 73 74 75 76 77 78 79 80 81
import os
import web

class Parser:
    def __init__(self):
        self.mode = 'normal'
        self.text = ''
        
    def go(self, pyfile):
        for line in file(pyfile):
            if self.mode == 'in def':
                self.text += ' ' + line.strip()
                if line.strip().endswith(':'):
                    if self.definition(self.text):
                        self.text = ''
                        self.mode = 'in func'
                    else:
                        self.text = ''
                        self.mode = 'normal'

            elif self.mode == 'in func':
                if '"""' in line:
                    self.text += line.strip().strip('"')
                    self.mode = 'in doc'
                    if line.count('"""') == 2:
                        self.mode = 'normal'
                        self.docstring(self.text)
                        self.text = ''
                else:
                    self.mode = 'normal'

            elif self.mode == 'in doc':
                self.text += ' ' + line
                if '"""' in line:
                    self.mode = 'normal'
                    self.docstring(self.text.strip().strip('"'))
                    self.text = ''
            
            elif line.startswith('## '):
                self.header(line.strip().strip('#'))
            
            elif line.startswith('def ') or line.startswith('class '):
                self.text += line.strip().strip(':')
                if line.strip().endswith(':'):
                    if self.definition(self.text):
                        self.text = ''
                        self.mode = 'in func'
                    else:
                        self.text = ''
                        self.mode = 'normal'
                else:
                    self.mode = 'in def'
    
    def clean(self, text):
        text = text.strip()
        text = text.replace('*', r'\*')
        return text
    
    def definition(self, text):
        text = web.lstrips(text, 'def ')
        if text.startswith('_') or text.startswith('class _'):
            return False
        print '`'+text.strip()+'`'
        return True
    
    def docstring(self, text):
        print ' :', text.strip()
        print
    
    def header(self, text):
        print '##', text.strip()
        print
        
for pyfile in os.listdir('trunk/web'):
    if pyfile[-2:] == 'py':
        print
        print '## ' + pyfile
        print
        Parser().go('trunk/web/' + pyfile)
print '`ctx`\n :',
print '\n'.join(' '+x for x in web.ctx.__doc__.strip().split('\n'))
Something went wrong with that request. Please try again.