Skip to content

Commit

Permalink
Merge pull request #353 from tocic/feat/more_chars_in_refs
Browse files Browse the repository at this point in the history
Support more chars in refs
  • Loading branch information
knatten authored Sep 16, 2024
2 parents 76c9aa7 + 0eb7fbc commit ca67f87
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
32 changes: 25 additions & 7 deletions quiz/templatetags/quiz_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,39 @@ def format_reference(match):
full_link = "https://timsong-cpp.github.io/cppwp/n4659/" + section_name
if paragraph_number:
full_link = full_link + "#" + paragraph_number
return "<em><a href=\"" + full_link + "\">" + full_reference + "</a></em>"
return f"*[{full_reference}]({full_link})*"


def standard_ref(text):
section_name = u'(\[(?P<section_name>[\w:]+(\.[\w:]+)*)\])'
possible_paragraph = u'(¶(?P<paragraph>\d+(\.\d+)*))*'
regex = re.compile('§(' + section_name + possible_paragraph + ')')
return re.sub(regex, format_reference, text)
# https://regex101.com/r/hf2P3X/4
regex = re.compile(
r'''
§\[ # mandatory section part starts with §[
(?P<section_name> #
[^]]+ # at least one character other than ]
) #
\] # section part must end with ]
(?:¶ # optional paragraph part starts with ¶
(?P<paragraph> #
[^\s*]* # any number of non-whitespace characters except * (to support italicized links)
[\w&\]<>/^|+%=~-] # we support only those paragraphs that end with the following characters:
# any Unicode letter/digit plus _ (use * instead of _ in Markdown) plus
# characters that shouldn't clash with surrounding punctuation/Markdown
) #
)? #
''',
re.VERBOSE,
)
return regex.sub(format_reference, text)


@register.filter(needs_autoescape=True)
def to_html(text, autoescape=None):
return mark_safe(
standard_ref(
markdown.markdown(text, extensions=['nl2br', 'pymdownx.superfences'])))
markdown.markdown(
standard_ref(text), extensions=['nl2br', 'pymdownx.superfences']
)
)


@register.filter()
Expand Down
17 changes: 10 additions & 7 deletions quiz/tests/unit/test_quiz_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ def test_given_no_reference(self):

def test_given_just_section(self):
self.assertEqual(
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/foo">§[foo]</a></em>',
'*[§[foo]](https://timsong-cpp.github.io/cppwp/n4659/foo)*',
standard_ref('§[foo]'))
self.assertEqual(
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/foo.bar">§[foo.bar]</a></em>',
'*[§[foo.bar]](https://timsong-cpp.github.io/cppwp/n4659/foo.bar)*',
standard_ref('§[foo.bar]'))
self.assertEqual(
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/foo::bar">§[foo::bar]</a></em>',
'*[§[foo::bar]](https://timsong-cpp.github.io/cppwp/n4659/foo::bar)*',
standard_ref('§[foo::bar]'))

def test_given_section_and_paragraph(self):
self.assertEqual(
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/foo.bar#1.2">§[foo.bar]¶1.2</a></em>',
'*[§[foo.bar]¶1.2](https://timsong-cpp.github.io/cppwp/n4659/foo.bar#1.2)*',
standard_ref('§[foo.bar]¶1.2'))
self.assertEqual(
'*[§[foo]¶note-1](https://timsong-cpp.github.io/cppwp/n4659/foo#note-1)*',
standard_ref('§[foo]¶note-1'))

def test_doesnt_include_too_much(self):
self.assertEqual(
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/foo.bar#1">§[foo.bar]¶1</a></em>.', standard_ref('§[foo.bar]¶1.'))
'*[§[foo.bar]¶1](https://timsong-cpp.github.io/cppwp/n4659/foo.bar#1)*.', standard_ref('§[foo.bar]¶1.'))

def test_doesnt_try_to_format_numbered_references(self):
self.assertEqual('§1.2.3', standard_ref('§1.2.3'))
self.assertEqual('§1.2.3¶1', standard_ref('§1.2.3¶1'))

def test_given_multiple_paragraphs(self):
self.assertEqual(
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/foo">§[foo]</a></em>'
'<em><a href="https://timsong-cpp.github.io/cppwp/n4659/bar">§[bar]</a></em>',
'*[§[foo]](https://timsong-cpp.github.io/cppwp/n4659/foo)*'
'*[§[bar]](https://timsong-cpp.github.io/cppwp/n4659/bar)*',
standard_ref('§[foo]§[bar]'))

0 comments on commit ca67f87

Please sign in to comment.