Skip to content

Commit 3d8a46f

Browse files
committed
Script to fix missing "match" attribute on cs:if and cs:else-if, part III
1 parent 9fe16c0 commit 3d8a46f

File tree

1 file changed

+40
-56
lines changed

1 file changed

+40
-56
lines changed

csl-reindenting-and-match-fix.py

+40-56
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
# Python script to find "type" conditionals that test for multiple item types
3-
# with "match" set (explicitly or implicitly) to "all"
2+
# Python script to add 'match="any"' to ambiguous conditionals (citeproc-js
3+
# default was incorrectly set to "any", should be "all")
44
# Author: Rintze M. Zelle
5-
# Version: 2013-02-25
5+
# Version: 2013-03-04
66
# * Requires lxml library (http://lxml.de/)
77

88
import os, glob, re
@@ -14,68 +14,52 @@
1414
for stylepath in glob.glob( os.path.join(path, '*.csl') ):
1515
styles.append(os.path.join(stylepath))
1616

17-
# Determine which terms should be defined in a locale element
18-
def findNamesInUse(styleElement):
19-
termsToDefine = []
20-
21-
if (styleElement.find('.//{http://purl.org/net/xbiblio/csl}label[@form="verb-short"]') is not None):
22-
changingNames = ["director", "editor", "editorial-director", "illustrator", "translator"]
23-
for csNames in styleElement.findall(".//{http://purl.org/net/xbiblio/csl}names"):
24-
for changingName in changingNames:
25-
if ((changingName in csNames.get("variable")) and (changingName not in termsToDefine)):
26-
termsToDefine.append(changingName)
27-
return(termsToDefine)
28-
2917
# cs:if or cs:else-if
30-
# with "type", with multiple values (contains a space)
31-
# without "match", or with 'match="all"'
32-
# for now, just print style names
18+
# * conditional with 2 or more test values (attribute value contains a space)
19+
# * two or more conditionals
20+
# * no "match"
3321
for style in styles:
3422
parser = etree.XMLParser(remove_blank_text=True)
3523
parsedStyle = etree.parse(style, parser)
3624
styleElement = parsedStyle.getroot()
3725

3826
fixedStyle = False
3927

40-
typeTests = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}if[@type]')
41-
for typeTest in typeTests:
42-
itemTypesTested = typeTest.get("type")
43-
# if the value of "type" includes a space, it tests for multiple item types
44-
if " " in itemTypesTested:
45-
# there is a problem if "match" is set to "all"
46-
if "match" in typeTest.attrib:
47-
if typeTest.get("match") == "all":
48-
print("Warning: 'match' set to 'all' while testing for multiple item types with 'type'")
49-
print(os.path.basename(style))
50-
# there is a problem if "match" is not set
51-
else:
52-
# if there is only a "type" attribute, add 'match="any"'
53-
if(len(typeTest.attrib) == 1):
54-
typeTest.attrib["match"]="any"
55-
fixedStyle = True
56-
else:
57-
print("Warning: can't set 'match', testing multiple conditionals")
58-
print(os.path.basename(style))
28+
ifElements = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}if')
29+
for element in ifElements:
30+
elementAttributes = element.attrib
31+
32+
if "match" in elementAttributes:
33+
continue
34+
35+
if(len(elementAttributes) > 1):
36+
element.attrib["match"]="any"
37+
fixedStyle = True
38+
continue
39+
40+
for attribute in elementAttributes:
41+
if " " in elementAttributes[attribute]:
42+
element.attrib["match"]="any"
43+
fixedStyle = True
44+
continue
5945

60-
typeTests = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}else-if[@type]')
61-
for typeTest in typeTests:
62-
itemTypesTested = typeTest.get("type")
63-
# if the value of "type" includes a space, it tests for multiple item types
64-
if " " in itemTypesTested:
65-
# there is a problem if "match" is set to "all"
66-
if "match" in typeTest.attrib:
67-
if typeTest.get("match") == "all":
68-
print("Warning: 'match' set to 'all' while testing for multiple item types with 'type'")
69-
print(os.path.basename(style))
70-
# there is a problem if "match" is not set
71-
else:
72-
# if there is only a "type" attribute, add 'match="any"'
73-
if(len(typeTest.attrib) == 1):
74-
typeTest.attrib["match"]="any"
75-
fixedStyle = True
76-
else:
77-
print("Warning: can't set 'match', testing multiple conditionals")
78-
print(os.path.basename(style))
46+
elseifElements = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}else-if')
47+
for element in elseifElements:
48+
elementAttributes = element.attrib
49+
50+
if "match" in elementAttributes:
51+
continue
52+
53+
if(len(elementAttributes) > 1):
54+
element.attrib["match"]="any"
55+
fixedStyle = True
56+
continue
57+
58+
for attribute in elementAttributes:
59+
if " " in elementAttributes[attribute]:
60+
element.attrib["match"]="any"
61+
fixedStyle = True
62+
continue
7963

8064
if (fixedStyle == False):
8165
continue

0 commit comments

Comments
 (0)