My Personal Text to HTML Utility

As I find links that I’d like to save to read later, I often copy the URL into a text document with a line below it describing the link.  I’ve used a variety of AWK scripts to convert these text files into HTML files so that I can simply click on the links.  I finally wrote a short, brute-force C program that works well … if you observe my text file format 😉 …

// text2links
// Copyright (c) 2013 by Jim Lawless
// MIT/X11 License: http://www.mailsend-online.com/license2013.php

#include <stdio.h>
#include <string.h>

int main(int argc,char **argv) {
#define SZ (2048)
   char buff[SZ];
   char *p;
   if(argc<2) {
      fprintf(stderr,
         "Syntax:\n\ttext2links \"title\" < input-text-file > output-html-file\n");
      return 1;
   }
   printf("<html><head><title>%s</title></head><body>",argv[1]);
   while(fgets(buff,SZ-1,stdin)!=NULL) {
      buff[strlen(buff)-1]=0;
      if(!*buff) {
         printf("&nbsp;<br>\n");
         continue;
      }
      p=strstr(buff,"http");
      if(p!=NULL) {
         if((p[4]==':')||(p[5]==':')) {
            printf("<a href=\"%s\">%s</a><br>",buff,buff);
            continue;
         }
      }
      printf("%s<br>\n",buff);
   }
   printf("</body></html>");
   return 0;
}

To use the above program, enter the following command-line

text2links "title" < input-html-file > output-text-file

Where “title” is the string of characters you’d like to see in the output document’s TITLE tag.

Please consider the following text file:

http://www.reddit.com/r/bitcoin
Reddit Bitcoin site.

http://libertyblitzkrieg.com/2013/10/31/five-years-ago-today-bitcoin-was-born-read-satoshis-original-white-paper/
The original Bitcoin whitepaper

http://www.proofofexistence.com/
Service lets you "certify" a document using the Bitcoin blockchain

If the text above is contained in a file called “bitcoin.txt”, we can convert the document into HTML using the following command:


text2links "Bitcoin Links" < bitcoin.txt > bitcoin.htm

When rendered, the output HTML document would look something like this:

bitcoin

When I mentioned above that my “format” has to be observed in order for the utility to work, the only real rules are as follows:

  • The URL must begin immediately on the line with no whitespace.
  • The http and https protocols must appear in lower-case.
  • And end-of-line character must appear immediately after the URL with no other whitespace before the line ends.
Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, Nuts and Volts, and others.
This entry was posted in Programming and tagged . Bookmark the permalink.

One Response to My Personal Text to HTML Utility

  1. Steven Irwin says:

    We share a similar aesthetic. Rapidly prototype in awk (because it looks a lot like C) then if the project works out, move it on up to a “real” C program. I imagine though, few kids are learning awk these days.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s