Skip to content

Commit

Permalink
Bugfix: Cares about correct handling of dyn funcs in links
Browse files Browse the repository at this point in the history
  • Loading branch information
danwos committed Apr 15, 2020
1 parent 7ae5332 commit 4692ce3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog

* Improvement: Added options :ref:`need_pre_template` and :ref:`need_post_template` for needs. (`#139 <https://github.com/useblocks/sphinxcontrib-needs/issues/139>`_)
* Bugfix: Setting correct default value for :ref:`needs_statuses` (`#136 <https://github.com/useblocks/sphinxcontrib-needs/issues/136>`_)
* Bugfix: Dynamic functions can be used in links (text and url) now.

0.5.3
-----
Expand Down
64 changes: 37 additions & 27 deletions sphinxcontrib/needs/functions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,44 @@ def find_and_replace_node_content(node, env, need):
:return: None
"""
new_children = []
if not node.children:
if isinstance(node, nodes.Text):
func_match = func_pattern.findall(node)
if not node.children and isinstance(node, nodes.Text) or isinstance(node, nodes.reference):
if isinstance(node, nodes.reference):
new_text = node.attributes['refuri']
else:
new_text = node
for func_string in func_match:
if not is_python3:
func_string = func_string.encode('utf-8')
# sphinx is replacing ' and " with language specific quotation marks (up and down), which makes
# it impossible for the later used AST render engine to detect a python function call in the given
# string. Therefor a replacement is needed for the execution of the found string.
func_string_org = func_string[:]
func_string = func_string.replace('„', '"')
func_string = func_string.replace('“', '"')
func_string = func_string.replace('”', '"')
func_string = func_string.replace('”', '"')

func_string = func_string.replace('‘', '\'')
func_string = func_string.replace('’', '\'')
func_return = execute_func(env, need, func_string)

# This shoudl never happen, but we can not be sure.
if isinstance(func_return, list):
func_return = ", ".join(func_return)

if not is_python3:
new_text = new_text.replace(u'[[{}]]'.format(func_string_org.decode('utf-8')), func_return)
else:
new_text = new_text.replace(u'[[{}]]'.format(func_string_org), func_return)
func_match = func_pattern.findall(new_text)
for func_string in func_match:
if not is_python3:
func_string = func_string.encode('utf-8')
# sphinx is replacing ' and " with language specific quotation marks (up and down), which makes
# it impossible for the later used AST render engine to detect a python function call in the given
# string. Therefor a replacement is needed for the execution of the found string.
func_string_org = func_string[:]
func_string = func_string.replace('„', '"')
func_string = func_string.replace('“', '"')
func_string = func_string.replace('”', '"')
func_string = func_string.replace('”', '"')

func_string = func_string.replace('‘', '\'')
func_string = func_string.replace('’', '\'')
func_return = execute_func(env, need, func_string)

# This should never happen, but we can not be sure.
if isinstance(func_return, list):
func_return = ", ".join(func_return)

if not is_python3:
new_text = new_text.replace(u'[[{}]]'.format(func_string_org.decode('utf-8')), func_return)
else:
new_text = new_text.replace(u'[[{}]]'.format(func_string_org), func_return)
if isinstance(node, nodes.reference):
node.attributes['refuri'] = new_text
# Call normal handling for children of reference node (will contain related Text node with link-text)
for child in node.children:
new_child = find_and_replace_node_content(child, env, need)
new_children.append(new_child)
node.children = new_children
else:
node = nodes.Text(new_text, new_text)
return node
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/doc_test/doc_dynamic_functions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ DYNAMIC FUNCTIONS
.. spec:: TEST_5
:id: TEST_5
:tags: [[copy('id')]]

Test a `link <http://www.[[copy('id')]]>`_
2 changes: 2 additions & 0 deletions tests/test_dynamic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def test_doc_dynamic_functions(app, status, warning):

assert 'Test output of need TEST_3. args:' in html

assert '<a class="reference external" href="http://www.TEST_5">link</a>' in html


@with_app(buildername='html', srcdir='doc_test/doc_df_calc_sum')
def test_doc_df_calc_sum(app, status, warning):
Expand Down

0 comments on commit 4692ce3

Please sign in to comment.