mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
ENHANCEMENT Started code block parser (still needs work)
This commit is contained in:
parent
e1da17e9d9
commit
7516d31ce0
@ -1,9 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for MarkdownUltra parsing in the template and related functionality for
|
|
||||||
* parsing paths and documents
|
|
||||||
*
|
|
||||||
* @package sapphiredocs
|
* @package sapphiredocs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -32,21 +28,53 @@ class DocumentationParser {
|
|||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
public static function parse(DocumentationPage $page, $baselink = null) {
|
public static function parse(DocumentationPage $page, $baselink = null) {
|
||||||
require_once('../sapphiredocs/thirdparty/markdown.php');
|
|
||||||
|
|
||||||
$md = $page->getMarkdown();
|
$md = $page->getMarkdown();
|
||||||
|
|
||||||
// Pre-processing
|
// Pre-processing
|
||||||
$md = self::rewrite_image_links($md, $page);
|
$md = self::rewrite_image_links($md, $page);
|
||||||
$md = self::rewrite_relative_links($md, $page, $baselink);
|
$md = self::rewrite_relative_links($md, $page, $baselink);
|
||||||
$md = self::rewrite_api_links($md, $page);
|
$md = self::rewrite_api_links($md, $page);
|
||||||
|
$md = self::rewrite_code_blocks($md, $page);
|
||||||
|
|
||||||
|
require_once('../sapphiredocs/thirdparty/markdown.php');
|
||||||
$html = Markdown($md);
|
$html = Markdown($md);
|
||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function rewrite_code_blocks($md) {
|
||||||
|
// $tabwidth = (defined('MARKDOWN_TAB_WIDTH')) ? MARKDOWN_TAB_WIDTH : 4;
|
||||||
|
// $md = preg_replace_callback('{
|
||||||
|
// (?:\n\n|\A\n?)
|
||||||
|
// [ ]*(\{[a-zA-Z]*\})? # lang
|
||||||
|
// [ ]* \n # Whitespace and newline following marker.
|
||||||
|
// ( # $1 = the code block -- one or more lines, starting with a space/tab
|
||||||
|
// (?>
|
||||||
|
// [ ]{'.$tabwidth.'} # Lines must start with a tab or a tab-width of spaces
|
||||||
|
// .*\n+
|
||||||
|
// )+
|
||||||
|
// )
|
||||||
|
// ((?=^[ ]{0,'.$tabwidth.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
|
||||||
|
// }xm',
|
||||||
|
// array('DocumentationParser', '_do_code_blocks'), $md);
|
||||||
|
//
|
||||||
|
// return $md;
|
||||||
|
// }
|
||||||
|
// static function _do_code_blocks($matches) {
|
||||||
|
// $tabwidth = (defined('MARKDOWN_TAB_WIDTH')) ? MARKDOWN_TAB_WIDTH : 4;
|
||||||
|
// $codeblock = $matches[2];
|
||||||
|
//
|
||||||
|
// // outdent
|
||||||
|
// $codeblock = preg_replace('/^(\t|[ ]{1,'.$tabwidth.'})/m', '', $codeblock);
|
||||||
|
// $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
|
||||||
|
//
|
||||||
|
// # trim leading newlines and trailing newlines
|
||||||
|
// $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
|
||||||
|
//
|
||||||
|
// $codeblock = "<pre><code>$codeblock\n</code></pre>";
|
||||||
|
// return "\n\n".$this->hashBlock($codeblock)."\n\n";
|
||||||
|
// }
|
||||||
|
|
||||||
static function rewrite_image_links($md, $page) {
|
static function rewrite_image_links($md, $page) {
|
||||||
// Links with titles
|
// Links with titles
|
||||||
$re = '/
|
$re = '/
|
||||||
|
@ -4,6 +4,24 @@
|
|||||||
*/
|
*/
|
||||||
class DocumentationParserTest extends SapphireTest {
|
class DocumentationParserTest extends SapphireTest {
|
||||||
|
|
||||||
|
// function testRewriteCodeBlocks() {
|
||||||
|
// $page = new DocumentationPage(
|
||||||
|
// 'test.md',
|
||||||
|
// new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'),
|
||||||
|
// 'en',
|
||||||
|
// '2.4'
|
||||||
|
// );
|
||||||
|
// $result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
|
||||||
|
// $expected = <<<HTML
|
||||||
|
// <pre class="brush: php">
|
||||||
|
// code block
|
||||||
|
// with multiple
|
||||||
|
// lines
|
||||||
|
// </pre>
|
||||||
|
// HTML;
|
||||||
|
// $this->assertContains($expected, $result);
|
||||||
|
// }
|
||||||
|
|
||||||
function testImageRewrites() {
|
function testImageRewrites() {
|
||||||
// Page on toplevel
|
// Page on toplevel
|
||||||
$page = new DocumentationPage(
|
$page = new DocumentationPage(
|
||||||
|
@ -10,3 +10,13 @@ test
|
|||||||
[link: http](http://silverstripe.org)
|
[link: http](http://silverstripe.org)
|
||||||
[link: api](api:DataObject)
|
[link: api](api:DataObject)
|
||||||
[api:DataObject::$has_one]
|
[api:DataObject::$has_one]
|
||||||
|
|
||||||
|
:::php
|
||||||
|
code block
|
||||||
|
with multiple
|
||||||
|
lines
|
||||||
|
|
||||||
|
Normal text after code block
|
||||||
|
|
||||||
|
code block
|
||||||
|
without formatting prefix
|
Loading…
Reference in New Issue
Block a user