Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created March 13, 2016 18:26
Show Gist options
  • Save joshuaadickerson/5288841b1036615a6d05 to your computer and use it in GitHub Desktop.
Save joshuaadickerson/5288841b1036615a6d05 to your computer and use it in GitHub Desktop.
<?php
$message = $parser->parse($message);
function captureTrackedContent($message, $code, $pos_padding = 0)
{
$start = $pos_padding + $code[Codes::TRACKED_CONTENT]['start'];
$end = $pos_padding + isset($code[Codes::TRACKED_CONTENT]['end']) ? $code[Codes::TRACKED_CONTENT]['end'] : strlen($message);
$content = substr($message, $start, $end - $start);
return $content;
}
function handleFootnotes(&$message, $parser)
{
$list = '<ol class="footnotes">';
$pos_padding = 0;
foreach ($parser->getTrackedContent('footnote') as $i => $code)
{
$start = $pos_padding + $code[Codes::TRACKED_CONTENT]['start'];
$end = $pos_padding + isset($code[Codes::TRACKED_CONTENT]['end']) ? $code[Codes::TRACKED_CONTENT]['end'] : strlen($message);
$content = substr($message, $start, $end - $start);
$list .= '<li id="footnote' . $i . '>' . $content . '</li>';
$insert = '<sup class="bbc_footnotes">' . $i . '</sup>';
$message = substr_replace($message, $insert, $start, 0);
$pos_padding += strlen($insert);
}
$list .= '</ul>';
}
function handleTableOfContents($message, $parser)
{
$toc = '<div class="toc">';
$level = -1;
foreach ($parser->getTrackedContent('h') as $i => $code)
{
$content = captureTrackedContent($message, $code);
$last_level = $level;
// getEquals() has to be implemented in some way
$level = $code->getEquals();
// This is a child node
if ($level > $last_level)
{
$toc .= '<li><ul>' . $content. '</ul></li>';
}
else
{
$toc .= '<li>' . $content . '</li>';
}
}
$toc .= '</div>';
$toc_len = strlen($toc);
$pos_padding = 0;
foreach ($parser->getTrackedContent('toc') as $code)
{
// Just need the start pos to know where to insert it.
$start = $pos_padding + $code[Codes::TRACKED_CONTENT]['start']
// Insert the toc in the message
$message = substr_replace($message, $toc, $start, 0);
// Increase the pos by the count
$pos_padding += $toc_len;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment