Explorar o código

Start workinbg on project.

Cixo Develop hai 4 meses
pai
achega
9aee0bf2eb

+ 1 - 0
cx_maketool

@@ -0,0 +1 @@
+Subproject commit 860204b2d674c08e98ae8ab598877c5f763c2bb2

+ 11 - 0
functions.php

@@ -0,0 +1,11 @@
+<?php
+
+if (function_exists("get_theme_directory")) {
+    $source = get_theme_directory()."/source/functions/";
+} else {
+    $source = dirname(__FILE__)."/source/functions/";
+}
+
+require_once($source."renderer.php");
+
+unset($source);

+ 15 - 0
index.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Theme name: LA Restaurant Theme
+ * Author: Les Amis Reunis
+ * Author URI: https://lesamisreunis.pl/
+ * Description: This is custom theme for restaurants by Les Amis Reunis.
+ * Version: 0.1.2
+ * License: MIT License
+ */
+
+namespace la_restaurant_theme;
+
+require_once(get_theme_directory() + "/functions.php");
+
+

+ 52 - 0
make.py

@@ -0,0 +1,52 @@
+#!/usr/bin/python 
+
+from pathlib import Path
+from json import loads
+from shutil import rmtree
+from shutil import copytree
+
+from cx_maketool import sass_compiler
+from cx_maketool import esbuild_compiler
+from cx_maketool import render
+from cx_maketool import script
+from cx_maketool import link
+
+root = Path(__file__).absolute().parent
+source = root / Path("source/")
+build = root / Path("./")
+
+styles = source / Path("stylesheet/")
+scripts = source / Path("scripts/")
+assets = source / Path("assets/")
+
+assets_output = build / Path("assets/")
+
+scripts_loader = scripts / Path("core.js")
+theme_loader = styles / Path("core.sass")
+info_file = source / Path("about.txt")
+
+script_bundle = build / Path("script.js")
+theme_bundle = build / Path("style.css")
+
+if theme_bundle.is_file():
+    theme_bundle.unlink()
+
+if script_bundle.is_file():
+    script_bundle.unlink()
+
+sass_compiler(theme_loader).build(theme_bundle)
+esbuild_compiler(scripts_loader).build(script_bundle)
+
+if assets_output.exists():
+    rmtree(assets_output)
+
+copytree(assets, assets_output)
+
+with info_file.open() as info:
+    info_content = info.read()
+
+with theme_bundle.open() as theme:
+    content = theme.read()
+
+with theme_bundle.open("w") as theme:
+    theme.write(info_content + content)

+ 2 - 0
script.js

@@ -0,0 +1,2 @@
+(() => {
+})();

+ 8 - 0
source/about.txt

@@ -0,0 +1,8 @@
+/**
+ * Theme name: LA Restaurant Theme
+ * Author: Les Amis Reunis
+ * Author URI: https://lesamisreunis.pl/
+ * Description: This is custom theme for restaurants by Les Amis Reunis.
+ * Version: 0.1.2
+ * License: MIT License
+ */

+ 78 - 0
source/functions/renderer.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace la_restaurant_theme;
+
+class renderer {
+    private string $start;
+    private string $stop;
+    private array $attributes;
+
+    public static function from(string $template): object {
+        return new renderer($template);
+    }
+
+    public function __construct(private string $content) {
+        $this->start = '<<!';
+        $this->stop = '!>>';
+        $this->attributes = array();
+    }
+
+    public function set(string $name, string $content): object {
+        $name = trim($name);
+        $this->attributes[$name] = $content;
+
+        return $this;
+    }
+
+    public function render(): string {
+        $parts = explode($this->start, $this->content);
+        $first = array_shift($parts);
+        $results = array();
+
+        array_push($results, $first);
+
+        foreach ($parts as $count) {
+            array_push($results, $this->process_single($count));
+        }
+
+        return join("", $results);
+    }
+
+    public function get_attribute(
+        string $name, 
+        ?string $blank = null
+    ): ?string {
+        $name = trim($name);
+
+        if (array_key_exists($name, $this->attributes)) {
+            return $this->attributes[$name];
+        }
+
+        return $blank;
+    }
+
+    private function process_single(string $count): string {
+        $parts = explode($this->stop, $count);
+
+        if (count($parts) === 1) {
+            return $this->start.$count;
+        }
+
+        $name = array_shift($parts);
+        $name = trim($name);
+        $rest = join($this->stop, $parts);
+        $replace = $this->get_attribute($name, '');
+                       
+        return $replace.$rest; 
+    }
+
+    public function __get(string $name): mixed {
+        switch ($name) {
+            case 'start':
+                return $this->start;
+
+            case 'stop':
+                return $this->stop;
+        }
+    }
+}

