Quantum Forest logs are written by Luis A. Apiolaza in Christchurch, New Zealand.
« previous post · next post »
It is very easy to get the HTML version of Markdown text using any of the online processors (e.g., original Perl markdown dingus and PHP markdown dingus). However, lot of the time I am not online and I do not want to have a web server running in my machine to run these scripts. I think that the easiest way to work is just to run a script in the command line to transform the text.
The options were:
I downloaded PHP (and installed it), markdown.php and smartypants.php (from the PHP markdown web site to support em-dashes and curly quotes). Then I wrote my first ever PHP script to read a text file, convert it using markdown and smartypants, and finally add body and html tags to obtain a full html document rather than an HTML snippet.
The glorious—crappy, I know—code is below:
<?php
include_once "markdown.php";
include_once "smartypants.php";
$infile_name = $argv[1];
$outfile_name = $argv[2];
// Reading input file in Markdown markup
$in_handler = fopen($infile_name, "r");
$markdown_text = fread($in_handler, filesize($infile_name));
fclose($in_handler);
// Transforming Markdown to HTML and then using Smartypants
// to change quotes and dashes.
$html_text = Markdown($markdown_text);
$html_text = SmartyPants($html_text);
// Adding html and body tags to Markdown output
// to get complete html page
$top = "<html>\n<body>\n";
$bottom = "\n</body>\n</html>";
$full_html = $top . $html_text . $bottom;
// Saving output file using HTML markup
$out_handler = fopen($outfile_name, "w");
fwrite($out_handler, $full_html);
fclose($out_handler);
?>
The script runs fine, and I should expand it to automatically accept a single file (say file.txt) and transform the output to file.html. I will also add recognition for meta tags at the beginning of the file (e.g., author and title) using a flag when calling the script. Writing this small PHP script was very easy, even starting with zero PHP knowledge.
Currently I run the script using php.exe myscript.php infile.txt outfile.html, where php.exe is the console (not web) version of the interpreter. However, it would be possible to run it as myscript.php infile.txt outfile.html if one includes #!c:\php\php.exe as the first line of the script.
PS. A few minutes later. I did have quite a few problems to post this PHP code, mainly because of the insistence of Textpattern to, first, parse the code in Textile to ‘beautify it’ and then Textpattern wanted to process it! Workarounds: indent the code lines by two spaces to avoid Textile touching it and, second, use the HTML entities for lower than (<) and greater than (>) to signal the start and the end of the script.
del.icio.us tags: textpattern markdown php.
אאא