summaryrefslogtreecommitdiff
path: root/biblio/bibtex/utils/mab2bib/utf8_to_latex.py
blob: 3636464d687ecac6ff2bad1c24248e5fa8858ead (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Universelle Textcodierung
2009-06-15 by Henning Hraban Ramm, fiムe virtuムlle

quellcodierung_to_zielcodierung.py [Optionen] Quelldatei [Zieldatei]

Es kレnnen auch ganze Verzeichnisse bearbeitet werden.

Die gew゚nschte Codierung wird aus dem Dateinamen ermittelt.
Mレgliche Werte sind z.B.
latin1 (iso-8859-1), utf8, macroman, latex (sofern latex.py vorhanden ist)

Optionen:
--filter=Dateiendung
--overwrite          (sonst wird die Originaldatei gesichert)
--hidden             (sonst werden versteckte Dateien ignoriert)
"""

import os, os.path, sys, codecs, getopt, shutil
try:
    import latex
except:
    pass

modes = ('filter', 'overwrite', 'hidden')
mode = {}

def help(message=""):
    print message
    print __doc__
    sys.exit(1)

def backup(datei):
    original = datei
    pfad, datei = os.path.split(datei)
    datei, ext = os.path.splitext(datei)
    count = 0
    while os.path.exists(os.path.join(pfad, u"%s.%d%s" % (datei, count, ext))):
        count += 1
    neudatei = os.path.join(pfad, u"%s.%d%s" % (datei, count, ext))
    print u"Sichere %s als %s" % (original, neudatei)
    shutil.copy(original, neudatei)
    return neudatei

def is_hidden(datei):
    return (datei.startswith('.') or os.sep+'.' in datei)

def convert(source, target, so_enc, ta_enc):
    from_exists = os.path.exists(source)
    to_exists = os.path.exists(target)
    from_isdir = os.path.isdir(source)
    to_isdir = os.path.isdir(target)
    from_path, from_name = os.path.split(source)
    to_path, to_name = os.path.split(target)
    #from_name = os.path.basename(source)
    #to_name = os.path.basename(target)

    if not from_exists:
        help(u"Quelle '%s' nicht gefunden!" % from_name)

    if from_isdir:
        if is_hidden(source) and not mode['hidden']:
            print u"Ignoriere verstecktes Verzeichnis %s" % source
            return
        if not to_isdir:
            help(u"Wenn die Quelle ein Verzeichnis ist, muss auch das Ziel ein Verzeichnis sein!")
        print u"Verarbeite Verzeichnis %s" % source
        dateien = os.listdir(source)
        #if not mode['hidden']:
        #    dateien = [d for d in dateien if not is_hidden(d)]
        if mode['filter']:
            dateien = [d for d in dateien if d.endswith(mode['filter'])]
        for datei in dateien:
            s = os.path.join(source, datei)
            t = os.path.join(target, datei)
            convert(s, t, so_enc, ta_enc)
    else:
        if is_hidden(from_name) and not mode['hidden']:
            print u"Ignoriere versteckte Datei %s" % source
            return
        if to_isdir:
            target = os.path.join(target, from_name)
        if not mode['overwrite']:
            if source==target:
                source=backup(source)
            elif os.path.exists(target):
                backup(target)
        print u"Konvertiere %s (%s)\n\tnach %s (%s)" % (source, so_enc, target, ta_enc)
        so_file = file(source, "rU")
        lines = so_file.readlines()
        so_file.close()
        ta_file = file(target, "w")
        for l in lines:
            ta_file.write(unicode(l, so_enc).encode(ta_enc))
        ta_file.close()
        

opts, args = getopt.getopt(sys.argv[1:], "ohf:", ["overwrite","hidden","filter="])

if len(args)<1:
    help("Zu wenige Parameter angegeben!")

for m in modes:
    mode[m] = False
    for (o, a) in opts:
        if o=='-'+m[0] or o=='--'+m:
            if a:
                print u"Modus %s = %s" % (m, a)
            else:
                a = True
                print u"Modus %s aktiv" % m
            mode[m] = a

#print "modes:", mode
#print "opts :", opts
#print "args :", args

# gew゚nschte Codierung aus dem Dateinamen ablesen
scriptname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
from_enc, to_enc = scriptname.split("_to_")

from_name = to_name = args[0]
if len(args)>1: to_name = args[1]

convert(from_name, to_name, from_enc, to_enc)