def set_site(self, site): """Set site, which is a Nikola instance.""" super().set_site(site) self.minimum_post_count_per_classification_in_overview = self.site.config[ "TAGLIST_MINIMUM_POSTS"] self.translation_manager = utils.ClassificationTranslationManager() self.pages_index_path = utils.TranslatableSetting( "TAGGED_PAGES_INDEX_PATH", self.site.config["TAGGED_PAGES_INDEX_PATH"], self.site.config["TRANSLATIONS"], ) self.pages_path = utils.TranslatableSetting( "TAGGED_PAGES_PATH", self.site.config["TAGGED_PAGES_PATH"], self.site.config["TRANSLATIONS"], )
def set_site(self, site): self.site = site if self.site.config['DATE_FANCINESS'] == 0 and not self.site.config.get('BABEL_DATE_FORMAT') is None: babel_date_format = utils.TranslatableSetting( 'BABEL_DATE_FORMAT', self.site.config['BABEL_DATE_FORMAT'], self.site.config['TRANSLATIONS']) for lang in self.site.config['TRANSLATIONS']: try: self.site.config['DATE_FORMAT'][lang] = babel_date_format( lang) except KeyError: self.site.config['DATE_FORMAT'][lang] = 'medium' utils.LocaleBorg().add_handler(formatted_date_handler=self.babel_date_formatter) super(BabelDates, self).set_site(site)
def set_site(self, site): """Set site, which is a Nikola instance.""" super().set_site(site) self.show_list_as_index = self.site.config['CATEGORY_PAGES_ARE_INDEXES'] self.template_for_single_list = "tagindex.tmpl" if self.show_list_as_index else "tag.tmpl" self.translation_manager = utils.ClassificationTranslationManager() # Needed to undo names for CATEGORY_PAGES_FOLLOW_DESTPATH self.destpath_names_reverse = {} for lang in self.site.config['TRANSLATIONS']: self.destpath_names_reverse[lang] = {} for k, v in self.site.config['CATEGORY_DESTPATH_NAMES'](lang).items(): self.destpath_names_reverse[lang][v] = k self.destpath_names_reverse = utils.TranslatableSetting( '_CATEGORY_DESTPATH_NAMES_REVERSE', self.destpath_names_reverse, self.site.config['TRANSLATIONS'])
def crawl(node, destinations_so_far, root=True): if node.post_source is not None: try: post = Post(node.post_source, self.site.config, '', False, self.site.MESSAGES, template_name, self.site.get_compiler(node.post_source), destination_base=utils.TranslatableSetting( 'destinations', destinations_so_far, self.site.config['TRANSLATIONS']), metadata_extractors_by=self.site. metadata_extractors_by) timeline.append(post) except Exception as err: LOGGER.error('Error reading post {}'.format(base_path)) raise err # Compute slugs slugs = {} for lang in self.site.config['TRANSLATIONS']: slug = post.meta('slug', lang=lang) if slug: slugs[lang] = slug if not slugs: slugs[self.site.config['DEFAULT_LANG']] = node.name node.slugs = _spread(slugs, self.site.config['TRANSLATIONS'], self.site.config['DEFAULT_LANG']) # Update destinations_so_far if not root: if node.slugs is not None: destinations_so_far = { lang: os.path.join(dest, node.slugs[lang]) for lang, dest in destinations_so_far.items() } else: destinations_so_far = { lang: os.path.join(dest, node.name) for lang, dest in destinations_so_far.items() } for p, n in node.children.items(): crawl(n, destinations_so_far, root=False)
def scan(self): """Create list of posts from POSTS and PAGES options.""" seen = set([]) if not self.site.quiet: print("Scanning posts", end='', file=sys.stderr) timeline = [] for wildcard, destination, template_name, use_in_feeds in \ self.site.config['post_pages']: if not self.site.quiet: print(".", end='', file=sys.stderr) destination_translatable = utils.TranslatableSetting( 'destination', destination, self.site.config['TRANSLATIONS']) dirname = os.path.dirname(wildcard) for dirpath, _, _ in os.walk(dirname, followlinks=True): rel_dest_dir = os.path.relpath(dirpath, dirname) # Get all the untranslated paths dir_glob = os.path.join( dirpath, os.path.basename(wildcard)) # posts/foo/*.rst untranslated = glob.glob(dir_glob) # And now get all the translated paths translated = set([]) for lang in self.site.config['TRANSLATIONS'].keys(): if lang == self.site.config['DEFAULT_LANG']: continue lang_glob = utils.get_translation_candidate( self.site.config, dir_glob, lang) # posts/foo/*.LANG.rst translated = translated.union(set(glob.glob(lang_glob))) # untranslated globs like *.rst often match translated paths too, so remove them # and ensure x.rst is not in the translated set untranslated = set(untranslated) - translated # also remove from translated paths that are translations of # paths in untranslated_list, so x.es.rst is not in the untranslated set for p in untranslated: translated = translated - set([ utils.get_translation_candidate( self.site.config, p, l) for l in self.site.config['TRANSLATIONS'].keys() ]) full_list = list(translated) + list(untranslated) # We eliminate from the list the files inside any .ipynb folder full_list = [ p for p in full_list if not any([x.startswith('.') for x in p.split(os.sep)]) ] for base_path in sorted(full_list): if base_path in seen: continue try: post = Post(base_path, self.site.config, rel_dest_dir, use_in_feeds, self.site.MESSAGES, template_name, self.site.get_compiler(base_path), destination_base=destination_translatable, metadata_extractors_by=self.site. metadata_extractors_by) for lang in post.translated_to: seen.add(post.translated_source_path(lang)) timeline.append(post) except Exception: LOGGER.error('Error reading post {}'.format(base_path)) raise return timeline
def set_site(self, site): """ Map navstories config to nav_config[*] as TranslatableSettings """ # Read NAVSTORIES_SUBMENU_INDENTION and store in self.navstories_submenu_indention if 'NAVSTORIES_SUBMENU_INDENTION' in site.config: self.navstories_submenu_indention = site.config[ 'NAVSTORIES_SUBMENU_INDENTION'] nav_config = {} for i in self.conf_vars: # Read config variables in a try...except in case a variable is missing try: nav_config[i] = utils.TranslatableSetting( i, site.config[i], site.config['TRANSLATIONS']) except KeyError: # Initialize to "empty" in case config variable i is missing nav_config[i] = utils.TranslatableSetting( i, self.conf_defaults[i](), site.config['TRANSLATIONS']) site.scan_posts() # NAVIGATION_LINKS is a TranslatableSetting, values is an actual dict for lang in site.config['NAVIGATION_LINKS'].values: # navstories config for lang nav_conf_lang = {} for i in self.conf_vars: nav_conf_lang[i] = nav_config[i](lang) # Which paths are navstories active for current lang? - Must start and end with / paths = tuple(('/' + s.strip('/') + '/') for s in nav_conf_lang['NAVSTORIES_PATHS']) # Unsorted (raw) new entries, deleted as mapped to new new_raw = {} # Sorted entries as a list of top-level menu entries, later new = [] # Map site pages to new_raw structure for p in site.pages: # Generate navpath (menu) based on permalink without language prefix # If TRANSLATION[DEFAULT_LANG] = '', then "permalink_nolang = p.permalink()" is ok permalink_nolang = re.sub( r'^/' + nav_conf_lang['TRANSLATIONS'].lstrip('./') + '/?', '/', p.permalink(lang)) s_candidates = [ s for s in paths if permalink_nolang.startswith(s) ] if not s_candidates: continue # get longest path s = max(s_candidates, key=len) # Strip off the longest path in paths navpath = permalink_nolang[len(s):].strip('/').split('/') if len(navpath) == 0: # Should not happen that navpath is empty, but to prevent errors, and inform via a warning LOGGER.warn( "Page with permalink: '%s', title: '%s', not added to menu by navstories." % (p.permalink(lang), p.title(lang))) continue if lang in p.translated_to and not p.meta('hidefromnav'): # Add entry if not navpath[0] in new_raw: new_raw[navpath[0]] = [] new_raw[navpath[0]].append( self.NavNode(navpath, p.permalink(lang), p.title(lang))) # Map from new_raw to new, sorting by NAVSTORIES_MAPPING for map_key, map_txt in nav_conf_lang['NAVSTORIES_MAPPING']: # Loop over all new_raw entries, checking if it matches map_key; if match: add it and delete from new_raw if map_key in new_raw: new.append([map_txt, new_raw[map_key]]) del (new_raw[map_key]) # Add remaing new_raw entries which didn't match any map_key new.extend([[None, new_raw[_]] for _ in sorted(new_raw)]) # Map to tuple new_entries = self.map_to_menu(new) old_entries = site.config['NAVIGATION_LINKS'](lang) # Update NAVIGATION_LINKS with navstories dynamically generated entries and NAVIGATION_LINKS_POST_NAVSTORIES entries site.config['NAVIGATION_LINKS'].values[ lang] = old_entries + new_entries + nav_conf_lang[ 'NAVIGATION_LINKS_POST_NAVSTORIES'] super(NavStories, self).set_site(site)