Usunięcie dev.sqlite z plików śledzonych przez gita.
[redakcja.git] / lib / librarian / bin / book2txt.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import re
4 import os
5 import optparse
6 import codecs
7
8
9 HEADER = u"""\
10 Kodowanie znaków w dokumencie: UTF-8.
11 -----
12 Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl/). Reprodukcja cyfrowa wykonana przez
13 Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. Ten utwór nie jest chroniony prawem autorskim i znajduje
14 się w domenie publicznej, co oznacza, że możesz go swobodnie wykorzystywać, publikować i rozpowszechniać.
15 -----
16
17 """
18
19
20 REGEXES = [
21     (r'<rdf:RDF[^>]*>(.|\n)*?</rdf:RDF>', ''),
22     (r'<motyw[^>]*>(.|\n)*?</motyw>', ''),
23     ('<(begin|end)\\sid=[\'|"][b|e]\\d+[\'|"]\\s/>', ''),
24     (r'<extra>((<!--<(elementy_poczatkowe|tekst_glowny)>-->)|(<!--</(elementy_poczatkowe|tekst_glowny)>-->))</extra>', ''),
25     (r'<uwaga>(.|\n)*?</uwaga>', ''),
26     (r'<p[a|e|r|t]>(.|\n)*?</p[a|e|r|t]>', ''),
27     (r'<[^>]+>', ''),
28     (r'/\n', '\n'),
29     (r'---', u'—'),
30     (r'--', u'-'),
31     (r',,', u'„'),
32     (r'"', u'”'),
33 ]
34
35
36 if __name__ == '__main__':
37     # Parse commandline arguments
38     usage = """Usage: %prog [options] SOURCE [SOURCE...]
39     Convert SOURCE files to TXT format."""
40
41     parser = optparse.OptionParser(usage=usage)
42
43     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
44         help='print status messages to stdout')
45
46     options, input_filenames = parser.parse_args()
47
48     if len(input_filenames) < 1:
49         parser.print_help()
50         exit(1)
51
52     # Do some real work
53     for input_filename in input_filenames:
54         if options.verbose:
55             print input_filename
56         
57         output_filename = os.path.splitext(input_filename)[0] + '.txt'
58         
59         xml = codecs.open(input_filename, 'r', encoding='utf-8').read()
60         for pattern, repl in REGEXES:
61             # print pattern, repl
62             xml, n = re.subn(pattern, repl, xml)
63             # print n
64         
65         output = codecs.open(output_filename, 'w', encoding='utf-8')
66         output.write(HEADER)
67         output.write(xml)
68