Chip's Tips for Developers

Contains coding, but not narcotic.

OPML-based feed subscriptions made easy

July 30th, 2010 12:50:12 pm pst by Sterling Camden

I’ve been a much happier feed consumer since I started using Newspipe to pump my feeds to mutt. Subscribing to new feeds, however, got a bit more complicated. I’d have to edit my OPML file and make sure I got the new element inserted with all the right syntax. Even before that, I’d have to find the link to the feed I wanted. You’d be suprised how many sites don’t provide a direct link to their feed, so I’d often have to view the page source and search for an RSS autodiscovery link.

Then a few days ago, a reader asked me if I knew of a utility that would list all the autodiscovered feeds on a site. Since I could obviosuly use the same thing myself, I wrote it. It’s called feeds.rb. You just type a command like this at the command prompt:
feeds.rb http://example.com

… and it lists out all the feeds that have autodiscovery links on that page.

The next step, of course, is to automate including one of these links in my OPML file. Yes, I’m lazy — it’s one of the traits of an efficient programmer. I created a second script, called opmlsub.rb:

opmlsub.rb myopmlfile.opml -s http://example.com > myopmlfile.new

This one takes the given URL and adds an outline element for it to the incoming OPML file, spitting the result to stdout. If the URL is a feed, it will just use it directly. If it’s an HTML page, it will look for autodiscovery links and use the first RSS 2.0 link, if available, or the first ATOM link if an RSS 2.0 link can’t be found (I have to prefer RSS 2.0, being a member of The Board).

You can also instruct opmlsub.rb where to place the new link. The option -i TEXT specifies the value of the “text” attribute of an existing “outline” entry inside which the new link will be placed, as the new last child element. If not specified or not found, then the new element will be the last child of the “body” element.

I didn’t include options for editing and deleting elements. That’s easy enough to do with vim on the rare occasions when it’s needed.

Finally, I wanted to be able to do all this while looking at a page in Firefox (or Chromium, when it’s released on FreeBSD). So I created a third script named ‘clipsub’, which takes a URL from the clipboard (using xclip) and adds it to my OPML file without any intervention. I then mapped that to a key shortcut (mod4+shift+S) in my window manager, xmonad. Because opmlsub.rb validates a subset of the feed, I don’t have to worry about accidentally invoking this when a non-URL is on the clipboard. If any error occurs, clipsub pops up the error message in an xmessage window.

So now, when I see a site to which I’d like to subscribe, I just press ctrl+L (highlight the URL in the address bar), ctrl+C (copy it to the clipboard), and mod4+shift+S (subscribe) — and the posts start magically showing up in my feeds folder in mutt.

Mercurial repository on BitBucket

download

Posted in ATOM, OPML, RSS, Ruby | 1 Comment » RSS 2.0 | Sphere it!

OPML Browser for WordPress gets a new parser

August 24th, 2009 3:36:49 pm pst by Sterling Camden

I thought of a new use for my OPML Browser plugin, but the OPML file I intended to use with it threw me a couple of curve balls.

First of all, the file contained nested outline elements, with xmlURL attributes on the containing elements.  Until now, I displayed the feed icon if an item had a feed, and only displayed the folder icon if it didn’t (and it had child elements).  Because the only way to expand/collapse the tree is by clicking on the folder icon, I’ve changed this to display both icons in that case.

Second, my file nests the outline tags more than two levels deep.  I was using preg_match to parse the XML, with a non-greedy search for the end tag.  Can you guess what the problem was with that approach?  I’d get the end tag for an internal outline item when I was looking for the end of the outer item.  This only happened, of course, when an inner item was terminated by </outline>.

Rather than trying to get even fancier with my regular expressions, I decided to implement a real XML parser instead.  I took a look at SimpleXML, but it requires PHP5 and allow_fopen_url.  Then I looked at PHP’s built-in XML parser functions, but that’s event-based and I needed to wade through the document on my own terms.  So I implemented my own simple XML DOM parser, and included that with the plugin.

Posted in OPML, PHP, WordPress | No Comments » RSS 2.0 | Sphere it!

OPML Browser 2.0 for WordPress

May 11th, 2009 4:38:37 pm pst by Sterling Camden

