forked from WikiToLearn/texla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexla.py
More file actions
66 lines (60 loc) · 2.54 KB
/
texla.py
File metadata and controls
66 lines (60 loc) · 2.54 KB
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
import logging
import json
import yaml
from texla.Parser import Parser
from texla.Renderers.MediaWikiRenderer import MediaWikiRenderer
import texla.PageTree.Exporter as exporter
from texla.Exceptions.TexlaExceptions import *
def execute_texla_mediawiki(config):
p = Parser(config)
a = open(config['input_path'], 'r').read()
try:
tree = p.parse(a)
except (PreparserError, ParserError) as err:
err.print_error()
exit()
f = open(config['output_path'] + '.tree', 'w')
json_tree = tree.to_json(0)
n_blocks = tree.n_blocks()
logging.info('PARSED %i Blocks', n_blocks)
f.write(json_tree)
logging.info('\033[0;34m############### STARTING RENDERING ###############\033[0m')
#rendering
rend = MediaWikiRenderer(config)
rend.start_rendering(tree)
o = open(config['output_path'] + '.json', 'w')
o.write(json.dumps(rend.tree.get_tree_json(), indent=3))
p = open(config['output_path'] + '.debug', 'w')
p.write(json.dumps(rend.used_tags, indent=2))
#print page tree before POST-PROCESSING
logging.info('PageTree:\n'+rend.tree.get_tree_debug())
#collpasing
logging.info('\033[0;34m############### STARTING POST-PROCESSING ###############\033[0m')
tree = rend.tree
tree.collapse_tree(config['collapse_content_level'],
config['collapse_pages_level'])
#printing tree after POST-PROCESSING
logging.info('PageTree:\n'+rend.tree.get_tree_debug())
oc = open(config['output_path'] + '-coll.json', 'w')
oc.write(json.dumps(rend.tree.get_tree_json(), indent=3))
logging.info('######## STARTING EXPORTING ########')
if config['create_index']:
tree.create_indexes(config["export_book_page"])
exporter.exportPages(tree.pages, config['output_path'] + '.mw',
config['export_format'])
if config['export_single_pages']:
exporter.export_singlePages(tree.pages,
config['output_path'] + '_pages',
config['export_format'])
if config['export_pages_tree']:
exporter.export_pages_tree(tree.pages.values(),
config['output_path'] + "_pages")
logging.info('Finished')
if __name__ == '__main__':
#reading JSON configs
config = yaml.load(open('configs.yaml','r'))
#loading localized keywords
config['keywords'] = yaml.load(open('i18n.yaml','r'))[config['lang']]
#executing process for alla renderers
if config['renderer'] == 'mediawiki':
execute_texla_mediawiki(config)