PXSL entry parser

"Hiếu Hoàng" <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
  Hi all,

  I have written a PXSL entry parser for pyblosxom. You can see some
example posts in <http://hdh.dyn-o-saur.com/blog/entries/>, and the
first PXSL post at <http://hdh.dyn-o-saur.com/plos/pxsl>. The .edf
file in /style is also part of PXSL.

  I think because of the markup-heavy orientation of PXSL, it leaves a
lot of newlines after elements, thus by default outputs HTML that
renders like "PXSL .", note the space. Doing replace(">\n", ">") works
well, but I'm open for suggestions.

  I give the whole entry file for pxslcc to parse. Luckily it treats #
as comment. On the other hand, the title line "Title in fragments" is
parsed into '<Title DEFAULT="in" DEFAULT="fragments"/>'. I just went
the easy way and read the title line seperately when returning entry
data. Is there a better way?

  Related to the last question, do I have to pass metadata lines in
the returned entry data?

  I would like to contribute this script to the contrib section.

  Hiếu

P.S.: hdh.dyn-o-saur.com is not online 24/7, it sleeps when I do :)

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

_______________________________________________
Pyblosxom-devel mailing list
Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
pxsl.py (text/x-python, 2.3 KB)
# coding: utf-8
"""
A PXSL entry parser for PyBlosxom. PXSL is the Parsimonious XML Shorthand
Language (http://community.moertel.com/pxsl/). To use this, you must have pxslcc
installed (Debian name: pxsl-tools).

Installation:
* Copy the file to a plugins directory
* Add 'pxsl' to py['load_plugins']
* Posts with '.pxsl' extension are parsed as PXSL
* Strongly recommended: add py['pxsl_options'] = ['--indent=0'] to collapse
  whitespace between generated XML elements

You can include defaults by defining a list of them in py['pxsl_defaults'], and
macros in py['pxsl_macros']

Tested with pxslcc 0.9.3.

Notes:
* I once installed pxslcc into /usr/local/bin but either the CGI script or the
web server couldn't find it
* The Popen line is rather ugly, but using executable results in empty stdout
* Extracting post title is really messy, opening the entry twice

This file is in the public domain. In case that is not legally possible: I grant
anyone the right to use this work for any purpose, without any conditions,
unless such conditions are required by law.
"""
__version__ = '0.1'
__author__ = 'Hoàng Đức Hiếu <[email protected]>'

def cb_entryparser(args):
    args['pxsl'] = parse
    return args

def parse(filename, request):
    args = []
    config = request.getConfiguration()
    defaults = config.get('pxsl_defaults', None)
    if defaults:
        from itertools import chain, repeat
        args = list(chain(repeat('-a', len(defaults)), defaults))
    args.extend(config.get('pxsl_options', []))
    args.extend(config.get('pxsl_macros', []))
    args.append(filename)
    from subprocess import Popen, PIPE, STDOUT
    pxslcc = Popen(["pxslcc"] + args,
                   stdout=PIPE, stderr=STDOUT
                   )
    out = pxslcc.stdout.read()
    pxslcc.poll()
    if pxslcc.returncode:
        body = "<pre><samp>%s</samp></pre>" % out.replace("<", "&gt;")
    else:
        body = out.partition('\n')[2] # the entry is parsed into <Title DEFAULT="in" DEFAULT="fragments"/>\n<blah blah=...
        body = body.replace(">\n", '>') # those totally ruin spacing, and HTML doesn't have xml:space
    entryData = {'body': body,
                 'title': file(filename).readline() # munging the XML title is a lot more trouble than this
                 }
    return entryData
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.