-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
csl-reindenting.py
39 lines (34 loc) · 1.43 KB
/
csl-reindenting.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
# -*- coding: utf-8 -*-
# Python script for style (re)indentation
# Author: Rintze M. Zelle
# Version: 2012-05-12
# * Requires lxml library (http://lxml.de/)
#
# - Indents xml with 2 spaces per level
# - Escapes non-breaking spaces ( )
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))
for stylepath in glob.glob( os.path.join(path, 'dependent', '*.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()
try:
verbatimsStyle = styleElement.find(".//{http://purl.org/net/xbiblio/csl}rights").text
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