From 708b436ff17a5818ded1b2268ec940333bd3048e Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sun, 5 Aug 2018 21:28:05 -0400 Subject: [PATCH] Changed the help text format when editing comments & submissions --- rtv/docs.py | 61 +++++++++++++++++++++++------------------- rtv/page.py | 8 +++--- rtv/submission_page.py | 2 +- rtv/terminal.py | 28 ++++++++++++++----- tests/test_terminal.py | 37 ++++++++++++++++++++++--- 5 files changed, 93 insertions(+), 43 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index 886a552..4c776fb 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -114,37 +114,42 @@ FOOTER_SUBSCRIPTION = """ [?]Help [q]Quit [h]Return [l]Select """ -COMMENT_FILE = """ -# Please enter a comment. Lines starting with '#' will be ignored, -# and an empty message aborts the comment. -# -# Replying to {author}'s {type} -{content} -""" +TOKEN = "INSTRUCTIONS" -COMMENT_EDIT_FILE = """{content} -# Please enter a comment. Lines starting with '#' will be ignored, -# and an empty message aborts the comment. -# -# Editing your comment -""" +COMMENT_FILE = """ +""".format(token=TOKEN) -SUBMISSION_EDIT_FILE = """{content} -# Please enter your submission. Lines starting with '#' will be ignored, -# and an empty message aborts the submission. -# -# Editing {name} -""" +COMMENT_EDIT_FILE = """ + +{{content}} +""".format(token=TOKEN) + +SUBMISSION_FILE = """ +""".format(token=TOKEN) + +SUBMISSION_EDIT_FILE = """ + +{{content}} +""".format(token=TOKEN) OAUTH_ACCESS_DENIED = """\

Access Denied


diff --git a/rtv/page.py b/rtv/page.py index 2958b79..21d1da1 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -264,19 +264,19 @@ class Page(object): return if data['type'] == 'Submission': - subreddit = self.reddit.get_subreddit(self.content.name) content = data['text'] info = docs.SUBMISSION_EDIT_FILE.format( - content=content, name=subreddit) + content=content, id=data['object'].id) elif data['type'] == 'Comment': content = data['body'] - info = docs.COMMENT_EDIT_FILE.format(content=content) + info = docs.COMMENT_EDIT_FILE.format( + content=content, id=data['object'].id) else: self.term.flash() return with self.term.open_editor(info) as text: - if text == content: + if not text or text == content: self.term.show_notification('Canceled') return diff --git a/rtv/submission_page.py b/rtv/submission_page.py index 72881ab..5cbc5ad 100644 --- a/rtv/submission_page.py +++ b/rtv/submission_page.py @@ -192,7 +192,7 @@ class SubmissionPage(Page): # Construct the text that will be displayed in the editor file. # The post body will be commented out and added for reference - lines = ['# |' + line for line in body.split('\n')] + lines = [' |' + line for line in body.split('\n')] content = '\n'.join(lines) comment_info = docs.COMMENT_FILE.format( author=data['author'], diff --git a/rtv/terminal.py b/rtv/terminal.py index d031bcf..94cd7a7 100644 --- a/rtv/terminal.py +++ b/rtv/terminal.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import os +import re import sys import time import shlex @@ -21,6 +22,7 @@ import six from kitchen.text.display import textual_width_chop from . import exceptions, mime_parsers, content +from .docs import TOKEN from .theme import Theme, ThemeList from .objects import LoadScreen @@ -594,10 +596,11 @@ class Terminal(object): """ Open a file for editing using the system's default editor. - After the file has been altered, the text will be read back and lines - starting with '#' will be stripped. If an error occurs inside of the - context manager, the file will be preserved. Otherwise, the file will - be deleted when the context manager closes. + After the file has been altered, the text will be read back and the + HTML comment tag will be stripped. If an error + occurs inside of the context manager, the file will be preserved so + users can recover their data. Otherwise, the file will be deleted when + the context manager closes. Params: data (str): If provided, text will be written to the file before @@ -634,8 +637,8 @@ class Terminal(object): self.show_notification('Could not open file with %s' % editor) with codecs.open(filepath, 'r', 'utf-8') as fp: - text = ''.join(line for line in fp if not line.startswith('#')) - text = text.rstrip() + text = fp.read() + text = self.strip_instructions(text) try: yield text @@ -829,6 +832,19 @@ class Terminal(object): out = '\n'.join(stack) return out + @staticmethod + def strip_instructions(text): + """ + Remove instructional HTML comment tags inserted by RTV. + + We used to use # to annotate comments, but it conflicted with the + header tag for markdown, which some people use to format their posts. + """ + # Pattern can span multiple lines, allows dot to match newline chars + flags = re.MULTILINE | re.DOTALL + pattern = ''.format(token=TOKEN) + return re.sub(pattern, '', text, flags=flags).strip() + def clear_screen(self): """ In the beginning this always called touchwin(). However, a bug diff --git a/tests/test_terminal.py b/tests/test_terminal.py index 831de36..c76b0f0 100644 --- a/tests/test_terminal.py +++ b/tests/test_terminal.py @@ -10,7 +10,8 @@ import six import pytest from rtv.theme import Theme -from rtv.docs import HELP, COMMENT_EDIT_FILE +from rtv.docs import (HELP, COMMENT_FILE, COMMENT_EDIT_FILE, TOKEN, + SUBMISSION_FILE, SUBMISSION_EDIT_FILE) from rtv.exceptions import TemporaryFileError, BrowserError try: @@ -289,21 +290,21 @@ def test_open_editor(terminal, use_ascii): terminal.config['ascii'] = use_ascii - comment = COMMENT_EDIT_FILE.format(content='#| This is a comment! ❤') + comment = COMMENT_EDIT_FILE.format(id=1, content='Comment 1 ❤') data = {'filename': None} def side_effect(args): data['filename'] = args[1] with codecs.open(data['filename'], 'r+', 'utf-8') as fp: assert fp.read() == comment - fp.write('This is an amended comment! ❤') + fp.write('Comment 2 ❤') return mock.Mock() with mock.patch('subprocess.Popen', autospec=True) as Popen: Popen.side_effect = side_effect with terminal.open_editor(comment) as reply_text: - assert reply_text == 'This is an amended comment! ❤' + assert reply_text == 'Comment 1 ❤\nComment 2 ❤' assert os.path.isfile(data['filename']) assert curses.endwin.called assert curses.doupdate.called @@ -670,3 +671,31 @@ def test_set_theme_no_colors(terminal, stdscr): terminal.set_theme(Theme(use_color=True)) assert not terminal.theme.use_color + + +def test_strip_instructions(terminal): + + # These templates only contain instructions, so they should be empty + assert terminal.strip_instructions(SUBMISSION_FILE) == '' + assert terminal.strip_instructions(COMMENT_FILE) == '' + + # These templates should strip everything but the {content} tag, + # which will be replaced with the submission/content to be edited + assert terminal.strip_instructions(SUBMISSION_EDIT_FILE) == '{content}' + assert terminal.strip_instructions(COMMENT_EDIT_FILE) == '{content}' + + # Comments without the INSTRUCTIONS marker shouldn't be stripped + text = '' + assert terminal.strip_instructions(text) == text + + # Hashes shouldn't be stripped anymore + text = '# This is no longer interpreted as a comment!' + assert terminal.strip_instructions(text) == text + + # Nested HTML comment tags shouldn't be shown + text = ' {0}-->'.format(TOKEN) + assert terminal.strip_instructions(text) == '' + + # Another edge case + text = ''.format(TOKEN) + assert terminal.strip_instructions(text) == ''