Yes, this is such a major upgrade to my OPML Browser plugin for WordPress that it warrants bumping the initial digit in the version number.  Here’s what changed:

  1. The plugin now expects to be installed in its own folder: (siteurl)/wp-content/plugins/opml-browser
  2. JavaScript and CSS have been separated into their own files.
  3. JavaScript events are now hooked on window load instead of in the HTML.
  4. Added “alt” attribute to all “img” tags.
  5. Images are now looked for in (siteurl)/wp-content/plugins/opml-browser/images
  6. If an image cannot be found, it will be replaced by the supplied unknown.png (a question mark).
  7. Added “Sort items?” option.  This sorts within categories, unless the “flatten” option is enabled (see next item).
  8. Added the “Flatten hierarchy?” option.  This combines all items into one level.  If sorting, all items get sorted together.
  9. The OPML description attribute is now displayed as a tooltip (as in the OPML Blogroll widget, but it also works in IE and Opera now!).

As far as I know, the OPML Browser can now do everything that the OPML Blogroll widget could do (items 7 through 9 filled that out), so I am herewith declaring the OPML Blogroll widget retired from further upgrades.  Please use this plugin’s widget instead from now on.  Let me know if I’ve forgotten any functionality that you need.

A big “thank you” to all of my users who requested various features and reported bugs.  This plugin would not be what it is today without your input. 

Posted in AJAX, ATOM, CSS, JavaScript, OPML, PHP, RSS, WordPress | 1 Comment » RSS 2.0 | Sphere it!

OPML Blogroll widget 1.3 for WordPress adds tooltips

April 17th, 2008 1:47:33 pm pst by Sterling Camden

Thanks to a suggestion from Diego, I have added tooltips to each entry in the OPML Blogroll widget for WordPress.  These tooltips display the content of the “description” attribute of each outline entry, if one exists, when the user hovers the mouse over the entry.

Note that this is not an update to the OPML Browser plugin, although I plan to add the same feature to that plugin as well (along with a ton of other enhancements).

I used JavaScript to pop up and remove the Tooltips.  I decided to roll my own (with much help from Shelley Powers’ book Adding Ajax) rather than use an existing library, in order to keep the widget lean and to avoid conflicts with other libraries that might be in use on your blog.

Thanks to Internet Explorer, this seemingly simple enhancement required about twice as much JavaScript code as as a Firefox/Opera/Safari-only version would have.  Here are the differences that required special code for IE:

  1. Of course, events don’t get passed to event handlers.  So you use window.event.  No big deal.
  2. And we all know about addEventListener vs. attachEvent.  Not a problem, just wrap it in a function.
  3. If you attach a property to a DOM object, lose the reference, and then reacquire that same element from the DOM later, it doesn’t have the property you added any more.  Apparently IE gives you a new object each time.  So in IE, this plugin has to look up the tooltip for the current item every time it wants to be displayed.
  4. Not having the “currentTarget” property on the Event object in IE really sucks.  You can use “srcElement”, but it isn’t the same element as “currentTarget”.  For instance, if you have a “mouseover” event on an element that contains a link around an image, “currentTarget” gives you the element on which the event was specified, whereas srcElement will give you whatever contained element the mouse was actually over.  So this plugin accesses parentNode until it finds the one that’s meaningful.

Another nice “feature” of Internet Explorer keeps the tooltips from displaying at all in my theme – perhaps due to the use of float on nearby div’s.  I tried the fix outlined here to no avail.  It does work using the WordPress default theme, though, so there is hope.  Let me know how it works with your theme.

I also fixed a problem with the auto-discovery link in which the ending quote on the href was missing. :oops:

Linking to earlier versions for pingback.

UPDATE: 2009-05-11: I have incorporated all the features of this widget into my OPML Browser plugin version 2.0.  Please upgrade (it’s free). 

 

 

Posted in CSS, JavaScript, OPML, PHP, RSS, Web, WordPress | 16 Comments » RSS 2.0 | Sphere it!

More OPML Browser corrections

September 26th, 2007 1:19:04 pm pst by Sterling Camden

Shack Dougall spotted several HTML coding errors in the latest verion of my OPML Browser plugin.  Get the version 1.3 below.

Pingbacks to previous posts.

Posted in OPML, PHP, Web, WordPress | 25 Comments » RSS 2.0 | Sphere it!

OPML browsing just got better

September 22nd, 2007 1:21:03 pm pst by Sterling Camden

Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home/camden/public_html/chipstips/wp-content/plugins/opml-browser/opml-browser.php on line 220

 Version 1.2 of the OPML Browser plugin for WordPress has just been released!  This version includes some significant upgrades, including:

  1. You may now have up to 9 opml-browser widgets. When upgrading, the options from your existing opml-browser widget are copied to the first opml-browser widget.  Thanks to Mark Barnes for suggesting this enhancement.
  2. The option to exclude your domain now ignores differences in case when comparing domain names.  Thanks to Tony Lindskog for finding that one.
  3. You can now override the title for the OPML link by entering text into the “OPML title override” field.  I didn’t want mine to read “FeedDemon Subscriptions”, so I added that for myself.
  4. Added the ability to embed the browser within a page or post using a special

    Could not open OPML URL

    element in the text.  See the readme.
  5. Added a “Get this widget” link at the bottom of the widget.  Thanks to TDavid for suggesting that little bit of self-promotion.

Download version 1.2 via the button below.  Pinging back to all previous posts for those following comments.

UPDATE: More corrections here.

Posted in JavaScript, OPML, PHP, Web, Wildly popular, WordPress | 22 Comments » RSS 2.0 | Sphere it!

OPML Browser with class

March 30th, 2007 3:52:28 pm pst by Sterling Camden

Well, the OPML Browser plugin for WordPress has only been released for two days and already we’re up to version 1.1. Hopefully this one will last a while longer.

I added a new feature: start with all folders closed. Selecting this option will display the browser with all categories collapsed initially, unless the browser doesn’t support Javascript (otherwise the user wouldn’t be able to open them!).

I also completely reworked the code, using a class for the rendering engine. That allowed me to eliminate all global variables (almost always a good thing) and reduce the number of parameters being passed around. I still say PHP’s object syntax sucks, but it’s a good bit better than what I had before.

I even added some comments.

Let me know how you like it.

UPDATE: Get the latest version here.

Posted in JavaScript, OPML, PHP, RSS, Web, Wildly popular, WordPress | 16 Comments » RSS 2.0 | Sphere it!

New OPML browser plugin for WordPress

March 28th, 2007 4:39:54 pm pst by Sterling Camden

Since I first released the OPML Blogroll widget, I’ve received numerous suggestions for enhancements. One that keeps coming up is the ability to show the hierarchy within the OPML file — categories, if you will.

Because of the way the original widget parses and displays the outline, this request was incompatible with the existing code. So I developed a new widget/plugin that does keep the hierarchy intact, and you can download it below. It also uses javascript to expand/collapse categories when the associated folder icon is clicked.

The code also includes an API for displaying an OPML browser within PHP code, so you don’t have to use the widget if your theme is not widget-compatible, or you want to display the browser somewhere else.

See the opml-browser-readme.txt file included in the download for full documentation and tips on how you can control the display through CSS styling to achieve some of the other suggested enhancements — such as scrolling within a limited height, bolding the categories, and removing the ability to expand/collapse categories.

There’s still one thing I haven’t figured out how to do, though. If the text of an item wraps, it wraps beneath the feed icon to the left. I’d like to be able to make it line up with the first line of text, but I’m not enough of a CSS wizard to manage that yet. Any suggestions?

UPDATE: Corrected a missing end-quote error in the autodiscovery link at 14:02 3-29-07

UPDATE: Get the latest version here.

Posted in JavaScript, OPML, PHP, RSS, Web, Wildly popular, WordPress | 15 Comments » RSS 2.0 | Sphere it!

OPML Blogroll widget update 1.2

March 4th, 2007 3:09:57 pm pst by Sterling Camden

Sorry Paul, this isn’t the big update you’re waiting for — just a few loose screws that needed tightening up.

After upgrading to WordPress 2.1, I noticed that the RSS icons in my OPML blogroll widget disappeared. I was using an image that is no longer distributed in the same location, so I put a copy of rss.png in the wp-content/widgets/images directory.

