X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/05f8cc4122caa9cc40e1df6e412b0dcfab7c6d40..38343a3fc11f5509c8522fec94c0ae7085b7244f:/lib/librarian/bin/book2txt.py diff --git a/lib/librarian/bin/book2txt.py b/lib/librarian/bin/book2txt.py new file mode 100755 index 00000000..9c470805 --- /dev/null +++ b/lib/librarian/bin/book2txt.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import re +import os +import optparse +import codecs + + +HEADER = u"""\ +Kodowanie znaków w dokumencie: UTF-8. +----- +Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl/). Reprodukcja cyfrowa wykonana przez +Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. Ten utwór nie jest chroniony prawem autorskim i znajduje +się w domenie publicznej, co oznacza, że możesz go swobodnie wykorzystywać, publikować i rozpowszechniać. +----- + +""" + + +REGEXES = [ + (r']*>(.|\n)*?', ''), + (r']*>(.|\n)*?', ''), + ('<(begin|end)\\sid=[\'|"][b|e]\\d+[\'|"]\\s/>', ''), + (r'(()|())', ''), + (r'(.|\n)*?', ''), + (r'(.|\n)*?', ''), + (r'<[^>]+>', ''), + (r'/\n', '\n'), + (r'---', u'—'), + (r'--', u'-'), + (r',,', u'„'), + (r'"', u'”'), +] + + +if __name__ == '__main__': + # Parse commandline arguments + usage = """Usage: %prog [options] SOURCE [SOURCE...] + Convert SOURCE files to TXT format.""" + + parser = optparse.OptionParser(usage=usage) + + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, + help='print status messages to stdout') + + options, input_filenames = parser.parse_args() + + if len(input_filenames) < 1: + parser.print_help() + exit(1) + + # Do some real work + for input_filename in input_filenames: + if options.verbose: + print input_filename + + output_filename = os.path.splitext(input_filename)[0] + '.txt' + + xml = codecs.open(input_filename, 'r', encoding='utf-8').read() + for pattern, repl in REGEXES: + # print pattern, repl + xml, n = re.subn(pattern, repl, xml) + # print n + + output = codecs.open(output_filename, 'w', encoding='utf-8') + output.write(HEADER) + output.write(xml) +