diff --git a/updates_autogeneration_plugin/plugin.py b/updates_autogeneration_plugin/plugin.py index 2a7a05418fef20eb284671fed67e92fa559b9a2d..d2c54f454ba5f55f7129e79d6d6e3b8a7ff5edad 100644 --- a/updates_autogeneration_plugin/plugin.py +++ b/updates_autogeneration_plugin/plugin.py @@ -14,9 +14,6 @@ from mkdocs.structure.nav import Navigation, Section, Page, \ # Files need to be in YYYYMMDD.md format, on the root of the configured path for autogeneration, # i.e. updates/c8/20200101.md FILE_NAME_REGEX = re.compile(r'.+\/(\d*)\.md') -# Exclude pages like updates/slc6/test/latest_updates/ from having footer links, -# previous and next do not make sense here -REGEX_EXCLUDE_FOOTER_LINKS = re.compile(r'updates/.+\/.+\/latest_updates\/') SOURCE_FILES = [] @@ -35,6 +32,18 @@ plugins: path: updates/c8/test/ # This template will be used for the latest_updates.md final result. It is optional template_path: updates/test.tpl + # URL and title for the latest updates section. It is optional. + latest_section_url: 'latest_updates' + latest_section_title: 'Latest updates' + # URL and title for the monthly updates section. It is optional. + monthly_section_url: 'monthly_updates' + monthly_section_title: 'updates' + # Number of latest updates to show. It is optional. + latest_count: 4 + # Whether to show all updates in the latest updates section. It is optional. + latest_shows_all: false + # Generate a single page only with the latest updates, don't generate the chronological pages. It is optional. + generate_single_page: false autogenerate_sitemap: true - search: """ @@ -72,11 +81,17 @@ class UpdatesAutogenerationPlugin(BasePlugin): grouped_list = [[list(subgroup) for _, subgroup in itertools.groupby(group, key=lambda i: i[1].strftime("%m"))] for _, group in itertools.groupby(file_dates_to_order_reversed, key=lambda i: i[1].strftime("%Y"))] + latest_page = self.build_last_updates(self, section_plugin_config, config, files, file_dates_to_order_reversed) + + generate_single_page = bool(section_plugin_config.get("generate_single_page", False)) + if generate_single_page: + return latest_page + # Section where we add our autogenerated content built_section = Section(title=section_plugin_config['section_name'], children=[]) # We add a "Latest updates" Page as the first item of the year - built_section.children.append(self.build_last_updates(self, section_plugin_config, config, files, file_dates_to_order_reversed)) + built_section.children.append(latest_page) # We order years from last to first for year in grouped_list: @@ -92,7 +107,7 @@ class UpdatesAutogenerationPlugin(BasePlugin): # This markdown File has the combined result of all monthly updates, concat in a single file combined_file_path = os.path.join(section_plugin_config['path'], - year_title, month_numeric, "monthly_updates.md") + year_title, month_numeric, section_plugin_config.get("monthly_section_url", "monthly_updates") + ".md") docs_combined_file_path = os.path.join(config['docs_dir'], combined_file_path) if not os.path.exists(os.path.dirname(docs_combined_file_path)): @@ -113,7 +128,7 @@ class UpdatesAutogenerationPlugin(BasePlugin): if len(month) == 0: continue # We need to add a header for the combined result, not depend on input update files, otherwise the table of contents does not build - file_handler_combined.write("# %s updates\n" % (month_title)) + file_handler_combined.write(f"# {month_title} {section_plugin_config.get('monthly_section_title', 'updates')}\n") for update in month: # Reading data from file1 @@ -131,7 +146,7 @@ class UpdatesAutogenerationPlugin(BasePlugin): def build_last_updates(self, section_plugin_config, config, files, file_dates): # This markdown File has the combined result of all monthly updates, concat in a single file - combined_file_path = os.path.join(section_plugin_config['path'], "latest_updates.md") + combined_file_path = os.path.join(section_plugin_config['path'], section_plugin_config.get("latest_section_url", "latest_updates") + ".md") docs_combined_file_path = os.path.join(config['docs_dir'], combined_file_path) if not os.path.exists(os.path.dirname(docs_combined_file_path)): @@ -148,7 +163,7 @@ class UpdatesAutogenerationPlugin(BasePlugin): files.append(file_to_add) # Add the last updates page with combined updates - last_updates_page = Page("Latest updates", file_to_add, config) + last_updates_page = Page(section_plugin_config.get("latest_section_title", "Latest updates"), file_to_add, config) # Use template for updates page if it exists, fallback to a simple header if not if "template_path" in section_plugin_config and os.path.exists(os.path.join(os.getcwd(), config['docs_dir'], section_plugin_config['template_path'])): @@ -158,10 +173,15 @@ class UpdatesAutogenerationPlugin(BasePlugin): file_handler_combined.write(data) else: # We need to add a header for the combined result or TOC won't build - file_handler_combined.write("# Latest updates\n") + file_handler_combined.write(f"# {section_plugin_config.get('latest_section_title', 'Latest updates')}\n") + + latest_shows_all = bool(section_plugin_config.get("latest_shows_all", False)) + if not latest_shows_all: + # Last updates page will only show the 4 latest update dates (by default, can be configured) + latest_count = int(section_plugin_config.get("latest_count", 4)) + file_dates = file_dates[:latest_count] - # Last updates page will only show the 4 latest update dates - for file_date in file_dates[:4]: + for file_date in file_dates: # Reading data from file file_handler = file_date[2] with open(os.path.join(os.getcwd(), config['docs_dir'], file_handler.src_path)) as fp: @@ -276,26 +296,30 @@ class UpdatesAutogenerationPlugin(BasePlugin): return Files(result_files) -def get_regexmatch_or_default(page): +def get_regexmatch_or_default(page, type_list): try: - return REGEX_EXCLUDE_FOOTER_LINKS.match(page.url) + for t in type_list: + if page.url.endswith(f"/{t}/"): + return True except (AttributeError): - return None + pass + return None # We use our custom function to invert footer links when dealing with autogenerated pages, # otherwise next and previous links point to ilogical pages according to the natural month order def _add_previous_and_next_links(pages, config): # Used to control which pages need adjustments on footer links to_generate_paths = tuple(list(entry.values())[0]['path'] for entry in config['autogenerate_path']) + type_list = set(list(entry.values())[0].get('latest_section_url', 'latest_updates') for entry in config['autogenerate_path']) filtered = [page for page in pages if page.url.startswith(to_generate_paths)] bookended = [None] + pages + [None] zipped = zip(bookended[:-2], bookended[1:-1], bookended[2:]) for page0, page1, page2 in zipped: if page1 in filtered: # Matches will be None if they do not need to be excluded - page_exclude_match0 = get_regexmatch_or_default(page0) - page_exclude_match1 = get_regexmatch_or_default(page1) - page_exclude_match2 = get_regexmatch_or_default(page2) + page_exclude_match0 = get_regexmatch_or_default(page0, type_list) + page_exclude_match1 = get_regexmatch_or_default(page1, type_list) + page_exclude_match2 = get_regexmatch_or_default(page2, type_list) # Do not add links for those pages matching the regexr if page_exclude_match1 is None: if page_exclude_match0 is None and page_exclude_match2 is None: