Skip to content

Commit

Permalink
WIP Started on ticket #4
Browse files Browse the repository at this point in the history
  • Loading branch information
danuker committed Jan 25, 2021
1 parent f9c8737 commit 4d3b6a6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
27 changes: 26 additions & 1 deletion test/test_wiki_trac_rst_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from wiki_trac_rst_convert import convert_content


class TracRstToVanillaRst(unittest.TestCase):
class TracToVanillaRst(unittest.TestCase):
"""
Test conversion of content from Trac-flavored reStructuredText to
vanilla reStructuredText, that is supported by GitHub
Expand Down Expand Up @@ -100,6 +100,31 @@ def test_several_trac_wiki_rst_links_with_content(self):
' List of free software used by Chevah Project.'
)

def test_tracwiki_general_link(self):
"""
Process general links from TracWiki format to GitHub-compatible
RST links
"""
self.assertConvertedContent(
'`Buildbot <https://chevah.com/buildbot/>`__\n',
'[https://chevah.com/buildbot/ Buildbot]'
)

def test_tracwiki_wiki_link(self):
"""
Process wiki links from TracWiki format to GitHub-compatible
RST simple links
"""
self.assertConvertedContent(
'`Project management and administration <Administrative>`__\n'
'`Administrative <Administrative>`__ - Project management [and administration]\n'
'`Administrative/AllHandMeeting/Past <Administrative AllHandMeeting Past>`__',

'[wiki:Administrative Project management and administration]\n'
'[wiki:Administrative Administrative] - Project management [and administration]\n'
'[wiki:"Administrative/AllHandMeeting/Past"]'
)


if __name__ == '__main__':
unittest.main()
23 changes: 23 additions & 0 deletions wiki_trac_rst_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def convert_content(text: str):
text = text.replace(seq, '')
text = text.strip() + '\n'
text = _trac_rst_wiki_to_github_links(text)
text = _tracwiki_to_github_links(text)

return text

Expand Down Expand Up @@ -73,6 +74,28 @@ def _trac_rst_wiki_to_github_links(text: str):
return text


def _tracwiki_to_github_links(text: str):
"""
Takes TracWiki content and converts the links and link text in it
to inline GitHub links.
"""

url = '[a-z]+://[^ ]+'
link_text = '[^]]+'
link_re = re.compile(f'\[({url}) ({link_text})]')

matches = re.findall(link_re, text)
for url, link_text in matches:
text = re.sub(
link_re,
rf'`{link_text} <{url}>`__',
text,
1
)

return text


def _wiki_url(title):
"""
GitHub Wiki collapses directory structure.
Expand Down

0 comments on commit 4d3b6a6

Please sign in to comment.