def add_static_comments(gen, content): if gen.settings["PELICAN_COMMENT_SYSTEM"] is not True: return global _all_comments content.comments_count = 0 content.comments = [] # Modify the local context, so we get proper values for the feed context = copy.copy(gen.context) context["SITEURL"] += "/" + content.url context["SITENAME"] += " - Comments: " + content.title context["SITESUBTITLE"] = "" folder = os.path.join(gen.settings["PATH"], gen.settings["PELICAN_COMMENT_SYSTEM_DIR"], content.slug) if not os.path.isdir(folder): logger.debug("No comments found for: %s", content.slug) write_feed(gen, [], context, content.slug) return reader = Readers(gen.settings) comments = [] replies = [] for file in os.listdir(folder): name, extension = os.path.splitext(file) if extension[1:].lower() in reader.extensions: com = reader.read_file(base_path=folder, path=file, content_class=Comment, context=context) com.article = content _all_comments.append(com) if hasattr(com, "replyto"): replies.append(com) else: comments.append(com) feed_items = sorted(comments + replies) feed_items.reverse() warn_on_slug_collision(feed_items) write_feed(gen, feed_items, context, content.slug) # TODO: Fix this O(n²) loop for reply in replies: for comment in chain(comments, replies): if comment.slug == reply.replyto: comment.addReply(reply) count = 0 for comment in comments: comment.sortReplies() count += comment.countReplies() comments = sorted(comments) content.comments_count = len(comments) + count content.comments = comments
def assert_rst_equal(self, rstfile, expectations): reader = Readers(DEFAULT_CONFIG) content = reader.read_file(base_path=RESOURCES_PATH, path=rstfile).content extracted_parts = RE_EXTRACT.findall(content) self.assertEqual(len(extracted_parts), len(expectations)) for expected, extracted in zip(expectations, extracted_parts): self.assertEqual(extracted, expected)
def add_static_comments(gen, content): if gen.settings['PELICAN_COMMENT_SYSTEM'] is not True: return content.comments_count = 0 content.comments = [] # Modify the local context, so we get proper values for the feed context = copy.copy(gen.context) context['SITEURL'] += "/" + content.url context['SITENAME'] += " - Comments: " + content.title context['SITESUBTITLE'] = "" folder = os.path.join( gen.settings['PATH'], gen.settings['PELICAN_COMMENT_SYSTEM_DIR'], content.slug ) if not os.path.isdir(folder): logger.debug("No comments found for: " + content.slug) write_feed(gen, [], context, content.slug) return reader = Readers(gen.settings) comments = [] replies = [] for file in os.listdir(folder): name, extension = os.path.splitext(file) if extension[1:].lower() in reader.extensions: com = reader.read_file( base_path=folder, path=file, content_class=Comment, context=context) if hasattr(com, 'replyto'): replies.append(com) else: comments.append(com) warn_on_slug_collision(comments + replies) write_feed(gen, comments + replies, context, content.slug) # TODO: Fix this O(n²) loop for reply in replies: for comment in chain(comments, replies): if comment.slug == reply.replyto: comment.addReply(reply) count = 0 for comment in comments: comment.sortReplies() count += comment.countReplies() comments = sorted(comments) content.comments_count = len(comments) + count content.comments = comments
def get_posts(pelican_root_directory: str, ): """Given a directory containing pelican draft files, return a generator of all pelican post files.""" # certain keys are required by the reader, even if empty. reader_settings = {"READERS": None, "CACHE_PATH": "/tmp"} pelican_readers = Readers(reader_settings) for (dirpath, _, filename_list) in os.walk(target_directory): for filename in filename_list: content = pelican_readers.read_file(dirpath, filename) yield content
def sync_file(self, settings, filename, content_type): """syncs a single file to the database.""" context = settings.copy() from pelican.readers import Readers from pelican.contents import Article, Page readers = Readers(context) content_class = Article if content_type == "article" else Page content = readers.read_file(base_path=settings['PATH'], path=filename, content_class=content_class, context=context) self.__sync_content(content)
class FeedAlterSettingsWriter(Writer): def __init__(self, output_path, settings=None): super(FeedAlterSettingsWriter, self).__init__( output_path, settings=settings) # copy and alter settings new_settings = copy.deepcopy(self.settings) self.settings[CALLBACK_NAME](new_settings) # put the new settings into readers self.readers = Readers(new_settings) def alter_item(self, item): # split path to dir and filename abspath = os.path.abspath(item.source_path) base_path = os.path.dirname(abspath) path = os.path.basename(abspath) # read content with the new settings return self.readers.read_file(base_path=base_path, path=path, content_class=item.__class__) def _add_item_to_the_feed(self, feed, item): # sunstitute the item with new one created with the new settings item = self.alter_item(item) super(FeedAlterSettingsWriter, self)._add_item_to_the_feed(feed, item)
def add_static_comments(gen, content): if gen.settings['PELICAN_COMMENT_SYSTEM'] is not True: return global _all_comments content.comments_count = 0 content.comments = [] mirror_to_translations(content) # Modify the local context, so we get proper values for the feed context = copy.copy(gen.context) context['SITEURL'] += "/" + content.url context['SITENAME'] += " - Comments: " + content.title context['SITESUBTITLE'] = "" folder = os.path.join(gen.settings['PATH'], gen.settings['PELICAN_COMMENT_SYSTEM_DIR'], content.slug) if not os.path.isdir(folder): logger.debug("No comments found for: %s", content.slug) write_feed(gen, [], context, content.slug) return reader = Readers(gen.settings) comments = [] replies = [] for file in os.listdir(folder): name, extension = os.path.splitext(file) if extension[1:].lower() in reader.extensions: com = reader.read_file(base_path=folder, path=file, content_class=Comment, context=context) com.article = content _all_comments.append(com) if hasattr(com, 'replyto'): replies.append(com) else: comments.append(com) feed_items = sorted(comments + replies) feed_items.reverse() warn_on_slug_collision(feed_items) write_feed(gen, feed_items, context, content.slug) # TODO: Fix this O(n²) loop for reply in replies: found_parent = False for comment in chain(comments, replies): if comment.slug == reply.replyto: comment.addReply(reply) found_parent = True break if not found_parent: logger.warning( 'Comment "%s/%s" is a reply to non-existent comment "%s". ' 'Make sure the replyto attribute is set correctly.', content.slug, reply.slug, reply.replyto) count = 0 for comment in comments: comment.sortReplies() count += comment.countReplies() comments = sorted(comments) content.comments_count = len(comments) + count content.comments = comments mirror_to_translations(content)
def add_static_comments(gen, content): if gen.settings['PELICAN_COMMENT_SYSTEM'] is not True: return global _all_comments content.comments_count = 0 content.comments = [] mirror_to_translations(content) # Modify the local context, so we get proper values for the feed context = copy.copy(gen.context) context['SITEURL'] += "/" + content.url context['SITENAME'] += " - Comments: " + content.title context['SITESUBTITLE'] = "" folder = os.path.join( gen.settings['PATH'], gen.settings['PELICAN_COMMENT_SYSTEM_DIR'], content.slug ) if not os.path.isdir(folder): logger.debug("No comments found for: %s", content.slug) write_feed(gen, [], context, content.slug) return reader = Readers(gen.settings) comments = [] replies = [] for file in os.listdir(folder): name, extension = os.path.splitext(file) if extension[1:].lower() in reader.extensions: com = reader.read_file( base_path=folder, path=file, content_class=Comment, context=context) com.article = content _all_comments.append(com) if hasattr(com, 'replyto'): replies.append(com) else: comments.append(com) feed_items = sorted(comments + replies) feed_items.reverse() warn_on_slug_collision(feed_items) write_feed(gen, feed_items, context, content.slug) # TODO: Fix this O(n²) loop for reply in replies: found_parent = False for comment in chain(comments, replies): if comment.slug == reply.replyto: comment.addReply(reply) found_parent = True break if not found_parent: logger.warning('Comment "%s/%s" is a reply to non-existent comment "%s". ' 'Make sure the replyto attribute is set correctly.', content.slug, reply.slug, reply.replyto) count = 0 for comment in comments: comment.sortReplies() count += comment.countReplies() comments = sorted(comments) content.comments_count = len(comments) + count content.comments = comments mirror_to_translations(content)
def read_file(self, path, **kwargs): # Isolate from future API changes to readers.read_file r = Readers(settings=get_settings(**kwargs)) return r.read_file(base_path=CONTENT_PATH, path=path)
def assert_rst_equal(self, rstfile, expected, **kwargs): pelican = Pelican(settings=get_settings(**kwargs)) reader = Readers(pelican.settings) content = reader.read_file(base_path=RESOURCES_PATH, path=rstfile).content self.assertEqual(normalize(content), normalize(expected))