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
"""
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 = """<!--{token}
Replying to {{author}}'s {{type}}:
{{content}}
SUBMISSION_FILE = """
# Please enter your submission. Lines starting with '#' will be ignored,
# and an empty message aborts the submission.
#
# The first line will be interpreted as the title
# The following lines will be interpreted as the content
#
# Posting to {name}
"""
Enter your reply below this instruction block,
an empty message will abort the comment.
{token}-->
""".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 = """<!--{token}
Editing comment #{{id}}.
The comment is shown below, update it and save the file.
{token}-->
{{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 = """\
<h1 style="color: red">Access Denied</h1><hr>

View File

@@ -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

View File

@@ -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'],

View File

@@ -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 <!--INSRUCTIONS --> 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 = '<!--{token}(.*?){token}-->'.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

View File

@@ -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 = '<!-- 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 -->'