Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Preflighting blog comments in the Pythonista share screen

Jerry Stratton, August 22, 2018

Pythonista’s share screen lets you run any script you have stored locally; you can also bring up the console, and add commonly-used scripts to a shortcuts area.

The more I use it, the more useful Pythonista becomes at customizing my iPad and iPhone experience. I don’t do a whole lot of commenting on other people’s blogs. One I do comment on occasionally is the Ace of Spades HQ, and it has a somewhat… fragile… 1 commenting system. Specifically, (a) there’s a disconnect with how it handles UTF-8 characters and what it tells browsers it can handle, which means any diacriticals or smart quotes will cause it to fail spectacularly; and (b) it uses a primitive BBCode-like means of italicizing that makes it very possible to “end up in the barrel” by not closing your tags. Not closing your tags means every succeeding comment will be italicized (or bolded, or underlined, or so on) too.

I do a lot of my commenting on the iPhone or iPad, where it is very easy to get screwed over by either autocorrect or fat fingers.

It occurred to me yesterday that this was a great opportunity to start using Pythonista’s sharing extension. You can turn on Pythonista in the sharing window, and it will allow you to run scripts2. Normally, this means running scripts on a text document that you’re “sharing” with Pythonista, but it also has access to the clipboard and whatever text is currently selected.

Having written a script to ensure that I don’t post non-ASCII characters and that I don’t forget to close my brackets, I realized I could make formatting a lot easier by using a simple Markdown converter.

[toggle code]

from unicodedata import normalize, name

from codecs import register_error

from re import sub

import clipboard

import appex

def ascify(text):

characters = text.object[text.start:text.end]

newValue = ''

for character in characters:

if character in '’‘':

character = "'"

elif character in '”“':

character = '"'

elif character == '—':

character = '--'

elif name(character).startswith('COMBINING '):

character = ''

else:

print(character, ord(character), name(character))

character = ''

newValue += character

return newValue, text.end

register_error('ascify', ascify)

def markdown2BB(text):

newText = ''

for line in text.splitlines():

if line.startswith('> '):

line = '[i]' + line[2:] + '[/i]'

line = sub('\*\*([^*]+)\*\*', '[b]\\1[/b]', line)

line = sub('\*([^*]+)\*', '[i]\\1[/i]', line)

line = sub('~~([^~]+)~~', '[s]\\1[/s]', line)

line = sub('__([^_]+)__', '[u]\\1[/u]', line)

newText += line + "\n"

newText = newText.strip()

return newText

def closeBBCode(text):

for code in 'ibus':

start = '[' + code + ']'

end = '[/' + code + ']'

starts = text.count(start)

ends = text.count(end)

while ends < starts:

text += end

ends += 1

return text

text = appex.get_text()

if not text:

text = clipboard.get()

if text:

fixed = normalize('NFKD', text).encode('ascii', 'ascify')

fixed = fixed.decode('ascii')

fixed = markdown2BB(fixed)

fixed = closeBBCode(fixed)

clipboard.set(fixed)

#print(fixed)

appex.finish()

This does three things:

It uses normalize and encode to turn any UTF8 characters into their ASCII equivalents.

It converts simple Markdown to BBCode-style formatting.

It makes sure that any manual formatting is closed.

In the first case, it sets up a function called ascify that will be called every time .encode runs into a non-ASCII character. The function doesn’t worry about most of them—Python knows that ‘é’ needs to be degraded to ‘e’ and that ‘…’ needs to be degraded to ‘...’. It does not, however, know that smart quotes need to be converted to their straight equivalents, or that emdashes need to be turned into double-dashes. Because .encode also sends the function a COMBINING character with each diacritical, the function ignores any character that has a UTF8 name beginning with COMBINING.

If the function is sent any character it doesn’t know what to do with, it prints out the character, the numerical code for the character, and its UTF8 name.

In the second case, it just converts double asterisks to bold, single asterisks to italics, double tildes to strikethrough, and double underscores to underline3. There is a semi-standard on the blog for quotes to be italicized; so I convert Markdown quotes to just italicize the line. It will, in other words, take this:

In the third case, it just counts up the number of start and end codes for italics, bold, underline, and strikethrough, and if there are more start codes than end codes, it appends enough end codes to match.

It first tries to get text using appex.get_text() which will provide text only if some text is highlighted and the share button from the highlight bar was chosen, or an actual text file has been shared. If not, it will look in the clipboard for text.

In both cases, once it has modified the text it sets the clipboard to the modified (fixed) text. I use it by highlighting my proposed comment, sharing to ascify in Pythonista, and then pasting back the modified text.

This is pretty basic stuff. But it has, so far, kept me out of the barrel on the site4 and made it a lot easier to conform to the fragile standards of the comments system there.

Obviously, your own needs will be different, but the basic idea of preflighting using Pythonista and the share screen works very well. And the use of Markdown for comments really is nice. While I do the Markdown conversion by hand, because my needs are both simple and nonstandard, if you’re using this for comments on a blog that supports links and images you could use the Markdown modules built in to Pythonista.

To run the script directly from Safari on the iPad or iPhone, you’ll need to tell iOS to show Pythonista in the list of sharing options:

If Pythonista isn’t already showing up, add it to your share options by flipping the switch to green. You can also change where it appears in the list using the drag bars.

It’s the backend that’s fragile. The front-end is perfect, at least for the kind of commenting that goes on on the site. It’s simple and eschews a lot of the stuff that gets in the way of commenting, such as cascading comments. It was the major inspiration for my own comment system.

Irreverant, insightful, and often even informative, I suggest putting Ace of Spades HQ on your regular reading list. “Every normal man must be tempted at times to spit upon his hands, hoist the black flag, and begin slitting throats.”

I’ve enabled comments on recent articles on this blog. A long time ago I used Haloscan, but that didn’t work out very well and eventually they were bought by JS-Kit. I use JS-Kit for the main blog, but they started charging sometime afterward, and while the hoboes.com address is grandfathered in, the godsmonsters.com address was not.

Blogroll

Keep in touch

About Mimsy

Comments?

The undiscovered comment form, whose bourn no poster returns.

Your comment

Your name

Your email

Your web page

Your location

Your email, URL, and location are optional—but I won’t be able to contact you if you don’t leave a working email. Your email does not get displayed, your URL and location do. Your name is required but may vary as the needs of the day demand, or you can just use the anonymous Hark Thrice name. You can use the following tags: <em>, <a>, <blockquote>. Use them wisely and post intelligently. Comments may take some time to approve, especially if I’m stuck in a Mexican jail.

Lost?

The value of the computer for artists lies not in its ability to mimic what man can do, but in providing a means for man to accomplish artistic endeavors which ordinarily would lie beyond his technical scope. — Ann H. Murray (Artist and Computer)