Joseph Scott

Archive for November, 2008

MakeItLink - Detecting URLs In Text And Making Them Links

with 7 comments

In late October Jeff Atwood wrote about The Problems With URLs, describing the problems of parsing out URLs in text and transforming them into links. Here’s a simple example:

My website is at http://josephscott.org/

Would be changed into:

My website is at <a href=’http://josephscott.org/’>http://josephscott.org/</a>

Sounds simple right? Once you start looking at what the valid character set is for URLs things get tricky. I won’t rehash all of items, go the The Problem With URLs post to see an example of some of the problems.

I knew that WordPress had a make_clickable function (in wp-includes/formatting.php) that did this exact thing. After testing this against some of the problems that Jeff points out it became clear that make_clickable() didn’t handle these edge cases. I made some rather crude tweaks to the WordPress code to fix some of these and opened ticket 8300 with my patches. Then filosofo came along and not only cleaned up my hacks, but reduced the amount of code needed in general. Major kudos to filosofo!

At this point it looks like we’ve got code to make make_clickable() work correctly with problem URLs. I’m going to wait until after WordPress 2.7 is released to push for getting this code committed since we’re trying to get 2.7 wrapped up.

I got thinking, this bit of code would be really handy to have as a stand alone library. So I pulled out the various pieces of code needed to make this work and put it together in a single PHP class: MakeItLink

class MakeItLink {
    protected function _link_www( $matches ) {
        $url = $matches[2];
        $url = MakeItLink::cleanURL( $url );
        if( empty( $url ) ) {
            return $matches[0];
        }

        return "{$matches[1]}<a href='{$url}'>{$url}</a>";
    }

    public function cleanURL( $url ) {
        if( $url == '' ) {
            return $url;
        }

        $url = preg_replace( "|[^a-z0-9-~+_.?#=!&;,/:%@$*'()x80-xff]|i", '', $url );
        $url = str_replace( array( "%0d", "%0a" ), '', $url );
        $url = str_replace( ";//", "://", $url );

        /* If the URL doesn't appear to contain a scheme, we
         * presume it needs http:// appended (unless a relative
         * link starting with / or a php file).
         */
        if(
            strpos( $url, ":" ) === false
            && substr( $url, 0, 1 ) != "/"
            && !preg_match( "|^[a-z0-9-]+?.php|i", $url )
        ) {
            $url = "http://{$url}";
        }

        // Replace ampersans and single quotes
        $url = preg_replace( "|&([^#])(?![a-z]{2,8};)|", "&#038;$1", $url );
        $url = str_replace( "'", "&#039;", $url );

        return $url;
    }