Taking a look at how I was referencing images, I realized that it also wouldn’t work if your blog was not in the root folder of your domain. So I added a call to get_settings(‘siteurl’) to fix that up.

Last month I nearly ran out of bandwidth on Chip’s Quips, and after analyzing the requests I found that nearly half of the bandwidth for the month was consumed by requests for my OPML file. In my case, the OPML file resides on the same server as my site, so there’s no need to request it over HTTP. I added a “Local path” option to the widget which, if specified, will open the file locally.

So now of course the “OPML URL” option is no longer required. If you leave that empty, then the auto-discovery link will be suppressed. It doesn’t make much sense to auto-discover a link on the local file system.

I also discovered that WordPress’s cache support in wp-includes/cache.php ignores the $expire parameter that you can pass when adding or setting cache data. This has not been fixed in WordPress 2.1 either. I’ll have to come up with a better solution so the OPML file doesn’t get banged every fifteen minutes, whether or not it’s on a local drive. But we’ll leave that for the next revision.

UPDATE (3/29/07): Tony Lindskog found a bug where a feed that has no “text” attribute caused a regular expression parsing error, and Paul McGillivary found a missing end-quote in the auto-discovery link. I’ve included fixes for those as well.

UPDATE (3/29/07): For an OPML plugin that preserves the hierarchy within the OPML file (categories, usually) and lets you expand/collapse that hierarchy, go here.

UPDATE (4/17/08): The latest version of this plugin can be found here.

Posted in OPML, PHP, RSS, Web, Wildly popular, WordPress | 31 Comments » RSS 2.0 | Sphere it!

New and improved WordPress OPML Blogroll widget

October 9th, 2006 11:50:50 am pst by Sterling Camden

Thanks to some suggestions from Sergio Longoni, this morning I whipped up version 1.1 of the opml-blogroll widget for WordPress.

New features include:

  • OPML auto-discovery link tag added to the “head” section of your page automatically (Sergio provided the code).
  • You can suppress the OPML image and link by unchecking a new option “Link to OPML?” which is checked by default when upgrading.
  • If you keep the OPML link, I’ve added the new semi-standard OPML icon.
  • Closed the “img” tag for the RSS icons for XHTML compliance (oops).

Suggestions for future enhancements:

  • Sergio suggested adding more control over how items are displayed. For instance, he would prefer “li” tags instead of a table. I’m going to give that some thought, but perhaps I could include options for prolog, epilog, pre-item, post-item, etc. and let you control the HTML.
  • The Dead 2.0 skeptic (who appears to have been hammered out of the blogosphere by the traffic surge incumbent upon his identity intrigue) suggested a while ago that I add some form of paging, so that only the first N items would be displayed unless the user took some action.

What do you think of these suggestions? Got any more?

UPDATE: Latest version here.

Posted in OPML, PHP, RSS, Web, Wildly popular, WordPress | 25 Comments » RSS 2.0 | Sphere it!

OPML blogroll widget for WordPress

June 14th, 2006 10:59:01 am pst by Sterling Camden

I don’t know about you, but when Share Your OPML went live, my first thought was “Oh great, something else to keep updated!” It’s bad enough trying to keep my blogroll on my blog up to date with my subscriptions in my feed reader(s). Now there’s SYO to keep updated, too. Not to mention other similar sites like OPML Workstation and OPML Manager.

This little widget hopes to bring us some relief. Rather than having to reload your blogroll or manually update it every time your subscriptions change, you can display your blogroll directly from a hosted OPML file. So, you can keep your OPML at OPML Workstation or OPML Manager (would be nice if SYO offered an external link as well), and just keep that up to date. Or, you can host the OPML file on your own site and just hand the URL to SYO. Either way, you only have to keep this file updated in one place.

For me, that means that when I add a subcription to my Favorite Feeds folder in FeedDemon, I’ll export my OPML and upload it to my site. Done.

Installation and configuration instructions are included in the opml-blogroll-readme.txt file enclosed.

UPDATE: Latest version here.

Posted in OPML, PHP, Wildly popular, WordPress | 33 Comments » RSS 2.0 | Sphere it!

Better Tag Cloud