Permalink
Browse files

Allow target element name to be specified in excerpt filter

  • Loading branch information...
1 parent cc4a43c commit 2f792f919e369c3d19d2a71eb61913c5747bec41 @rubys committed May 29, 2007
Showing with 26 additions and 4 deletions.
  1. +10 −4 filters/excerpt.py
  2. +1 −0 tests/data/filter/excerpt-lorem-ipsum.ini
  3. +15 −0 tests/test_filters.py
View
@@ -4,6 +4,7 @@
Parameters:
width: maximum number of characters in the excerpt. Default: 500
omit: whitespace delimited list of html tags to remove. Default: none
+ target: name of element created. Default: planet:excerpt
Notes:
* if 'img' is in the list of tags to be omitted <img> tags are replaced with
@@ -23,6 +24,7 @@
wrapper = textwrap.TextWrapper(width=int(args.get('width','500')))
omit = args.get('omit', '').split()
+target = args.get('target', 'planet:excerpt')
class copy:
""" recursively copy a source to a target, up to a given width """
@@ -94,10 +96,14 @@ def copyText(self, source, target):
# if present, recursively copy it to a planet:excerpt element
if source:
- dom.documentElement.setAttribute('xmlns:planet', planetNS)
- target = dom.createElementNS(planetNS, 'planet:excerpt')
- source[0].parentNode.appendChild(target)
- copy(dom, source[0], target)
+ if target.startswith('planet:'):
+ dom.documentElement.setAttribute('xmlns:planet', planetNS)
+ if target.startswith('atom:'): target = target.split(':',1)[1]
+ excerpt = dom.createElementNS(planetNS, target)
+ source[0].parentNode.appendChild(excerpt)
+ copy(dom, source[0], excerpt)
+ if source[0].nodeName == excerpt.nodeName:
+ source[0].parentNode.removeChild(source[0])
# print out results
print dom.toxml('utf-8')
@@ -4,3 +4,4 @@ filters = excerpt.py
[excerpt.py]
width = 100
omit = p
+target = planet:excerpt
View
@@ -54,6 +54,21 @@ def test_excerpt_lorem_ipsum(self):
u'adipiscing elit. Nullam velit. Vivamus tincidunt, erat ' +
u'in \u2026', excerpt.firstChild.firstChild.nodeValue)
+ def test_excerpt_lorem_ipsum_summary(self):
+ testfile = 'tests/data/filter/excerpt-lorem-ipsum.xml'
+ config.load('tests/data/filter/excerpt-lorem-ipsum.ini')
+ config.parser.set('excerpt.py', 'target', 'atom:summary')
+
+ output = open(testfile).read()
+ for filter in config.filters():
+ output = shell.run(filter, output, mode="filter")
+
+ dom = xml.dom.minidom.parseString(output)
+ excerpt = dom.getElementsByTagName('summary')[0]
+ self.assertEqual(u'Lorem ipsum dolor sit amet, consectetuer ' +
+ u'adipiscing elit. Nullam velit. Vivamus tincidunt, erat ' +
+ u'in \u2026', excerpt.firstChild.firstChild.nodeValue)
+
def test_stripAd_yahoo(self):
testfile = 'tests/data/filter/stripAd-yahoo.xml'
config.load('tests/data/filter/stripAd-yahoo.ini')

0 comments on commit 2f792f9

Please sign in to comment.