Markdown for everything

by Dave Dash 28Apr06

Today, in a matter of minutes, I wrote a really simple markdown web environment in PHP. It's nothing complicated by any stretch of the imagination, but for me at least it'll be quite useful.

My intro to markdown

[symfony] had me seriously investigating [Markdown] for use in my own apps. The [symfony] Team has made a lot of design choices which for the most part seemed to ones that I liked. [Markdown] was interesting because my first thought was "that's all fine and dandy, but I think I'll stick with a [TinyMCE] editor instead."

But, that's a bit JavaScript heavy, and a bit much for comment fields, for example, or even an entire blog post. And after reading some posts from Daring Fireball over the years, I've started to take notice of its elegant format, and the captivating writing style. Obviously this was all the doing of [Markdown]... or not, but it served as a good selling point.

A month ago, I decided to start writing --- not my boring day-to-day journal entries, but my more web related musings. I do a lot of coding that seemingly goes nowhere[1], but I generally learn some very cool things. So I wanted to start documenting. I was writing for the web, but I really didn't feel like writing things out was saving me any time, so I decided to take another peek at [Markdown].

I was sold pretty quickly, and with [TextMate] having support, I was sold right away. There's something comforting about having easy to write code, that didn't require me to write the same URLs over and over and generated consistent XHTML. They even had a plugin for it for [Drupal].

Building the markdown environment

For an article I was writing, I needed to put up some filler pages really quickly. I didn't need an elegant environment, so even plain HTML would do. I wasn't looking forward to writing plain boring XHTML though...

So I took some mod_rewrite and a Michael Fortin's PHP port of [Markdown] and had all my web requests for a folder go through a Markdown processor.

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

snippet of PHP:

require('markdown.php');
$q = $_GET['q'] ? $_GET['q'] : 'index';
$file = $q . '.markdown';
if (is_file($file)) {
    $text = Markdown(file_get_contents($file));
} else {
    $text = 'file not found';
}
// ... some fancy decoration and then we just echo $text

Not too much going on here. The script just looks for a relevant .markdown file and converts it to XHTML and displays it.

Give it a try

Again, it's not incredibly useful, but it saves me time. It would probably take you less time to write this than to download it, but who cares: markdown_everywhere-2006-04-28.tar.bz. Enjoy[2].

  1. Over the last few years I've tried to write (or rewrite) a yearbook site, a random sentence generator, a photo sharing site, a scheduling software, some google map enabled toys and some school related applications.
  2. Note that this includes PHP-Markdown which is copywrited by others.


Where am I?

This is a single entry in the weblog.

"Markdown for everything" is filed under programming. It was published in April 2006.

April 2006
M T W T F S S
    May »
 12
3456789
10111213141516
17181920212223
24252627282930

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.

 

Further Help

If you require more hands on assistance, we do offer affordable hands on support.

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.