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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#Copyright ReportLab Europe Ltd. 2000-2012
#see license.txt for license details
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/tools/docco/yaml.py
# parses "Yet Another Markup Language" into a list of tuples.
# Each tuple says what the data is e.g.
# ('Paragraph', 'Heading1', 'Why Reportlab Rules')
# and the pattern depends on type.
"""
Parser for "Aaron's Markup Language" - a markup language
which is easier to type in than XML, yet gives us a
reasonable selection of formats.
The general rule is that if a line begins with a '.',
it requires special processing. Otherwise lines
are concatenated to paragraphs, and blank lines
separate paragraphs.
If the line ".foo bar bletch" is encountered,
it immediately ends and writes out any current
paragraph.
It then looks for a parser method called 'foo';
if found, it is called with arguments (bar, bletch).
If this is not found, it assumes that 'foo' is a
paragraph style, and the text for the first line
of the paragraph is 'bar bletch'. It would be
up to the formatter to decide whether on not 'foo'
was a valid paragraph.
Special commands understood at present are:
.image filename
- adds the image to the document
.beginPre Code
- begins a Preformatted object in style 'Code'
.endPre
- ends a preformatted object.
"""
import sys
import imp
from . import codegrab
#modes:
PLAIN = 1
PREFORMATTED = 2
BULLETCHAR = '\267' # assumes font Symbol, but works on all platforms
class Parser:
def __init__(self):
self.reset()
def reset(self):
self._lineNo = 0
self._style = 'Normal' # the default
self._results = []
self._buf = []
self._mode = PLAIN
def parseFile(self, filename):
#returns list of objects
data = open(filename, 'r').readlines()
for line in data:
#strip trailing newlines
self.readLine(line[:-1])
self.endPara()
return self._results
def readLine(self, line):
#this is the inner loop
self._lineNo = self._lineNo + 1
stripped = line.lstrip()
if len(stripped) == 0:
if self._mode == PLAIN:
self.endPara()
else: #preformatted, append it
self._buf.append(line)
elif line[0]=='.':
# we have a command of some kind
self.endPara()
words = stripped[1:].split()
cmd, args = words[0], words[1:]
#is it a parser method?
if hasattr(self.__class__, cmd):
#this was very bad; any type error in the method was hidden
#we have to hack the traceback
try:
getattr(self,cmd)(*args)
except TypeError as err:
sys.stderr.write("Parser method: %s(*%s) %s at line %d\n" % (cmd, tuple(args), err, self._lineNo))
raise
else:
# assume it is a paragraph style -
# becomes the formatter's problem
self.endPara() #end the last one
words = stripped.split(' ', 1)
assert len(words)==2, "Style %s but no data at line %d" % (words[0], self._lineNo)
(styletag, data) = words
self._style = styletag[1:]
self._buf.append(data)
else:
#we have data, add to para
self._buf.append(line)
def endPara(self):
#ends the current paragraph, or preformatted block
text = ' '.join(self._buf)
if text:
if self._mode == PREFORMATTED:
#item 3 is list of lines
self._results.append(('Preformatted', self._style,
'\n'.join(self._buf)))
else:
self._results.append(('Paragraph', self._style, text))
self._buf = []
self._style = 'Normal'
def beginPre(self, stylename):
self._mode = PREFORMATTED
self._style = stylename
def endPre(self):
self.endPara()
self._mode = PLAIN
def image(self, filename):
self.endPara()
self._results.append(('Image', filename))
def vSpace(self, points):
"""Inserts a vertical spacer"""
self._results.append(('VSpace', points))
def pageBreak(self):
"""Inserts a frame break"""
self._results.append(('PageBreak','blah')) # must be a tuple
def custom(self, moduleName, funcName):
"""Goes and gets the Python object and adds it to the story"""
self.endPara()
self._results.append(('Custom',moduleName, funcName))
def getModuleDoc(self, modulename, pathname=None):
"""Documents the entire module at this point by making
paragraphs and preformatted objects"""
docco = codegrab.getObjectsDefinedIn(modulename, pathname)
if docco.doc != None:
self._results.append(('Paragraph', 'DocString', docco.doc))
if len(docco.functions) > 0:
for fn in docco.functions:
if fn.status == 'official':
self._results.append(('Preformatted','FunctionHeader', fn.proto))
self._results.append(('Preformatted','DocString', fn.doc))
if len(docco.classes) > 0:
for cls in docco.classes:
if cls.status == 'official':
self._results.append(('Preformatted','FunctionHeader', 'Class %s:' % cls.name))
self._results.append(('Preformatted','DocString', cls.doc))
for mth in cls.methods:
if mth.status == 'official':
self._results.append(('Preformatted','FunctionHeader', mth.proto))
self._results.append(('Preformatted','DocStringIndent', mth.doc))
def getClassDoc(self, modulename, classname, pathname=None):
"""Documents the class and its public methods"""
docco = codegrab.getObjectsDefinedIn(modulename, pathname)
found = 0
for cls in docco.classes:
if cls.name == classname:
found = 1
self._results.append(('Preformatted','FunctionHeader', 'Class %s:' % cls.name))
self._results.append(('Preformatted','DocString', cls.doc))
for mth in cls.methods:
if mth.status == 'official':
self._results.append(('Preformatted','FunctionHeader', mth.proto))
self._results.append(('Preformatted','DocStringIndent', mth.doc))
break
assert found, 'No Classes Defined in ' + modulename
def nextPageTemplate(self, templateName):
self._results.append(('NextPageTemplate',templateName))
if __name__=='__main__': #NORUNTESTS
if len(sys.argv) != 2:
print('usage: yaml.py source.txt')
else:
p = Parser()
results = p.parseFile(sys.argv[1])
import pprint
pprint.pprint(results)
|