Publish your links in one of three different formats…

Whenever creating a list of links for publishing in a blog or wiki, you have to enter formatting or commands text like <a href="http://...">description</a> or [[http://...][description]].
So I decided to write a short script that takes a generic input file with a formatting like the following example (2 spaces in front of the link and 4 spaces in front of the each link description, with the link description separated into a linked and a unlinked part, using multiple languages):

http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser
[en]Code examples|for access to Firefox tabs
[de]Code-Beispiele|für den Zugang zu den Firefox-Tabs
http://kb.mozillazine.org/Extension_development#Introduction
[en]Firefox extensions|Firefox extension development basics
[de]Firefox-Erweiterungen| - Grundlagen der Entwicklung

This input file is then converted into one of three output formats. I am pretty sure there is a better solution out there, I just couldn’t find one 😉

Here’s the script:

#!/bin/ksh
# create-link-list.ksh: create a list of links based on a formatted input file
if [ $# -le 1 ]; then
echo "$0: Create a formatted linked list out of a multiline input link list"
echo "Usage: $0 file lang [output format]"
echo "      file: input file"
echo "      lang: \"en\", \"de\" or any other"
echo "      output format: leave empty or use \"twiki\" or \"table\""
echo ""
echo "input file sample:"
echo "  http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser"
echo "    [en]Code examples|for access to Firefox tabs"
echo "    [de]Code-Beispiele|für den Zugang zu den Firefox-Tabs"
echo "  http://kb.mozillazine.org/Extension_development#Introduction"
echo "    [en]Firefox extensions|Firefox extension development basics"
echo "    [de]Firefox-Erweiterungen| - Grundlagen der Entwicklung"
echo ""
exit 1
fi
file=$1
lang=$2
type=${3:-simple}
nawk 'BEGIN{type="'${type}'"}
/^  [a-zA-Z0-9]/{gsub ("^  ", ""); link=$0}
/^    \['${lang}'/{gsub ("^    \\['${lang}'\\]", "");
split ($0, desc, "|");
if (type == "twiki") {printf ("[[%s][%s]] %s<br>\n", link, desc[1], desc[2])}
else if (type == "table") {printf ("<tr><td><a href=\"%s\">%s</a></td><td>%s\n", link, desc[1], desc[2])}
else {printf("<a href=\"%s\">%s</a> %s<br>\n", link, desc[1], desc[2])}
}' \
$file

Here’s a sample command line, producing standard HTML output with German link descriptions:

create-link-list.ksh links.txt de

Output:
Code-Beispiele für den Zugang zu den Firefox-Tabs
Firefox-Erweiterungen – Grundlagen der Entwicklung

And here a command that produces Twiki formatted output, this time with English link descriptions:

create-link-list.ksh links.txt en twiki

Output:
[[http://developer.mozilla.org/en/docs/Code_snippets:Tabbed_browser][Code examples]] for access to Firefox tabs
[[http://kb.mozillazine.org/Extension_development#Introduction][Firefox extensions]] Firefox extension development basics

For producing HTML table formatted output, use the following command:

create-link-list.ksh links.txt en table

Output (table statements added manually):

Code examples for access to Firefox tabs
Firefox extensions Firefox extension development basics

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: