def __get_data(key, cls, method): if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) data = getattr(cls, method) if Options().caching: DataCacher().cache(key, data) return data
def tags_skip_list(self): key = 'tags_skip_list' if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) tags_list = Tags().skip_tags() if Options().caching: DataCacher().cache(key, tags_list) return tags_list
def tags_list(self): key = 'tags_list' if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) if Options().include_drafts: posts = self.posts_files else: posts = self.posts_published tags_list = Tags().list(posts) if Options().caching: DataCacher().cache(key, tags_list) return tags_list
def tags_posts_count(self, tag): key = 'tags_posts_count/{0}'.format(tag) if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) if Options().include_drafts: posts = self.posts_files else: posts = self.posts_published data = Tags().count_posts(posts, tag) if Options().caching: DataCacher().cache(key, data) return data
def posts_data(self, post): key = '/posts/{0}'.format(post) if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) common = self.common_data meta, content, data = Posts().data(post, self.tags_skip_list) combined = self.__combine(common, data) if Options().caching: DataCacher().cache(key, combined) DataCacher().cache('{0}/meta'.format(key), meta) DataCacher().cache('{0}/content'.format(key), content) return combined
def pages_data(self, page): key = '/pages/{0}'.format(page) if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) common = self.common_data meta, content, data = Pages().data( page, self.tags_skip_list) # No catching the meta and raw data yet combined = self.__combine(common, data) if Options().caching: DataCacher().cache(key, combined) DataCacher().cache('{0}/meta'.format(key), meta) DataCacher().cache('{0}/content'.format(key), content) return combined
def tags_data(self, tag, page_index): key = 'tags_data/{0}/{1}'.format(tag, page_index) if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) if Options().include_drafts: posts = self.posts_files else: posts = self.posts_published common = self.common_data data = Tags().data(posts, tag, page_index, self.index_max_posts, self.tags_posts_count(tag)) combined = self.__combine(common, data) if Options().caching: DataCacher().cache(key, combined) return combined
def index_data(self, page_index): key = '/index/{0}'.format(page_index) if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) common = self.common_data if Options().include_drafts: posts = self.posts_files else: posts = self.posts_published data, _ = Index().data(page_index, posts) # We don't care yet about the introduction content combined = self.__combine(common, data) if Options().caching: DataCacher().cache(key, combined) return combined
def common_data(self): key = 'common_data' if Options().caching and DataCacher().cached_already(key): return DataCacher().get_cached(key) data = { 'i8n': self.i8n, 'settings': self.global_settings, 'tags_list': self.tags_list, 'tags_list_count': self.tags_list_count, 'tags_skip_list': self.tags_skip_list, 'main_menu': self.index_main_menu, 'footer_menu': self.index_footer_menu, 'important_news': self.important_news_data, 'code_version': self.code_version_data } if Options().caching: DataCacher().cache(key, data) return data
def data(self, query, page_index, search_base, max_entries): self.__logger.debug('data - query: {0}'.format(query)) self.__logger.debug('data - page_index: {0}'.format(page_index)) self.__logger.debug('data - search_base: {0}'.format(search_base)) settings = Content().load_data_settings_yaml(self.__base_dir) excluded = settings['excluded'] self.__logger.debug('data - excluded: {0}'.format(excluded)) data = {'found': []} page_index = int(page_index) count_entries = 0 skip_entries = (page_index - 1) * max_entries first_entry = skip_entries + 1 for item in search_base: self.__logger.debug('data - search_base item: {0}'.format(item)) directory = item['directory'] file = item['file'] stem = Path(file).stem unmapped = '/{0}/{1}'.format(directory, stem) remapped = Remapper().remap_document(unmapped) if unmapped != remapped: stem = remapped.split('/')[2] # adding the stem to the object for the final url item['stem'] = stem if remapped in excluded: self.__logger.debug('data - excluded item: {0}'.format(item)) continue cached_already = DataCacher().cached_already(unmapped) if cached_already: self.__logger.debug('data - cached: {0}'.format(unmapped)) meta = DataCacher().get_cached('{0}/meta'.format(unmapped)) content = DataCacher().get_cached('{0}/content'.format(unmapped)) else: self.__logger.debug('data - not cached: {0}'.format(unmapped)) meta, content, _ = Content().read_content(directory, file) lowered_query = query.lower() lowered_raw = content.lower() self.__logger.debug('data - lowered_query: {0}'.format(lowered_query)) self.__logger.debug('data - lowered_raw: {0}'.format(lowered_raw)) if lowered_query in lowered_raw: count_entries += 1 len_query = len(lowered_query) if skip_entries >= count_entries: self.__logger.debug('data - item skipped}') continue else: self.__logger.debug('data - item added') index = lowered_raw.find(lowered_query, 0) extra = 70 prefix = '' postfix = '' start = 0 if index > extra: self.__logger.debug('data - cutting text at the beginning') prefix = '... ' start = index - extra if index + extra < len(content): self.__logger.debug('data - cutting text at the end') postfix = " ..." stop = start + (extra * 2) + len(query) self.__logger.debug('data - index: {0}'.format(index)) self.__logger.debug('data - start: {0}'.format(start)) self.__logger.debug('data - stop: {0}'.format(stop)) # removing stuff until first blank for c in range(start, index): if content[c:c + 1] == ' ': self.__logger.debug('data - blank found: {0}'.format(c)) start = c break # removing stuff until first blank, backwards for c in range(stop, index + len(query), -1): if content[c:c + 1] == ' ': self.__logger.debug('data - blank found: {0}'.format(c)) stop = c break self.__logger.debug('data - spaced start: {0}'.format(start)) self.__logger.debug('data - spaced stop: {0}'.format(stop)) # vindevoy - 2020-05-06 - issue-187 # Do not use replace on sample here because replace is case-sensitive sample = prefix + content[start:index] + '<b>' + content[index:index + len_query] + '</b>' sample += content[index + len_query:stop] + postfix self.__logger.debug('data - sample: {0}'.format(sample)) occurrences = lowered_raw.count(lowered_query) self.__logger.debug('data - occurrences: {0}'.format(occurrences)) if not cached_already: meta['date'] = DateTimeSupport().rewrite_date(meta['date']) # cached data has the correct format already entry = { 'item': item, 'meta': meta, 'occurrences': occurrences, 'sample': sample } data['found'].append(entry) if count_entries == (max_entries + skip_entries): self.__logger.debug('data - enough posts') break data['search'] = {'query': query} last_entry = count_entries data['pagination'] = { 'current_page': page_index, 'found_entries': count_entries - skip_entries, 'max_entries': max_entries, 'first_entry': first_entry, 'last_entry': last_entry } return data