Changed the help text format when editing comments & submissions

This commit is contained in:
Michael Lazar
2018-08-05 21:28:05 -04:00
parent 2eef270438
commit 708b436ff1
5 changed files with 93 additions and 43 deletions

View File

@@ -114,37 +114,42 @@ FOOTER_SUBSCRIPTION = """
[?]Help [q]Quit [h]Return [l]Select [?]Help [q]Quit [h]Return [l]Select
""" """
COMMENT_FILE = """ TOKEN = "INSTRUCTIONS"
# Please enter a comment. Lines starting with '#' will be ignored,
# and an empty message aborts the comment.
#
# Replying to {author}'s {type}
{content}
"""
COMMENT_EDIT_FILE = """{content} COMMENT_FILE = """<!--{token}
# Please enter a comment. Lines starting with '#' will be ignored, Replying to {{author}}'s {{type}}:
# and an empty message aborts the comment. {{content}}
#
# Editing your comment
"""
SUBMISSION_FILE = """ Enter your reply below this instruction block,
# Please enter your submission. Lines starting with '#' will be ignored, an empty message will abort the comment.
# and an empty message aborts the submission. {token}-->
# """.format(token=TOKEN)
# The first line will be interpreted as the title
# The following lines will be interpreted as the content
#
# Posting to {name}
"""
SUBMISSION_EDIT_FILE = """{content} COMMENT_EDIT_FILE = """<!--{token}
# Please enter your submission. Lines starting with '#' will be ignored, Editing comment #{{id}}.
# and an empty message aborts the submission. The comment is shown below, update it and save the file.
# {token}-->
# Editing {name}
""" {{content}}
""".format(token=TOKEN)
SUBMISSION_FILE = """<!--{token}
Submitting a selfpost to {{name}}.
Enter your submission below this instruction block:
- The first line will be interpreted as the title
- The following lines will be interpreted as the body
- An empty message will abort the submission
{token}-->
""".format(token=TOKEN)
SUBMISSION_EDIT_FILE = """<!--{token}
Editing submission #{{id}}.
The submission is shown below, update it and save the file.
{token}-->
{{content}}
""".format(token=TOKEN)
OAUTH_ACCESS_DENIED = """\ OAUTH_ACCESS_DENIED = """\
<h1 style="color: red">Access Denied</h1><hr> <h1 style="color: red">Access Denied</h1><hr>

View File

@@ -264,19 +264,19 @@ class Page(object):
return return
if data['type'] == 'Submission': if data['type'] == 'Submission':
subreddit = self.reddit.get_subreddit(self.content.name)
content = data['text'] content = data['text']
info = docs.SUBMISSION_EDIT_FILE.format( info = docs.SUBMISSION_EDIT_FILE.format(
content=content, name=subreddit) content=content, id=data['object'].id)
elif data['type'] == 'Comment': elif data['type'] == 'Comment':
content = data['body'] content = data['body']
info = docs.COMMENT_EDIT_FILE.format(content=content) info = docs.COMMENT_EDIT_FILE.format(
content=content, id=data['object'].id)
else: else:
self.term.flash() self.term.flash()
return return
with self.term.open_editor(info) as text: with self.term.open_editor(info) as text:
if text == content: if not text or text == content:
self.term.show_notification('Canceled') self.term.show_notification('Canceled')
return return

View File

@@ -192,7 +192,7 @@ class SubmissionPage(Page):
# Construct the text that will be displayed in the editor file. # Construct the text that will be displayed in the editor file.
# The post body will be commented out and added for reference # 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) content = '\n'.join(lines)
comment_info = docs.COMMENT_FILE.format( comment_info = docs.COMMENT_FILE.format(
author=data['author'], author=data['author'],

View File

@@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os import os
import re
import sys import sys
import time import time
import shlex import shlex
@@ -21,6 +22,7 @@ import six
from kitchen.text.display import textual_width_chop from kitchen.text.display import textual_width_chop
from . import exceptions, mime_parsers, content from . import exceptions, mime_parsers, content
from .docs import TOKEN
from .theme import Theme, ThemeList from .theme import Theme, ThemeList
from .objects import LoadScreen from .objects import LoadScreen
@@ -594,10 +596,11 @@ class Terminal(object):
""" """
Open a file for editing using the system's default editor. 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 After the file has been altered, the text will be read back and the
starting with '#' will be stripped. If an error occurs inside of the HTML comment tag <!--INSRUCTIONS --> will be stripped. If an error
context manager, the file will be preserved. Otherwise, the file will occurs inside of the context manager, the file will be preserved so
be deleted when the context manager closes. users can recover their data. Otherwise, the file will be deleted when
the context manager closes.
Params: Params:
data (str): If provided, text will be written to the file before 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) self.show_notification('Could not open file with %s' % editor)
with codecs.open(filepath, 'r', 'utf-8') as fp: with codecs.open(filepath, 'r', 'utf-8') as fp:
text = ''.join(line for line in fp if not line.startswith('#')) text = fp.read()
text = text.rstrip() text = self.strip_instructions(text)
try: try:
yield text yield text
@@ -829,6 +832,19 @@ class Terminal(object):
out = '\n'.join(stack) out = '\n'.join(stack)
return out 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 = '<!--{token}(.*?){token}-->'.format(token=TOKEN)
return re.sub(pattern, '', text, flags=flags).strip()
def clear_screen(self): def clear_screen(self):
""" """
In the beginning this always called touchwin(). However, a bug In the beginning this always called touchwin(). However, a bug

View File

@@ -10,7 +10,8 @@ import six
import pytest import pytest
from rtv.theme import Theme 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 from rtv.exceptions import TemporaryFileError, BrowserError
try: try:
@@ -289,21 +290,21 @@ def test_open_editor(terminal, use_ascii):
terminal.config['ascii'] = 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} data = {'filename': None}
def side_effect(args): def side_effect(args):
data['filename'] = args[1] data['filename'] = args[1]
with codecs.open(data['filename'], 'r+', 'utf-8') as fp: with codecs.open(data['filename'], 'r+', 'utf-8') as fp:
assert fp.read() == comment assert fp.read() == comment
fp.write('This is an amended comment!') fp.write('Comment 2')
return mock.Mock() return mock.Mock()
with mock.patch('subprocess.Popen', autospec=True) as Popen: with mock.patch('subprocess.Popen', autospec=True) as Popen:
Popen.side_effect = side_effect Popen.side_effect = side_effect
with terminal.open_editor(comment) as reply_text: 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 os.path.isfile(data['filename'])
assert curses.endwin.called assert curses.endwin.called
assert curses.doupdate.called assert curses.doupdate.called
@@ -670,3 +671,31 @@ def test_set_theme_no_colors(terminal, stdscr):
terminal.set_theme(Theme(use_color=True)) terminal.set_theme(Theme(use_color=True))
assert not terminal.theme.use_color 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 = '<!-- A normal HTML comment -->'
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} <!-- A nested HTML comment --> {0}-->'.format(TOKEN)
assert terminal.strip_instructions(text) == ''
# Another edge case
text = '<!--{0} instructions {0}--><!-- An HTML comment -->'.format(TOKEN)
assert terminal.strip_instructions(text) == '<!-- An HTML comment -->'