    public function transform( $text ) {
        $text = " {$text}";

        $text = preg_replace_callback(
            '#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))*)#is',
            array( 'MakeItLink', '_link_www' ),
            $text
        );

        $text = preg_replace( '#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $text );
        $text = trim( $text );

        return $text;
    }
}

It’s very easy to use, just load up the text you want to search for link and call the transform method:

$text = MakeItLink::transform( $text );

All of this code came out of WordPress, which is licensed under the GPL, so consider the MakeItLink code GPL as well. If you’ve got some improvements let me know and make sure that it gets back into the original WordPress functions as well.

Written by Joseph Scott

November 28th, 2008 at 9:00 am

Tagged with , ,

Eureka! Episode 3 - Speed

leave a comment

Written by Joseph Scott

November 28th, 2008 at 7:00 am

Tagged with , ,

Giving Thanks - Child Number Three On The Way

with 12 comments

The Family

The Family

We’ve got something extra special to be grateful for this year, we just found out this week that we are now expecting kid number three. The ETA for the new arrival is July 2009. We’re all very excited to be welcoming a new member to the family next summer.

Thanksgiving is also a great time to count your blessings. I’ve got a great job, a house that keeps us warm during these cold Utah winters and the kids are healthy and doing well. I even get out to play basketball on occasion.

Hopefully this Thanksgiving day we can take a break from watching our stock portfolios and mortgage companies tank long enough to enjoy a good meal with friends and family. You won’t be disappointed in the returns that are yielded from investing time with those closest to you.

Happy Thanksgiving Everyone!

Written by Joseph Scott

November 27th, 2008 at 5:00 pm

Tagged with

The Constitution of the United States: Article I - Section 4

leave a comment

Article I - Section 4:

The Times, Places and Manner of holding Elections for Senators and Representatives, shall be prescribed in each State by the Legislature thereof; but the Congress may at any time by Law make or alter such Regulations, except as to the Places of chusing Senators.

The Congress shall assemble at least once in every Year, and such Meeting shall [be on the first Monday in December]1, unless they shall by Law appoint a different Day.

1- This was changed by the 20th Amendment

Each state legislature gets to decide when to hold elections, except where Congress can override them. The start date for Congress has been changed by constitutional amendment in 1933 to the 3rd of January. For you trivia buffs, the 3rd of January also happens to be my wedding anniversary and my oldest daughters birthday.

Other Constitution posts.

Written by Joseph Scott

November 27th, 2008 at 7:00 am

Tagged with ,

Solid-State Drive (SSD) Reliability Problems?

with 2 comments

Over the last month or two I’ve heard from a number of people who have had Solid-State Drives (SSD) fail. The first couple of times I heard this I thought it was odd. After the fifth time (all from different people) I began to worry about a potential trend. All of these were in computer systems, being used as primary storage (what most of us use hard drives for).

What I don’t know is who made all of these drives. It’s entirely possible (although seems unlikely) that they were all made by the same vendor, who is having some serious problems.

This is completely unscientific, but I’ve heard this enough times to make note of it.

Written by Joseph Scott

November 25th, 2008 at 1:53 pm

Tagged with

The Constitution of the United States: Article I - Section 3

leave a comment

Article I - Section 3, paragraph 1:

The Senate of the United States shall be composed of two Senators from each State, [chosen by the Legislature]1 thereof for six Years; and each Senator shall have one Vote.

1- This was changed in 1912 by the 17th Amendment.

Section 2 started details on House of Representatives, in section 3 we get details on the Senate. Interesting that they specify that each Senator gets one vote, but there’s no such language for Representatives.

Paragraph 2:

Immediately after they shall be assembled in Consequence of the first Election, they shall be divided as equally as may be into three Classes. The Seats of the Senators of the first Class shall be vacated at the Expiration of the second Year, of the second Class at the Expiration of the fourth Year, and of the third Class at the Expiration of the sixth Year, so that one third may be chosen every second Year; [and if Vacancies happen by Resignation, or otherwise, during the Recess of the Legislature of any State, the Executive thereof may make temporary Appointments until the next Meeting of the Legislature, which shall then fill such Vacancies.]2

2- This was changed by the 17th Amendment.

Elections for Senators are done in groups, every two years.

Paragraph 3:

No Person shall be a Senator who shall not have attained to the Age of thirty Years, and been nine Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State for which he shall be chosen.

Similar requirements to being a Representative, with a higher age and citizen requirements.

Paragraph 4:

The Vice President of the United States shall be President of the Senate, but shall have no Vote, unless they be equally divided.

The famous job of breaking ties in the Senate.

Paragraph 5:

The Senate shall chuse their other Officers, and also a President pro tempore, in the Absence of the Vice President, or when he shall exercise the Office of President of the United States.

The Senate gets to choose their leaders from among themselves.

Paragraph 6:

The Senate shall have the sole Power to try all Impeachments. When sitting for that Purpose, they shall be on Oath or Affirmation. When the President of the United States is tried, the Chief Justice shall preside: And no Person shall be convicted without the Concurrence of two thirds of the Members present.

The Senate gets to do impeachments and it takes a 2/3rds vote to to convict.

Paragraph 7:

Judgment in Cases of Impeachment shall not extend further than to removal from Office, and disqualification to hold and enjoy any Office of honor, Trust or Profit under the United States: but the Party convicted shall nevertheless be liable and subject to Indictment, Trial, Judgment and Punishment, according to Law.

Here’s something that I hadn’t really thought about, the impeachment results is limited to removal and disqualification of office. They can’t bring criminal charges as part of the process, but such charges could still be brought through the normal legal channels.

The impeachment process has been used against three Presidents: Andrew Johnson (1868), Richard Nixon (1974) and Bill Clinton (1999). While the last two happened during my life time, I was was too young to remember Nixon.

The Nixon situation has all sorts of interesting details, one that I only recently read about was what a landslide the 1972 election was (for Nixon’s second term). He won the Electoral College 520 to 17 against George McGovern. The popular vote was massively lopsided as well, 47,169,911 to 29,170,383. Just two and half years later he would be resigning from office.

Other Constitution posts.

Written by Joseph Scott

November 25th, 2008 at 7:00 am

Tagged with ,

eBusiness Day

with 4 comments

Matthew Reinbold and I went down to the BYU campus in Provo, Utah for their eBusiness Day. Living in Utah now for about 18 months with no plans to move (hate moving, can’t stand the thought of doing that again) I certainly have an interest in seeing the tech community do well here. The schedule of events included to two keynotes and three panel discussions, focusing on Utah’s image as a tech center, training/education for a tech workforce and funding for companies.

The most interesting part of the event was listening to Josh James, CEO of Omniture. He had no problem bluntly talking about what things that he might do differently and convering what some would consider sensitive issues. The second keynote was from Matt Bowman of Novell, looking back at their history in Utah. This was hard to watch, because Novell’s glory days are long behind them.

The first panel was on “Does Utah’s image Limit Utah’s high-tech corridor?”. Lots of numbers thrown around, but little discussion about the question.

Next up was a panel about “Does Utah get a passing grade for high-tech workforce preparation?”. This one was more interesting because it has some specific give and take on ideas and techniques for educating the next generation of tech workers. Although none of them came out and said it, much of it danced around trade schools versus traditional college/universities.

The final panel covered “Show me the money! Is there enough capital to fund Utah’s high-tech industry?”. Both angel and traditional VC firms were represented on the panel, which helped give wider perspective of the current issues in getting and funding local Utah companies in the current economic conditions. This panel also did the best job in getting the audience questions involved early enough so that a real discussion could take place.

What I found interesting is that through out the whole event there was little (none?) discussion about the mass globalization that has been accelerating over the last 10 years. I can sympathize with the urge to only focus on things local, but there is so much more to the picture in todays world. For instance, I work from home and could live any where with a decent Internet connection. For a variety of reasons we now live in Utah. I didn’t hear a single person talk about attracting virtual office workers to the area. Perhaps that’s a harder section of the market to focus on.

In the end, it was good for me to hear what other people are thinking about; what they see as problems/challenges and how they’d like to solve them. Spending most days working out of my basement might make me a little too sheltered sometimes.

Written by Joseph Scott

November 21st, 2008 at 4:57 pm

Tagged with ,

Eureka! Episode 2 - Mass

leave a comment

Written by Joseph Scott

November 21st, 2008 at 7:00 am

Tagged with , ,

The Constitution of the United States: Article I - Section 2

with 2 comments

Article I - Section 2 is a bit longer, so we’ll break it into chunks, starting with paragraph 1:

The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature.

The first part of the sentence is pretty direct, but what does the second half mean? It sounds like each state gets to determine what the qualifications are for being allowed to vote and that those qualifications are determined by the largest branch of each state legislature. If anyone has a reference that explains this clause in simple terms be sure to leave a comment.

Moving on to paragraph 2:

No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen.

Qualifications for being elected as Representative: age 25 or older, citizen of the US for at least 7 years and live in the state that you are representing. So there are no legal barriers for me to run for one the three House of Representative seats in Utah, good to know :-)

