From 1734c1d2b9409d03ab28ab68a0a8f23321ce2be7 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Fri, 19 Jul 2024 14:25:45 +0300 Subject: [PATCH 01/21] added includes map generation --- foliant/preprocessors/includes.py | 44 ++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index eb6a582..e3e1deb 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -1,5 +1,7 @@ import re import urllib +import json +import os from shutil import rmtree from io import StringIO from hashlib import md5 @@ -43,6 +45,8 @@ def __init__(self, *args, **kwargs): self._cache_dir_path = self.project_path / self.options['cache_dir'] self._downloaded_dir_path = self._cache_dir_path / '_downloaded_content' + self.src_dir = self.config.get("src_dir") + self.includes_map = {} self.logger = self.logger.getChild('includes') @@ -850,6 +854,7 @@ def process_includes( :returns: Markdown content with resolved includes ''' + recipient_md_path = markdown_file_path.relative_to(self.working_dir).as_posix() markdown_file_path = markdown_file_path.resolve() self.logger.debug(f'Processing Markdown file: {markdown_file_path}') @@ -867,11 +872,19 @@ def process_includes( include_statement = self.pattern.fullmatch(content_part) if include_statement: + donor_md_path = None + current_project_root_path = project_root_path body = self._tag_body_pattern.match(include_statement.group('body').strip()) options = self.get_options(include_statement.group('options')) + self.logger.debug(f'Include pair: {markdown_file_path} <- {options} {body}') + + # TODO: + # :param markdown_file_path: + # :returns date: + self.logger.debug( f'Processing include statement; body: {body}, options: {options}, ' + f'current project root path: {current_project_root_path}' @@ -949,6 +962,8 @@ def process_includes( self.logger.debug(f'Local path of the repo: {repo_path}') included_file_path = repo_path / body.group('path') + + donor_md_path = included_file_path.as_posix() + "1" if included_file_path.name.startswith('^'): included_file_path = self._find_file( @@ -975,6 +990,7 @@ def process_includes( else: self.logger.debug('Local file referenced') + donor_md_path = f"{self.src_dir}/{markdown_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" + "2" included_file_path = self._get_included_file_path(body.group('path'), markdown_file_path) if included_file_path.name.startswith('^'): @@ -1000,7 +1016,7 @@ def process_includes( nohead=options.get('nohead') ) - else: # if body + else: # if body missed self.logger.debug('Using the new syntax rules') if options.get('repo_url') and options.get('path'): @@ -1036,6 +1052,8 @@ def process_includes( include_link=include_link ) + donor_md_path = include_link + "3" + elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1061,13 +1079,22 @@ def process_includes( sethead=current_sethead, nohead=options.get('nohead') ) + + donor_md_path = options['url'] + "4" elif options.get('src'): self.logger.debug('Local file referenced') + # donor_md_path = f"{self.src_dir}/{markdown_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" + "5" included_file_path = self._get_included_file_path(options.get('src'), markdown_file_path) - self.logger.debug(f'Resolved path to the included file: {included_file_path}') + + if included_file_path.as_posix().startswith(os.getcwd()): + _path = included_file_path.relative_to(os.getcwd()) + if _path.as_posix().startswith(self.working_dir.as_posix()): + donor_md_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" + "5" + else: + donor_md_path = _path.as_posix() + "6" if options.get('project_root'): current_project_root_path = ( @@ -1087,6 +1114,7 @@ def process_includes( sethead=current_sethead, nohead=options.get('nohead') ) + else: self.logger.warning( 'Neither repo_url+path nor src specified, ignoring the include statement' @@ -1144,6 +1172,12 @@ def process_includes( processed_content_part = re.sub(r'\s+', ' ', processed_content_part).strip() + if donor_md_path: + if self.includes_map.get(recipient_md_path) == None : + self.includes_map[recipient_md_path] = [] + + self.includes_map[recipient_md_path].append({"path": donor_md_path}) + else: processed_content_part = content_part @@ -1191,7 +1225,6 @@ def apply(self): for source_file_path in self.working_dir.rglob(source_files_extension): with open(source_file_path, encoding='utf8') as source_file: source_content = source_file.read() - processed_content = self.process_includes( source_file_path, source_content, @@ -1201,5 +1234,10 @@ def apply(self): if processed_content: with open(source_file_path, 'w', encoding='utf8') as processed_file: processed_file.write(processed_content) + + # Write includes map + Path(f'{self.working_dir}/static/').mkdir(parents=True, exist_ok=True) + with open(f'{self.working_dir}/static/includes_map.json', 'w', encoding='utf8') as f: + json.dump(self.includes_map, f) self.logger.info('Preprocessor applied') From 443b5163fe95dd46a81de1368c51612f283ca23e Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Fri, 19 Jul 2024 14:30:01 +0300 Subject: [PATCH 02/21] fix --- foliant/preprocessors/includes.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index e3e1deb..243131b 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -963,7 +963,7 @@ def process_includes( included_file_path = repo_path / body.group('path') - donor_md_path = included_file_path.as_posix() + "1" + donor_md_path = included_file_path.as_posix() if included_file_path.name.startswith('^'): included_file_path = self._find_file( @@ -990,7 +990,7 @@ def process_includes( else: self.logger.debug('Local file referenced') - donor_md_path = f"{self.src_dir}/{markdown_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" + "2" + donor_md_path = f"{self.src_dir}/{markdown_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" included_file_path = self._get_included_file_path(body.group('path'), markdown_file_path) if included_file_path.name.startswith('^'): @@ -1016,7 +1016,7 @@ def process_includes( nohead=options.get('nohead') ) - else: # if body missed + else: # if body is missing self.logger.debug('Using the new syntax rules') if options.get('repo_url') and options.get('path'): @@ -1052,7 +1052,7 @@ def process_includes( include_link=include_link ) - donor_md_path = include_link + "3" + donor_md_path = include_link elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1080,21 +1080,20 @@ def process_includes( nohead=options.get('nohead') ) - donor_md_path = options['url'] + "4" + donor_md_path = options['url'] elif options.get('src'): self.logger.debug('Local file referenced') - # donor_md_path = f"{self.src_dir}/{markdown_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" + "5" included_file_path = self._get_included_file_path(options.get('src'), markdown_file_path) self.logger.debug(f'Resolved path to the included file: {included_file_path}') if included_file_path.as_posix().startswith(os.getcwd()): _path = included_file_path.relative_to(os.getcwd()) if _path.as_posix().startswith(self.working_dir.as_posix()): - donor_md_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" + "5" + donor_md_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" else: - donor_md_path = _path.as_posix() + "6" + donor_md_path = _path.as_posix() if options.get('project_root'): current_project_root_path = ( From 17643be6104aca61fdf38b37619f47609f476afc Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Fri, 19 Jul 2024 16:01:45 +0300 Subject: [PATCH 03/21] update --- foliant/preprocessors/includes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 243131b..565200c 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -854,7 +854,7 @@ def process_includes( :returns: Markdown content with resolved includes ''' - recipient_md_path = markdown_file_path.relative_to(self.working_dir).as_posix() + recipient_md_path = f'{self.src_dir}/{markdown_file_path.relative_to(self.working_dir).as_posix()}' markdown_file_path = markdown_file_path.resolve() self.logger.debug(f'Processing Markdown file: {markdown_file_path}') @@ -1175,7 +1175,7 @@ def process_includes( if self.includes_map.get(recipient_md_path) == None : self.includes_map[recipient_md_path] = [] - self.includes_map[recipient_md_path].append({"path": donor_md_path}) + self.includes_map[recipient_md_path].append(donor_md_path) else: processed_content_part = content_part @@ -1224,6 +1224,7 @@ def apply(self): for source_file_path in self.working_dir.rglob(source_files_extension): with open(source_file_path, encoding='utf8') as source_file: source_content = source_file.read() + processed_content = self.process_includes( source_file_path, source_content, From ff5e3b43ec9e9be4ec6a46242558f059bf0952e1 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Fri, 19 Jul 2024 16:45:24 +0300 Subject: [PATCH 04/21] fix --- foliant/preprocessors/includes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 565200c..48abeb2 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -990,8 +990,8 @@ def process_includes( else: self.logger.debug('Local file referenced') - donor_md_path = f"{self.src_dir}/{markdown_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" included_file_path = self._get_included_file_path(body.group('path'), markdown_file_path) + donor_md_path = f"{self.src_dir}/{included_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" if included_file_path.name.startswith('^'): included_file_path = self._find_file( From 2d91a1fb2c7b2ade22b2e71d8ab183cc4a964f0b Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Fri, 19 Jul 2024 20:55:46 +0300 Subject: [PATCH 05/21] update and add debug --- foliant/preprocessors/includes.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 48abeb2..dfe7dcb 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -855,6 +855,7 @@ def process_includes( ''' recipient_md_path = f'{self.src_dir}/{markdown_file_path.relative_to(self.working_dir).as_posix()}' + markdown_file_path = markdown_file_path.resolve() self.logger.debug(f'Processing Markdown file: {markdown_file_path}') @@ -964,6 +965,8 @@ def process_includes( included_file_path = repo_path / body.group('path') donor_md_path = included_file_path.as_posix() + + self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path}') if included_file_path.name.startswith('^'): included_file_path = self._find_file( @@ -991,7 +994,6 @@ def process_includes( self.logger.debug('Local file referenced') included_file_path = self._get_included_file_path(body.group('path'), markdown_file_path) - donor_md_path = f"{self.src_dir}/{included_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" if included_file_path.name.startswith('^'): included_file_path = self._find_file( @@ -1016,6 +1018,10 @@ def process_includes( nohead=options.get('nohead') ) + donor_md_path = f"{self.src_dir}/{included_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" + + self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path}') + else: # if body is missing self.logger.debug('Using the new syntax rules') @@ -1053,6 +1059,8 @@ def process_includes( ) donor_md_path = include_link + + self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path}') elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1081,6 +1089,8 @@ def process_includes( ) donor_md_path = options['url'] + + self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path}') elif options.get('src'): self.logger.debug('Local file referenced') @@ -1094,6 +1104,8 @@ def process_includes( donor_md_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" else: donor_md_path = _path.as_posix() + + self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path}') if options.get('project_root'): current_project_root_path = ( From 5203691159376d8251761ff7a418510e4a365aa0 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Fri, 19 Jul 2024 21:23:25 +0300 Subject: [PATCH 06/21] update debug --- foliant/preprocessors/includes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index dfe7dcb..9d6c928 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -966,7 +966,7 @@ def process_includes( donor_md_path = included_file_path.as_posix() - self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path}') + self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path} (1)') if included_file_path.name.startswith('^'): included_file_path = self._find_file( @@ -1020,7 +1020,7 @@ def process_includes( donor_md_path = f"{self.src_dir}/{included_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" - self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path}') + self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') else: # if body is missing self.logger.debug('Using the new syntax rules') @@ -1060,7 +1060,7 @@ def process_includes( donor_md_path = include_link - self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path}') + self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1090,7 +1090,7 @@ def process_includes( donor_md_path = options['url'] - self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path}') + self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path} (4)') elif options.get('src'): self.logger.debug('Local file referenced') @@ -1105,7 +1105,7 @@ def process_includes( else: donor_md_path = _path.as_posix() - self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path}') + self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (5)') if options.get('project_root'): current_project_root_path = ( From 22542cfe8c5db7e797a1aeba1dbe830db1d8a5b1 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Sat, 20 Jul 2024 15:07:27 +0300 Subject: [PATCH 07/21] fix case 3 --- foliant/preprocessors/includes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 9d6c928..cd3bbfa 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -1058,7 +1058,7 @@ def process_includes( include_link=include_link ) - donor_md_path = include_link + donor_md_path = include_link + options.get('path') self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') From 518e8d7f00cbc3f2b8c0c3dc465127c600077aec Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Mon, 22 Jul 2024 13:56:37 +0300 Subject: [PATCH 08/21] update --- foliant/preprocessors/includes.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index cd3bbfa..d8a3ed1 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -835,6 +835,16 @@ def _process_include( return included_content + def _prepare_path_for_includes_map(self, path: Path) -> str: + donor_path = None + if path.as_posix().startswith(os.getcwd()): + _path = path.relative_to(os.getcwd()) + if _path.as_posix().startswith(self.working_dir.as_posix()): + donor_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" + else: + donor_path = _path.as_posix() + return donor_path + def process_includes( self, markdown_file_path: Path, @@ -965,7 +975,6 @@ def process_includes( included_file_path = repo_path / body.group('path') donor_md_path = included_file_path.as_posix() - self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path} (1)') if included_file_path.name.startswith('^'): @@ -1018,8 +1027,8 @@ def process_includes( nohead=options.get('nohead') ) - donor_md_path = f"{self.src_dir}/{included_file_path.relative_to(os.getcwd()).relative_to(self.working_dir).as_posix()}" + donor_md_path = f"{self.src_dir}/{self._prepare_path_for_includes_map(included_file_path)}" self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') else: # if body is missing @@ -1059,7 +1068,6 @@ def process_includes( ) donor_md_path = include_link + options.get('path') - self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') elif options.get('url'): @@ -1089,7 +1097,6 @@ def process_includes( ) donor_md_path = options['url'] - self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path} (4)') elif options.get('src'): @@ -1098,13 +1105,7 @@ def process_includes( included_file_path = self._get_included_file_path(options.get('src'), markdown_file_path) self.logger.debug(f'Resolved path to the included file: {included_file_path}') - if included_file_path.as_posix().startswith(os.getcwd()): - _path = included_file_path.relative_to(os.getcwd()) - if _path.as_posix().startswith(self.working_dir.as_posix()): - donor_md_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" - else: - donor_md_path = _path.as_posix() - + donor_md_path = self._prepare_path_for_includes_map(included_file_path) self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (5)') if options.get('project_root'): From c3a04d4854551266fbb9cce2c441db0274effdff Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Tue, 23 Jul 2024 14:30:26 +0300 Subject: [PATCH 09/21] add option --- foliant/preprocessors/includes.py | 68 ++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index d8a3ed1..54ed724 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -22,7 +22,8 @@ class Preprocessor(BasePreprocessor): 'allow_failure': True, 'cache_dir': Path('.includescache'), 'aliases': {}, - 'extensions': ['md'] + 'extensions': ['md'], + 'includes_map': False } tags = 'include', @@ -46,7 +47,9 @@ def __init__(self, *args, **kwargs): self._cache_dir_path = self.project_path / self.options['cache_dir'] self._downloaded_dir_path = self._cache_dir_path / '_downloaded_content' self.src_dir = self.config.get("src_dir") - self.includes_map = {} + self.includes_map_enable = self.options['includes_map'] + if self.includes_map_enable: + self.includes_map = {} self.logger = self.logger.getChild('includes') @@ -837,10 +840,17 @@ def _process_include( def _prepare_path_for_includes_map(self, path: Path) -> str: donor_path = None - if path.as_posix().startswith(os.getcwd()): + if path.as_posix().startswith(self.working_dir.as_posix()): + _path = path.relative_to(self.working_dir) + donor_path = f"{self.src_dir}/{_path.as_posix()}" + elif path.as_posix().startswith(os.getcwd()): _path = path.relative_to(os.getcwd()) if _path.as_posix().startswith(self.working_dir.as_posix()): - donor_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" + _path = _path.relative_to(self.working_dir) + if _path.as_posix().startswith(self.working_dir.as_posix()): + donor_path = f"{self.src_dir}/{_path.relative_to(self.working_dir).as_posix()}" + else: + donor_path = f"{self.src_dir}/{_path.as_posix()}" else: donor_path = _path.as_posix() return donor_path @@ -864,7 +874,8 @@ def process_includes( :returns: Markdown content with resolved includes ''' - recipient_md_path = f'{self.src_dir}/{markdown_file_path.relative_to(self.working_dir).as_posix()}' + if self.includes_map_enable: + recipient_md_path = f'{self.src_dir}/{markdown_file_path.relative_to(self.working_dir).as_posix()}' markdown_file_path = markdown_file_path.resolve() @@ -883,7 +894,8 @@ def process_includes( include_statement = self.pattern.fullmatch(content_part) if include_statement: - donor_md_path = None + if self.includes_map_enable: + donor_md_path = None current_project_root_path = project_root_path @@ -974,8 +986,9 @@ def process_includes( included_file_path = repo_path / body.group('path') - donor_md_path = included_file_path.as_posix() - self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path} (1)') + if self.includes_map_enable: + donor_md_path = included_file_path.as_posix() + self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path} (1)') if included_file_path.name.startswith('^'): included_file_path = self._find_file( @@ -1027,9 +1040,9 @@ def process_includes( nohead=options.get('nohead') ) - - donor_md_path = f"{self.src_dir}/{self._prepare_path_for_includes_map(included_file_path)}" - self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') + if self.includes_map_enable: + donor_md_path = f"{self.src_dir}/{self._prepare_path_for_includes_map(included_file_path)}" + self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') else: # if body is missing self.logger.debug('Using the new syntax rules') @@ -1067,8 +1080,9 @@ def process_includes( include_link=include_link ) - donor_md_path = include_link + options.get('path') - self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') + if self.includes_map_enable: + donor_md_path = include_link + options.get('path') + self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1096,8 +1110,9 @@ def process_includes( nohead=options.get('nohead') ) - donor_md_path = options['url'] - self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path} (4)') + if self.includes_map_enable: + donor_md_path = options['url'] + self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path} (4)') elif options.get('src'): self.logger.debug('Local file referenced') @@ -1105,8 +1120,9 @@ def process_includes( included_file_path = self._get_included_file_path(options.get('src'), markdown_file_path) self.logger.debug(f'Resolved path to the included file: {included_file_path}') - donor_md_path = self._prepare_path_for_includes_map(included_file_path) - self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (5)') + if self.includes_map_enable: + donor_md_path = self._prepare_path_for_includes_map(included_file_path) + self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (5)') if options.get('project_root'): current_project_root_path = ( @@ -1184,11 +1200,12 @@ def process_includes( processed_content_part = re.sub(r'\s+', ' ', processed_content_part).strip() - if donor_md_path: - if self.includes_map.get(recipient_md_path) == None : - self.includes_map[recipient_md_path] = [] + if self.includes_map_enable: + if donor_md_path: + if self.includes_map.get(recipient_md_path) == None : + self.includes_map[recipient_md_path] = [] - self.includes_map[recipient_md_path].append(donor_md_path) + self.includes_map[recipient_md_path].append(donor_md_path) else: processed_content_part = content_part @@ -1249,8 +1266,11 @@ def apply(self): processed_file.write(processed_content) # Write includes map - Path(f'{self.working_dir}/static/').mkdir(parents=True, exist_ok=True) - with open(f'{self.working_dir}/static/includes_map.json', 'w', encoding='utf8') as f: - json.dump(self.includes_map, f) + if self.includes_map_enable: + output = f'{self.working_dir}/static/includes_map.json' + Path(f'{self.working_dir}/static/').mkdir(parents=True, exist_ok=True) + with open(f'{self.working_dir}/static/includes_map.json', 'w', encoding='utf8') as f: + json.dump(self.includes_map, f) + self.logger.debug(f'includes_map write to {output}') self.logger.info('Preprocessor applied') From 8f7845dfb5a71e145b747af3fbabe4c071eca743 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Tue, 23 Jul 2024 15:39:30 +0300 Subject: [PATCH 10/21] fix import --- foliant/preprocessors/includes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 54ed724..d44f906 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -1,13 +1,13 @@ import re import urllib -import json -import os from shutil import rmtree from io import StringIO from hashlib import md5 from pathlib import Path import socket from subprocess import run, CalledProcessError, PIPE, STDOUT +from json import dump +from os import getcwd from foliant.preprocessors.base import BasePreprocessor @@ -23,7 +23,7 @@ class Preprocessor(BasePreprocessor): 'cache_dir': Path('.includescache'), 'aliases': {}, 'extensions': ['md'], - 'includes_map': False + 'includes_map': True } tags = 'include', @@ -843,8 +843,8 @@ def _prepare_path_for_includes_map(self, path: Path) -> str: if path.as_posix().startswith(self.working_dir.as_posix()): _path = path.relative_to(self.working_dir) donor_path = f"{self.src_dir}/{_path.as_posix()}" - elif path.as_posix().startswith(os.getcwd()): - _path = path.relative_to(os.getcwd()) + elif path.as_posix().startswith(getcwd()): + _path = path.relative_to(getcwd()) if _path.as_posix().startswith(self.working_dir.as_posix()): _path = _path.relative_to(self.working_dir) if _path.as_posix().startswith(self.working_dir.as_posix()): @@ -1270,7 +1270,7 @@ def apply(self): output = f'{self.working_dir}/static/includes_map.json' Path(f'{self.working_dir}/static/').mkdir(parents=True, exist_ok=True) with open(f'{self.working_dir}/static/includes_map.json', 'w', encoding='utf8') as f: - json.dump(self.includes_map, f) + dump(self.includes_map, f) self.logger.debug(f'includes_map write to {output}') self.logger.info('Preprocessor applied') From d6ae30c2a749221713e170e037ef321c9bef640f Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Tue, 23 Jul 2024 16:53:50 +0300 Subject: [PATCH 11/21] remove uncessary comments --- foliant/preprocessors/includes.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index d44f906..1e7f215 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -902,12 +902,6 @@ def process_includes( body = self._tag_body_pattern.match(include_statement.group('body').strip()) options = self.get_options(include_statement.group('options')) - self.logger.debug(f'Include pair: {markdown_file_path} <- {options} {body}') - - # TODO: - # :param markdown_file_path: - # :returns date: - self.logger.debug( f'Processing include statement; body: {body}, options: {options}, ' + f'current project root path: {current_project_root_path}' From d1673a14b41a59d1f4e148f243ab7ec4069f6f62 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Thu, 25 Jul 2024 14:44:57 +0300 Subject: [PATCH 12/21] update --- foliant/preprocessors/includes.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 1e7f215..b80e684 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -49,7 +49,7 @@ def __init__(self, *args, **kwargs): self.src_dir = self.config.get("src_dir") self.includes_map_enable = self.options['includes_map'] if self.includes_map_enable: - self.includes_map = {} + self.includes_map = [] self.logger = self.logger.getChild('includes') @@ -855,6 +855,12 @@ def _prepare_path_for_includes_map(self, path: Path) -> str: donor_path = _path.as_posix() return donor_path + def _exist_in_includes_map(self, map: list, path: str) -> bool: + for obj in map: + if obj["file"] == path: + return True + return False + def process_includes( self, markdown_file_path: Path, @@ -1035,7 +1041,7 @@ def process_includes( ) if self.includes_map_enable: - donor_md_path = f"{self.src_dir}/{self._prepare_path_for_includes_map(included_file_path)}" + donor_md_path = self._prepare_path_for_includes_map(included_file_path) self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') else: # if body is missing @@ -1196,10 +1202,11 @@ def process_includes( if self.includes_map_enable: if donor_md_path: - if self.includes_map.get(recipient_md_path) == None : - self.includes_map[recipient_md_path] = [] - - self.includes_map[recipient_md_path].append(donor_md_path) + if not self._exist_in_includes_map(self.includes_map, recipient_md_path): + self.includes_map.append({ 'file': recipient_md_path, "includes": [] }) + for i, f in enumerate(self.includes_map): + if f['file'] == recipient_md_path: + self.includes_map[i]['includes'].append(donor_md_path) else: processed_content_part = content_part From 7e18b4185b5165a62d2f13d20490cb94fb143dd3 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Thu, 25 Jul 2024 15:07:49 +0300 Subject: [PATCH 13/21] add test includes map --- foliant/preprocessors/includes.py | 2 +- test/test_includes.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index b80e684..fbf1f76 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -23,7 +23,7 @@ class Preprocessor(BasePreprocessor): 'cache_dir': Path('.includescache'), 'aliases': {}, 'extensions': ['md'], - 'includes_map': True + 'includes_map': False } tags = 'include', diff --git a/test/test_includes.py b/test/test_includes.py index d5638ce..049de10 100644 --- a/test/test_includes.py +++ b/test/test_includes.py @@ -243,3 +243,21 @@ def test_extensions(self): 'index.j2': '# My title\n\nIncluded content', 'sub/sub.md': 'Included content' } + + def test_includes_map(self): + self.ptf.options = {'includes_map': True } + input_map = { + 'index.md': '# My title\n\n\n\n', + 'sub/sub-1.md': 'Included content 1', + 'sub/sub-2.md': 'Included content 2' + } + expected_map = { + 'index.md': '# My title\n\nIncluded content 1\n\nIncluded content 2', + 'static/includes_map.json': "[{\"file\": \"__src__/index.md\", \"includes\": [\"__src__/sub/sub-1.md\", \"__src__/sub/sub-2.md\"]}]", + 'sub/sub-1.md': 'Included content 1', + 'sub/sub-2.md': 'Included content 2' + } + self.ptf.test_preprocessor( + input_mapping=input_map, + expected_mapping=expected_map, + ) From d75579f19e82a6ccd7e0844d014aece80368d8df Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Thu, 25 Jul 2024 17:46:52 +0300 Subject: [PATCH 14/21] update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f246c0f..b90c8ec 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![](https://img.shields.io/pypi/v/foliantcontrib.includes.svg)](https://pypi.org/project/foliantcontrib.includes/) [![](https://img.shields.io/github/v/tag/foliant-docs/foliantcontrib.includes.svg?label=GitHub)](https://github.com/foliant-docs/foliantcontrib.includes) +[![](https://img.shields.io/pypi/v/foliantcontrib.includes.svg)](https://pypi.org/project/foliantcontrib.includes/) [![](https://img.shields.io/github/v/tag/foliant-docs/foliantcontrib.includes.svg?label=GitHub)](https://github.com/foliant-docs/foliantcontrib.includes) [![Tests](https://github.com/foliant-docs/foliantcontrib.includes/actions/workflows/python-test.yml/badge.svg)](https://github.com/foliant-docs/foliantcontrib.includes/actions/workflows/python-test.yml) # Includes for Foliant From 8987e8d0b94b848b88170b6d96ab38054c3adce6 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Thu, 25 Jul 2024 17:52:38 +0300 Subject: [PATCH 15/21] update python-test.yml --- .github/workflows/python-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index f516353..197a2e2 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -1,4 +1,4 @@ -name: Python package +name: Python package tests on: [push] From 0624b849499f6d67de410155b23e2d00afb75280 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Tue, 13 Aug 2024 10:20:04 +0300 Subject: [PATCH 16/21] test --- foliant/preprocessors/includes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index fbf1f76..b80e684 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -23,7 +23,7 @@ class Preprocessor(BasePreprocessor): 'cache_dir': Path('.includescache'), 'aliases': {}, 'extensions': ['md'], - 'includes_map': False + 'includes_map': True } tags = 'include', From 05fef21f4e7a3dbf9954f61fcbe0867d5223c1b0 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Tue, 3 Sep 2024 14:42:52 +0300 Subject: [PATCH 17/21] add: anchors into include map --- foliant/preprocessors/includes.py | 89 ++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index b80e684..861b366 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -22,8 +22,7 @@ class Preprocessor(BasePreprocessor): 'allow_failure': True, 'cache_dir': Path('.includescache'), 'aliases': {}, - 'extensions': ['md'], - 'includes_map': True + 'extensions': ['md'] } tags = 'include', @@ -47,9 +46,13 @@ def __init__(self, *args, **kwargs): self._cache_dir_path = self.project_path / self.options['cache_dir'] self._downloaded_dir_path = self._cache_dir_path / '_downloaded_content' self.src_dir = self.config.get("src_dir") - self.includes_map_enable = self.options['includes_map'] - if self.includes_map_enable: + self.includes_map_enable = False + self.includes_map_anchors = False + if 'includes_map' in self.options: + self.includes_map_enable = True self.includes_map = [] + if 'anchors' in self.options['includes_map']: + self.includes_map_anchors = True self.logger = self.logger.getChild('includes') @@ -169,7 +172,7 @@ def _download_file_from_url(self, url: str) -> Path: for line in dict_new_link: downloaded_content = downloaded_content.replace(line, dict_new_link[line]) - # End of the conversion code block + # End of the conversion code block with open(downloaded_file_path, 'w', encoding='utf8') as downloaded_file: @@ -224,6 +227,8 @@ def _sync_repo( except CalledProcessError as exception: self.logger.warning(str(exception)) + except Exception as exception: + self.logger.warning(str(exception)) else: self.logger.error(str(exception)) @@ -691,7 +696,7 @@ def _get_included_file_path( ) self.logger.debug(f'Finally, included file path: {included_file_path}') - + return included_file_path def _process_include( @@ -730,8 +735,8 @@ def _process_include( f'Included file path: {included_file_path}, from heading: {from_heading}, ' + f'to heading: {to_heading}, sethead: {sethead}, nohead: {nohead}' ) - - + + if included_file_path.exists(): included_file_path = included_file_path else: @@ -764,9 +769,9 @@ def _process_include( old_found_link = regexp_find_link.findall(included_content) - for line in old_found_link: + for line in old_found_link: relative_path = regexp_find_path.findall(line) - + for ex_line in relative_path: exceptions_characters = re.findall(r'https?://[^\s]+|@|:|\.png|\.jpeg|.svg', ex_line) if exceptions_characters: @@ -778,7 +783,7 @@ def _process_include( for line in dict_new_link: included_content = included_content.replace(line, dict_new_link[line]) - # End of the conversion code block + # End of the conversion code block if self.config.get('escape_code', False): if isinstance(self.config['escape_code'], dict): @@ -861,6 +866,27 @@ def _exist_in_includes_map(self, map: list, path: str) -> bool: return True return False + def _find_anchors(self, content: str) -> list: + anchors_list = [] + + anchors = re.findall(r'\([\-\_A-Za-z0-9]+)\<\/anchor\>', content) + for anchor in anchors: + anchors_list.append(anchor) + custom_ids = re.findall(r'\{\#([\-A-Za-z0-9]+)\}', content) + for anchor in custom_ids: + anchors_list.append(anchor) + elements_with_ids = re.findall(r'id\=[\"\']([\-A-Za-z0-9]+)[\"\']', content) + for anchor in elements_with_ids: + anchors_list.append(anchor) + return anchors_list + + def _add_anchors(self, l: list, content: str) -> list: + anchors = self._find_anchors(content) + if len(anchors) > 0: + for anchor in anchors: + l.append(anchor) + return l + def process_includes( self, markdown_file_path: Path, @@ -881,7 +907,10 @@ def process_includes( ''' if self.includes_map_enable: - recipient_md_path = f'{self.src_dir}/{markdown_file_path.relative_to(self.working_dir).as_posix()}' + if markdown_file_path.as_posix().startswith(self.working_dir.as_posix()): + recipient_md_path = f'{self.src_dir}/{markdown_file_path.relative_to(self.working_dir).as_posix()}' + else: + recipient_md_path = f'{self.src_dir}/{markdown_file_path.as_posix()}' markdown_file_path = markdown_file_path.resolve() @@ -902,6 +931,7 @@ def process_includes( if include_statement: if self.includes_map_enable: donor_md_path = None + donor_anchors = [] current_project_root_path = project_root_path @@ -985,11 +1015,14 @@ def process_includes( self.logger.debug(f'Local path of the repo: {repo_path}') included_file_path = repo_path / body.group('path') - + if self.includes_map_enable: donor_md_path = included_file_path.as_posix() self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path} (1)') + if self.includes_map_anchors: + donor_anchors = self._add_anchors(donor_anchors, processed_content_part) + if included_file_path.name.startswith('^'): included_file_path = self._find_file( included_file_path.name[1:], included_file_path.parent @@ -1044,6 +1077,9 @@ def process_includes( donor_md_path = self._prepare_path_for_includes_map(included_file_path) self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') + if self.includes_map_anchors: + donor_anchors = self._add_anchors(donor_anchors, processed_content_part) + else: # if body is missing self.logger.debug('Using the new syntax rules') @@ -1084,6 +1120,9 @@ def process_includes( donor_md_path = include_link + options.get('path') self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') + if self.includes_map_anchors: + donor_anchors = self._add_anchors(donor_anchors, processed_content_part) + elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1109,21 +1148,27 @@ def process_includes( sethead=current_sethead, nohead=options.get('nohead') ) - + if self.includes_map_enable: donor_md_path = options['url'] self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path} (4)') + if self.includes_map_anchors: + donor_anchors = self._add_anchors(donor_anchors, processed_content_part) + elif options.get('src'): self.logger.debug('Local file referenced') included_file_path = self._get_included_file_path(options.get('src'), markdown_file_path) self.logger.debug(f'Resolved path to the included file: {included_file_path}') - + if self.includes_map_enable: donor_md_path = self._prepare_path_for_includes_map(included_file_path) self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (5)') + if self.includes_map_anchors: + donor_anchors = self._add_anchors(donor_anchors, processed_content_part) + if options.get('project_root'): current_project_root_path = ( markdown_file_path.parent / options.get('project_root') @@ -1203,11 +1248,19 @@ def process_includes( if self.includes_map_enable: if donor_md_path: if not self._exist_in_includes_map(self.includes_map, recipient_md_path): - self.includes_map.append({ 'file': recipient_md_path, "includes": [] }) + if not self.includes_map_anchors: + self.includes_map.append({ 'file': recipient_md_path, "includes": []}) + else: + self.includes_map.append({ 'file': recipient_md_path, "includes": [], 'anchors': []}) + for i, f in enumerate(self.includes_map): if f['file'] == recipient_md_path: self.includes_map[i]['includes'].append(donor_md_path) + if self.includes_map_anchors: + for anchor in donor_anchors: + self.includes_map[i]['anchors'].append(anchor) + else: processed_content_part = content_part @@ -1243,7 +1296,7 @@ def _get_source_files_extensions(self) -> list: return source_files_extensions def apply(self): - + self.logger.info('Applying preprocessor') # Cleaning up downloads because the content of remote source may have modified @@ -1265,7 +1318,7 @@ def apply(self): if processed_content: with open(source_file_path, 'w', encoding='utf8') as processed_file: processed_file.write(processed_content) - + # Write includes map if self.includes_map_enable: output = f'{self.working_dir}/static/includes_map.json' From 06896734d81dc56bbd5c1b28216ee88340302b0e Mon Sep 17 00:00:00 2001 From: Timur Osmanov <54434686+TOsmanov@users.noreply.github.com> Date: Mon, 9 Sep 2024 13:57:23 +0300 Subject: [PATCH 18/21] test: enabled by default --- foliant/preprocessors/includes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 861b366..7cb1747 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -46,13 +46,14 @@ def __init__(self, *args, **kwargs): self._cache_dir_path = self.project_path / self.options['cache_dir'] self._downloaded_dir_path = self._cache_dir_path / '_downloaded_content' self.src_dir = self.config.get("src_dir") - self.includes_map_enable = False - self.includes_map_anchors = False + self.includes_map_enable = True # TODO: the default value is False + self.includes_map_anchors = True # TODO: the default value is False if 'includes_map' in self.options: self.includes_map_enable = True - self.includes_map = [] if 'anchors' in self.options['includes_map']: self.includes_map_anchors = True + if self.includes_map_enable: + self.includes_map = [] self.logger = self.logger.getChild('includes') From 0bd8ed7dfcfe34424418a741f97701ba42ddc275 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Wed, 2 Oct 2024 09:45:02 +0300 Subject: [PATCH 19/21] fix: tests and remove anchors --- foliant/preprocessors/includes.py | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 7cb1747..7c24ce6 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -46,12 +46,9 @@ def __init__(self, *args, **kwargs): self._cache_dir_path = self.project_path / self.options['cache_dir'] self._downloaded_dir_path = self._cache_dir_path / '_downloaded_content' self.src_dir = self.config.get("src_dir") - self.includes_map_enable = True # TODO: the default value is False - self.includes_map_anchors = True # TODO: the default value is False + self.includes_map_enable = False if 'includes_map' in self.options: self.includes_map_enable = True - if 'anchors' in self.options['includes_map']: - self.includes_map_anchors = True if self.includes_map_enable: self.includes_map = [] @@ -1021,9 +1018,6 @@ def process_includes( donor_md_path = included_file_path.as_posix() self.logger.debug(f'Set the repo URL of the included file to {recipient_md_path}: {donor_md_path} (1)') - if self.includes_map_anchors: - donor_anchors = self._add_anchors(donor_anchors, processed_content_part) - if included_file_path.name.startswith('^'): included_file_path = self._find_file( included_file_path.name[1:], included_file_path.parent @@ -1078,9 +1072,6 @@ def process_includes( donor_md_path = self._prepare_path_for_includes_map(included_file_path) self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (2)') - if self.includes_map_anchors: - donor_anchors = self._add_anchors(donor_anchors, processed_content_part) - else: # if body is missing self.logger.debug('Using the new syntax rules') @@ -1121,9 +1112,6 @@ def process_includes( donor_md_path = include_link + options.get('path') self.logger.debug(f'Set the link of the included file to {recipient_md_path}: {donor_md_path} (3)') - if self.includes_map_anchors: - donor_anchors = self._add_anchors(donor_anchors, processed_content_part) - elif options.get('url'): self.logger.debug('File to get by URL referenced') @@ -1154,9 +1142,6 @@ def process_includes( donor_md_path = options['url'] self.logger.debug(f'Set the URL of the included file to {recipient_md_path}: {donor_md_path} (4)') - if self.includes_map_anchors: - donor_anchors = self._add_anchors(donor_anchors, processed_content_part) - elif options.get('src'): self.logger.debug('Local file referenced') @@ -1167,9 +1152,6 @@ def process_includes( donor_md_path = self._prepare_path_for_includes_map(included_file_path) self.logger.debug(f'Set the path of the included file to {recipient_md_path}: {donor_md_path} (5)') - if self.includes_map_anchors: - donor_anchors = self._add_anchors(donor_anchors, processed_content_part) - if options.get('project_root'): current_project_root_path = ( markdown_file_path.parent / options.get('project_root') @@ -1249,19 +1231,12 @@ def process_includes( if self.includes_map_enable: if donor_md_path: if not self._exist_in_includes_map(self.includes_map, recipient_md_path): - if not self.includes_map_anchors: - self.includes_map.append({ 'file': recipient_md_path, "includes": []}) - else: - self.includes_map.append({ 'file': recipient_md_path, "includes": [], 'anchors': []}) + self.includes_map.append({ 'file': recipient_md_path, "includes": []}) for i, f in enumerate(self.includes_map): if f['file'] == recipient_md_path: self.includes_map[i]['includes'].append(donor_md_path) - if self.includes_map_anchors: - for anchor in donor_anchors: - self.includes_map[i]['anchors'].append(anchor) - else: processed_content_part = content_part From 1fefcd2e4c8f0fb25eba25f719e059afae514598 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Wed, 2 Oct 2024 09:55:31 +0300 Subject: [PATCH 20/21] update: README --- README.md | 6 ++++++ README_ru.md | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b90c8ec..55688c9 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ preprocessors: - j2 aliases: ... + includes_map: true ``` `cache_dir` @@ -79,6 +80,11 @@ Default `true`. Note that in the second example the default revision (`develop`) will be overridden with the custom one (`master`). +`includes_map` +: Enables generation of the `includes_map.json` file containing information about files inserted using the includes preprocessor. + + From this file, third-party services can receive information about the presence of inclusions in files, for example, to check links using a linter. + ## Usage The preprocessor allows two syntax variants for include statements. diff --git a/README_ru.md b/README_ru.md index ddede50..dd652e2 100644 --- a/README_ru.md +++ b/README_ru.md @@ -1,4 +1,4 @@ -[![](https://img.shields.io/pypi/v/foliantcontrib.includes.svg)](https://pypi.org/project/foliantcontrib.includes/) [![](https://img.shields.io/github/v/tag/foliant-docs/foliantcontrib.includes.svg?label=GitHub)](https://github.com/foliant-docs/foliantcontrib.includes) +[![](https://img.shields.io/pypi/v/foliantcontrib.includes.svg)](https://pypi.org/project/foliantcontrib.includes/) [![](https://img.shields.io/github/v/tag/foliant-docs/foliantcontrib.includes.svg?label=GitHub)](https://github.com/foliant-docs/foliantcontrib.includes) [![Tests](https://github.com/foliant-docs/foliantcontrib.includes/actions/workflows/python-test.yml/badge.svg)](https://github.com/foliant-docs/foliantcontrib.includes/actions/workflows/python-test.yml) # Препроцессор Includes для Foliant @@ -34,6 +34,7 @@ preprocessors: - j2 aliases: ... + includes_map: true ``` `cache_dir` @@ -64,6 +65,10 @@ preprocessors: `aliases` : Сопоставление псевдонимов с URL-адресами репозитория Git. После определения этого параметра псевдоним может использоваться для ссылки на репозиторий вместо его полного URL-адреса. +`includes_map` +: Включает генерацию файла `includes_map.json`, содержащего информацию о файлах, вставленных с помощью препроцессора includes. + Из этого файла сторонние сервисы могут получать информацию о наличии текста вставленного в файл с помощью препроцессора, например, для проверки ссылок с помощью линтера. + >**Внимание!** > > Псевдонимы доступны только в рамках устаревшего синтаксиса инструкций include (см. ниже) From adb34980ffc7c2ad0f43e706ff5557eb0622fa06 Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Wed, 2 Oct 2024 10:08:39 +0300 Subject: [PATCH 21/21] bump version --- changelog.md | 4 ++++ setup.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index dfb0923..f012ea8 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# 1.1.18 + +- Add: option for generation of the includes map containing information about files inserted using the preprocessor. + # 1.1.17 - Fix: fixed a link processing error for files with diagrams diff --git a/setup.py b/setup.py index d518e3e..7a10d0e 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ description=SHORT_DESCRIPTION, long_description=LONG_DESCRIPTION, long_description_content_type='text/markdown', - version='1.1.17', + version='1.1.18', author='Konstantin Molchanov', author_email='moigagoo@live.com', url='https://github.com/foliant-docs/foliantcontrib.includes',