Go to content Go to sidebar

Learning UserTalk: Categories and Post Links

In our previous tutorial, we introduced local macros and loops. In this installment, we'll take a look at handling post categories and links. Once again, much of this code has been inspired by the Userland recentTitledBlogPosts macro.

First, we'll extend the local getTitle macro to support post categories by returning the empty string when the post does not belong to the requested category. As far as I can tell, by convention an empty category string refers to the home page. When checking a post for the home page, we test the status of the post flNotOnHomePage flag before returning the post title (testing whether the flag exists supports posts predating the addition of the flag). And when checking a post for a named category, we test the appropriate named categories element.

   on getTitle(adrPost,categoryName) {
      if not defined( adrPost^.title ) {return ""};
      if categoryName == "" {
         if defined( adrPost^.flNotOnHomePage ) {
            if adrPost^.flNotOnHomePage { return "" }};
         return adrPost^.title;
      } else {
         if defined( adrPost^.categories[categoryName] ) {
            if adrPost^.categories[categoryName] { return adrPost^.title }};
         return "";
      };
   };

Next, we'll add a getTitleLink macro. The getTitle macro sorts out titles and category and the radio.weblog.getUrlForPost macro generates the url. A little assembly and we have the title link.

   on getTitleLink(adrPost,categoryName) {
      local(title=getTitle(adrPost,categoryName));
      if title == "" { return "" };
      local(url);
      if radio.weblog.getUrlForPost(adrPost,\@url,categoryName,adrBlog) {
         return "<a href=\"" + url + "\">" + title + "</a>" };
      return title;
   };

I am once again experimenting with Usertalk by including the code in .txt files stored in my Radio Userland post hierarchy. In this case, identical files that represent both a dateless and a dated entry:

Radio_Root/categories/draft/work.txt
at http://127.0.0.1:5335/categories/draft/work.html
Radio_Root/categories/draft/2004/12/10.txt
at http://127.0.0.1:5335/categories/draft/2004/12/10.html

Here is the complete listing of our target txt files. It consists of a macro definition followed by the invocation of that macro. Note that this example has been restricted to the sports category.

<%
on myRecentPosts( adrBlog=radio.weblog.init(), catName="", maxPosts=5 ) {
   local (htmlText="");
   on append(s) { htmlText = htmlText + "<li>" + s + "</li>\n" };
   on getTitle(adrPost,categoryName) {
      if not defined( adrPost^.title ) {return ""};
      if categoryName == "" {
         if defined( adrPost^.flNotOnHomePage ) {
            if adrPost^.flNotOnHomePage { return "" }};
         return adrPost^.title;
      } else {
         if defined( adrPost^.categories[categoryName] ) {
            if adrPost^.categories[categoryName] { return adrPost^.title }};
         return "" };
   };
   on getTitleLink(adrPost,categoryName) {
      local(title=getTitle(adrPost,categoryName));
      if title == "" { return "" };
      local(url);
      if radio.weblog.getUrlForPost(adrPost,\@url,categoryName,adrBlog) {
         return "<a href=\"" + url + "\">" + title + "</a>" };
      return title;
   };
   local (adrPost, numPost=sizeof(adrBlog^.posts), bfrPost=numPost);
   local (date, file = radioResponder.fileBeingRendered);
   if radio.weblog.file.getArchiveFileDate( file,\@date ) {
      if radio.weblog.getLastPostBeforeDate( adrBlog,date,\@adrPost,catName ) {
         bfrPost = indexOf(adrPost^) }};
   local (i,txt,cnt=0);
   for i = bfrPost downto 1 {
       adrPost = @adrBlog^.posts[i];
       txt = getTitleLink(adrPost,catName);
       if txt != "" {
          append(txt);
          if ( ++cnt >= maxPosts ) {break}}};
   return "<ul>" + htmlText + "</ul>"};
myRecentPosts(catName:"sports")
%>

When executed on my system the results are:

dateless entry …/draft/work.html
A list of the 5 most recent posts in the sports category (at the time of writing):
dated entry …/draft/2004/12/10.html
The 5 sports category posts that precede 10 December 2004:

In the next installment, we'll add the finishing touches to this macro.

7 Feb: Learning UserTalk: the myRecentTitledPosts Macro