TextExpander: Plain Text to HTML Paragraphs

TextExpander is one of my most frequently used Mac utilities and I have all sorts of snippets that help me with all sorts of tasks. From correcting my seldom perfect typing and inserting standard sets of boilerplate text through to more advanced ones that produce fill-in forms and carry out conversions of clipboard text. This focus of this post certainly falls into the latter, but does so in doing something surprisingly simple. It takes plain text and simply adds HTML paragraph tags in at the appropriate locations.

As I said this isn’t something that is incredibly complex, but it is something I find quite useful. I’m often writing in plain text and then want to convert in into some basic HTML paragraphs before I get into applying more complex HTML.

The snippet runs a small Perl script to do the conversion. Once it fetches the text from the clipboard it looks for blank lines in the text and inserts paragraph markers between them. The line that does this is $text =~ s/\n\n/\n<\/p>\n<p>\n/g; It looks for two consecutive new lines and then replaces it with a newline, closing paragraph tag, two further new lines, an opening paragraph tag and finally another new line.

Since this replaces only the blank lines within the clipboard text with blank lines wrapped in reversed HTML paragraph tags, the final step is to concatenate an opening HTML paragraph tag to the front and a closing HTML paragraph tag to the end. The resulting get is then output by TextExpander.

Basic, but functional and saves me some tedium. Admittedly I frequently write in Markdown, but there are almost as many occasions where I want to transform some plain text into some fairly plain HTML and this just happens to be a quick way for me to do it anywhere.

Label Mark plain text with
Description Convert plain text to HTML paragraphs
Abbreviation cb2hp
#!/usr/bin/perl -w

use strict;

#Get the text from the clipboard
$text =`pbpaste`;

#Change new lines to closing then opening paragraph tags
$text =~ s/\n\n/\n<\/p>\n\n\n/g;

#Prefix with opening and closing paragraph tags
$text = "<p>\n" . $text . "\n";

# Output the marked up text
print $text;

This snippet can be downloaded as part of the Thought Asylum web snippets group.

Author: Stephen Millard
Tags: | perl | textexpander |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read