'Represent a (key, value) as hypertext link.
- Typically, k is a foreign key value, and v the
representation of the referenced record, and the link
shall open a read view of the referenced record.
- The linkto-parameter expects a URL (as string) with "[id]"
as placeholder for the key.
@param k: the key
@param v: the representation of the key
@param row: the row with this key'

def link(self, k, v, row=None):

    if (not k):
        return v
    if (self.linkto == URL(c='pr', f='pentity', args=['[id]'])):
        k = s3_unicode(k)
        db = current.db
        petable = db.pr_pentity
        pe_record = db((petable._id == k)).select(petable.instance_type, limitby=(0, 1)).first()
        if (not pe_record):
            return v
        tablename = pe_record.instance_type
        (prefix, name) = tablename.split('_', 1)
        url = URL(c=prefix, f=name, args=['read'], vars={'~.pe_id': k})
        url = url.replace('.aadata', '')
        return A(v, _href=url)
    else:
        k = s3_unicode(k)
        return A(v, _href=self.linkto.replace('[id]', k).replace('%5Bid%5D', k))
