Gitit Hacks

This page is for some of the Gitit customizations that I use on this wiki, recorded mainly so I don’t forget how I did them.

Search for Exact Phrases

My wiki contains a checkbox under the search box that enables me to search for exact phrases, which is not a feature of the current development version of Gitit.

If you want to implement exact phrase on your wiki, you can adopt the changes made in this commit.

That commit leaves the search box in its current place, but I found that it looks nicer to move the search box after the “Go” box and add an HTML linebreak after the search box.

Link to Most Recent Revision

By default, when you are looking at a _diff page in Gitit, the “View” tab shows you the preview for the particular revision you are looking at. This can be confusing if a user navigates to a _diff page from the “Recent Activity” page, because often the user may want to jump from the revision directly to the most recent version.

To solve the problem, I modify the default content.st file and move the changed file to the “templates” folder in my Gitit wiki directory. The modified file includes these lines in the $if(revision) block:

$if(revision)$
<h2 class="revision">Revision $revision$ - <a href="$base$$pageUrl$">click here for latest revision</a></h2>
$endif$

Show Incoming Links

In the sidebar, I have a “related” box that includes a link to “Pages That Link to This One.” That link actually just runs a search that looks for any pages containing Markdown links to current page. So it only captures incoming links that use Gitit’s [Page Title]() syntax. Still, it’s better than nothing!

To implement this, I modify the default page template and then put the customized page.st in my templates folder. The customized template includes these lines at the end of the body tag:

<div class="pageTools">
      <fieldset>
      <legend>Related</legend>
        <ul>
         <li><script type="text/javascript">
         var e=encodeURIComponent('$pagetitle$'); document.write('<a href="http://wiki.wcaleb.rice.edu/_search?patterns=%5B'+e+'%5D%28%29&search=Search&exactphrase=on">Pages linking to this one</a>');
         </script><noscript>Javascript not supported</noscript>
         </li>
         <li><script type="text/javascript">
         var e=encodeURIComponent('$pagetitle$'); document.write('<a href="http://wcaleb.rice.edu/omeka/search?query='+e+'&query_type=keyword&record_types%5B%5D=Item&record_types%5B%5D=File">Search for title in Omeka archive</a>');
         </script><noscript>Javascript not supported</noscript>
         </li>
        </ul>
      </fieldset>
</div>

The first list item is the one that creates the incoming links search. It first URL encodes the page title, using a built-in Gitit variable, and then creates the URL that searches for Markdown links to that page.

Note that the exactphrase on parameter is not native to Gitit and works only on my Gitit fork. It’s not required, so you can remove it from the URL, but it does make the search work better. Otherwise, if you search for [Page Title](), Gitit will actually be searching for either [Page or Title]().

The second list item performs a search on another website using the page title, which demonstrates how this method can be used to create other custom links.

Automatically Generate Discuss Pages

I wanted to put Disqus comment boxes on all of the “discuss” pages on my wiki. But Gitit doesn’t create these “discuss” pages (whose names are preceded by an @ symbol) by default. So I wrote a little bash script that automatically creates a “discuss” page whenever a new page is created:

#!/bin/sh

git diff-index --name-status --cached HEAD | grep -v ^D | cut -c3- |\
while read -r f
do
    if [[ "$f" == *.page && ! -f @"$f" ]]; then
            cat /Users/wcm1/Scripts/disqus.html > @"$f"
            git add @"$f"
    fi
done

I save that file as pre-commit in the .git/hooks directory of my wikidata repo. Every time I “commit” at the command line, this script runs, creates any needed new discussion pages, and adds them to the commit before actually committing.

Hide/Show Surrounding Lines in Diff Page

GitHub has a nice feature when looking at diffs. By default, it hides unchanged lines, and you can then click on an accordion icon to expand the context in diffs.

I’ve implemented something similar (though nowhere near as fancy) on my diff pages using this javascript, which is modeled on a script by Andy Langton:

$(document).ready(function() {

// choose text for the show/hide link - can contain HTML (e.g. an image)

// initialise the visibility check
var is_visible = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('pre.diff span').addClass('toggle')
$('.added').removeClass('toggle');
$('.deleted').removeClass('toggle');
$('.toggle').before('<p>(<a href="#" class="toggleLink">show more lines</a>)</p>');

// hide all of the elements with a class of 'toggle'
$('.toggle').hide();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
is_visible = !is_visible;

// toggle the display - uncomment the next line for a basic "accordion" style
//$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().next('.toggle').slideToggle('slow');

// change the link depending on whether the element is shown or hidden
$(this).parent().hide()

// return false so any link destination is not followed
return false;

});
});

I save that javascript in my static/js directory, and then add a link to it at the bottom of the page.st template. That means it technically gets linked to every page, whether it’s a diff page or not. This is not ideal but it’s the easiest way I’ve found so far to do it.

Post Updates to Twitter

I wrote a Python script that uses GitPython and python-twitter to post a tweet about changed files whenever I push to the remote server. The script is run by a post-receive hook in the remote git repository. Here’s the Gist, and here’s a problem I had getting it to work.

Post Updates to Micro.blog

Since leaving Twitter and starting a new updates site using Micro.blog, I wrote a new bash script to publish updates from Gitit to Micro.blog using Micropub.