+ 41 - 0
source/functions/view.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace la_restaurant_theme;
+
+require_once('renderer.php');
+
+class view {
+    public function __construct(private string $path) {
+        if (!$this->exists()) {
+            throw new ErrorException('View "'.$path.'" not exists.');
+        }
+    }
+
+    public function exists(): bool {
+        return is_file($this->path);
+    }
+
+    private function read(): string {
+        $file = fopen($this->path, 'r');
+        $size = filesize($this->path);
+        
+        if ($file === false) {
+            throw new ErrorException('View "'.$path.'" is not readable.');
+        }
+
+        $content = fread($file, $size);
+        fclose($file);
+
+        return $content;
+    }
+
+    public function rendering(): renderer {
+        static $content = null;
+
+        if ($content === null) {
+            $content = $this->read();
+        }
+
+        return new renderer($content);
+    }
+}

+ 53 - 0
source/functions/views.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace la_restaurant_theme;
+
+require_once('view.php');
+
+class views {
+    private string $path;
+
+    public function __construct(string $path) {
+        $this->path = $this->parse_path($path);
+
+        if (!$this->exists()) {
+            throw new ErrorException(
+                'Views directory "'.$this->path.'" not exists.'
+            );
+        }
+    }
+
+    private function parse_path($path): string {
+        if (strlen($path) === 0) {
+            $path = './';
+        }
+        
+        $size = strlen($path);
+        $last = $path[$size - 1];
+
+        if ($last !== '/' || $last !== '\\') {
+            return $path.'/';
+        }
+
+        return $path;
+    }
+
+    public function exists(): bool {
+        return is_dir($this->path);
+    }
+
+    private function get_file_for(string $name): string {
+        $unix = strpos($name, '/');
+        $dos = strpos($name, '\\');
+
+        if ($unix !== false || $dos !== false) {
+            throw new ErrorException('View "'.$name.'" not in views dir.');
+        }
+
+        return $this->path.$name;
+    }
+
+    public function get(string $name): view {
+        return new view($this->get_file_for($name));
+    }
+}

+ 0 - 0
source/stylesheet/core.sass


+ 9 - 0
style.css

@@ -0,0 +1,9 @@
+/**
+ * Theme name: LA Restaurant Theme
+ * Author: Les Amis Reunis
+ * Author URI: https://lesamisreunis.pl/
+ * Description: This is custom theme for restaurants by Les Amis Reunis.
+ * Version: 0.1.2
+ * License: MIT License
+ */
+/*# sourceMappingURL=style.css.map */

+ 31 - 0
tests/01-render.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace la_restaurant_theme;
+
+require_once("enviroment.php");
+
+$template = 'sample is <<! place !>> and <<!other!>> more';
+$first = 'sample is best and pink more';
+$second = 'sample is much and is more';
+
+$corrupted = 'sample is <<! <<! place !>> and <<!other!>> !>> more';
+$first_corrupted = 'sample is best and pink more';
+
+$first_result = renderer::from($template)
+->set('place', 'best')
+->set('other', 'pink')
+->render();
+
+$second_result = renderer::from($template)
+->set('place', 'much')
+->set('other', 'is')
+->render();
+
+$first_corrupted_result = renderer::from($template)
+->set('place', 'best')
+->set('other', 'pink')
+->render();
+
+test($first, $first_result);
+test($second, $second_result);
+test($first_corrupted, $first_corrupted_result);

+ 36 - 0
tests/enviroment.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace la_restaurant_theme;
+
+$root = dirname(__FILE__).'/../';
+
+require_once($root.'functions.php');
+
+function test(mixed $required, mixed $result){
+    static $number = 1;
+
+    echo ("Running test $number...\n");
+
+    $simple = ($result == $required);
+    $full = ($result === $required);
+
+    if ($simple && ! $full) {
+        $required_type = gettype($required);
+        $result_type = gettype($result);
+
+        echo("FAIL!!!\n");
+        echo("Values same, but other type.\n");
+        echo("Required type: $required_type.\n");
+        echo("Result type: $result_type.\n");
+    } elseif (!$simple) {
+        echo("FAIL!!!\n");
+        echo("Values are not same.\n");
+        echo("Required value: $required.\n");
+        echo("Result value: $result.\n");
+    } else {
+        echo("Success!\n");
+    }
+
+    echo("\n");
+    ++$number;
+}