Plain Text List to HTML List

Hello, I am trying to take a simple list (from echo, not a file) and turn it into a list with HTML codes.

List item one.
List item two.
List item three.

to

<ol>
<li>List item one.</li>
<li>List item two.</li>
<li>List item three.</li>
</ol>

The list is coming in via echo on standard input, and going to standard output.

(I am using OnMyCommand so my plan is that all I have to do is select the list, and choose the right command and it's pasted in place.

I have made some really simple commands in OMC for adding <b></b> tags around text, which is similar to this forums adding of tags. But trying to figure out sed and grep are too much for me for now. The main problem I've been having is trying to get the newlines in place, but for all intents and purposes, I'm back at square one, or should I say square zero. :slight_smile: )

I am not wed to any command line program, but it seems sed is the going to be best.

Any help appreciated! (I've searched all over and can't seem to cobble this together. I'm on OS X 10.4 and I believe bash is the default shell.)

root@isau02:/data/tmp/testfeld> echo -e "List item one.\nList item two.\nList item three."
List item one.
List item two.
List item three.
root@isau02:/data/tmp/testfeld> echo -e "List item one.\nList item two.\nList item three."| awk 'BEGIN{print "<ol>"} {print "<li>"$0"</li"} END{print "</ol>"}'
<ol>
<li>List item one.</li>
<li>List item two.</li>
<li>List item three.</li>
</ol>

If you want to take input from users you could use something like this:

root@isau02:/data/tmp/testfeld> cat mach.sh
#!/bin/sh

CA=0
CB=0

while read LINE; do
        ARRAY[CA]="${LINE}"
        let CA=$CA+1
done

echo "<ol>"
while (( $CB < $CA )); do
        echo "<li>${ARRAY[$CB]}</li>"
        let CB=$CB+1
done
echo "</ol>"

exit 0

Usage/Output example (to tell read that there is end of input just press ctrl+d):

root@isau02:/data/tmp/testfeld> ./mach*
lala eins              <- this line is input
blabla zwei            <- this line is input
yo yo yo               <- this line is input, but after this I pressed ctrl+d
<ol>                   <- first line of output ...
<li>lala eins</li>
<li>blabla zwei</li>
<li>yo yo yo</li>
</ol>

pretty much similar to zaxxon's reply...

usildb26:LT2:/home/ranjeetm > cat a
#!/bin/sh
echo "<ol>"
while read line
do
echo $line |sed 's/^/<li>/g'| sed 's/$/<li>/g'
done
echo "<ol>"

to break---> CTRL d

Another one with only sed, taking input from echo:

echo -e "List item one.\nList item two.\nList item three."| sed -e '1s/^/<ol>\n/'| sed -e '1!s/^/<li>/' -e '1!s/$/<\/li>/' -e '$s/$/\n<\/ol>/' 
<ol>
<li>List item one.</li>
<li>List item two.</li>
<li>List item three.</li>
</ol>

I still had a bit of a struggle trying to find the correct options in the GUI OMCEdit. I was escaping with a backslash instead of "Wrap with ' for Shell." But the latter was the trick!

Thank you both so much!

I ended up using the first suggestion with awk, since I could get that one to work. The user-input options were not exactly what I needed. The last sed suggestion curiously put "n" after the opening <ol> and before the closing </ol> so I went with awk.

In case you are not familiar with the AWESOME OnMyCommand, here is a little info. It allows Mac OS X users to put command lines, shell scripts, and more in contextual menus (i.e. right click). These commands can be directed at files, folders, or selected text. Your favorite program doesn't have an option you want? Create it as a command and add it to the menu! :slight_smile:

Here is the web site: OnMyCommand

My issue is that I find myself writing in various places that demand HTML codes and was tired of typing them in. I thought, "surely I can have OnMyCommand do this for me." So now I just write normally, typing up my list, then when I am ready to add the HTML codes, I just select the list and choose Ordered List and the codes are added automatically!

UNIX.com kept coming up as I spent untold hours trying to figure this out on my own. So in case someone else is trying to do the same thing, here is the plist that can be directly imported in to OMCEdit and it's up and running!

Just save the entire plist as a text file, then use OMCEdit to import it and it'll be added to your list of commands.

Thank you both again!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>COMMAND_LIST</key>
	<array>
		<dict>
			<key>ACTIVATION_MODE</key>
			<string>act_selected_text</string>
			<key>COMMAND</key>
			<array>
				<string>echo </string>
				<string>__OBJ_TEXT__</string>
				<string> | awk 'BEGIN{print "<ol>"} {print "<li>"$0"</li>"} END{print "</ol>"}' | pbcopy</string>
			</array>
			<key>ESCAPE_SPECIAL_CHARS</key>
			<string>esc_wrap_with_single_quotes_for_shell</string>
			<key>EXECUTION_MODE</key>
			<string>exe_silent_popen</string>
			<key>NAME</key>
			<array>
				<string>Ordered List</string>
			</array>
			<key>NOTES</key>
			<string>The goal of this command:
� put the correct XHTML tags around a selected list of text to make it an ordered list.

To Do:
� Error checking

Thanks to: 
� http://www.unix.com 
� zaxxon and ranjeetmenon who provided me with the command line used in this command!

My question and their help can be found here:
http://www.unix.com/shell-programming-scripting/100161-plain-text-list-html-list.html</string>
			<key>PASTE_AFTER_EXECUTION</key>
			<true/>
			<key>SUBMENU_NAME</key>
			<string>HTML</string>
			<key>TERM_BRING_TO_FRONT</key>
			<false/>
			<key>TERM_OPEN_NEW_SESSION</key>
			<false/>
			<key>VERSION</key>
			<integer>1</integer>
		</dict>
	</array>
	<key>VERSION</key>
	<integer>2</integer>
</dict>
</plist>