-
Notifications
You must be signed in to change notification settings - Fork 4
/
automagica.py
executable file
·127 lines (107 loc) · 4.91 KB
/
automagica.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function, unicode_literals
import argparse
import imp
import glob
import os
import sys
from datetime import datetime
from epub import generate_epub
from pdf import generate_pdf
from pdf.booklet import generate_booklet
from template import latex_env
from utils import filepath, latex_hyphenation, latex_chapter, latex_single, show_file
DEFAULTS = dict(
INDEX_TITLE='Índice',
HYPHENATION='',
CONTENT='',
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--BASE_FILENAME')
parser.add_argument('book_path', help='Carpeta con archivos para un libro.', metavar='carpeta')
parser.add_argument('--no-split', help='No separar párrafos.', action='store_true')
parser.add_argument('--pdf', help='Genera la versión pdf del libro.', action='store_true')
parser.add_argument('--booklet', help='Genera la versión booklet del pdf.', action='store_true')
parser.add_argument('--epub', help='Genera la versión epub del libro.', action='store_true')
parser.add_argument('--only_tex', help='Solo genera el archivo latex.', action='store_true')
parser.add_argument('--sections', help='Usar secciones en lugar de capítulos como elemento principal.', action='store_true')
parser.add_argument('--new_page_before_sections', help='Forzar página nueva en las secciones principales.', action='store_true')
parser.add_argument('--TITLE')
parser.add_argument('--SUBTITLE')
parser.add_argument('--AUTHOR')
parser.add_argument('--FONT_SIZE')
parser.add_argument('--PAGE_SIZE')
parser.add_argument('--YEAR')
parser.add_argument('--URL')
parser.add_argument('--exclude_index', action='store_true')
parser.add_argument('--no_open', help='No intenta abrir el archivo para verlo.', action='store_true')
args = parser.parse_args()
book_path = args.book_path
class EmptyConfig(object):
pass
if not os.path.isdir(book_path):
print('El argumento debe ser un directorio')
exit()
config_file = os.path.join(book_path, 'config.py')
if os.path.isfile(config_file):
config = imp.load_source('config', config_file)
else:
config = imp.load_source('config', 'config.py')
# TODO: ver precedencia de archivo config por sobre linea de comandos y valores por defecto (quizas tres pasos)
VARS = DEFAULTS.copy()
VARS.update(config.CONFIGS)
#for k,v in args._get_kwargs():
# VARS[k] = v
index_path = os.path.join(book_path, 'index.txt')
# documentar esto, qué hace por defecto el programa con el texto cuando genera el pdf
split_paragraphs = not VARS['no_split']
if os.path.isfile(index_path):
with open(index_path, 'r') as f:
content = ''
for filename in f.readlines():
if VARS['no_split']:
content += latex_chapter(os.path.join(book_path, filename).strip(), split_paragraphs)
else:
content += latex_single(os.path.join(book_path, filename).strip(), split_paragraphs, VARS['sections'], VARS['new_page_before_sections'])
VARS['CONTENT'] = content
else:
text_files = [f for f in glob.glob(os.path.join(book_path, '*.txt')) if not f.endswith('words.txt')]
if text_files:
VARS['CONTENT'] = latex_single(text_files[0], split_paragraphs, VARS['sections'], VARS['new_page_before_sections'])
# archivo con separación en sílabas para cortar palabras
sep_path = os.path.join(book_path, 'words.txt')
if os.path.isfile(sep_path):
with open(sep_path, 'r') as f:
hyphenation = ''
for word in f.readlines():
hyphenation += latex_hyphenation(word.strip())
VARS['HYPHENATION'] = hyphenation
TEMPLATE = 'template.tex'
local_template_path = os.path.join(book_path, 'template.tex')
if os.path.isfile(local_template_path):
template = latex_env.from_string(open(local_template_path).read())
else:
template = latex_env.get_template(TEMPLATE)
base_filename = VARS['BASE_FILENAME']
tex_file = filepath(book_path, base_filename, 'tex')
with open(tex_file, 'w') as f:
f.write(template.render(**VARS))
if not args.only_tex:
if args.pdf or not args.epub:
pdf_file = generate_pdf(book_path, base_filename, tex_file)
if not args.no_open:
show_file(pdf_file)
if args.booklet:
output_file = filepath(book_path, base_filename, 'booklet.pdf')
generate_booklet(pdf_file, output_file)
if not args.no_open:
show_file(output_file)
if args.epub:
generate_epub(book_path, base_filename, tex_file)
epub_file = filepath(book_path, base_filename, 'epub')
if not args.no_open:
show_file(epub_file)
if __name__ == '__main__':
main()