def __init__(self, options): self.options = options self.version = __init__.__version__ plugin_folders = [os.path.join(sys.path[0], 'plugins')] if os.path.exists('site.ini'): template.base_dir = os.path.join(options.dir, 'theme') self.verbose_log('Loading configuration files') self.configs = utils.load_configs(options.dir) config = utils.find_config(self.configs, '/') if config is not None: plugin_path = config.get('control', 'plugins_path') if plugin_path is not None: plugin_folders.append(os.path.join(os.getcwd(), plugin_path)) self.verbose_log('Initialising plugin manager') self.pm = PluginManager(plugin_folders) self.verbose_log(' - complete') if os.path.exists('site.ini'): self.verbose_log('Loading pages') self.pages = load_pages(options.dir, options.output, self) self.verbose_log(' - complete') self.commands = {} self.verbose_log('Running commands filter') apply_filter('commands', self.commands)
def write_index_page(index_page, output_path, path, page_num): page = '' if page_num > 0: page = '-%s' % page_num apply_filter('blog-index-page', index_page) apply_filter('pre-output', index_page) out = str(index_page.template) f = open(os.path.join(output_path, path, 'index%s.html' % page), 'w+') f.write(out) f.close()
def new_index_page(page_to_copy, page_num, count, total_posts, posts_per_page, path): index_page = Page() index_page.copy(page_to_copy) index_page.url = '/' + path + '/index-%s' % count index_page.template = template.get_template('post.html') apply_filter('page-head', index_page) apply_filter('page-meta', index_page) apply_filter('page-menu', index_page) apply_filter('page-foot', index_page) total_pages = math.ceil(total_posts / posts_per_page) - 1 if page_num > 0: index_page.template.set_value('prevpage', '<< Newer posts') if page_num - 1 == 0: index_page.template.set_attribute('prevpage', 'href', 'index.html') else: index_page.template.set_attribute('prevpage', 'href', 'index-%s.html' % (page_num - 1)) if page_num < total_pages: index_page.template.set_value('nextpage', 'Older posts >>') index_page.template.set_attribute('nextpage', 'href', 'index-%s.html' % (page_num + 1)) if page_num > 0 and page_num < total_pages: index_page.template.set_value('pagelinksep', ' | ') index_page.template.repeat('posts', min(posts_per_page, total_posts - count)) return index_page
def index_command(kernel, *args): """ Create a search index for pages in the site. """ print('Indexing site...') db = get_connection(kernel) with db.xact(): db.prepare('delete from search')() ps = db.prepare('''insert into search (url, title, search_text, actual_text, last_modified) values ($1, $2, to_tsvector($3), $4, $5)''') for page in kernel.pages.values(): with db.xact(): url = page.url if page.filename is not None and not page.filename.endswith('index.html'): url = page.filename if not url.startswith('/'): url = '/' + url if url.endswith('/index'): url = url[0:-6] content = apply_filter('markdown', page.content) ps(url, page.headers['title'], page.headers['title'] + "\n" + page.content, content, page.last_modified) print('Complete')
def process_path(path, output_path, pages): sorted_posts = sorted(filter_pages(path, pages.values()), key=lambda x: x.url[len(path)+1:len(path)+11], reverse=True) total_posts = len(sorted_posts) if total_posts == 0: return posts_per_page = get_posts_per_page(sorted_posts) count = 0 page_num = 0 index = 0 if path.startswith('/'): path = path[1:] index_page = new_index_page(sorted_posts[0], page_num, count, total_posts, posts_per_page, path) do_action('post-meta-reset') for page in sorted_posts: cpage = Page() cpage.copy(page) cpage.template = index_page.template # TODO: this isn't right, need to fix later apply_filter('pre-markdown', cpage) content = apply_filter('markdown', page.content) cpage.template.set_value('content', content, index) apply_filter('post-meta', cpage, index) index += 1 count += 1 if index >= posts_per_page: write_index_page(index_page, output_path, path, page_num) do_action('post-meta-reset') index = 0 page_num += 1 index_page = new_index_page(sorted_posts[0], page_num, count, total_posts, posts_per_page, path) if index > 0: write_index_page(index_page, output_path, path, page_num)
def get_html_content(self, include_title=False): domain = self.config.get('site', 'domain') if not include_title: if title_end_re.search(self.content) is not None: content = remove_title(content) else: content = self.content else: content = self.content html_content = apply_filter('markdown', content) return html_content.replace('src="/', 'src="http://%s/' % domain)
def generate(kernel, *args): """ Generate the html artifacts for the site, compress applicable files. Performs the following actions: 1. load pages 2. generate the output for each page 3. apply the 'pages' global filter for all pages 4. compress any applicable files (images, etc) """ print('Processing pages') for page in kernel.pages.values(): kernel.verbose_log(' - found file %s' % page.filename) generate_page(kernel, page) apply_filter('pages', kernel.pages, kernel.options.output) print('Compressing files') comp = utils.Compressor(kernel.options.dir, kernel.options.output) comp.compress_files() print('Generation complete')
def process_pages(pages, output_path): root_page = get_root_page(pages) if not root_page: return pages index_title = root_page.config.get('tags', 'index_title') tag_title = root_page.config.get('tags', 'title') tags = {} print('Generating tag index pages') for page in pages.values(): if 'tags' in page.headers: for tag in split_tags(page.headers['tags']): tag = sanitise_tag(tag) if tag not in tags: tags[tag] = [] tags[tag].append(page) tag_dir = os.path.join(output_path, 'tags') if not os.path.exists(tag_dir): os.makedirs(tag_dir) for fname in os.listdir(tag_dir): os.remove(os.path.join(tag_dir, fname)) for tag in tags: tmp = template.get_template('tag.html') print(' - generating tag %s' % tag) output_name = os.path.join(tag_dir, '%s.html' % tag) tmp.repeat('posts', len(tags[tag])) x = 0 do_action('post-meta-reset') for page in sorted(tags[tag], key=lambda x: x.last_modified, reverse=False): cpage = Page() cpage.copy(page) cpage.template = tmp tmp.set_value('content', page.get_html_content(include_title=True), x) apply_filter('post-meta', cpage, x) x += 1 apply_filter('page-head', cpage) apply_filter('page-meta', cpage) apply_filter('page-menu', cpage) apply_filter('page-foot', cpage) tmp.set_value('page-title', tag_title % tag) tmp.set_value('tag', tag_title % tag) apply_filter('tag-page', cpage) out = str(tmp) f = open(output_name, 'w+') f.write(out) f.close() tag_counts = {k: len(v) for k, v in tags.items()} tag_classes = {} page = next(iter(pages.values())) for (name, value) in page.config.items('tags'): if name.isdigit(): tag_classes[name] = value tmp = template.get_template('tags.html') if tmp is None: return tmp.repeat('tags', len(tags)) x = 0 for tag in sorted(tags): tmp.set_value('tag', tag, x) tmp.set_attribute('tag', 'href', '/tags/%s.html' % tag, x) count = tag_counts[tag] for key in sorted(tag_classes, reverse=True): if count >= int(key): tmp.set_attribute('tag', 'class', tag_classes[key], x) x += 1 cpage = Page() cpage.copy(page) cpage.template = tmp apply_filter('page-head', cpage) apply_filter('page-meta', cpage) apply_filter('page-menu', cpage) apply_filter('page-foot', cpage) tmp.set_value('page-title', index_title) apply_filter('tags-index-page', cpage) output_name = os.path.join(tag_dir, 'index.html') f = open(output_name, 'w+') f.write(str(tmp)) f.close() print(' - complete') return pages
def generate_page(kernel, page): """ Internal generator called for each page which applies the following filters (before writing the html output): 1. page-head 2. page-meta 3. page-markdown 4. page-menu 5. post-meta 6. page-foot """ if not page.template: kernel.verbose_log(' (no template, nothing to do)') return if os.path.exists(page.full_output_path): statbuf = os.stat(page.full_output_path) last_modified = datetime.datetime.fromtimestamp(statbuf.st_mtime) else: last_modified = datetime.datetime.strptime('1970-01-01 00:00:00', '%Y-%m-%d %H:%M:%S') page.output_url = page.filename do_action('post-meta-reset') apply_filter('page-head', page) apply_filter('page-meta', page) apply_filter('page-markdown', page) apply_filter('page-menu', page) apply_filter('post-meta', page) apply_filter('page-foot', page) if last_modified > page.last_modified and kernel.options.force == 'false': kernel.verbose_log(' (not processing lastmod %s > pagemod %s)' % (last_modified, page.last_modified)) return print(' - %s' % page.url) page.template.set_attribute('generator', 'content', 'SiteBaker v%s' % __init__.__version__) apply_filter('pre-output', page) out = str(page.template) f = open(page.full_output_path, 'w+') f.write(out) f.close()
def process(page, index=0): apply_filter('pre-markdown', page) content = apply_filter('markdown', page.content) content = add_anchors(content) page.template.set_value('content', content, index) return page