mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-12 09:44:27 +02:00
Converted chapters to markdown and changed pages to vertical layout
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Apprentice;
|
||||
|
||||
use Parsedown;
|
||||
|
||||
/**
|
||||
* Handles building pages in public folder
|
||||
* using configuration based on Page classes
|
||||
@@ -95,13 +97,15 @@ class Build
|
||||
*/
|
||||
private function buildPage(Page $page): string
|
||||
{
|
||||
if (!empty($page->code)) {
|
||||
if (!file_exists(config('code_dir') . '/' . $page->code)) {
|
||||
throw new \Exception('Code file not found: ' . $page->code);
|
||||
if (!empty($page->chapter)) {
|
||||
if (!file_exists(config('chapter_dir') . '/' . $page->chapter)) {
|
||||
throw new \Exception('Code file not found: ' . $page->chapter);
|
||||
}
|
||||
|
||||
$code = file_get_contents(config('code_dir') . '/' . $page->code);
|
||||
$page->variables['code'] = $code;
|
||||
$parser = new Parsedown();
|
||||
|
||||
$content = file_get_contents(config('chapter_dir') . '/' . $page->chapter);
|
||||
$page->variables['chapter'] = $parser->text($content);
|
||||
}
|
||||
|
||||
if ($page->template) {
|
||||
|
14
src/Page.php
14
src/Page.php
@@ -23,12 +23,12 @@ class Page
|
||||
public $template;
|
||||
|
||||
/**
|
||||
* Name of code file to load as table
|
||||
* Name of chapter file to load and parse
|
||||
* Will be skipped if none is provided
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $code;
|
||||
public $chapter;
|
||||
|
||||
/**
|
||||
* Map of data to passed to template
|
||||
@@ -42,18 +42,18 @@ class Page
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $template
|
||||
* @param string|null $code
|
||||
* @param string|null $chapter
|
||||
* @param array $variables
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
?string $template = null,
|
||||
?string $code = null,
|
||||
?string $chapter = null,
|
||||
?array $variables = []
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->template = $template;
|
||||
$this->code = $code;
|
||||
$this->chapter = $chapter;
|
||||
$this->variables = $variables;
|
||||
}
|
||||
|
||||
@@ -69,9 +69,9 @@ class Page
|
||||
public static function create(
|
||||
string $name,
|
||||
?string $template = null,
|
||||
?string $code = null,
|
||||
?string $chapter = null,
|
||||
?array $variables = []
|
||||
): Page {
|
||||
return new static($name, $template, $code, $variables);
|
||||
return new static($name, $template, $chapter, $variables);
|
||||
}
|
||||
}
|
||||
|
@@ -61,65 +61,6 @@ function icon(string $name): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes string of PHP code and converts it into html for display
|
||||
*
|
||||
* @param string $code
|
||||
* @return string
|
||||
*/
|
||||
function code_table(string $code): string {
|
||||
$tokens = token_get_all($code);
|
||||
$output = '<div class="grid-code">' .
|
||||
'<div class="doc"></div>' .
|
||||
'<div class="code">' .
|
||||
'<pre>' .
|
||||
'<code class="language-php">';
|
||||
|
||||
$previousTokenWasComment = false;
|
||||
foreach ($tokens as $token) {
|
||||
if (is_string($token)) {
|
||||
$output .= $token;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $token[0];
|
||||
$text = $token[1];
|
||||
|
||||
if ($id == T_COMMENT || $id == T_DOC_COMMENT) {
|
||||
$text = htmlspecialchars(trim(str_replace('/', '', $text)));
|
||||
|
||||
if ($previousTokenWasComment) {
|
||||
$output .= ' ' . $text;
|
||||
} else {
|
||||
$output .= '</code>' .
|
||||
'</pre>' .
|
||||
'</div>' .
|
||||
'<div class="doc">' .
|
||||
$text;
|
||||
}
|
||||
|
||||
$previousTokenWasComment = true;
|
||||
} else {
|
||||
if ($previousTokenWasComment) {
|
||||
$output .= '</div>' .
|
||||
'<div class="code">' .
|
||||
'<pre>' .
|
||||
'<code class="language-php">';
|
||||
}
|
||||
|
||||
$output .= htmlspecialchars($text);
|
||||
$previousTokenWasComment = false;
|
||||
}
|
||||
}
|
||||
|
||||
$output .= '</code>' .
|
||||
'</pre>' .
|
||||
'</div>' .
|
||||
'</div>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads config file into a global
|
||||
*
|
||||
|
Reference in New Issue
Block a user