diff --git a/README.md b/README.md index a6f8553..89301a1 100644 --- a/README.md +++ b/README.md @@ -889,7 +889,7 @@ APIReferences uses regular expressions to capture *references* to API methods in The default reg-ex is: ```re -`(\s*(?P[\w-]+):\s*)?(?POPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|LINK|UNLINK)\s+(?P[^`]+)\s*` +`\s*((?P[\w-]+):\s*)?(?POPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|LINK|UNLINK)\s+(?P[^`]+)\s*` ``` This expression accepts references like these: @@ -907,7 +907,7 @@ For example, if you want to capture ONLY references with prefixes, you may use t preprocessors: - apireferences: reference: - - regex: '(\s*(?P[\w-]+):\s*)(?PPOST|GET|PUT|UPDATE|DELETE)\s+(?P[^`]+)`\s*' + - regex: '\s*((?P[\w-]+):\s*)(?PPOST|GET|PUT|UPDATE|DELETE)\s+(?P[^`]+)`\s*' ``` > This example is for illustrative purposes only. You can achieve the same goal by just switching on the `only_with_prefixes` option. diff --git a/changelog.md b/changelog.md index 8e81c53..6e45848 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# 1.0.7 + +- Fix: small fix for proper work when input reference is a list item and have initial and last spaces inside + # 1.0.6 - Fix: now the preprocessor works correctly with initial and last spaces in input references like ``` ` Client-API: GET user/info ` ``` diff --git a/foliant/preprocessors/apireferences/apireferences.py b/foliant/preprocessors/apireferences/apireferences.py index eb029d8..6af3b21 100644 --- a/foliant/preprocessors/apireferences/apireferences.py +++ b/foliant/preprocessors/apireferences/apireferences.py @@ -17,7 +17,7 @@ from foliant.utils import output -DEFAULT_REF_REGEX = r'`(\s*(?P[\w-]+):\s*)?' +\ +DEFAULT_REF_REGEX = r'`\s*((?P[\w-]+):\s*)?' +\ rf'(?P{"|".join(HTTP_VERBS)})\s+' +\ r'(?P[^`]+)\s*`' DEFAULT_IGNORING_PREFIX = 'Ignore' diff --git a/setup.py b/setup.py index 7146a72..6aeb74e 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.0.6', + version='1.0.7', author='Daniil Minukhin', author_email='ddddsa@gmail.com', url='https://github.com/foliant-docs/foliantcontrib.apireferences', diff --git a/tests/test_apireferences.py b/tests/test_apireferences.py index 7014ea1..de12f68 100644 --- a/tests/test_apireferences.py +++ b/tests/test_apireferences.py @@ -4,6 +4,7 @@ import sys from foliant.preprocessors.apireferences.classes import HTTP_VERBS +from foliant.preprocessors.apireferences.apireferences import DEFAULT_REF_REGEX from foliant_test.preprocessor import PreprocessorTestFramework from foliant_test.preprocessor import unpack_dir from foliant_test.preprocessor import unpack_file_dict @@ -624,3 +625,61 @@ def test_warning_level(self): ) self.assertEqual(0, count_output_warnings(self.ptf.capturedOutput)) + + def test_reference_with_backtick_spaces(self): + pattern = DEFAULT_REF_REGEX + self.ptf.options = { + 'reference': [ + { + 'regex': pattern + } + ], + 'API': { + 'H2H3-Api': { + 'url': 'http://example.com/', + 'mode': 'find_by_anchor', + 'anchor_template': 'user content {verb} {command}', + 'endpoint_prefix': '/api/v2' + } + } + } + + self.run_with_mock_url( + 'data/simple_h2h3.html', + input_mapping={ + 'input.md': '` GET /user/login `' + }, + expected_mapping={ + 'input.md': '[GET /user/login](http://example.com/#user-content-get-userlogin)' + } + ) + self.assertEqual(0, count_output_warnings(self.ptf.capturedOutput)) + + def test_reference_with_backtick_spaces_as_a_list_item(self): + pattern = DEFAULT_REF_REGEX + self.ptf.options = { + 'reference': [ + { + 'regex': pattern + } + ], + 'API': { + 'H2H3-Api': { + 'url': 'http://example.com/', + 'mode': 'find_by_anchor', + 'anchor_template': 'user content {verb} {command}', + 'endpoint_prefix': '/api/v2' + } + } + } + + self.run_with_mock_url( + 'data/simple_h2h3.html', + input_mapping={ + 'input.md': '- ` GET /user/login `' + }, + expected_mapping={ + 'input.md': '- [GET /user/login](http://example.com/#user-content-get-userlogin)' + } + ) + self.assertEqual(0, count_output_warnings(self.ptf.capturedOutput)) \ No newline at end of file diff --git a/tests/test_reference.py b/tests/test_reference.py index b599cf6..44862ca 100644 --- a/tests/test_reference.py +++ b/tests/test_reference.py @@ -26,14 +26,6 @@ def test_init_from_match(self): self.assertEqual(ref.verb, 'GET') self.assertEqual(ref.command, '/user/status') - def test_source_with_backtick_spaces(self): - source = '` MyAPI: GET /user/status `' - pattern = re.compile(DEFAULT_REF_REGEX) - match = pattern.search(source) - ref = Reference() - ref.init_from_match(match) - self.assertEqual(ref.source, source) - def test_command_and_ep_slashes(self): ref = Reference( source='`MyAPI: GET /user/status`',