← Switched from WordPress to Blogofile | Excluding integration tests in NUnit's GUI, part 1 - Using categories →

How to display Markdown files from other sites...this time in Blogofile

| tags: markdown, blogofile, php

This is a follow-up to How to display Markdown files from other sites in WordPress.

When I migrated this site from WordPress to Blogofile, I had the same problem again: How do I display Markdown files from external URLs on Blogofile pages?

Based on the previous solution I found for WordPress, I found a similar one that works for me.

Warning: It certainly doesn’t work for everyone, because it involves using PHP on the web server.
Yes, this somehow defeats the purpose of a static site generator.
But the project pages are the only ones that I actually want to be dynamic…I don’t want to update the site manually each time I edit one of my project readme files.

There are probably other ways to do this, like loading the content on the client with JavaScript.
But I’m not a JavaScript guru, my server runs PHP and I already know how to do it in PHP…so I’ll stay with PHP for now.

The code is nearly the same that I used for the shortcode in WordPress (load Markdown file from URL and use PHP Markdown to convert to HTML), only this time I put it into a new PHP file:

<?php

include_once "markdown.php";

function GetMarkdown($url) {

    $result = @file_get_contents($url);

    if ($result === false)
    {
        return "Could not load $url";
    }
    else
    {
        return Markdown($result);
    }
    
}

?>

The markdown.php file, which contains PHP Markdown, needs to be in the same folder.

To put this into a Blogofile page, I created a new Mako template with the extension .php.mako (so it will be a .php file after compiling) and just inserted this PHP code instead of the “normal” content:

<?php
include_once "../php/md-include.php";
echo GetMarkdown("https://bitbucket.org/christianspecht/bitbucket-backup/raw/tip/readme-full.md");
?>

The rest of the template looks exactly like a normal Mako template (<%inherit file="site.mako" />, etc.).
When compiling, Blogofile creates the page as usual, only that it has the above PHP code as the content, which will be executed by the web server on each request.

Again, you can see it in action on all the project pages.
(example: raw Markdown filefinal project page)


← Switched from WordPress to Blogofile | Excluding integration tests in NUnit's GUI, part 1 - Using categories →