Skip to content

Commit

Permalink
add: strick check options
Browse files Browse the repository at this point in the history
  • Loading branch information
TOsmanov committed Oct 14, 2024
1 parent 12d440e commit 37f656a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ preprocessors:
- checksources
```
You can add a list of unmentioned files that wouldn't throw warnings by `not_in_chapters` option.
To perform a strict check, use the `strict_check` option:
### Options
- `not_in_chapters` – a list of files not mentioned in the the chapters.
No warnings will be displayed for the specified files.
This option is useful if you don't need to add some files to the table of contents.
- `strict_check` – if a critical error is detected, the build will be failed after applying the preprocessor.
Several checks are supported:
- `not_exist` – checking the existence of the file.
Checks if the file specified in chapters exists;
- `duplicate` – checking for duplicate in the chapters.

To disable strict check, use `strict_check: false`. And in order to enable all available checks, use `strict_check: true`.

**Example of options:**
```yaml
preprocessors:
- checksources:
not_in_chapters:
- tags.md
strict_check: true
strict_check:
- not_exist
```

The `not_in_chapters` option is useful if you don't need to add some files to the table of contents.

If the `strict_check` option is enabled, then if a critical error is detected, the build will be aborted after applying the preprocessor.



14 changes: 11 additions & 3 deletions foliant/preprocessors/checksources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
class Preprocessor(BasePreprocessorExt):
defaults = {
'not_in_chapters': [],
'strict_check': True,
'strict_check': [
'not_exist',
'duplicate'
],
}

def __init__(self, *args, **kwargs):
Expand All @@ -23,6 +26,11 @@ def __init__(self, *args, **kwargs):
self.logger.debug(f'Preprocessor inited: {self.__dict__}')
self.src_dir = self.project_path / self.config['src_dir']
self.critical_error = []
if isinstance(self.options['strict_check'], bool):
if self.options['strict_check']:
self.options['strict_check'] = self.defaults['strict_check']
else:
self.options['strict_check'] = []
self.files_list = []

def apply(self):
Expand All @@ -48,15 +56,15 @@ def _recursive_process_chapters(chapters_subset):
else:
self.logger.debug('Not exist, throw warning')
msg = f'{os.path.relpath(chapter_file_path)} does not exist'
if self.options['strict_check']:
if 'not_exist' in self.options['strict_check']:
self.logger.error(msg)
self.critical_error.append(msg)
output(f'ERROR: {msg}')
else:
self._warning(msg)
if chapters_subset in self.files_list:
msg = f'{os.path.relpath(chapter_file_path)} duplicated in chapters'
if self.options['strict_check']:
if 'duplicate' in self.options['strict_check']:
self.logger.error(msg)
self.critical_error.append(msg)
output(f'ERROR: {msg}')
Expand Down

0 comments on commit 37f656a

Please sign in to comment.