Skip to content

Commit 8e374d7

Browse files
authored
Merge pull request #5 from foliant-docs/1.0.7-fix-regex-for-backtick-spaces
fix (1.0.7): incorrect behaviour in case of list items with spaces inside backticks
2 parents f18d234 + 09f8bdb commit 8e374d7

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ APIReferences uses regular expressions to capture *references* to API methods in
889889
The default reg-ex is:
890890

891891
```re
892-
`(\s*(?P<prefix>[\w-]+):\s*)?(?P<verb>OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|LINK|UNLINK)\s+(?P<command>[^`]+)\s*`
892+
`\s*((?P<prefix>[\w-]+):\s*)?(?P<verb>OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH|LINK|UNLINK)\s+(?P<command>[^`]+)\s*`
893893
```
894894

895895
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
907907
preprocessors:
908908
- apireferences:
909909
reference:
910-
- regex: '(\s*(?P<prefix>[\w-]+):\s*)(?P<verb>POST|GET|PUT|UPDATE|DELETE)\s+(?P<command>[^`]+)`\s*'
910+
- regex: '\s*((?P<prefix>[\w-]+):\s*)(?P<verb>POST|GET|PUT|UPDATE|DELETE)\s+(?P<command>[^`]+)`\s*'
911911
```
912912
913913
> This example is for illustrative purposes only. You can achieve the same goal by just switching on the `only_with_prefixes` option.

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.7
2+
3+
- Fix: small fix for proper work when input reference is a list item and have initial and last spaces inside
4+
15
# 1.0.6
26

37
- Fix: now the preprocessor works correctly with initial and last spaces in input references like ``` ` Client-API: GET user/info ` ```

foliant/preprocessors/apireferences/apireferences.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from foliant.utils import output
1818

1919

20-
DEFAULT_REF_REGEX = r'`(\s*(?P<prefix>[\w-]+):\s*)?' +\
20+
DEFAULT_REF_REGEX = r'`\s*((?P<prefix>[\w-]+):\s*)?' +\
2121
rf'(?P<verb>{"|".join(HTTP_VERBS)})\s+' +\
2222
r'(?P<command>[^`]+)\s*`'
2323
DEFAULT_IGNORING_PREFIX = 'Ignore'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
description=SHORT_DESCRIPTION,
1717
long_description=LONG_DESCRIPTION,
1818
long_description_content_type='text/markdown',
19-
version='1.0.6',
19+
version='1.0.7',
2020
author='Daniil Minukhin',
2121
author_email='[email protected]',
2222
url='https://github.com/foliant-docs/foliantcontrib.apireferences',

tests/test_apireferences.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55

66
from foliant.preprocessors.apireferences.classes import HTTP_VERBS
7+
from foliant.preprocessors.apireferences.apireferences import DEFAULT_REF_REGEX
78
from foliant_test.preprocessor import PreprocessorTestFramework
89
from foliant_test.preprocessor import unpack_dir
910
from foliant_test.preprocessor import unpack_file_dict
@@ -624,3 +625,61 @@ def test_warning_level(self):
624625
)
625626

626627
self.assertEqual(0, count_output_warnings(self.ptf.capturedOutput))
628+
629+
def test_reference_with_backtick_spaces(self):
630+
pattern = DEFAULT_REF_REGEX
631+
self.ptf.options = {
632+
'reference': [
633+
{
634+
'regex': pattern
635+
}
636+
],
637+
'API': {
638+
'H2H3-Api': {
639+
'url': 'http://example.com/',
640+
'mode': 'find_by_anchor',
641+
'anchor_template': 'user content {verb} {command}',
642+
'endpoint_prefix': '/api/v2'
643+
}
644+
}
645+
}
646+
647+
self.run_with_mock_url(
648+
'data/simple_h2h3.html',
649+
input_mapping={
650+
'input.md': '` GET /user/login `'
651+
},
652+
expected_mapping={
653+
'input.md': '[GET /user/login](http://example.com/#user-content-get-userlogin)'
654+
}
655+
)
656+
self.assertEqual(0, count_output_warnings(self.ptf.capturedOutput))
657+
658+
def test_reference_with_backtick_spaces_as_a_list_item(self):
659+
pattern = DEFAULT_REF_REGEX
660+
self.ptf.options = {
661+
'reference': [
662+
{
663+
'regex': pattern
664+
}
665+
],
666+
'API': {
667+
'H2H3-Api': {
668+
'url': 'http://example.com/',
669+
'mode': 'find_by_anchor',
670+
'anchor_template': 'user content {verb} {command}',
671+
'endpoint_prefix': '/api/v2'
672+
}
673+
}
674+
}
675+
676+
self.run_with_mock_url(
677+
'data/simple_h2h3.html',
678+
input_mapping={
679+
'input.md': '- ` GET /user/login `'
680+
},
681+
expected_mapping={
682+
'input.md': '- [GET /user/login](http://example.com/#user-content-get-userlogin)'
683+
}
684+
)
685+
self.assertEqual(0, count_output_warnings(self.ptf.capturedOutput))

tests/test_reference.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ def test_init_from_match(self):
2626
self.assertEqual(ref.verb, 'GET')
2727
self.assertEqual(ref.command, '/user/status')
2828

29-
def test_source_with_backtick_spaces(self):
30-
source = '` MyAPI: GET /user/status `'
31-
pattern = re.compile(DEFAULT_REF_REGEX)
32-
match = pattern.search(source)
33-
ref = Reference()
34-
ref.init_from_match(match)
35-
self.assertEqual(ref.source, source)
36-
3729
def test_command_and_ep_slashes(self):
3830
ref = Reference(
3931
source='`MyAPI: GET /user/status`',

0 commit comments

Comments
 (0)