|
1 | 1 | # -*- 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") |
4 | 4 | # Author: Rintze M. Zelle
|
5 |
| -# Version: 2013-02-25 |
| 5 | +# Version: 2013-03-04 |
6 | 6 | # * Requires lxml library (http://lxml.de/)
|
7 | 7 |
|
8 | 8 | import os, glob, re
|
|
14 | 14 | for stylepath in glob.glob( os.path.join(path, '*.csl') ):
|
15 | 15 | styles.append(os.path.join(stylepath))
|
16 | 16 |
|
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 |
| - |
29 | 17 | # 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" |
33 | 21 | for style in styles:
|
34 | 22 | parser = etree.XMLParser(remove_blank_text=True)
|
35 | 23 | parsedStyle = etree.parse(style, parser)
|
36 | 24 | styleElement = parsedStyle.getroot()
|
37 | 25 |
|
38 | 26 | fixedStyle = False
|
39 | 27 |
|
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 |
59 | 45 |
|
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 |
79 | 63 |
|
80 | 64 | if (fixedStyle == False):
|
81 | 65 | continue
|
|
0 commit comments