On Wed, 22 Sep 2010 02:52:19 +1000, Gang Liang <[email protected]> wrote:

On Tue, Sep 21, 2010 at 7:34 AM, Joel Nothman
<[email protected]> wrote:

2. the template construction "{{...}}" is parsed as regular text. I am
wondering whether we can use a dedicated object type to caption the
template type.

template expansion is handled as a separate module in mwlib. parseString
will only perform template expansion if it is passed a non-None wikidb
object which contains the template markup.


thanks!

one question. do you mean "template expansion" as expanding the given
template to what we see on a page? what i think here is a simple task.
for example, "{{WM Help Guide}}" is now parsed into

mwlib.parser.node.Paragraph
    u"{{WM Help Guide}}"

can we parse it into something like:

mwlib.parser.node.Template
    tagname = u"WM Help Guide"

No, currently it is not being parsed at all, I assume, because you're not passing a value for wikidb to parseString, so the template expansion isn't being attempted.

As in the other thread on this group with Peter Wills, you will need to create an appropriate wikidb object, with a normalize_and_get_page function that returns whatever you want to appear as the template content.

It would be relatively easy, for instance, to expand a template transclusion {{xxx}} to <span class="template"></span>, which you can then postprocess to change those spans to Template objects:


from mwlib.uparser import parseString
from mwlib.parser import Node, TagNode

class Page(object):
  def __init__(self, raw):
    self.rawtext = raw

class MyWikiDb(object):
  def normalize_and_get_page(self, title, defaultns):
    return Page(u'<span class="template">%s</span>' % title)

  def get_siteinfo(self):
    from mwlib import siteinfo
    return siteinfo.get_siteinfo('en')

class Template(Node):
  @classmethod
  def convert_in_tree(cls, tree):
    for n in tree.find(TagNode):
      if n.vlist.get('class') == 'template':
        n.__class__ = cls
        n.tagname = None
        n.vlist = None


parsed = parseString(title="Foobar", raw="{{my template}}", wikidb=MyWikiDb())
Template.convert_in_tree(parsed)
parsed.show()

we can append template parameters there as well.

No you can't; not so easily. mwlib doesn't pass the template parameters to the wikidb. Rather, once it's got the markup back, it substitutes them in with the template evaluator.

So, unless you want to modify the internal template expansion code, the best you could do is get a list of parameters as *expected* by the template. So if you had a lookup function like get_tpl_params(title) which returned the list of parameter names expected by a particular template, you could then do something like:

params = ','.join(u'%s={{{%s}}}' % (param, param) for param in get_tpl_params(title))
return Page(u'<span class="template">%s(%s)</span>' % (title, params))


So if I hardcode get_tpl_params to return ['1','2','3'], and use raw='{{my template|a|b|c|foo=bar}}', I get back the following:

Article->'Foobar'
    Template->'span'
        u'my template(1=a,2=b,3=c)'

A bit hacky, but possible to make it work...

~J

--
You received this message because you are subscribed to the Google Groups 
"mwlib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/mwlib?hl=en.

Reply via email to