forked from citation-style-language/utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsl-remove-affixes-from-et-al.py
55 lines (43 loc) · 1.78 KB
/
csl-remove-affixes-from-et-al.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
# -*- coding: utf-8 -*-
# Python script to remove affixes from cs:et-al
# Author: Rintze M. Zelle
# Version: 2013-03-28
# * Requires lxml library (http://lxml.de/)
import os, glob, re, inspect
from lxml import etree
# http://stackoverflow.com/questions/50499
folderPath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentFolderPath = os.path.dirname (folderPath)
path = os.path.join(parentFolderPath, 'styles')
styles = []
for stylepath in glob.glob( os.path.join(path, '*.csl') ):
styles.append(os.path.join(stylepath))
for style in styles:
parser = etree.XMLParser(remove_blank_text=True)
parsedStyle = etree.parse(style, parser)
styleElement = parsedStyle.getroot()
fixedStyle = False
etalElements = styleElement.findall('.//{http://purl.org/net/xbiblio/csl}et-al')
for element in etalElements:
elementAttributes = element.attrib
if "prefix" in elementAttributes:
del elementAttributes["prefix"]
fixedStyle = True
if "suffix" in elementAttributes:
del elementAttributes["suffix"]
fixedStyle = True
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