Skip to content

Commit

Permalink
Merge pull request #5 from foliant-docs/1.0.7-fix-regex-for-backtick-…
Browse files Browse the repository at this point in the history
…spaces

fix (1.0.7): incorrect behaviour in case of list items with spaces inside backticks
  • Loading branch information
Rainary authored Oct 11, 2022
2 parents f18d234 + 09f8bdb commit 8e374d7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ APIReferences uses regular expressions to capture *references* to API methods in
The default reg-ex is:

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

This expression accepts references like these:
Expand All @@ -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<prefix>[\w-]+):\s*)(?P<verb>POST|GET|PUT|UPDATE|DELETE)\s+(?P<command>[^`]+)`\s*'
- regex: '\s*((?P<prefix>[\w-]+):\s*)(?P<verb>POST|GET|PUT|UPDATE|DELETE)\s+(?P<command>[^`]+)`\s*'
```
> This example is for illustrative purposes only. You can achieve the same goal by just switching on the `only_with_prefixes` option.
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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 ` ```
Expand Down
2 changes: 1 addition & 1 deletion foliant/preprocessors/apireferences/apireferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from foliant.utils import output


DEFAULT_REF_REGEX = r'`(\s*(?P<prefix>[\w-]+):\s*)?' +\
DEFAULT_REF_REGEX = r'`\s*((?P<prefix>[\w-]+):\s*)?' +\
rf'(?P<verb>{"|".join(HTTP_VERBS)})\s+' +\
r'(?P<command>[^`]+)\s*`'
DEFAULT_IGNORING_PREFIX = 'Ignore'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
url='https://github.com/foliant-docs/foliantcontrib.apireferences',
Expand Down
59 changes: 59 additions & 0 deletions tests/test_apireferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
8 changes: 0 additions & 8 deletions tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`',
Expand Down

0 comments on commit 8e374d7

Please sign in to comment.