/usr/share/pyshared/zope/structuredtext/stng.py is in python-zope.structuredtext 3.5.1-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | ##############################################################################
#
# Copyright (c) 2001 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
""" Core document model.
"""
import re
import stdom
__metaclass__ = type
def indention(str, front=re.compile("^\s+").match):
"""Find the number of leading spaces. If none, return 0.
"""
result = front(str)
if result is not None:
start, end = result.span()
return end-start
else:
return 0 # no leading spaces
def insert(struct, top, level):
"""Find what will be the parant paragraph of a sentence and return
that paragraph's sub-paragraphs. The new paragraph will be
appended to those sub-paragraphs
"""
if not top-1 in range(len(struct)):
if struct:
return struct[len(struct)-1].getSubparagraphs()
return struct
run = struct[top-1]
i = 0
while i+1 < level:
run = run.getSubparagraphs()[len(run.getSubparagraphs())-1]
i = i + 1
return run.getSubparagraphs()
def display(struct):
"""Runs through the structure and prints out the paragraphs. If
the insertion works correctly, display's results should mimic the
orignal paragraphs.
"""
if struct.getColorizableTexts():
print '\n'.join(struct.getColorizableTexts())
if struct.getSubparagraphs():
for x in struct.getSubparagraphs():
display(x)
def display2(struct):
"""Runs through the structure and prints out the paragraphs. If
the insertion works correctly, display's results should mimic the
orignal paragraphs.
"""
if struct.getNodeValue():
print struct.getNodeValue(),"\n"
if struct.getSubparagraphs():
for x in struct.getSubparagraphs():
display(x)
def findlevel(levels,indent):
"""Remove all level information of levels with a greater level of
indentation. Then return which level should insert this paragraph
"""
keys = levels.keys()
for key in keys:
if levels[key] > indent:
del(levels[key])
keys = levels.keys()
if not(keys):
return 0
else:
for key in keys:
if levels[key] == indent:
return key
highest = 0
for key in keys:
if key > highest:
highest = key
return highest-1
def flatten(obj, append):
if obj.getNodeType() == stdom.TEXT_NODE:
append(obj.getNodeValue())
else:
for child in obj.getChildNodes():
flatten(child, append)
para_delim = r'(\n\s*\n|\r\n\s*\r\n)' # UNIX or DOS line endings, respectively
def structurize(paragraphs, delimiter=re.compile(para_delim)):
"""Accepts paragraphs, which is a list of lines to be
parsed. structurize creates a structure which mimics the
structure of the paragraphs. Structure =>
[paragraph,[sub-paragraphs]]
"""
currentlevel = 0
currentindent = 0
levels = {0:0}
level = 0 # which header are we under
struct = [] # the structure to be returned
run = struct
paragraphs = paragraphs.expandtabs()
paragraphs = '%s%s%s' % ('\n\n', paragraphs, '\n\n')
paragraphs = delimiter.split(paragraphs)
paragraphs = [ x for x in paragraphs if x.strip() ]
if not paragraphs:
return StructuredTextDocument()
ind = [] # structure based on indention levels
for paragraph in paragraphs:
ind.append([indention(paragraph), paragraph])
currentindent = indention(paragraphs[0])
levels[0] = currentindent
for indent,paragraph in ind :
if indent == 0:
level = level + 1
currentlevel = 0
currentindent = 0
levels = {0:0}
struct.append(StructuredTextParagraph(paragraph,
indent=indent,
level=currentlevel))
elif indent > currentindent:
currentlevel = currentlevel + 1
currentindent = indent
levels[currentlevel] = indent
run = insert(struct,level,currentlevel)
run.append(StructuredTextParagraph(paragraph,
indent=indent,
level=currentlevel))
elif indent < currentindent:
result = findlevel(levels,indent)
if result > 0:
currentlevel = result
currentindent = indent
if not level:
struct.append(StructuredTextParagraph(paragraph,
indent=indent,
level=currentlevel))
else:
run = insert(struct,level,currentlevel)
run.append(StructuredTextParagraph(paragraph,
indent=indent,
level=currentlevel))
else:
if insert(struct,level,currentlevel):
run = insert(struct,level,currentlevel)
else:
run = struct
currentindent = indent
run.append(StructuredTextParagraph(paragraph,
indent=indent,
level=currentlevel))
return StructuredTextDocument(struct)
class StructuredTextParagraph(stdom.Element):
indent = 0
def __init__(self, src, subs=None, **kw):
if subs is None:
subs=[]
self._src = src
self._subs = list(subs)
self._attributes = kw.keys()
for k, v in kw.items():
setattr(self, k, v)
def getChildren(self):
src=self._src
if not isinstance(src, list):
src=[src]
return src+self._subs
def getAttribute(self, name):
return getattr(self, name, None)
def getAttributeNode(self, name):
if hasattr(self, name):
return stdom.Attr(name, getattr(self, name))
else:
return None
def getAttributes(self):
d = {}
for a in self._attributes:
d[a]=getattr(self, a, '')
return stdom.NamedNodeMap(d)
def getSubparagraphs(self):
return self._subs
def setSubparagraphs(self, subs):
self._subs=subs
def getColorizableTexts(self):
return (self._src,)
def setColorizableTexts(self, src):
self._src=src[0]
def __repr__(self):
r=[]; a=r.append
a((' '*(self.indent or 0))+
('%s(' % self.__class__.__name__)
+str(self._src)+', ['
)
for p in self._subs: a(`p`)
a((' '*(self.indent or 0))+'])')
return '\n'.join(r)
class StructuredTextDocument(StructuredTextParagraph):
"""A StructuredTextDocument holds StructuredTextParagraphs
as its subparagraphs.
"""
_attributes=()
def __init__(self, subs=None, **kw):
super(StructuredTextDocument, self).__init__('', subs, **kw)
def getChildren(self):
return self._subs
def getColorizableTexts(self):
return ()
def setColorizableTexts(self, src):
pass
def __repr__(self):
r=[]; a=r.append
a('%s([' % self.__class__.__name__)
for p in self._subs: a(`p`+',')
a('])')
return '\n'.join(r)
class StructuredTextExample(StructuredTextParagraph):
"""Represents a section of document with literal text, as for examples"""
def __init__(self, subs, **kw):
t = []
for s in subs:
flatten(s, t.append)
super(StructuredTextExample, self).__init__('\n\n'.join(t), (), **kw)
def getColorizableTexts(self):
return ()
def setColorizableTexts(self, src):
pass # never color examples
class StructuredTextBullet(StructuredTextParagraph):
"""Represents a section of a document with a title and a body
"""
class StructuredTextNumbered(StructuredTextParagraph):
"""Represents a section of a document with a title and a body
"""
class StructuredTextDescriptionTitle(StructuredTextParagraph):
"""Represents a section of a document with a title and a body
"""
class StructuredTextDescriptionBody(StructuredTextParagraph):
"""Represents a section of a document with a title and a body
"""
class StructuredTextDescription(StructuredTextParagraph):
"""Represents a section of a document with a title and a body
"""
def __init__(self, title, src, subs, **kw):
super(StructuredTextDescription, self).__init__(src, subs, **kw)
self._title = title
def getColorizableTexts(self):
return self._title, self._src
def setColorizableTexts(self, src):
self._title, self._src = src
def getChildren(self):
return (StructuredTextDescriptionTitle(self._title),
StructuredTextDescriptionBody(self._src, self._subs))
class StructuredTextSectionTitle(StructuredTextParagraph):
"""Represents a section of a document with a title and a body"""
class StructuredTextSection(StructuredTextParagraph):
"""Represents a section of a document with a title and a body"""
def __init__(self, src, subs=None, **kw):
super(StructuredTextSection, self).__init__(
StructuredTextSectionTitle(src), subs, **kw)
def getColorizableTexts(self):
return self._src.getColorizableTexts()
def setColorizableTexts(self,src):
self._src.setColorizableTexts(src)
# a StructuredTextTable holds StructuredTextRows
class StructuredTextTable(StructuredTextParagraph):
"""
rows is a list of lists containing tuples, which
represent the columns/cells in each rows.
EX
rows = [[('row 1:column1',1)],[('row2:column1',1)]]
"""
def __init__(self, rows, src, subs, **kw):
super(StructuredTextTable, self).__init__(subs, **kw)
self._rows = []
for row in rows:
if row:
self._rows.append(StructuredTextRow(row,kw))
def getRows(self):
return [self._rows]
def _getRows(self):
return self.getRows()
def getColumns(self):
result = []
for row in self._rows:
result.append(row.getColumns())
return result
def _getColumns(self):
return self.getColumns()
def setColumns(self,columns):
for index in range(len(self._rows)):
self._rows[index].setColumns(columns[index])
def _setColumns(self,columns):
return self.setColumns(columns)
def getColorizableTexts(self):
"""
return a tuple where each item is a column/cell's
contents. The tuple, result, will be of this format.
("r1 col1", "r1=col2", "r2 col1", "r2 col2")
"""
result = []
for row in self._rows:
for column in row.getColumns()[0]:
result.append(column.getColorizableTexts()[0])
return result
def setColorizableTexts(self,texts):
"""
texts is going to a tuple where each item is the
result of being mapped to the colortext function.
Need to insert the results appropriately into the
individual columns/cells
"""
for row_index in range(len(self._rows)):
for column_index in range(len(self._rows[row_index]._columns)):
self._rows[row_index
]._columns[column_index
].setColorizableTexts((texts[0],))
texts = texts[1:]
def _getColorizableTexts(self):
return self.getColorizableTexts()
def _setColorizableTexts(self, texts):
return self.setColorizableTexts(texts)
# StructuredTextRow holds StructuredTextColumns
class StructuredTextRow(StructuredTextParagraph):
def __init__(self,row,kw):
"""
row is a list of tuples, where each tuple is
the raw text for a cell/column and the span
of that cell/column.
EX
[('this is column one',1), ('this is column two',1)]
"""
super(StructuredTextRow, self).__init__([], **kw)
self._columns = []
for column in row:
self._columns.append(StructuredTextColumn(column[0],
column[1],
column[2],
column[3],
column[4],
kw))
def getColumns(self):
return [self._columns]
def _getColumns(self):
return [self._columns]
def setColumns(self,columns):
self._columns = columns
def _setColumns(self,columns):
return self.setColumns(columns)
# this holds the text of a table cell
class StructuredTextColumn(StructuredTextParagraph):
"""
StructuredTextColumn is a cell/column in a table.
A cell can hold multiple paragraphs. The cell
is either classified as a StructuredTextTableHeader
or StructuredTextTableData.
"""
def __init__(self,text,span,align,valign,typ,kw):
super(StructuredTextColumn, self).__init__(text, [], **kw)
self._span = span
self._align = align
self._valign = valign
self._type = typ
def getSpan(self):
return self._span
def _getSpan(self):
return self._span
def getAlign(self):
return self._align
def _getAlign(self):
return self.getAlign()
def getValign(self):
return self._valign
def _getValign(self):
return self.getValign()
def getType(self):
return self._type
def _getType(self):
return self.getType()
class StructuredTextTableHeader(StructuredTextParagraph):
pass
class StructuredTextTableData(StructuredTextParagraph):
pass
class StructuredTextMarkup(stdom.Element):
def __init__(self, value, **kw):
self._value = value
self._attributes = kw.keys()
for key, value in kw.items():
setattr(self, key, value)
def getChildren(self):
v=self._value
if not isinstance(v, list):
v = [v]
return v
def getColorizableTexts(self):
return self._value,
def setColorizableTexts(self, v):
self._value=v[0]
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, `self._value`)
class StructuredTextLiteral(StructuredTextMarkup):
def getColorizableTexts(self):
return ()
def setColorizableTexts(self, v):
pass
class StructuredTextEmphasis(StructuredTextMarkup):
pass
class StructuredTextStrong(StructuredTextMarkup):
pass
class StructuredTextInnerLink(StructuredTextMarkup):
pass
class StructuredTextNamedLink(StructuredTextMarkup):
pass
class StructuredTextUnderline(StructuredTextMarkup):
pass
class StructuredTextSGML(StructuredTextMarkup):
pass
class StructuredTextLink(StructuredTextMarkup):
pass
class StructuredTextXref(StructuredTextMarkup):
pass
class StructuredTextImage(StructuredTextMarkup):
"""A simple embedded image
"""
|