Created
April 30, 2011 13:52
-
-
Save nfreear/949687 to your computer and use it in GitHub Desktop.
Display Bitbucket wiki pages with SimpleWiki parser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/**Display Bitbucket wiki pages with SimpleWiki parser. | |
* A rough script for refactoring. | |
* | |
* @copyright Copyright Nick Freear, 29 April 2011. | |
* @uses http://simplewiki.org/language#patterns | |
*/ | |
ini_set('display_errors', 1); | |
header('Content-Type: text/html; charset=utf-8'); | |
// Configuration. | |
$wiki_path = 'cloude-wiki'; | |
$project_url = 'https://bitbucket.org/cloudengine/cloudengine'; | |
$Sym_link= 'Wiki'; #'Local'; | |
$Sym_url = '/w/swiki.php?f='; //'http://getcloudengine.org//wiki/'; | |
$Rel_link= 'Rel'; | |
$Rel_url = "$project_url/wiki/"; | |
$File_link='File'; | |
$File_url= "https://bytebucket.org/cloudengine/cloudengine/wiki/assets/"; | |
$Mail_link='Email'; | |
$Mail_url ='mailto:'; | |
$file = isset($_GET['f']) ? $_GET['f'] : 'Home'; | |
$is_wiki = false; | |
$path = "$wiki_path/".str_replace(' ', '_', $file); | |
if (false===strpos($file, '.')) { | |
$path .= ".wiki"; | |
$is_wiki = true; | |
} | |
echo "<title>*wiki / $file - SimpleWiki</title>".PHP_EOL; | |
if (!file_exists($path)) { | |
die("Error, $file.wiki"); | |
} | |
$markup= file_get_contents($path); | |
$stat = stat($path); | |
// Pre-parsing - emails, '..' links, internal links, and image assets. | |
#$markup = preg_replace('#\[\[mailto:([^\]]+?)\]\]#ms', "[[$Mail_link:\$1]]", $markup); | |
$markup = preg_replace('#\[\[(\.+\/[^:]+?)\]\]#ms', "[[$Rel_link:\$1]]", $markup); | |
$markup = preg_replace('#\[\[([^:]+?)\]\]#ms', "[[$Sym_link:\$1]]", $markup); | |
#$markup = preg_replace('#{{assets\/([^}]+)}}#ms', "{\{$File_link:\$1}}", $markup); | |
function __autoload($class_name) { | |
require_once('simplewikiRC1.02/'. strtolower($class_name) .'.php'); | |
} | |
#http://confluence.atlassian.com/display/BITBUCKET/Linking+to+an+Issue+from+a+Bitbucket+Wiki+Page | |
#http://groups.google.com/group/bitbucket-users/browse_thread/thread/ea8229218861a304/104f86a5c276d0ff | |
function _bitbucket_macros($node) { | |
global $project_url; | |
#var_dump($node->type, $node->macroname, $node->decoration->classes[0]); | |
$value_0 = $issue_num = $node->decoration->classes[0]; | |
switch ($node->macroname) { | |
case 'issue': | |
$issue_num = $value_0; | |
$node->output =<<<EOF | |
<a href="$project_url/issue/$issue_num" class="wik issue" title="View bug or enhancement in the CloudEngine tracker">Issue #$issue_num</a> | |
EOF; | |
break; | |
case 'issues': //Fall-through. | |
case 'query': | |
$value_1 = $issue_num = $node->decoration->classes[1]; | |
$node->output =<<<EOF | |
<a href="$project_url/issues$value_0#!$value_1" class="wik $node->macroname" title="$node->macroname $value_0 $value_1">Query the issue tracker</a> | |
EOF; | |
break; | |
case 'file': | |
// Doesn't support revisions/ line-numbers at present. | |
$node->output =<<<EOF | |
<a href="$project_url/src/tip/$value_0#cl-1" class="wik $node->macroname" title="Browse source code on Bitbucket">$value_0</a> | |
EOF; | |
break; | |
case 'changeset': | |
$node->output =<<<EOF | |
<a href="$project_url/changeset/$value_0" class="wik $node->macroname" title="View a changeset on Bitbucket">Changeset:$value_0</a> | |
EOF; | |
break; | |
case 'user': | |
$node->output =<<<EOF | |
<a href="https://bitbucket.org/$value_0" class="wik $node->macroname" title="User page">Person:$value_0</a> | |
EOF; | |
break; | |
default: | |
$node->output =<<<EOF | |
<tt title="Macro not processed" class="no wik $node->macroname"><<$node->macroname $value_0>></tt> | |
EOF; | |
break; | |
} | |
$node->processed = true; | |
return $node; | |
} | |
$fn = '_bitbucket_macros'; | |
$macro_callbacks = array( | |
'issue' => $fn, | |
'issues'=> $fn, | |
'query' => $fn, | |
'milestone'=>$fn, | |
'changeset'=>$fn, | |
'src' => $fn, | |
'file' => $fn, | |
'toc' => $fn, | |
'user' => $fn, | |
/*float left|right*/ ); | |
$symlinks = array( | |
$Sym_link => $Sym_url, | |
$Rel_link => $Rel_url, | |
$File_link=> $File_url, | |
$Mail_link=> $Mail_url ); | |
if ($is_wiki) { | |
$wiki = new SimpleWiki(); | |
$wiki->register_symlinks($symlinks); | |
$wiki->register_macro_callbacks($macro_callbacks); | |
$html = $wiki->get_html($markup); | |
} else { | |
$html = $markup; | |
} | |
echo date('c', $stat['mtime']) | |
." <a href='$Rel_url./$file' title='Original on the Wiki'>$file</a> $html"; | |
/*[1] => object(SimpleWiki_DocNode)#54 (8) { | |
["parent"] => *RECURSION* | |
["children"] => array(0) {} | |
["type"] => string(5) "macro" | |
["macroname"] => string(5) "issue" | |
["decoration"] => object(stdClass)#55 (4) { | |
["classes"] => array(1) { [0] => string(2) "62" } | |
["properties"] => array(0) {} | |
["attributes"] => array(0) {} | |
["markup"] => NULL | |
} | |
["caption"] => string(0) "" | |
["processed"] => bool(false) | |
["output"] => string(0) "" | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment