예제 #1
0
def appendField(parent, fieldKey=None, fieldValue=None, childName="li", fieldType="string"):
    child = etree.SubElement(parent, childName)
    if fieldKey is None or len(fieldKey) == 0:
        tgt = child
        tgtTail = u""
    else:
        fieldName = deCamelize(fieldKey)
        strong = etree.SubElement(child, "strong")
        strong.text = "%s:" % fieldName
        tgt = strong
        tgtTail = u" "
    if fieldValue is None:
        pass
    elif fieldType == "string":
        if fieldKey is not None:
            if fieldKey in CAMEL_FIELDS:
                cleanVal = deCamelize(fieldValue)
            else:
                cleanVal = fieldValue
            tgt.tail = tgtTail
            span = etree.SubElement(child, "span")
            if fieldValue is not None:
                span.set("class", "fieldValue %s" % fieldKey)
            else:
                span.set("class", "fieldValue")
            span.text = cleanVal
        else:
            tgt.tail = u"%s%s" % (tgtTail, fieldValue)

    elif fieldType == "url":
        tgt.tail = tgtTail
        appendLink(child, fieldValue)
    return child
예제 #2
0
def mdOutStr(k, v, context, indent=0):
    if k == "listitem" and type(v).__name__ != "unicode" and indent > 0:
        indent = indent - 3
    if k == "listitem" and type(v).__name__ == "unicode":
        vtype = "listitem"
    elif k in ["listitem", "dictitem"]:
        vtype = "%s" % type(v).__name__
    else:
        vtype = zfields[k]["type"]

    try:
        mask = zfields[k]["mask"]
    except KeyError:
        try:
            mask = defmasks[context][vtype]
        except KeyError:
            l.error("no default mask for context = '%s' + type = '%s'" % (context, vtype))
            return
    mask = mask.rjust(len(mask) + indent)

    try:
        caption = zfields[k]["caption"]
    except KeyError:
        caption = deCamelize(k.encode("utf-8"))
    if k == "listitem":
        caption = ""

    try:
        val = v.encode("utf-8")
        if vtype == "camel":
            val = deCamelize(val)
    except AttributeError:
        val = v

    if context == "header" and k == "creators":
        print (mask % caption)
        mask = "+ **%s:** %s"
        mask = mask.rjust(len(mask) + indent)
        for creator in val:
            try:
                name = "%s %s" % (creator["firstName"], creator["lastName"].upper())
            except KeyError:
                name = "%s" % creator["name"]
            print (mask % (creator["creatorType"], name))
        print ("")
        return

    if context == "zotsection" and k == "authors":
        print (mask % caption)
        mask = '+ [%s](%s "%s")'
        mask = mask.rjust(len(mask) + indent + MD_INDENT)
        for author in val:
            try:
                name = "%s %s" % (author["firstName"], author["lastName"].upper())
            except KeyError:
                name = "%s" % author["name"]
            print (mask % (name, author["href"], "author's zotero username"))
        return

    if context == "zotsection" and k == "links":
        print (mask % caption)
        mask = '+ <a href="%(href)s" rel="alternate">%(type)s</a>'
        mask = mask.rjust(len(mask) + indent + MD_INDENT)
        for link in val:
            print (mask % link)
        return

    if vtype in ["str", "camel", "unicode", "mdurl"]:
        print (mask % (caption, val))
    elif vtype in ["url"]:
        print (mask % (caption, val, val))
    elif vtype in ["dict"]:
        if k != "listitem":
            print (mask % caption)
        for k in sorted(val.keys()):
            mdOutStr(k, val[k], context, indent + MD_INDENT)
    elif vtype in ["list"]:
        print (mask % caption)
        for li in val:
            mdOutStr("listitem", li, context, indent + MD_INDENT)
    elif vtype in ["listitem"]:
        print (mask % val)
    else:
        print ("skipping %s because type is '%s'" % (k, vtype))