Saturday, December 29, 2012

Re: citations


On Sat, Dec 29, 2012 at 10:26 PM, <samtygier@gmail.com> wrote:
Hi,

I am creating a site, and trying to implement a referencing/citation system.

I'd like to render something like
"Lorem ipsum dolor sit amet[[ref:foo2012]], consectetur adipisicing elit, sed do eiusmod tempor incididunt[[ref:bar2011]] ut labore et dolore magna aliqua."
stored in a TextField,
"Lorem ipsum dolor sit amet[1], consectetur adipisicing elit, sed do eiusmod tempor incididunt[2] ut labore et dolore magna aliqua."

"[1] foo, 'something something' (2012)
[2] bar, 'something something' (2011)
"
where "foo2012" is the slug of a reference object, that has a title, author, data etc.

I have made a filter that can scan though a string, pick out the [[]] tags and look up the reference. I can use this in  a template like {{ site.description|refs }}. However i need somewhere to store mapping of numbers to references for that page, so that i can filter multiple blocks on the same page without resetting the numbering. I would then use a tag at the end of the page, that rendered the collected list.

The docs on custom tags and filters talks about render_context, but does not show how to use them with filters (only with tags).

Am i going about this the wrong way? should filters be stateless? i realise that parsing relationships out of a text field is not good database style, but i can't think of another way to allow editors to put citations into text. i would also like to use a similar tag for cross links in the text (eg [[site:foo]] -> "/sites/view/foo").

What you're describing is just a markup language -- if you look in django.contrib.markup, you'll find 3 examples of analogous markup languages. So yes, this is an entirely appropriate way to handle user-provided content.

As for implementation; filters are generally stateless -- they're really not much more than a way to access a function in a template. However, that's got more to do with convention than hard rules. There's nothing that *requires* them to be stateless. Writing to the context in a filter isn't prohibited; it's just unusual (which is why the docs don't provide a specific example). Using one tag to extract references, and a second to print those references sounds entirely appropriate. 

The other approach would be to use a template tag. The only advantage here would be that tags often have context-modifying side effects, so it would be marginally less surprising to an outside observer to see a tag modify context. However, that's really just a developer communication issue -- if there's enough documentation (formal or informal) about what the pair of filters are doing, I don't see any reason why you shouldn't use a filter for this purpose.

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment