Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate downstream CEGID changes. #4

Merged
merged 14 commits into from
Jan 18, 2025
4 changes: 0 additions & 4 deletions marrow/__init__.py

This file was deleted.

271 changes: 136 additions & 135 deletions marrow/tags/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

import inspect

from copy import deepcopy
Expand All @@ -14,145 +12,148 @@


class Fragment(object):
def __init__(self, data=None, *args, **kw):
self.args = list(args)
self.attrs = kw
self.data = data
super(Fragment, self).__init__()
def __repr__(self):
return "<%s args=%r attrs=%r>" % (self.name, self.args, self.attrs)
def clear(self):
self.args = list()
self.attrs = dict()
def __init__(self, data=None, *args, **kw):
self.args = list(args)
self.attrs = kw
self.data = data
super(Fragment, self).__init__()
def __repr__(self):
return "<%s args=%r attrs=%r>" % (self.name, self.args, self.attrs)
def clear(self):
self.args = list()
self.attrs = dict()


class Text(Fragment):
def __init__(self, data, escape=True, *args, **kw):
super(Text, self).__init__(data, *args, **kw)
self.escape = escape
def __iter__(self):
yield escape(self.data) if self.escape else self.data
def __init__(self, data, escape=True, *args, **kw):
super(Text, self).__init__(data, *args, **kw)
self.escape = escape
def __iter__(self):
yield escape(self.data) if self.escape else self.data


class Flush(Fragment):
def __iter__(self):
yield ''
def __iter__(self):
yield ''


class Tag(Fragment):
def __init__(self, name, prefix=None, simple=False, strip=False, *args, **kw):
super(Tag, self).__init__([], *args, **kw)

self.name = name

self.prefix = prefix
self.simple = simple
self.strip = strip

def __call__(self, strip=NoDefault, *args, **kw):
self = deepcopy(self)

if strip is not NoDefault: self.strip = strip
self.args.extend(list(args))
self.attrs.update(kw)

return self

def __getitem__(self, k):
if not k: return self

self = deepcopy(self)

if not isinstance(k, (tuple, list)):
k = (k, )

for fragment in k:
if isinstance(fragment, basestring):
self.data.append(escape(fragment))
continue

self.data.append(fragment)

return self

def __repr__(self):
return "<%s children=%d args=%r attrs=%r>" % (self.name, len(self.data), self.args, self.attrs)

def __unicode__(self):
"""Return a serialized version of this tree/branch."""

return u''.join(unicode(i) for i in self)

def render(self):
buf = u""

for chunk in self:
if not chunk:
yield buf
buf = u""
continue

buf += chunk

# Handle the remaining data.
if buf:
yield buf

def __copy__(self):
return Tag(self.name, self.prefix, self.simple, self.strip, *self.args, **self.attrs)

def __iter__(self):
if not self.strip:
if self.prefix:
yield self.prefix

yield u'<' + self.name + u''.join([attr for attr in quoteattrs(self, self.attrs)]) + u'>'

if self.simple:
raise StopIteration()

for child in self.data:
if inspect.isgenerator(child):
for element in child:
if isinstance(element, unicode):
yield element
continue

for chunk in element:
yield chunk

continue

if isinstance(child, Fragment):
for element in child:
yield element

continue

if inspect.isroutine(child):
value = child()

if isinstance(value, unicode):
yield value
continue

for element in value:
yield element

continue

yield child

if not self.strip:
yield u'</' + self.name + u'>'

def clear(self):
self.data = []
super(Tag, self).clear()

def empty(self):
self.data = []
def __init__(self, name, prefix=None, simple=False, strip=False, *args, **kw):
super(Tag, self).__init__([], *args, **kw)

self.name = name

self.prefix = prefix
self.simple = simple
self.strip = strip

def __call__(self, strip=NoDefault, *args, **kw):
self = deepcopy(self)

if strip is not NoDefault: self.strip = strip
self.args.extend(list(args))
self.attrs.update(kw)

return self

def __getitem__(self, k):
if not k: return self

self = deepcopy(self)

if not isinstance(k, (tuple, list)):
k = (k, )

for fragment in k:
if isinstance(fragment, str):
self.data.append(escape(fragment))
continue

self.data.append(fragment)

return self

def __repr__(self):
return "<%s children=%d args=%r attrs=%r>" % (self.name, len(self.data), self.args, self.attrs)

def __unicode__(self):
"""Return a serialized version of this tree/branch."""

return u''.join(str(i) for i in self)

__str__ = __unicode__

def render(self):
buf = u""

for chunk in self:
if not chunk:
yield buf
buf = u""
continue

buf += chunk

# Handle the remaining data.
if buf:
yield buf

def __copy__(self):
return Tag(self.name, self.prefix, self.simple, self.strip, *self.args, **self.attrs)

def __iter__(self):
if not self.strip:
if self.prefix:
yield self.prefix

yield u'<' + self.name + u''.join([attr for attr in quoteattrs(self, self.attrs)]) + u'>'

if self.simple:
return

for child in self.data:
if inspect.isgenerator(child):
for element in child:
if isinstance(element, str):
yield element
continue

for chunk in element:
yield chunk

continue

if isinstance(child, Fragment):
for element in child:
yield element

continue

if inspect.isroutine(child):
value = child()

if isinstance(value, str):
yield value
continue

for element in value:
yield element

continue

yield child

if not self.strip:
yield u'</' + self.name + u'>'

def clear(self):
self.data = []
super(Tag, self).clear()

def empty(self):
self.data = []

20 changes: 9 additions & 11 deletions marrow/tags/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

# TODO: This is just an idea at the moment.
# See:
# http://code.activestate.com/recipes/276960/
Expand All @@ -10,12 +8,12 @@


class Filter(object):
def __init__(self):
self._input = None
def __ror__(self, left):
self._input = left
def __call__(self, context):
return self._input(context)
def __init__(self):
self._input = None
def __ror__(self, left):
self._input = left
def __call__(self, context):
return self._input(context)

3 changes: 1 addition & 2 deletions marrow/tags/html5.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

from marrow.tags import filters
from marrow.tags.base import Tag, Flush, Text

Expand Down Expand Up @@ -43,3 +41,4 @@ def __iter__(self):
__all__.append(f)

del _locals

3 changes: 1 addition & 2 deletions marrow/tags/release.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

"""Release information about Marrow Tags."""

from collections import namedtuple
Expand All @@ -11,3 +9,4 @@
version_info = namedtuple('version_info', ('major', 'minor', 'micro', 'releaselevel', 'serial'))(1, 0, 0, 'alpha', 1)

version = ".".join([str(i) for i in version_info[:3]]) + ((version_info.releaselevel[0] + str(version_info.serial)) if version_info.releaselevel != 'final' else '')

Loading