Paragraph 3:

[Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons]1. The actual Enumeration shall be made within three Years after the first Meeting of the Congress of the United States, and within every subsequent Term of ten Years, in such Manner as they shall by Law direct. The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode-Island and Providence Plantations one, Connecticut five, New-York six, New Jersey four, Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five, and Georgia three.

1 - This part has been modified by the 14th Amendment

Spells out the algorithm for determining the number of representatives that each state gets, with the minimum being 1. This algorithm then gets re-run every 10 years.

Paragraph 4:

When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies.

When a vacancy occurs then the Governor of that state has to call a special election in order to fill the seat. I wonder if there are any associated requirements. For instance, what if the Governor doesn’t want to call a special election, can he just stall and drag out the process, or must it be done with in a certain time frame?

Paragraph 5:

The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment.

The leadership of the House is determined by it’s members, and they the can only be impeached by the other members. This begs the question, how many members of the House of Representatives have been impeached?

Other Constitution posts.

Written by Joseph Scott

November 20th, 2008 at 7:00 am

Tagged with ,

The Constitution of the United States: Article I - Section 1

leave a comment

Article I - Section 1 :

All legislative Powers herein granted shall be vested in a Congress of the United States, which shall consist of a Senate and House of Representatives.

One specific power mentioned here: legislative. That seemed worth a trip to the dictionary, so I looked up the definition of legislative at merriam-webster.com. Definition 1b seems particularly pertinent:

belonging to the branch of government that is charged with such powers as making laws, levying and collecting taxes, and making financial appropriations — compare executive , judicial

I’d never really thought of the word legislative as having any meaning beyond just making laws. I guess you could say that the other items mentioned in the definition are executed through the process of passing laws.

Other Constitution posts.

Written by Joseph Scott

November 18th, 2008 at 12:30 pm

Tagged with ,