qr-code

函数

Basically any lisp function could be used withing a blog template as long as they are known when exporting. Meanwhile some of them are specifically made to be used withing templates. These functions are prefixed by ob:.

Descriptions are taken from function docstrings.

Function: (ob:get-posts &optional PREDICATE COUNT SORTFUNC COLLECT)

Return posts (from POSTS as defined in org-publish-blog) matching PREDICATE. Limit to COUNT results if defined and sorted using SORTFUNC.

PREDICATE is a function run for each post with the post itself as argument. If PREDICATE is nil, no filter would be done on posts.

SORTFUNC is used a sort PREDICATE.

If COLLECT is defined, only returns the COLLECT field of a ob:post structure.

Examples:

Getting last 10 posts:

(ob:get-posts nil 10)

Getting post from January 2012:

(ob:get-posts
   (lambda (x)
      (and (= 2012 (ob:post-year x))
           (= 1 (ob:post-month x)))))

Getting all categories:

(ob:get-posts nil nil nil 'category)

Template usages

For archive navigation:

<nav id="archives">
  <h1>Archives</h1>
  <ul>
    <lisp>
      (loop for p in (ob:get-posts nil 10)
            do (insert (format "<li><a href=\"%s/%s\">%s</a></li> "
                               (ob:path-to-root)
                               (ob:post-htmlfile p)
                               (ob:post-title p))))
    </lisp>
  </ul>
</nav>

Function: (ob:get-post-by-id ID)

Return post which id is ID.

Template usages

Posts navigation:

<nav class="articles-nav">
  <ul>
    <lisp>
      (progn
        ;; Get previous post
        (let ((ppost (ob:get-post-by-id (1+ (ob:post-id POST)))))
          (if ppost
              (insert (format "<li class=\"prev\"><a href=\"%s/%s\">%s</a></li>"
                              (ob:path-to-root)
                              (ob:post-htmlfile ppost)
                              (ob:post-title ppost)))
            (insert "<li>&nbsp;</li>")))
        ;; Get next post
        (let ((npost (ob:get-post-by-id (1- (ob:post-id POST)))))
          (if npost
              (insert (format "<li class=\"next\"><a href=\"%s/%s\">%s</a></li>"
                              (ob:path-to-root)
                              (ob:post-htmlfile npost)
                              (ob:post-title npost)))
            (insert "<li>&nbsp;</li>"))))
    </lisp>
  </ul>
</nav>

Function: (ob:get-snippet NAME)

Get first snippet matching NAME.

Template usages

Insert the About section in page footer:

<h1>About</h1>
<address>
  <lisp>(ob:post-content-html (ob:get-snippet "About"))</lisp>
</address>

Function: (ob:get-header HEADER &optional ALL)

Get HEADER from blog buffer as defined in BLOG global context variable.

Returns only fist match except if ALL is defined.

Template usages

Get the last updated header for rss export:

<updated><lisp>(ob:format-date (ob:get-header "DATE"))</lisp></updated>

Function: (ob:insert-template TEMPLATE)

Insert TEMPLATE in current buffer.

Template usages

Insert html header:

<lisp>(ob:insert-template "page_header.html")</lisp>

Function: (ob:format-date DATE &optional FORMAT LOCALE)

Format DATE using FORMAT and LOCALE.

DATE can heither be string suitable for parse-time-string or a list of interger using current-time format.

FORMAT is a format-time-string compatible definition. If not set ISO8601 %Y-%m-%dT%TZ format would be used.

Template usages

Add a human readable timestamp for a post:

Posted on <time datetime="<lisp> (ob:format-date (ob:post-timestamp POST)) </lisp>">
  <lisp> (ob:format-date (ob:post-timestamp POST) "%A %B, %d %Y at %H:%M:%S") </lisp>
</time>.