/** * Build each maths block * * @param array $matches A set of results of the `transform()` function * @return string */ protected function _callback($matches) { $texblock = $matches[1]; $texblock = trim($texblock); $block = Kernel::get('OutputFormatBag')->buildTag('maths_block', $texblock, array()); return "\n\n" . parent::hashBlock($block) . "\n\n"; }
/** * Build `<pre><code>` blocks. * * @param array $matches A set of results of the `transform()` function * @return string */ protected function _callback($matches) { $codeblock = Lexer::runGamut(GamutLoader::TOOL_ALIAS . ':Outdent', $matches[1]); $codeblock = Helper::escapeCodeContent($codeblock); # trim leading newlines and trailing newlines $codeblock = preg_replace('/\\A\\n+|\\n+\\z/', '', $codeblock); $codeblock = Kernel::get('OutputFormatBag')->buildTag('preformatted', $codeblock); return "\n\n" . parent::hashBlock($codeblock) . "\n\n"; }
/** * Process the fenced code blocks * * @param array $matches Results form the `transform()` function * @return string */ protected function _callback($matches) { $language = $matches[2]; $codeblock = Helper::escapeCodeContent($matches[3]); $codeblock = preg_replace_callback('/^\\n+/', array($this, '_newlines'), $codeblock); $attributes = array(); if (!empty($language)) { $attributes['language'] = $language; } $codeblock = Kernel::get('OutputFormatBag')->buildTag('preformatted', $codeblock, $attributes); return "\n\n" . parent::hashBlock($codeblock) . "\n\n"; }
/** * @param string $text * @return string */ public function transform($text) { return preg_replace('{ ^[ ]{0,3} # Leading space ([-*_]) # $1: First marker (?> # Repeated marker group [ ]{0,2} # Zero, one, or two spaces. \\1 # Marker character ){2,} # Group repeated at least twice [ ]* # Tailing spaces $ # End of line. }mx', "\n" . parent::hashBlock(Kernel::get('OutputFormatBag')->buildTag('horizontal_rule')) . "\n", $text); }
/** * Build each blockquote block * * @param array $matches A set of results of the `transform()` function * @return string */ protected function _callback($matches) { $blockq = $matches[1]; $cite = isset($matches[2]) ? $matches[2] : null; // trim one level of quoting - trim whitespace-only lines $blockq = preg_replace('/^[ ]*>[ ]?(\\((.+?)\\))?|^[ ]+$/m', '', $blockq); $blockq = Lexer::runGamut('html_block_gamut', $blockq); # recurse $blockq = preg_replace('/^/m', " ", $blockq); // These leading spaces cause problem with <pre> content, // so we need to fix that: $blockq = preg_replace_callback('{(\\s*<pre>.+?</pre>)}sx', array($this, '_callback_spaces'), $blockq); $attributes = array(); if (!empty($cite)) { $attributes['cite'] = $cite; } $block = Kernel::get('OutputFormatBag')->buildTag('blockquote', $blockq, $attributes); return "\n" . parent::hashBlock($block) . "\n\n"; }
/** * Process ATX-style headers * * @param array $matches The results from the `transform()` function * @return string */ protected function _atx_callback($matches) { $level = strlen($matches[1]) + $this->_getRebasedHeaderLevel(); $domid = !empty($matches[3]) ? $matches[3] : Helper::header2Label($matches[2]); $domid = Kernel::get('DomId')->set($domid); $title = Lexer::runGamut('span_gamut', $matches[2]); Kernel::addConfig('menu', array('level' => $level, 'text' => parent::unhash($title)), $domid); $block = Kernel::get('OutputFormatBag')->buildTag('title', $title, array('level' => $level, 'id' => $domid)); $this->_setContentTitle($title); return "\n" . parent::hashBlock($block) . "\n\n"; }
/** * Turn double returns into triple returns, so that we can make a * paragraph for the last item in a list, if necessary * * @param array $matches The results form the `transform()` method * @return string */ protected function _callback($matches) { // Re-usable patterns to match list item bullets and number markers: $result = trim(self::transformItems($matches[1])); $result = str_replace('<!--dt-->', '', $result); $result = Kernel::get('OutputFormatBag')->buildTag('definition_list', $result); return parent::hashBlock($result) . "\n\n"; }
/** * @param array $matches A set of results of the `transform` function * @return string */ protected function _callback($matches) { $marker_any_re = '(?:' . self::$marker_ul_re . '|' . self::$marker_ol_re . ')'; $list = $matches[1] . "\n"; $list_type = preg_match('/' . self::$marker_ul_re . '/', $matches[4]) ? "unordered" : "ordered"; $marker_any_re = $list_type == "unordered" ? self::$marker_ul_re : self::$marker_ol_re; $list = self::transformItems($list, $marker_any_re); $block = Kernel::get('OutputFormatBag')->buildTag($list_type . '_list', $list); return "\n" . parent::hashBlock($block) . "\n\n"; }
/** * Form HTML tables: parses table contents * * @param array $matches * @return string */ protected function _callback($matches) { $attributes = array(); //self::doDebug($matches); // The head string may have a begin slash $caption = count($matches) > 3 ? $matches[1] : null; $head = count($matches) > 3 ? preg_replace('/^ *[|]/m', '', $matches[2]) : preg_replace('/^ *[|]/m', '', $matches[1]); $underline = count($matches) > 3 ? $matches[3] : $matches[2]; $content = count($matches) > 3 ? preg_replace('/^ *[|]/m', '', $matches[4]) : preg_replace('/^ *[|]/m', '', $matches[3]); // Remove any tailing pipes for each line. $underline = preg_replace('/[|] *$/m', '', $underline); $content = preg_replace('/[|] *$/m', '', $content); // Reading alignement from header underline. $separators = preg_split('/ *[|] */', $underline); foreach ($separators as $n => $s) { $attributes[$n] = array(); if (preg_match('/^ *-+: *$/', $s)) { $attributes[$n]['style'] = 'text-align:right;'; } elseif (preg_match('/^ *:-+: *$/', $s)) { $attributes[$n]['style'] = 'text-align:center;'; } elseif (preg_match('/^ *:-+ *$/', $s)) { $attributes[$n]['style'] = 'text-align:left;'; } } // Split content by row. $headers = explode("\n", trim($head, "\n")); $text = ''; if (!empty($caption)) { $this->table_id = Helper::header2Label($caption); $text .= preg_replace_callback('/\\[(.*)\\]/', array($this, '_doCaption'), Lexer::runGamut('span_gamut', $caption)); } $lines = ''; foreach ($headers as $_header) { $line = ''; // Parsing span elements, including code spans, character escapes, // and inline HTML tags, so that pipes inside those gets ignored. $_header = Lexer::runGamut('filter:Span', $_header); // Split row by cell. $_header = preg_replace('/[|] *$/m', '', $_header); $_headers = preg_split('/[|]/', $_header); $col_count = count($_headers); // Write column headers. // we first loop for colspans $headspans = array(); foreach ($_headers as $_i => $_cell) { if ($_cell == '') { if ($_i == 0) { $headspans[1] = 2; } else { if (isset($headspans[$_i - 1])) { $headspans[$_i - 1]++; } else { $headspans[$_i - 1] = 2; } } } } foreach ($_headers as $n => $__header) { if ($__header != '') { $cell_attributes = $attributes[$n]; if (isset($headspans[$n])) { $cell_attributes['colspan'] = $headspans[$n]; } $line .= Kernel::get('OutputFormatBag')->buildTag('table_cell_head', Lexer::runGamut('span_gamut', trim($__header)), $cell_attributes) . "\n"; } } $lines .= Kernel::get('OutputFormatBag')->buildTag('table_line', $line) . "\n"; } $text .= Kernel::get('OutputFormatBag')->buildTag('table_header', $lines); // Split content by row. $rows = explode("\n", trim($content, "\n")); $lines = ''; foreach ($rows as $row) { $line = ''; // Parsing span elements, including code spans, character escapes, // and inline HTML tags, so that pipes inside those gets ignored. $row = Lexer::runGamut('filter:Span', $row); // Split row by cell. $row_cells = preg_split('/ *[|] */', $row, $col_count); $row_cells = array_pad($row_cells, $col_count, ''); // we first loop for colspans $colspans = array(); foreach ($row_cells as $_i => $_cell) { if ($_cell == '') { if ($_i == 0) { $colspans[1] = 2; } else { if (isset($colspans[$_i - 1])) { $colspans[$_i - 1]++; } else { $colspans[$_i - 1] = 2; } } } } foreach ($row_cells as $n => $cell) { if ($cell != '') { $cell_attributes = $attributes[$n]; if (isset($colspans[$n])) { $cell_attributes['colspan'] = $colspans[$n]; } $line .= Kernel::get('OutputFormatBag')->buildTag('table_cell', Lexer::runGamut('span_gamut', trim($cell)), $cell_attributes) . "\n"; } } $lines .= Kernel::get('OutputFormatBag')->buildTag('table_line', $line) . "\n"; } $text .= Kernel::get('OutputFormatBag')->buildTag('table_body', $lines); $table = Kernel::get('OutputFormatBag')->buildTag('table', $text); return parent::hashBlock($table) . "\n"; }
/** * Process each abbreviation * * @param array $matches One set of results form the `transform()` function * @return string */ protected function _callback($matches) { $abbr = $matches[0]; $abbr_desciptions = Kernel::getConfig('abbr_desciptions'); if (isset($abbr_desciptions[$abbr])) { $attributes = array(); $desc = trim($abbr_desciptions[$abbr]); if (!empty($desc)) { $attributes['title'] = Lexer::runGamut(GamutLoader::TOOL_ALIAS . ':EncodeAttribute', $desc); } $abbr = Kernel::get('OutputFormatBag')->buildTag('abbreviation', $abbr, $attributes); return parent::hashBlock($abbr); } else { return $abbr; } }