diff --git a/tests/test_subreddit.py b/tests/test_subreddit.py index 0bbf8fe..cc2226c 100644 --- a/tests/test_subreddit.py +++ b/tests/test_subreddit.py @@ -679,6 +679,24 @@ def test_subreddit_page__submission_attr(config, terminal, subreddit_page): terminal.attr.assert_called_with('SubmissionTitleSeen') +def test_subreddit_page__url_str(config, terminal, subreddit_page): + data = { + 'url_type': 'selfpost', + 'url': 'self.AskReddit', + 'url_full': 'https://www.reddit.com/r/AskReddit/comments/99eh6b/without_saying_what_the_category_is_what_are_your/' + } + + assert subreddit_page._url_str(data) == 'self.AskReddit' + + data['url_type'] = 'x-post subreddit' + + assert subreddit_page._url_str(data) == 'self.AskReddit' + + data['url_type'] = 'external' + + assert subreddit_page._url_str(data) == 'https://www.reddit.com/r/AskReddit/comments/99eh6b/without_saying_what_the_category_is_what_are_your/' + + def test_subreddit_page__url_attr(config, terminal, subreddit_page): data = {} data['url_full'] = 'www.test.com' diff --git a/tuir/subreddit_page.py b/tuir/subreddit_page.py index 2357208..584c967 100644 --- a/tuir/subreddit_page.py +++ b/tuir/subreddit_page.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import re import time +from urllib.parse import urlparse from . import docs from .content import SubredditContent @@ -251,6 +252,15 @@ class SubredditPage(Page): else: return self.term.attr('SubmissionTitle') + def _url_str(self, data): + # Both of these url_types indicate a URL of a subreddit/comment, and + # self.subreddit should be used as the display url + if data['url_type'] == 'selfpost' or \ + data['url_type'].startswith('x-post'): + return data['url'] + else: + return urlparse(data['url']).hostname + def _url_attr(self, data): if data['url_full'] in self.config.history: return self.term.attr('LinkSeen') @@ -331,7 +341,8 @@ class SubredditPage(Page): lambda data: self.term.attr('SubmissionSubreddit'), first)) elif item == "%u": - raise NotImplementedError("'%u' subreddit_format specifier not yet supported") + form.append((lambda data: self._url_str(data), + lambda data: self._url_attr(data), first)) elif item == "%U": form.append((lambda data: data['url'], lambda data: self._url_attr(data), first)) diff --git a/tuir/templates/tuir.cfg b/tuir/templates/tuir.cfg index 3dafeb6..902e8fa 100644 --- a/tuir/templates/tuir.cfg +++ b/tuir/templates/tuir.cfg @@ -84,7 +84,7 @@ hide_username = False ; %E absolute edit time (NotImplemented) ; %a author ; %S subreddit -; %u short url - 'self.reddit' or 'gfycat.com' (NotImplemented) +; %u short url - 'self.reddit' or 'gfycat.com' for example ; %U full url ; %A saved ; %h hidden