-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcsl-reindenting-and-match-fix.py
79 lines (64 loc) · 2.56 KB
/
csl-reindenting-and-match-fix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
# Python script to add 'match="any"' to ambiguous conditionals (citeproc-js
# default was incorrectly set to "any", should be "all")
# Author: Rintze M. Zelle
# Version: 2013-03-04
# * Requires lxml library (http://lxml.de/)
import os, glob, re
from lxml import etree
path = 'C:\Documents and Settings\zelle\My Documents\CSL\styles\\'
styles = []
for stylepath in glob.glob( os.path.join(path, '*.csl') ):
styles.append(os.path.join(stylepath))
# cs:if or cs:else-if
# * conditional with 2 or more test values (attribute value contains a space)
# * two or more conditionals
# * no "match"
for style in styles:
parser = etree.XMLParser(remove_blank_text=True)
parsedStyle = etree.parse(style, parser)
styleElement = parsedStyle.getroot()
fixedStyle = False
ifElements = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}if')
for element in ifElements:
elementAttributes = element.attrib
if "match" in elementAttributes:
continue
if(len(elementAttributes) > 1):
element.attrib["match"]="any"
fixedStyle = True
continue
for attribute in elementAttributes:
if " " in elementAttributes[attribute]:
element.attrib["match"]="any"
fixedStyle = True
continue
elseifElements = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}else-if')
for element in elseifElements:
elementAttributes = element.attrib
if "match" in elementAttributes:
continue
if(len(elementAttributes) > 1):
element.attrib["match"]="any"
fixedStyle = True
continue
for attribute in elementAttributes:
if " " in elementAttributes[attribute]:
element.attrib["match"]="any"
fixedStyle = True
continue
if (fixedStyle == False):
continue
try:
parsedStyle = etree.tostring(parsedStyle, pretty_print=True, xml_declaration=True, encoding="utf-8")
parsedStyle = parsedStyle.replace("'", '"', 4)
parsedStyle = parsedStyle.replace(" ", " ")#no-break space
parsedStyle = parsedStyle.replace("ᵉ", "ᵉ")
parsedStyle = parsedStyle.replace("‑", "‑")#non-breaking hyphen
parsedStyle = parsedStyle.replace("–", "–")#en dash
parsedStyle = parsedStyle.replace("—", "—")#em dash
f = open(style, 'w')
f.write ( parsedStyle )
f.close()
except:
pass