Fix typo preventing canonicalization.
[xhtml-compiler.git] / XHTMLCompiler / Markup / Markdown.php
blob61a912daa4cbc9f3294fcc9cf04e21849b071507
1 <?php
3 class XHTMLCompiler_Markup_Markdown extends XHTMLCompiler_TextFilter
6 var $name = 'Markdown';
8 public function process($text, $page, $manager) {
9 // This code assumes that the input is \n. I don't the code actually
10 // handles this right now.
11 $title = "Untitled";
12 $bits = explode("\n", $text, 3);
13 if (isset($bits[2])) {
14 if (trim($bits[0]) === '---') {
15 // this means we've got YAML: find the next --- line
16 $end = strpos($text, "---\n", 4);
17 $yaml = substr($text, 4, $end - 4);
18 $data = Spyc::YAMLLoad($yaml);
19 $title = $data['title'];
20 $text = substr($text, $end + 4);
21 } elseif (strlen($bits[1]) > 0 && trim($bits[1], '=') === '') {
22 // parse out a leading header, which we will treat as the header
23 // (this is the only bit of metadata we're going to do)
24 $text = $bits[2];
25 $title = $bits[0];
29 $body = Markdown($text);
30 // perform template search
31 $dir = $page->getDir();
32 $template = $dir->getFile('template.xml');
33 while (!$template->exists()) {
34 $dir = $dir->getParent();
35 if (!$dir->isAllowed()) break;
36 $template = $dir->getFile('template.xml');
38 if (!$template->exists()) {
39 // maybe a cheap copout, maybe an exception
40 throw new XHTMLCompiler_Exception(500, 'No template file',
41 'Markup snippet had no template file to inline into.');
43 $html = $template->get();
44 $html = str_replace('{body}', $body, $html);
45 $html = str_replace('{title}', $title, $html);
46 return $html;