<?php
-const OPEN = true;
+const OPEN = true;
const CLOSE = false;
class CodeGenerator
* Iterate through all folders and subfolders and make copy of files from
* $fromFolder to $toFolder.
*/
- function copyRecursive(string $fromFolder, string $toFolder)
+ public function copyRecursive(string $fromFolder, string $toFolder)
{
foreach ($i = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($fromFolder, \RecursiveDirectoryIterator::SKIP_DOTS),
/**
* Will delete a given folder and all its contents
*/
- function deleteRecursive(string $dir)
+ public function deleteRecursive(string $dir)
{
- if (empty($dir))
+ if (empty($dir)) {
return;
+ }
if (file_exists($dir)) {
foreach ($i = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
) as $file) {
- if ($file->isDir())
+ if ($file->isDir()) {
rmdir($file);
- else
+ } else {
unlink($file);
+ }
}
rmdir($dir);
/**
* Create a folder structure for a given list of pages
*/
- function generateFoldersForPages(array $folderList, $basePath)
+ public function generateFoldersForPages(array $folderList, $basePath)
{
foreach ($folderList as $folder) {
- if ($folder == '/')
+ if ($folder == '/') {
mkdir($basePath . "/index");
- else
+ } else {
mkdir($basePath . '/' . $folder);
+ }
}
}
/**
* Create or append to a file ($filePath) the text in $content
*/
- function writeToFile(string $filePath, string | null $content)
+ public function writeToFile(string $filePath, string | null $content)
{
- if (is_null($content))
+ if (is_null($content)) {
return;
+ }
file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX);
}
/**
* returns a string with the var name encoded to be set later with setTemplateVar()
*/
- function templateVar(string $name): string
+ public function templateVar(string $name): string
{
return "$$" . $name . "$$";
}
/**
* Replace a template variable with a given value and return the new content string
*/
- function setTemplateVar(string $filePath, string $var, string $value)
+ public function setTemplateVar(string $filePath, string $var, string $value)
{
$replaceVar = "$$" . $var . "$$";
- if (file_exists($filePath))
+ if (file_exists($filePath)) {
return str_replace($replaceVar, $value, file_get_contents($filePath));
- else
+ } else {
return str_replace($replaceVar, $value, $filePath);
+ }
}
/**
* Returns a string with a span tag with a title set
*/
- function spanTitle(string $title, string $text, array | null $class): string
+ public function spanTitle(string $title, string $text, array | null $class): string
{
$html = "<span";
- if (!is_null($class))
+ if (!is_null($class)) {
$html .= ' class="' . implode(' ', $class) . '"';
+ }
$html .= " title='$title'>$text</span> ";
/**
* Returns a string with an anchor tag
*/
- function a(string $url, string $text, string | null $title, array | null $class): string
+ public function a(string $url, string $text, string | null $title, array | null $class): string
{
$html = "<a";
- if (!is_null($class))
+ if (!is_null($class)) {
$html .= ' class="' . implode(' ', $class) . '"';
+ }
$html .= " href='$url'";
- if (!is_null($title))
+ if (!is_null($title)) {
$html .= " title='$title'";
+ }
$html .= ">$text</a> ";
/**
* Returns a string with an image tag
- *
+ *
* @param string $src - path to the image
* @param string $alt - alt text for the image
* @param string | null $title - title for the image
* @param string | null $id - id for the image, if $url is not null used for the anchor tag else for the image tag
* @param array | null $class - array of classes for the image
*/
- function img(string $src, string $alt, string | null $title, string | null $url, string | null $id, array | null $class): string
+ public function img(string $src, string $alt, string | null $title, string | null $url, string | null $id, array | null $class): string
{
$html = "";
- if (!is_null($url))
- $html .= "<a href='$url' id='$id'>";
+ if (!is_null($url)) {
+ $html .= "<a href='$url' id='$id'>";
+ }
$html .= "<img";
}
$html .= " src='$src' alt='$alt'";
-
- if (!is_null($title))
+
+ if (!is_null($title)) {
$html .= " title='$title'";
+ }
- if (is_null($url) && !is_null($id))
+ if (is_null($url) && !is_null($id)) {
$html .= " id='$id'";
+ }
$html .= "/>";
/**
* Forms the html for a given array of data
- *
- * @param array $data
- * - array of arrays with page data.
+ *
+ * @param array $data
+ * - array of arrays with page data.
* - Keys expected: section, text, url, title, class
* @param string $lang - TODO: LANGUAGE NOT IMPLEMENTED
* - language of the page
* - will try to get localized strings (section-lang, text-lang, title-lang....)
- *
+ *
* @return string the html formed from the given data with the given language or the default language if given blank or missing
*/
- function form_html(array $data, string $lang = null): string
+ public function form_html(array $data, string $lang = null): string
{
- $html = "";
- $is_p_closed = true;
+ $html = "";
+ $is_p_closed = true;
$is_list_closed = true;
- $is_div_closed = true;
-
+ $is_div_closed = true;
foreach ($data as $i => $line) {
$section = $this->str_val_or_empty($line, 'section');
- $text = $this->str_val_or_empty($line, 'text');
- $url = $this->str_val_or_empty($line, 'url');
- $title = $this->str_val_or_empty($line, 'title');
- $class = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class')));
+ $text = $this->str_val_or_empty($line, 'text');
+ $url = $this->str_val_or_empty($line, 'url');
+ $title = $this->str_val_or_empty($line, 'title');
+ $class = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class')));
// if new-col close p and ul and open div with col class
if (in_array("new-col", $class)) {
$is_div_closed = false;
}
-
switch ($class) {
case in_array("list", $class):
// if previous iteration was diferent section close li
// if class not empty add class
$filtered_classes = $this->filter_meta_classes($class);
- if (!empty($filtered_classes))
+ if (!empty($filtered_classes)) {
$html .= ' class="' . implode(' ', $filtered_classes) . '"';
+ }
$html .= '>';
} elseif (!$is_p_closed && !$open) {
// if class not empty and text empty add class
$filtered_classes = $this->filter_meta_classes($class);
- if (empty($text) && !empty($filtered_classes))
+ if (empty($text) && !empty($filtered_classes)) {
$html .= ' class="' . implode(' ', $filtered_classes) . '"';
+ }
$html .= '>';
} elseif (!$is_list_closed && !$open) {
$html .= '<h2';
// if text is empty add class, title and url
$filtered_classes = $this->filter_meta_classes($class);
- if (empty($text) && !empty($filtered_classes))
+ if (empty($text) && !empty($filtered_classes)) {
$html .= ' class="' . implode(' ', $filtered_classes) . '"';
+ }
+
$html .= '>';
- if (empty($text) && !empty($url))
+ if (empty($text) && !empty($url)) {
$html .= $this->a($url, $section, $title, $filtered_classes);
- elseif (empty($text) && !empty($title))
+ } elseif (empty($text) && !empty($title)) {
$html .= $this->spanTitle($title, $section, $filtered_classes);
- else
+ } else {
$html .= $section;
+ }
$html .= '</h2>';
}
return array_diff($class, $meta_classes);
}
-
private function form_content(string $text, array $class, string $url, string $title): string
{
- $html = "";
+ $html = "";
$filtered_classes = $this->filter_meta_classes($class);
// if has title and url = a with title
- if (!empty($title) && !empty($url))
+ if (!empty($title) && !empty($url)) {
$html .= $this->a($url, $text, $title, $filtered_classes);
+ }
+
// if has only title = span with title
elseif (!empty($title)) {
$html .= $this->spanTitle($title, $text, $filtered_classes);
}
// if has only url = a
- elseif (!empty($url))
+ elseif (!empty($url)) {
$html .= $this->a($url, $text, null, $filtered_classes);
+ }
+
// else just text = p
- else
+ else {
$html .= $text;
+ }
// if in class array is not "same-line" add br
- if (!in_array("same-line", $class))
+ if (!in_array("same-line", $class)) {
$html .= '<br>';
- else
+ } else {
$html .= ' ';
+ }
- if (in_array("no-space", $class))
+ if (in_array("no-space", $class)) {
$html = trim($html);
+ }
return $html;
}
private function form_list(string $text, array $class, string $url, string $title): string
{
// if in class array is not "same-line" set flag
- if (!in_array("same-line", $class))
+ if (!in_array("same-line", $class)) {
$this->is_li_same_line = false;
- else
+ } else {
$this->is_li_same_line = true;
+ }
- if (!isset($this->was_li_same_line) || !$this->was_li_same_line)
+ if (!isset($this->was_li_same_line) || !$this->was_li_same_line) {
$html = "<li";
- else
+ } else {
$html = "";
+ }
// if class not empty add class
$filtered_classes = $this->filter_meta_classes($class);
- if (!empty($filtered_classes))
+ if (!empty($filtered_classes)) {
$html .= ' class="' . implode(' ', $filtered_classes) . '"';
+ }
// if has title and url = a with title
if (!empty($url)) {
- if (!isset($this->was_li_same_line) || !$this->was_li_same_line)
+ if (!isset($this->was_li_same_line) || !$this->was_li_same_line) {
$html .= '>';
+ }
+
$html .= $this->a($url, $text, $title, $filtered_classes);
- if (in_array("no-space", $class))
+ if (in_array("no-space", $class)) {
$html = trim($html);
+ }
}
// if has only title = span with title
elseif (!empty($title)) {
$html .= " title='$title'>$text";
} else {
- if (!isset($this->was_li_same_line) || !$this->was_li_same_line)
+ if (!isset($this->was_li_same_line) || !$this->was_li_same_line) {
$html .= '>';
+ }
+
$html .= "$text";
}
- if (!$this->is_li_same_line)
+ if (!$this->is_li_same_line) {
$html .= '</li>';
+ }
$this->was_li_same_line = $this->is_li_same_line;
return $html;
<?php
-require_once("generator.php");
+require_once "generator.php";
// constans
-const URL = "http://localhost:8000";
+const URL = "http://localhost:8000";
const BASE_PATH_TEMP = "../generated_temp";
-const SRC_PATH = "../src";
-const PAGE_TITLE = "TheRiceGoat";
+const SRC_PATH = "../src";
+const PAGE_TITLE = "TheRiceGoat";
$generator = new CodeGenerator();
return $data;
}
-$data = [];
-$data['langs'] = load_data(URL . '?sheet=_langs');
+$data = [];
+$data['langs'] = load_data(URL . '?sheet=_langs');
$data['images'] = load_data(URL . '?sheet=_images');
$data['header'] = load_data(URL . '?sheet=_header');
$data['footer'] = load_data(URL . '?sheet=_footer');
-$data['menu'] = load_data(URL . '?sheet=_menu');
-$data['pages'] = load_data(URL . '?sheet=_pages');
+$data['menu'] = load_data(URL . '?sheet=_menu');
+$data['pages'] = load_data(URL . '?sheet=_pages');
// remove langs that are not active
$data['langs'] = array_filter($data['langs'], function ($item) {
#endregion
#region lang folders
-foreach ($data['langs'] as $item)
+foreach ($data['langs'] as $item) {
mkdir(BASE_PATH_TEMP . "/" . $item['lang']);
+}
#endregion
#region assets and images
}
// format image classes
foreach ($data['images'] as $key => $image) {
- if (empty($image['class']))
+ if (empty($image['class'])) {
$data['images'][$key]['class'] = null;
- else
+ } else {
$data['images'][$key]['class'] = array_map('trim', explode(",", $image['class']));
+ }
}
// create partials:
}
}
-foreach ($data['langs'] as $lang)
+foreach ($data['langs'] as $lang) {
$generator->writeToFile($head_path, '<link rel="alternate" hreflang="' . $lang['lang'] . '" href="' . URL . "/" . $lang['lang'] . '/">');
+}
$generator->writeToFile($head_path, '
<title>' . $generator->templateVar("page-title") . '</title>
if (boolval($item['active'])) {
$generator->writeToFile($header_path, '<li>');
$generator->writeToFile($header_path, $generator->a('../' . $item['path'], $item['item'], null, null));
- $generator->writeToFile($header_path, '</li>');
+ $generator->writeToFile($header_path, '</li>');
}
}
$generator->writeToFile($header_path, '</ul>');
// populate page array with paths
$page_array = [];
-foreach ($data['menu'] as $item)
- foreach ($item as $key => $value)
- if ($key == 'path')
+foreach ($data['menu'] as $item) {
+ foreach ($item as $key => $value) {
+ if ($key == 'path') {
$page_array[] = $value;
+ }
+ }
+}
// generate page folders
mkdir(BASE_PATH_TEMP . "/pages");
// populate an array with path => title
$path_title = [];
-foreach ($menu_list as $item)
+foreach ($menu_list as $item) {
$path_title[$item['path']] = $item['title'];
+}
foreach ($data['pages'] as $page) {
- $page_path = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html";
+ $page_path = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html";
$page['class'] = array_map('trim', explode(",", $page['class']));
// put page title in header
- if (!empty($path_title[$page['path']]))
+ if (!empty($path_title[$page['path']])) {
$formed_head = $generator->setTemplateVar($tmp_head, "page-title", $path_title[$page['path']] . " | " . PAGE_TITLE);
- else
+ } else {
$formed_head = $generator->setTemplateVar($tmp_head, "page-title", PAGE_TITLE);
+ }
// put headers
$generator->writeToFile($page_path, $formed_head);
}
// get page data
- if (empty($data['pages'][$page['path']]))
+ if (empty($data['pages'][$page['path']])) {
$data['pages'][$page['path']] = load_data(URL . '?sheet=' . $page['path']);
+ }
$page_data = $data['pages'][$page['path']];
-
+
// generate page
$generator->writeToFile($page_path, $generator->form_html($page_data, "en"));
$generator->writeToFile($page_path, '</section>');