/
Page.py
164 lines (122 loc) · 5.18 KB
/
Page.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'''base page object'''
import os
from datetime import datetime
import config
from common import read_tb_and_content, pandoc_pipe, write_out
from plugin_handler import get_cdata, plugin_cdata_handler, back_substitute
from nav_primary import gen_nav_primary
from nav_pages import gen_nav_pagelist
class Page:
def __init__(self, subpath, file_md):
self.subpath = subpath
self.repo = subpath.repo
self.branch = subpath.repo.branch
self.file_md = file_md
# set out dir
self.out_dir = os.path.join( config.PUBLISH_DIR,
self.branch.out_name,
self.repo.out_name,
self.subpath.path )
# out filename
if file_md.num == 0:
self.out_filename = "index.html"
else:
self.out_filename = os.path.splitext(self.file_md.name)[0]+".html"
# out path
self.out_filepath = os.path.join(self.out_dir, self.out_filename)
# load markdown and title block
self.tb_values, \
self.page_body_md = read_tb_and_content(self.file_md.filepath)
self.title = self.tb_values[0]
self.author = self.tb_values[1]
self.date = self.tb_values[2]
self.date_obj = create_date_obj(self.date)
if self.date_obj == config.BADDATE_STR:
print("pds: WARNING:\n page has a bad date string:", self.file_md.filepath)
self.active = False
def process(self):
self.active = True
# substitute and process plugin content
self.process_plugin_content()
# sets:
# - self.page_body_subst
# - self.cdata_blocks
# - self.plugin_blocks
# - self.plugin_blocks_pdf
# - self.plugin_pandoc_opts
# primary navigation
self.nav_primary = gen_nav_primary(self.branch)
# page list
self.nav_pagelist = gen_nav_pagelist(self.subpath)
# prepare pandoc opts
self.prepare_pandoc()
# process through pandoc
self.page_html_subst = pandoc_pipe(self.page_body_subst,
self.pandoc_opts)
# back-substitute plugin content
if self.plugin_blocks != []:
self.page_html = back_substitute(self.page_html_subst,
self.plugin_blocks)
else:
self.page_html = self.page_html_subst
self.active = False
def process_plugin_content(self):
# plugin substitution
self.page_body_subst, \
self.cdata_blocks = get_cdata(self.page_body_md)
# process the plug-in content
if self.cdata_blocks != []:
self.plugin_blocks, \
self.plugin_blocks_pdf, \
self.plugin_pandoc_opts = plugin_cdata_handler(self.branch,
self.subpath,
self.cdata_blocks)
else:
self.plugin_blocks = []
self.plugin_blocks_pdf = []
self.plugin_pandoc_opts = []
def prepare_pandoc(self):
'''Collect the Pandoc options.'''
self.pandoc_opts = []
# set plugin opts
for opt in self.plugin_pandoc_opts:
self.pandoc_opts.append(opt)
# set the out format
self.pandoc_opts.append('--to=html5')
# set the template
self.pandoc_opts.append('--template='+config.HTML_TEMPLATE)
# include the current branch
# (the check is probably not needed, pandoc may do this as well)
if self.branch.out_name != "":
self.pandoc_opts.append('--variable=branch:'+self.branch.name)
# set the title block opts
for index, tb_value in enumerate(self.tb_values):
self.pandoc_opts.append('--variable='+config.TB_LINES[index]+':'+tb_value)
# don't put title on first base layout pages
if self.repo.name == config.BASE_REPO_NAME and self.file_md.num == 0:
self.pandoc_opts.append('--variable=title:')
# include a table of content
self.pandoc_opts.append('--toc')
self.pandoc_opts.append('--toc-depth='+config.TOC_DEPTH)
# use div's to delimit sections (optional)
if config.SECTION_DIV:
self.pandoc_opts.append('--section-divs')
# include navigation
self.pandoc_opts.append('--variable=nav-primary:'+self.nav_primary)
# add page list
if self.nav_pagelist != "":
self.pandoc_opts.append('--variable=nav-pagelist:'+self.nav_pagelist)
# add path / directory lists (not on main page)
if self.subpath.nav_path != "":
self.pandoc_opts.append('--variable=nav-path:'+self.subpath.nav_path)
if self.subpath.nav_dirlist != "":
self.pandoc_opts.append('--variable=nav-dirlist:'+self.subpath.nav_dirlist)
# ... add more opts here ...
def write_out(self):
write_out(self.page_html, self.out_filepath)
def create_date_obj(date_str):
try:
date_obj = datetime.strptime(date_str, config.DATE_FORMAT)
except ValueError:
date_obj = config.BADDATE_STR
return date_obj