-
Notifications
You must be signed in to change notification settings - Fork 93
/
pkgindex_compiler.py
257 lines (210 loc) · 9.2 KB
/
pkgindex_compiler.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# -*- coding: utf-8 -*-
# Copyright © 2016-2017, Chris Warrick and Roberto Alsina.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Post compiler for package indexes."""
from __future__ import unicode_literals
import configparser
import io
import os
import pygments
from nikola import utils
from nikola.plugin_categories import PageCompiler
from pygments.lexers import PythonLexer
def dirname_as_title(post, pkg_dir, config):
"""Use the directory name as title."""
return {'title': os.path.basename(pkg_dir)}
def _version_from_path(path):
if path.startswith('../'):
path = path[3:]
return int(path.split('/')[0].split('v')[-1])
def _cp_try_get(config, section, option):
try:
return config.get(section, option)
except (configparser.NoOptionError, configparser.NoSectionError):
return None
def parse_plugin_file(post, pkg_dir, config):
plugin = os.path.basename(pkg_dir)
data = {}
data['name'] = plugin
conf_sample = os.path.join(pkg_dir, 'conf.py.sample')
ini = os.path.join(pkg_dir, plugin + '.plugin')
reqpy = os.path.join(pkg_dir, 'requirements.txt')
reqnonpy = os.path.join(pkg_dir, 'requirements-nonpy.txt')
reqplugins = os.path.join(pkg_dir, 'requirements-plugins.txt')
if os.path.exists(ini):
post.add_dependency(ini)
try:
c = configparser.ConfigParser()
c.read(ini)
data['author'] = c.get('Documentation', 'Author')
data['version'] = c.get('Documentation', 'Version')
data['description'] = c.get('Documentation', 'Description')
data['website'] = _cp_try_get(c, 'Documentation', 'Website')
data['minver'] = _cp_try_get(c, 'Nikola', 'MinVersion')
data['maxver'] = _cp_try_get(c, 'Nikola', 'MaxVersion')
category = c.get('Nikola', 'PluginCategory')
data['category'] = category
if data['minver']:
minver = data['minver'].split('.')[0]
else:
minver = _version_from_path(pkg_dir)
data['minver'] = minver
if data['maxver']:
maxver = data['maxver'].split('.')[0]
else:
maxver = config['versions_supported'][-1]
data['allver'] = list(range(int(minver), int(maxver) + 1))
tags = ['plugin', category]
if category == 'CompilerExtension':
data['compiler'] = c.get('Nikola', 'Compiler')
tags.append(data['compiler'])
for version in data['allver']:
tags.append('v{0}'.format(version))
data['tags'] = ','.join(tags)
data['tests'] = _cp_try_get(c, 'Core', 'Tests')
except Exception as e:
raise Exception('Exception while reading plugin config "{0}": {1}'.format(ini, e)) from e
else:
raise ValueError('Plugin {0} has no .plugin file in the main '
'directory.'.format(plugin))
if os.path.exists(conf_sample):
post.add_dependency(conf_sample)
with io.open(conf_sample, 'r', encoding='utf-8') as f:
data['confpy'] = pygments.highlight(
f.read(),
PythonLexer(), utils.NikolaPygmentsHTML(plugin + '_conf_sample', linenos=False))
else:
data['confpy'] = None
if os.path.exists(reqpy):
post.add_dependency(reqpy)
with io.open(reqpy, 'r', encoding='utf-8') as f:
data['pyreqs'] = f.readlines()
else:
data['pyreqs'] = []
if os.path.exists(reqnonpy):
post.add_dependency(reqnonpy)
with io.open(reqnonpy, 'r', encoding='utf-8') as f:
r = f.readlines()
data['nonpyreqs'] = [i.strip().split('::') for i in r]
else:
data['nonpyreqs'] = []
if os.path.exists(reqplugins):
post.add_dependency(reqplugins)
with io.open(reqplugins, 'r', encoding='utf-8') as f:
data['pluginreqs'] = f.readlines()
else:
data['pluginreqs'] = []
data['reqcount'] = len(data['pyreqs']) + len(data['nonpyreqs']) + len(data['pluginreqs'])
return data
def parse_theme_info(post, pkg_dir, config):
theme = os.path.basename(pkg_dir)
data = {}
data['name'] = theme
out_path = post.folder_relative + '/' + theme
demo_dir = config['demo_screenshots_map'].get(out_path, out_path)
data['previewimage'] = '/' + demo_dir + '.png'
data['previewimage_thumbnail'] = '/' + demo_dir + '.thumbnail.png'
data['demo_link'] = '/' + demo_dir + '/demo/'
conf_sample = os.path.join(pkg_dir, 'conf.py.sample')
engine = os.path.join(pkg_dir, 'engine')
parent = os.path.join(pkg_dir, 'parent')
if os.path.exists(conf_sample):
post.add_dependency(conf_sample)
with io.open(conf_sample, 'r', encoding='utf-8') as f:
data['confpy'] = pygments.highlight(
f.read(),
PythonLexer(), utils.NikolaPygmentsHTML(theme + '_conf_sample', linenos=False))
else:
data['confpy'] = None
if os.path.exists(engine):
post.add_dependency(engine)
with io.open(engine, 'r', encoding='utf-8') as f:
data['engine'] = f.read().strip()
else:
data['engine'] = 'mako'
if os.path.exists(parent):
post.add_dependency(parent)
with io.open(parent, 'r', encoding='utf-8') as f:
data['parent'] = f.read().strip()
elif theme == 'base':
pass
else:
raise ValueError("Theme {0} has no parent.".format(theme))
data['chain'] = utils.get_theme_chain(theme, [os.path.dirname(pkg_dir), 'themes'])
data['chain'] = [os.path.basename(i) for i in reversed(data['chain'])]
data['bootswatch'] = (('bootstrap' in data['chain'] or
'bootstrap-jinja' in data['chain'] or
'bootstrap3-jinja' in data['chain'] or
'bootstrap3' in data['chain']) and
'bootstrap3-gradients' not in data['chain'])
data['tags'] = 'theme,' + data['engine']
return data
def add_category(post, pkg_dir, config, args):
return {'category': args}
BUILTIN_HANDLERS = {
'dirname_as_title': dirname_as_title,
'parse_plugin_file': parse_plugin_file,
'parse_theme_info': parse_theme_info,
'add_category': add_category
}
def _parse_handler(handler):
if callable(handler):
return handler
elif handler in BUILTIN_HANDLERS:
return BUILTIN_HANDLERS[handler]
else:
raise ValueError("Unknown pkgindex handler {0!r}".format(handler))
class CompilePackageIndexEntries(PageCompiler):
"""Compile package indexes."""
name = "pkgindex_compiler"
friendly_name = "pkgindex_compiler"
markdown_compiler = None
pi_enabled = False
def set_site(self, site):
"""Set site for the compiler."""
# Workaround for plugins site (which includes those plugins, and makes
# tests fail)
if 'PKGINDEX_CONFIG' in site.config:
self.config = site.config['PKGINDEX_CONFIG']
self.pi_enabled = True
super(CompilePackageIndexEntries, self).set_site(site)
def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None):
"""Read the metadata from a post, and return a metadata dict."""
if not self.pi_enabled:
raise Exception("PKGINDEX_CONFIG not found")
pkg_dir = os.path.split(post.source_path)[0]
top_dir = post.folder_relative
metadata = {'slug': os.path.basename(pkg_dir)}
for handler in self.site.config['PKGINDEX_HANDLERS'][top_dir]:
if isinstance(handler, tuple):
function = _parse_handler(handler[0])
metadata.update(function(post, pkg_dir, self.config, handler[1]))
else:
function = _parse_handler(handler)
metadata.update(function(post, pkg_dir, self.config))
return metadata
def compile(self, source, dest, is_two_file=True, post=None, lang=None):
"""Compile to HTML (by passing through to Markdown compiler)."""
if self.markdown_compiler is None:
self.markdown_compiler = self.site.get_compiler('README.md')
return self.markdown_compiler.compile(source, dest, is_two_file, post, lang)