34 lines
962 B
Python
34 lines
962 B
Python
"""
|
|
Module for FB2 file conversion to html or txt
|
|
"""
|
|
|
|
import sys
|
|
import argparse
|
|
from .txt import join_tokens_to_txt
|
|
from .tokens import fb2_to_tokens
|
|
from .html import join_tokens as join_tokens_to_html
|
|
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('file', help="file to process", type=argparse.FileType('rb'))
|
|
parser.add_argument('-o', '--output', help="path to output file", default=sys.stdout, type=argparse.FileType('w'))
|
|
parser.add_argument('-t', '--target', help='specify target format', choices=['html', 'txt', 'text'], default='html')
|
|
args = parser.parse_args()
|
|
|
|
file = args.file
|
|
|
|
tokens = fb2_to_tokens(file)
|
|
|
|
if args.target == 'html':
|
|
content = join_tokens_to_html(tokens)
|
|
else:
|
|
content = join_tokens_to_txt(tokens)
|
|
|
|
output_file = args.output
|
|
|
|
output_file.write(content)
|
|
if output_file != sys.stdout:
|
|
print(f'Finished writing to {output_file.name}')
|