-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_ebook.py
More file actions
86 lines (71 loc) · 2.9 KB
/
Copy pathbuild_ebook.py
File metadata and controls
86 lines (71 loc) · 2.9 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""
Compile all files into one file so that they can be rendered to LaTeX and ePub.
"""
from __future__ import print_function
from __future__ import absolute_import
import codecs
from functools import partial
from . import config
from . import glossary
from . import macros
from .renderer import Renderer, filters
from . import structure
from . import template
class EbookWriter(object):
def __init__(self):
pass
def configure(self):
"""Configure everything for the build."""
# register all macros before processing templates
macros.register_macro('full-glossary', partial(glossary.full_glossary_macro, glossary.MarkdownGlossaryRenderer()))
macros.register_macro('index', macros.IndexMacro.render)
macros.register_macro('glossary', glossary.glossary_term_macro)
macros.register_macro('define', glossary.glossary_definition_macro)
# set up filters for renderer:
self.filters = [
partial(filters.MetadataFilter.filter, target_format=config.cfg.target_format),
filters.remove_breaks_and_conts,
filters.SkipOnlyFilter.filter,
partial(filters.convert_section_links, 'title'),
macros.MacroFilter.filter,
filters.clean_images,
]
# process glossary links
if config.cfg.target_format == 'html':
style = 'tooltip'
else:
style = 'plain'
self.filters.append(glossary.get_glossary_link_processor(style))
def build(self):
"""
Add all documents into one target file.
"""
self.configure()
# process templates _after_ registering macros!
template.process_templates_in_config()
# start by copying the main template
if config.cfg.template:
template.template('default', config.cfg.template, config.cfg.target)
else:
# truncate file if it exists
with open(config.cfg.target, 'w'):
pass
# then append all the content pages
with codecs.open(config.cfg.target, 'a', 'utf-8') as target:
current_node = structure.structure.parts[0]
while current_node:
self._append_content(target, current_node)
current_node = current_node.successor
def _append_content(self, target, node):
"""
Append content of one node to target.
"""
header_offset = config.cfg.header_offset + node.level - 1
with codecs.open(node.source_path, 'r', 'utf-8') as source:
renderer = Renderer(source, filters=self.filters)
# processor.add_filter(partial(mdp.prefix_headline, headline_prefix))
renderer.add_filter(partial(filters.increase_all_headline_levels, header_offset))
renderer.add_filter(partial(filters.write, target))
renderer.render()
target.write("\n\n")