Skip to content

Commit

Permalink
Add a makeEscapedOptionalTag, identical to makeOptionalTag but escapi…
Browse files Browse the repository at this point in the history
…ng content of xml tags
  • Loading branch information
nicolasmoreau committed Jan 22, 2018
1 parent 6e0ffb3 commit 7d4e82b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions vamdctap/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,26 @@ def GetValue(returnable_key, **kwargs):
return value

def makeOptionalTag(tagname, keyword, G, extraAttr={}):
return buildOptionalTag(tagname, keyword, G, extraAttr, False)

def makeEscapedOptionalTag(tagname, keyword, G, extraAttr={}):
return buildOptionalTag(tagname, keyword, G, extraAttr, True)

def buildOptionalTag(tagname, keyword, G, extraAttr={}, escaped=False):
content = G(keyword)

if escaped is True :
content = escape(content)

if not content:
return ''
elif isiterable(content):
s = []
for c in content:
s.append( '<%s>%s</%s>'%(tagname,c,tagname) )
iter_content = c
if escaped is True :
iter_content = escape(c)
s.append( '<%s>%s</%s>'%(tagname,iter_content,tagname) )
return ''.join(s)
else:
extra = "".join([' %s="%s"'% (k, v) for k, v in extraAttr.items()])
Expand Down Expand Up @@ -536,13 +548,13 @@ def XsamsSources(Sources, tap):
<Year>%s</Year>""" % ( G('SourceTitle'), G('SourceCategory'),
G('SourceYear') )

yield makeOptionalTag('SourceName','SourceName',G)
yield makeEscapedOptionalTag('SourceName','SourceName',G)
yield makeOptionalTag('Volume','SourceVolume',G)
yield makeOptionalTag('PageBegin','SourcePageBegin',G)
yield makeOptionalTag('PageEnd','SourcePageEnd',G)
yield makeOptionalTag('ArticleNumber','SourceArticleNumber',G)
yield makeOptionalTag('UniformResourceIdentifier','SourceURI',G)
yield makeOptionalTag('DigitalObjectIdentifier','SourceDOI',G)
yield makeEscapedOptionalTag('UniformResourceIdentifier','SourceURI',G)
yield makeEscapedOptionalTag('DigitalObjectIdentifier','SourceDOI',G)
yield makeOptionalTag('Comments','SourceComments',G)
yield '</Source>\n'
yield '</Sources>\n'
Expand Down

0 comments on commit 7d4e82b

Please sign in to comment.