<?xml version="1.0" encoding="iso-8859-1"?>
        <?xml-stylesheet type="text/css" href="http://www.miek.nl/blog/"?>
<rss version="2.0"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:admin="http://webns.net/mvcb/"
 xmlns:atom="http://www.w3.org/2005/Atom"
>
<channel>
<title>Filed under: nanoblogger | Miek</title>
<atom:link href="http://www.miek.nl/blog/archives/nanoblogger/index-rss.xml" rel="self" type="application/rss+xml" />
<link>http://www.miek.nl/blog</link>
<description>Thoughts on (technical) stuff</description>
<dc:language>en-us</dc:language>
<dc:creator>Miek Gieben</dc:creator>
<dc:date>2012-02-04T04:15:11+01:00</dc:date>
<admin:generatorAgent rdf:resource="http://nanoblogger.sourceforge.net" />

<item>
<link>http://www.miek.nl/blog/archives/2009/05/21/short_urls/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/05/21/short_urls/index.html</guid>
<title>Short URLs</title>
<dc:date>2009-05-21T00:31:55+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger</dc:subject>
<description><![CDATA[<p>Not wanting to miss anything on this <a href="http://www.osnews.com/story/21502">short urls business</a>,
I've implemented something similar in nanoblogger.</p>

<p>How? With a shell script and symlinks, my dear Watson. As with all
shell scripting this is probably something you can do drunk.</p>

<p>First I need to get a list of all my articles so that I can link
to them. Next I take this <em>permalink</em> address, pipe it through
<code>sha1sum</code>, take the last 10 characters and make a symlink from the
permalink path and presto: <strong>short urls</strong></p>

<p>The shell one-liner becomes something like this (note: I've put
the <code>echo</code> in there so you can <em>see</em> what happens).</p>

<pre><code>#!/bin/bash
NB_ARCHIVES=/home/miekg/miek.nl/blog/archives
NB_SHORT=/home/miekg/miek.nl/s
for i in $(find $NB_ARCHIVES -type d -wholename "*2???/*/*/*" -print)
do
    HASH=$(echo $i | sha1sum)
    echo ln -sf $i $NB_SHORT/${HASH:30:10}
done
</code></pre>

<p>This scripts can be dropped in your plugin directory, so that it runs
after each update in <code>nanoblogger</code>. </p>

<p>In your short urls directory (here: <code>miek.nl/s</code>) you can just use <code>ls</code> and <code>grep</code> to find
out which short url links to what article.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/05/11/related_articles_in_nanoblogger/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/05/11/related_articles_in_nanoblogger/index.html</guid>
<title>Related articles in nanoblogger</title>
<dc:date>2009-05-11T20:24:16+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger</dc:subject>
<description><![CDATA[<p>All these wordpress blogs have a <strong>related articles</strong> line below the
blog entries. Sadly <code>nanoblogger</code> does not have such a capability, so
I wrote my own. It's not a plugin in the nanoblogger sense of the word,
it may be possible to rewrite it as a regular plugin, but I do not now
how...</p>

<p>So for now a simple shell script, which you can find 
<a href="http://www.miek.nl/downloads/2009/related.txt">here</a>.</p>

<h2>Usage within a template</h2>

<p>In your templates you should include the following:</p>

<pre><code>$(/home/miekg/miek.nl/blog/bin/related $NB_EntryID)
</code></pre>

<p>the shell script returns the related articles separated with comma's.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/01/30/a_more____link_in_nanoblogger/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/01/30/a_more____link_in_nanoblogger/index.html</guid>
<title>A 'more...' link in nanoblogger</title>
<dc:date>2009-01-30T17:36:38+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, site</dc:subject>
<description><![CDATA[<blockquote>
  <p>Update (200911-08): I've stopped using
this, because it made the archive generating
fail in mysterious ways...</p>
</blockquote>

<p>As you might have seen I have now these nice
(opinions may vary) "more..." links in my blog.
This is done with some JavaScript and shell 
foo and in this entry I will explain how I did it.</p>

<p>The setup also works with text browsers and
people who disable JavaScript. In the latter
case you will get the normal <code>nb</code> behavior
where you see the entire article. When using
a text browser you also see the entire article
but there is also a (non functional) 'more' link.</p>

<h2>User side</h2>

<p>When writing a blog entry the only thing you
need to do is to put the following on a line</p>

<pre><code>- read more -
</code></pre>

<p>(technically you need more -'s to make it work, but 
 this will suffice for this example). </p>

<p>Anything you type below that line will become
collapsible with a 'more' link. Anything above
will be directly visible.</p>

<h2>Implementation</h2>

<p>First a shell script which reads your entire blog
entry and replaces the <code>- read more-</code> with the html
and JavaScript to make it work.</p>

<pre><code>#!/bin/bash
EPOCH=$(date +%s)
ART_ID="art_$$_$EPOCH"
HREF_ID="href_$$_$EPOCH"
DATA=$(cat)

# escape galore
repl="&lt;script type=\"text\/javascript\"&gt;\n\
&lt;!--\n\
function toggle_visibility(id, anchor) {\n\
   var e = document.getElementById(id);\n\
   var a = document.getElementById(anchor);\n\
   if(e.style.display == 'block') {\n\
      e.style.display = 'none';\n\
      a.innerHTML = \"\&amp;laquo; more ...\";\n\
   } else {\n\
      e.style.display = 'block';\n\
      a.innerHTML = \"\&amp;laquo; less\";\n\
   }\n\
}\n\
\/\/--&gt;\n\
&lt;\/script&gt;\n\
&lt;div class=\"readmore\"&gt;\n\
&lt;a class=\"readmore\" style=\"display: none;\" id=\"$HREF_ID\"\n\
href=\"javascript:\/\/\" onclick=\"toggle_visibility(\'$ART_ID\', \'$HREF_ID\');\"&gt;\
\&amp;raquo; more ...&lt;\/a&gt;\n\
&lt;\/div&gt;\n\
&lt;div id=\"$ART_ID\"&gt;\n\
&lt;script type=\"text\/javascript\"&gt;\n\
&lt;!--\n\
e = document.getElementById('$ART_ID');\n\
e.style.display = 'none';\n\
e = document.getElementById('$HREF_ID');\n\
e.style.display = 'block';\n\
\/\/--&gt;\n\
&lt;\/script&gt;\n\
"
if echo "$DATA" | egrep -q -- '-{3,}.?read.more.?-{3,}'; then
echo "$DATA" | sed -r "s/-{3,}.?read.more.?-{3,}/$repl/"
echo "&lt;/div&gt; &lt;!-- id=$ART_ID --&gt;";
else
echo "$DATA"
fi
</code></pre>

<p>As I'm using stdin twice I am capturing it in the variable 
<code>$DATA</code>.</p>

<p>The variable <code>$repl</code> is filled and is used
by <code>sed</code> the replace <code>- read more-</code>. It was a real
pain to correctly quote everything (as you might see).</p>

<p>The JavaScript trick to do this collapsing can be easily found
on the internet (google: hide/show divs).</p>

<h2>interfacing with nanobloggger</h2>

<p>You need to edit two templates, <code>entry.htm</code> and <code>permalink.htm</code>,
in the first one you enable the 'read more'-feature and the latter
you need to get rid of the '- read more -' line.</p>

<h3>entry.htm</h3>

<p>In <code>entry.htm</code> replace </p>

<pre><code>$NB_EntryBody
</code></pre>

<p>with</p>

<pre><code>$(echo "$NB_EntryBody" | ${BLOG_DIR}/bin/readmore)
</code></pre>

<p>Adjust this to where you have saved the shell script from
above.</p>

<h3>permalink.htm</h3>

<p>On your permalink page this read more stuff is ridiculous, so
there we must get rid of it.</p>

<p>Where it says</p>

<pre><code>$NB_EntryBody
</code></pre>

<p>replace that with</p>

<pre><code>$(echo "$NB_EntryBody" | sed -r 's/-{3,}.?read.more.?-{3,}//')
</code></pre>

<p>to filter out the line.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/01/26/nanoblogger_comments_update/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/01/26/nanoblogger_comments_update/index.html</guid>
<title>Nanoblogger comments (update)</title>
<dc:date>2009-01-26T17:08:21+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, site</dc:subject>
<description><![CDATA[<blockquote>
  <p>Basicly these blog items serve as a personal reminder
and documentation. Also the code needs to be refactored
a bit and some html tags need to be removed from it.</p>
</blockquote>

<h1>Files needed</h1>

<p>The following files are needed for this comment system:</p>

<ul>
<li><p><a href="/downloads/2009/comment.php.2.txt">comment.php</a>, this 
is <em>the</em> PHP file the implements everything</p></li>
<li><p><a href="/downloads/2009/nbadmin.2.txt">nbadmin</a>, a small shell script that
implemented the comment moderation (just a <code>mv</code> of the comment to
the correct directory)</p></li>
<li><p><a href="/downloads/2009/nbnotify.2.txt">nbnofity</a>, small shell script
that notifies you a new comments (to be put in <code>cron</code>)</p></li>
<li><p><a href="/downloads/2009/com.css.txt">com.css</a>, the <code>css</code> code for the
comment pages.</p></li>
</ul>

<h1>Templates</h1>

<p>In your templates you will need to <code>require</code> the php script, so a</p>

<pre><code>require_once "/home/miekg/miek.nl/comment.php";
</code></pre>

<p>must be there somewhere.</p>

<p>Further more all pages that <em>do</em> something with comments need the
following code:</p>

<ul>
<li>Get the current comments and put them in a <code>PHP</code> array</li>
</ul>

<pre>
<code>
$comments = gather("$NB_EntryID");
</code>
</pre>

<ul>
<li>Show the current comments as HTML </li>
</ul>

<pre>
<code>
show($comments);
</code>
</pre>

<ul>
<li>Check the current form input and act accordingly</li>
</ul>

<pre>
<code>
if ($_POST['preview'] == "Preview Comment") {
        preview();
}
if ($_POST['submit'] == "Submit Comment") {
        submit();
}
</code>
</pre>

<ul>
<li>Show the comment input form</li>
</ul>

<pre>
<code>
form("$NB_EntryID");
</code>
</pre>

<ul>
<li>Show a "recent comment" section in your side title</li>
</ul>

<pre>
<code>
recent_comments();
</code>
</pre>

<p>And that should be it.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/01/24/nb_comments/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/01/24/nb_comments/index.html</guid>
<title>NB comments</title>
<dc:date>2009-01-24T10:21:26+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, site</dc:subject>
<description><![CDATA[<p>I've implemented a simple comment system in which</p>

<ul>
<li>there are <em>no</em> user accounts</li>
<li>has a preview function</li>
<li>once a comment is submitted you cannot edit it anymore</li>
<li>all comments are moderated</li>
<li>a small set of BB tags are allowed</li>
<li>implemented as one <code>PHP</code> file</li>
<li>the <code>PHP</code> script has some <code>html</code> in it</li>
<li>needs a writeable directory in your document root</li>
</ul>

<p>You need this file: <a href="/downloads/2009/comment.php.txt">comment.php</a>.</p>

<p>The first few lines allow for some customization:</p>

<pre><code>$NB_TIME = "%Y-%m-%d %T %Z"; # strftime function
$NB_COM_BASE="/home/miekg/miek.nl/blog/comments";
$NB_COM = 1;    # 0 -&gt; disable comments
$NB_COM_CLOSE = 2592000; # number of seconds after which comments are closed
$NB_COM_MAX_SIZE = 2000; # max number of chars in a comment
</code></pre>

<p>which have the following meaning:</p>

<ol>
<li><code>NB_TIME</code>: how to format the time and date of the comment</li>
<li><code>NB_COM_BASE</code>: where to put the comment files</li>
<li><code>NB_COM</code>: emergency comment disable, when 0 all commenting is disabled</li>
<li><code>NB_COM_CLOSE</code>: number of seconds when commenting on an article is disabled</li>
<li><code>NB_COM_MAX_SIZE</code>: maximum number of character per comment.</li>
</ol>

<p>In <code>NB_COM_BASE</code> all comments are put, each article gets a
directory named after its <code>$NB_EntryID</code>. In that directory, two
sub dirs are made <code>ok</code> and <code>new</code>. All comments are first put
in the <code>new</code> directory, where they wait for moderation.
See below for the moderation script.</p>

<p>Further more you need to tweak apache to allow PHP 
code in .html files or make nb create .php files.</p>

<p>In all files you will need to following code:</p>

<pre><code>&lt;?php
require "/home/miekg/miek.nl/comment.php";
?&gt;
</code></pre>

<p>Of course you need to use a different path for your situation.</p>

<h2>comment string</h2>

<p>In articles you will need something like <code>comments: (5)</code>
with <code>comment.php</code> the following is needed (remember
the shell is touching this code too, so you'll need to
escape the PHP stuff):</p>

<pre><code>&lt;?php
echo commentstr($NB_EntryID, ${ARCHIVES_PATH}$NB_EntryPermalink);
\$comments =  gather($NB_EntryID);
echo "(" . count(\$comments) . ")";
?&gt;
</code></pre>

<p>Or whatever you want to put there. The <code>gather()</code> function will
retrieve the current comments of this article. It will also
create a link to the permalink of the article as I decided
that new comments are to be added there.</p>

<h2>adding comments</h2>

<p>In my permalink template I have the following code:
Note: 1) the shell is touching is and 2) my templates
are written in <code>m4</code>, where I've made '[' and ']' mean
something, hence the double '[[' and ']]'):</p>

<pre><code>&lt;?php
show(\$comments);
if (\$_POST[['preview']] == "Preview Comment") {
    preview();
}
if (\$_POST[['submit']] == "Submit Comment") {
    submit();
}
form("$NB_EntryID");
?&gt;
</code></pre>

<p>Well this kinda speaks for itself. A user can preview
comments when pressing 'Preview' in the form and 
for submitting submit is pressed.</p>

<p>The <code>form()</code> function displays the commenting form.</p>

<h2>moderation script</h2>

<p>After a comment has been left in the <code>new/</code> directory it needs
to be moved to the <code>ok/</code> dir. I've written
<a href="/downloads/2009/nbadmin">nbadmin</a> for this purpose. A typical
run looks like this:</p>

<pre><code>&gt; Article: e2009-01-12T19_34_47.txt
 &gt;&gt; Comment found: 1232783070_uvTSOy
&lt;a href="http://miek.nl"&gt;Miek Gieben&lt;/a&gt;
Test 123
(A)approve (D)discard (N)skip [A]: A
new/1232783070_uvTSOy ok/1232783070_uvTSOy
</code></pre>

<p>And with that the comment is okayed.</p>

<h3>cron job</h3>

<p>Of course you want to be notified of new comments, this is implemented
as a cronjob which checks if there are any files in a <code>new/</code> directory.</p>

<p>The quick and dirty script:</p>

<pre><code>#!/bin/sh
# send email for the comments that are found in my website
COM_BASE="/home/miekg/miek.nl/blog/comments"
BODY=""

for art in $COM_BASE/*.txt; do
for com in $art/new/*; do
    if [ $com = $art/new/* ]; then continue; fi
    BODY="$BODY\n o $com"
done
done

if [ ! -z "$BODY" ]; then
echo "New comments found for miek.nl"
echo ${BODY}
fi
</code></pre>

<p>And my cronjob:</p>

<pre><code>% crontab -l
MAILTO=miek@atoom.net
# check if there are new comments
10 8-22/2 * * * /home/miekg/bin/nbnotify
</code></pre>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/01/17/nanoblogger_comments/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/01/17/nanoblogger_comments/index.html</guid>
<title>Nanoblogger comments</title>
<dc:date>2009-01-17T23:56:11+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, site</dc:subject>
<description><![CDATA[<p>I've been thinking about comments on my blog ever since I started
using <code>nanoblogger</code>. While <code>nb</code> is great (with VIM and markdown) I'm
starting to miss the comment stuff.</p>

<h2>Comment systems for nb</h2>

<p>I've been searching for a good comment system that <em>I</em> want to use
for <code>nb</code>. It should meet the following criteria:</p>

<ol>
<li>Simple moderation system. With all the spam nowadays <em>every</em>
comment made should be moderated. </li>
<li>Should not require a database, the filesystem is enough of a
database to handle these tasks. This does imply that there will
be <em>no</em> user accounts, nor the possibility to edit your comments.</li>
<li>Simple interface with nanoblogger.</li>
</ol>

<p>I've looked at <code>blogkomm</code>, <code>haloscan</code> and <code>cgicomment</code>.</p>

<h2>blogkomm</h2>

<p>Looks nice, easy to setup, but I could <em>not</em> get the moderation to work. Also
I looked like <em>all</em> the comments are kept in a single file called
<code>comments.txt</code>, but I prefer the comments to be more linked to the
article they belong to.</p>

<h2>haloscan</h2>

<p>This is a external site which takes care of the comments. So here I'm I
running <code>nb</code> because I prefer to keep things simple and <em>then</em> I
outsource my comment system to a site which I do not control and which
does rely on databases and what not.</p>

<h2>cgicomment</h2>

<p>Could ... not ... found ... the ... damn ... thing ... anywhere.</p>

<h1>Conclusion</h1>

<p>I need to write my own system. I have the following in mind:</p>

<ul>
<li>Simple PHP based system</li>
<li>All comments are put in a 'to-be-moderated' directory. A simple
tool called <code>nbadmin</code> will control if a comment is let through. You
can compare it with <code>listadmin</code> which you can use when you use
<code>mailman</code>. If you 'OK' a comment, the comment will be moved to another
directory,</li>
<li>Notification of new comments to 'someone'. This will probably will
be implemented as a <code>cron</code> job which scans the 'to-be-moderated'
directories.</li>
<li>Some PHP functions which allow the display of comments and which
can be integrated with the rest of nanoblogger.</li>
</ul>

<p>The implementation will (probably) result in one PHP file called
<code>comment.php</code> which needs to be included in your templates. </p>

<p>I'm still in the early stages of this project and I do not yet know
if it will work like I want.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2008/09/14/bash_loadable_modules/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2008/09/14/bash_loadable_modules/index.html</guid>
<title>Bash loadable modules</title>
<dc:date>2008-09-14T20:24:08+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, zsh, linux</dc:subject>
<description><![CDATA[<p>Or how to create new builtins for use in <code>bash</code>. Short answer: you can,
but it is not enabled by default (at least on my distribution, Ubuntu)</p>

<p>This is also something that is done much better in <code>zsh</code>.</p>

<p>Why do want this? <em>SPEED</em>! </p>

<p>Shell scripting is a very easy
way to program, but forking all these helper programs takes
a lot of time. So one way to speed up your shell program is
to load these programs into the shell and making them a <em>builtin</em>.</p>

<h2>bash</h2>

<p>I needed the bash source for this to work. So download it and
compile it. Then in the <code>examples/loadables</code> you have some
new builtins, like <code>cat</code>, <code>cut</code>, <code>head</code> and then some.</p>

<p>Making <code>cat</code> a builtin, you need the <code>enable</code> command</p>

<pre><code>$ type cat
cat is /bin/cat

$ enable -f /tmp/bash-3.2/examples/loadables/cat cat
$ cat type
cat is a shell builtin
</code></pre>

<p>Same goes for other executables. When updating my site with
<code>nanoblogger</code>, using <code>cat</code> and others as builtins made the
update much quicker. I shaved off some 20 seconds on a total
of 250 seconds; so it may be worth the effort.</p>

<h2>zsh</h2>

<p>Here everything worked out of the box, but there is currently
no <code>cat</code> builtin for zsh (I created one). But there are
other interesting builtins.</p>

<p>What you need: <code>man zshmodules</code> and <code>zmodload</code>. With zmodload
you can create new builtins. </p>

<p>For example, I made a <code>cat</code> builtin. Next I copy the <code>cat.so</code> to
<code>/usr/lib/zsh/4.3.4/zsh</code></p>

<pre><code>% type cat
cat is /bin/cat

% zmodload zsh/cat
% type cat
cat is a shell builtin
</code></pre>

<p>Unload with <code>zmodload -u</code>. Works like a charm.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2007/12/31/nanoblogger_plugin_for_tagcloud/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2007/12/31/nanoblogger_plugin_for_tagcloud/index.html</guid>
<title>Nanoblogger plugin for tagcloud</title>
<dc:date>2007-12-31T13:48:53+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, site</dc:subject>
<description><![CDATA[<p>I've made a small plugin which generates a tag cloud for your
nb style blog. It's probably a bit rough aroudn the edges, but
it works, as you can see on the right.</p>

<p>You can fetch it <a href="/downloads/tagcloud.sh">here</a>.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2007/12/27/gallery_plugin/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2007/12/27/gallery_plugin/index.html</guid>
<title>gallery plugin</title>
<dc:date>2007-12-27T15:29:52+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> nanoblogger, site</dc:subject>
<description><![CDATA[<p>I'm using the <a href="http://www.heirbaut.nl/category/projects/gallery-plugin/">gallery plugin</a>
for the photos on this site. This plugin
works great and easy, but there wasn't an easy way to create a slide
show. So that you can click 'next' and go to the next photo. So I've
added this functionality to the script.</p>

<p>I'm putting this up for download, so that other people may use this
too.  <a href="/downloads/gallery.sh">gallery.sh</a>
If you want to regenerate the slides, you can just remove the <em>html</em> and
<em>slides</em> directories.</p>]]></description>

</item>
</channel>
</rss>

