289 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			289 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php 
 | 
						|
 | 
						|
    class MapeateConstructorWeb{
 | 
						|
 | 
						|
        const PARRAFO = 1 << 0;
 | 
						|
        const LINK = 1 << 1;
 | 
						|
 | 
						|
        public function __construct(){
 | 
						|
 | 
						|
            $html = file_get_contents(__DIR__ . "/../HTML/index.html");
 | 
						|
            
 | 
						|
            $this->cargar_modulos();
 | 
						|
            $this->cargar_diccionario();
 | 
						|
            $this->cargar_perfiles();
 | 
						|
 | 
						|
            $this->pseudomarkdown_patrones = [
 | 
						|
                "cabecera" => ['/^(\#{1,6})([^\r\n]+)$/m', fn($campos) => '<h' . ($l = strlen($campos[1][0])) . '>' . $this->montar_pseudomarkdown($campos[2][0], self::PARRAFO) . '</h' . $l . '>'], 
 | 
						|
                "negrilla" => ['/\*{2}(([^\*]+|\*[^\*]|[\n\r]+)+)\*{2}/', fn($campos) => '<b>' . $this->montar_pseudomarkdown($campos[1][0], self::PARRAFO) . '</b>'], 
 | 
						|
                "cursiva" => ['/\*(([^\*]+|[\r\n]+)+)\*/', fn($campos) => '<i>' . $this->montar_pseudomarkdown($campos[1][0], self::PARRAFO) . '</i>'], 
 | 
						|
                "lista" => ['/^(\s*(\-|[0-9]+\.)[^\r\n]+(\r\n|\n|\r))+/m', fn($campos) => $this->montar_lista($campos[0][0])], 
 | 
						|
                "markdown_link" => ['/\[([^\[\]]+)\]\(([^\(\)]+)\)/', fn($campos) => '<a href="' . $campos[1][0] . '" target="_blank" title="' . ($texto = $this->montar_pseudomarkdown($campos[2][0], self::PARRAFO | self::LINK)) . '">' . $texto . '</a>'], 
 | 
						|
                "link" => ['/\[{2}link\s+([^\]\s]+)(\s+([^\]]+))?\]{2}?/', fn($campos) => '<a href="' . $campos[1][0] . '" target="_blank" title="' . ($texto = $campos[count($campos) > 2 && $campos[2] ? 2 : 1][0]) . '">' . $texto . '</a>'], 
 | 
						|
                "link_crudo" => ['/https?\:\/{2}[^\s]+/', fn($campos) => '<a href="' . $campos[0][0] . '" target="_blank" title="' . $campos[0][0] . '">' . $campos[0][0] . '</a>'], 
 | 
						|
                "bloque_nota" => ['/^>([^\n\r]+((\r\n|\n|\r)[^\n\r]+)*)/m', fn($campos) => '<blockquote>' . $this->montar_pseudomarkdown($campos[1][0], self::PARRAFO) . '</blockquote>'], 
 | 
						|
                "perfil" => ['/^\[{2}perfil\s+([^\]]+)\]{2}/mi', fn($campo) => $this->construir_perfil($campo[1][0])], 
 | 
						|
                "parrafo" => ['/^(([^\r\n]+)(\r\n|\n|\r)?)+/m', fn($campos) => ($contenido = trim($campos[0][0])) ? (($tiene_etiqueta = $contenido[0] == '<') ? '' : '<p>') . $this->montar_pseudomarkdown($contenido, self::PARRAFO) . ($tiene_etiqueta ? '' : '</p>') : '']
 | 
						|
            ];
 | 
						|
 | 
						|
            $html = self::string_variables($html, [
 | 
						|
                "menu" => implode("", array_map(fn($campos) => self::string_variables($this->modulos["elemento_menu_principal"], [
 | 
						|
                    "i18n" => $campos[0], 
 | 
						|
                    "texto" => $campos[1], 
 | 
						|
                    "link" => $campos[2], 
 | 
						|
                    "target" => $campos[3], 
 | 
						|
                    "icono" => $campos[4]
 | 
						|
                ]), [
 | 
						|
                    ["mapeate_oficial", "Mapeate - Web oficial", "https://mapeate.k3y.pw/", "_self", "mapeate"], 
 | 
						|
                    ["web_app_oficial", "Mapeate - Aplicación Web oficial", "https://mapeate.k3y.pw/app.html", "_self", "mapeate"], 
 | 
						|
                    ["mapeate_git", "Mapeate - Git", "https://git.k3y.pw/CaritasSantaCruz/Mapeate", "_blank", "git"], 
 | 
						|
                    ["caritas_santa_cruz_facebook", "Cáritas Santa Cruz - Facebook", "https://www.facebook.com/santacruzcaritas/", "_blank", "facebook"], 
 | 
						|
                    ["caritas_santa_cruz_wix", "Cáritas Santa Cruz - Wix", "https://santacruzdecanido.wixsite.com/caritas", "_blank", "wix"], 
 | 
						|
                    ["caritas_santa_cruz_twitter", "Cáritas Santa Cruz - Twitter", "https://twitter.com/CanidoSantaCruz", "_blank", "twitter"], 
 | 
						|
                    ["caritas_santa_cruz_fan_web", "Cáritas Santa Cruz - Fan Web", "https://fanwebcaritassantacruz.k3y.pw/", "_blank", "fan_web"]
 | 
						|
                ])), 
 | 
						|
                "diccionario" => base64_encode(json_encode($this->diccionario)), 
 | 
						|
                "contenido" => $this->montar_pseudomarkdown(file_get_contents(__DIR__ . "/../HTML/contenido.md"))
 | 
						|
            ]);
 | 
						|
 | 
						|
            file_put_contents(__DIR__ . "/index.html", $html);
 | 
						|
 | 
						|
            echo $html;
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        private function cargar_modulos(){
 | 
						|
 | 
						|
            $modulos_html = file_get_contents(__DIR__ . "/../HTML/modulos.html");
 | 
						|
 | 
						|
            $this->modulos = [];
 | 
						|
 | 
						|
            while(preg_match('/<\!\-{2}\s*\[{2}([^\[\]]+)\]{2}(\s*\-{2}>)?/', $modulos_html, $matches, \PREG_OFFSET_CAPTURE)){
 | 
						|
 | 
						|
                $clave = trim($matches[1][0]);
 | 
						|
 | 
						|
                if(preg_match('/<\!\-{2}\s*\[{2}\s*' . $clave . '\s*\]{2}(\s*\-{2}>)?/', $modulos_html = substr($modulos_html, $matches[0][1] + strlen($matches[0][0])), $matches, \PREG_OFFSET_CAPTURE)){
 | 
						|
                    $this->modulos[$clave] = substr($modulos_html, 0, $matches[0][1]);
 | 
						|
                    $modulos_html = substr($modulos_html, $matches[0][1] + strlen($matches[0][0]));
 | 
						|
                };
 | 
						|
 | 
						|
            };
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        private function cargar_diccionario(){
 | 
						|
 | 
						|
            $i = -1;
 | 
						|
 | 
						|
            $this->diccionario = [];
 | 
						|
 | 
						|
            foreach(preg_split('/[\r\n]+/', file_get_contents(__DIR__ . "/../HTML/diccionario.md")) as $linea){
 | 
						|
                if(!$linea)
 | 
						|
                    continue;
 | 
						|
 | 
						|
                $tipo = $linea[0];
 | 
						|
 | 
						|
                switch($tipo){
 | 
						|
                    case "#":
 | 
						|
                        $this->diccionario[++ $i] = [
 | 
						|
                            "crudo" => $crudo = trim(substr($linea, 1)), 
 | 
						|
                            "clave" => preg_replace('/[^a-z0-9]+/', "_", strtolower($crudo)), 
 | 
						|
                            "patrones" => [], 
 | 
						|
                            "links" => [], 
 | 
						|
                            "descripcion" => []
 | 
						|
                        ];
 | 
						|
                    break;
 | 
						|
                    case "-":
 | 
						|
                        $this->diccionario[$i]["links"][] = trim(substr($linea, 1));
 | 
						|
                    break;
 | 
						|
                    case ">":
 | 
						|
 | 
						|
                        $patron = preg_split('/\s*\-\s*/', trim(substr($linea, 1)));
 | 
						|
 | 
						|
                        $this->diccionario[$i]["patrones"][] = [$patron[0], count($patron) > 1 ? $patron[1] : $this->diccionario[$i]["crudo"]];
 | 
						|
 | 
						|
                    break;
 | 
						|
                    default:
 | 
						|
                        $this->diccionario[$i]["descripcion"][] = $linea;
 | 
						|
                    break;
 | 
						|
                };
 | 
						|
 | 
						|
            };
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        private function cargar_perfiles(){
 | 
						|
 | 
						|
            $clave = null;
 | 
						|
 | 
						|
            $this->perfiles = [];
 | 
						|
 | 
						|
            foreach(preg_split('/[\r\n]+/', file_get_contents(__DIR__ . "/../HTML/perfiles.md")) as $linea){
 | 
						|
                if(!$linea)
 | 
						|
                    continue;
 | 
						|
 | 
						|
                $tipo = $linea[0];
 | 
						|
 | 
						|
                switch($tipo){
 | 
						|
                    case "#":
 | 
						|
                        $this->perfiles[$clave = preg_replace('/[^a-z0-9]+/i', "_", ($nombre = trim(substr($linea, 1))))] = [
 | 
						|
                            "nombre" => $nombre, 
 | 
						|
                            "clave" => $clave, 
 | 
						|
                            "links" => [], 
 | 
						|
                            "avatar" => null, 
 | 
						|
                            "descripcion" => []
 | 
						|
                        ];
 | 
						|
                    break;
 | 
						|
                    case "-":
 | 
						|
                        $this->perfiles[$clave]["links"][] = trim(substr($linea, 1));
 | 
						|
                    break;
 | 
						|
                    case ">":
 | 
						|
                        $this->perfiles[$clave]["avatar"] = trim(substr($linea, 1));
 | 
						|
                    break;
 | 
						|
                    default:
 | 
						|
                        $this->perfiles[$clave]["descripcion"][] = $linea;
 | 
						|
                    break;
 | 
						|
                };
 | 
						|
 | 
						|
            };
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        public function construir_perfil($clave){
 | 
						|
            return self::string_variables($this->modulos["elemento_perfil"], [
 | 
						|
                "clave" => $clave, 
 | 
						|
                "nombre" => $this->perfiles[$clave]["nombre"], 
 | 
						|
                "avatar" => $this->perfiles[$clave]["avatar"], 
 | 
						|
                "descripcion" => implode("", array_map(fn($parrafo) => '<p>' . $parrafo . '</p>', $this->perfiles[$clave]["descripcion"])), 
 | 
						|
                "links" => implode("", array_map(fn($link) => self::string_variables($this->modulos["link_perfil"], [
 | 
						|
                    "link" => $link, 
 | 
						|
                    "tipo" => $tipo = preg_replace('/[^a-z0-9]+/', "_", preg_replace('/^[^\:]+\:\/{2}([^\/]+)(\/.*)?$/', "$1", $link)), 
 | 
						|
                    "tipo_nombre" => $tipo
 | 
						|
                ]), $this->perfiles[$clave]["links"]))
 | 
						|
            ]);
 | 
						|
        }
 | 
						|
 | 
						|
        public static function string_variables($string, $variables){
 | 
						|
            return preg_replace_callback('/\{([^\{\}]+)\}/', function($valor) use($variables){
 | 
						|
                if(isset($variables[$valor[1]]))
 | 
						|
                    return $variables[$valor[1]];
 | 
						|
                return $valor[0];
 | 
						|
            }, $string);
 | 
						|
        }
 | 
						|
 | 
						|
        private function montar_lista($campos){
 | 
						|
 | 
						|
            $lineas = preg_split('/[\r\n]+/', $campos);
 | 
						|
            $html = '';
 | 
						|
            $niveles = [[0, "ul"]];
 | 
						|
            $l = 0;
 | 
						|
 | 
						|
            foreach($lineas as $linea){
 | 
						|
                if(!preg_match('/^(\s*)?(\-|[0-9]+\.)?(.*)$/', $linea, $matches))
 | 
						|
                    continue;
 | 
						|
 | 
						|
                $separador = strlen($matches[1]);
 | 
						|
                $tipo = $matches[2];
 | 
						|
                $entrada = $this->montar_pseudomarkdown(trim($matches[3]), self::PARRAFO);
 | 
						|
 | 
						|
                if($tipo){
 | 
						|
                    if($niveles[$l][0] == $separador)
 | 
						|
                        $html .= ($html ? '</li>' : '<' . ($niveles[$l][1] = $tipo == "-" ? "ul" : "ol") . '>') . '<li>' . $entrada;
 | 
						|
                    elseif($niveles[$l][0] < $separador){
 | 
						|
                        $niveles[++ $l] = [$separador, $tipo == "-" ? "ul" : "ol"];
 | 
						|
                        $html .= '<' . $niveles[$l][1] . '><li>' . $entrada;
 | 
						|
                    }else{
 | 
						|
                        while($niveles[$l][0] > $separador && $l --)
 | 
						|
                            $html .= '</li></' . array_pop($niveles)[1] . '>';
 | 
						|
                        $html .= '</li><li>' . $entrada;
 | 
						|
                    };
 | 
						|
                }elseif($entrada)
 | 
						|
                    $html .= " " . $entrada;
 | 
						|
 | 
						|
            };
 | 
						|
 | 
						|
            while(count($niveles))
 | 
						|
                $html .= '</li></' . array_pop($niveles)[1] . '>';
 | 
						|
 | 
						|
            return $html;
 | 
						|
        }
 | 
						|
 | 
						|
        public function montar_pseudomarkdown($pseudomarkdown, $modo = 0){
 | 
						|
 | 
						|
            $punteros = [];
 | 
						|
            $html = '';
 | 
						|
            $es_parrafo = $modo & self::PARRAFO;
 | 
						|
 | 
						|
            foreach(array_keys($this->pseudomarkdown_patrones) as $clave)
 | 
						|
                if(!$es_parrafo || $clave != "parrafo")
 | 
						|
                    $punteros[$clave] = [["", -1]];
 | 
						|
 | 
						|
            while(true){
 | 
						|
 | 
						|
                $eliminar = [];
 | 
						|
                $posicion = 2 << 28;
 | 
						|
                $elemento = null;
 | 
						|
 | 
						|
                foreach($punteros as $clave => $valor){
 | 
						|
                    if($punteros[$clave] === null)
 | 
						|
                        continue;
 | 
						|
 | 
						|
                    if($punteros[$clave][0][1] < 0)
 | 
						|
                        if(!preg_match($this->pseudomarkdown_patrones[$clave][0], $pseudomarkdown, $punteros[$clave], PREG_OFFSET_CAPTURE)){
 | 
						|
                            $eliminar[] = $clave;
 | 
						|
                            continue;
 | 
						|
                        };
 | 
						|
 | 
						|
                    if($punteros[$clave][0][1] < $posicion){
 | 
						|
                        $posicion = $punteros[$clave][0][1];
 | 
						|
                        $elemento = $clave;
 | 
						|
                    };
 | 
						|
 | 
						|
                };
 | 
						|
 | 
						|
                foreach($eliminar as $clave)
 | 
						|
                    unset($punteros[$clave]);
 | 
						|
 | 
						|
                if(!count($punteros))
 | 
						|
                    break;
 | 
						|
 | 
						|
                $i = $punteros[$elemento][0][1];
 | 
						|
 | 
						|
                $html .= (
 | 
						|
                    $this->analizar_con_diccionario(substr($pseudomarkdown, 0, $i)) . 
 | 
						|
                    ((
 | 
						|
                        ((!$punteros[$elemento][0][0] || $modo & self::PARRAFO) && $elemento == "parrafo") || 
 | 
						|
                        ($modo & self::LINK && $elemento == "link_crudo")
 | 
						|
                    ) ? $this->analizar_con_diccionario($punteros[$elemento][0][0]) : $this->pseudomarkdown_patrones[$elemento][1]($punteros[$elemento]))
 | 
						|
                );
 | 
						|
 | 
						|
                $i += strlen($punteros[$elemento][0][0]);
 | 
						|
                foreach(array_keys($punteros) as $clave)
 | 
						|
                    $punteros[$clave][0][1] -= $i;
 | 
						|
                $pseudomarkdown = substr($pseudomarkdown, $i);
 | 
						|
 | 
						|
            };
 | 
						|
 | 
						|
            return $html . $this->analizar_con_diccionario($pseudomarkdown);
 | 
						|
        }
 | 
						|
 | 
						|
        public function analizar_con_diccionario($string){
 | 
						|
 | 
						|
            $conjuntos = [];
 | 
						|
 | 
						|
            foreach($this->diccionario as $elemento)
 | 
						|
                foreach($elemento["patrones"] as $patron){
 | 
						|
                    // print_r([$patron]);
 | 
						|
                    while(preg_match($patron[0], $string, $matches, PREG_OFFSET_CAPTURE)){
 | 
						|
                        $string = substr($string, 0, $matches[0][1]) . "[[" . count($conjuntos) . "]]" . substr($string, $matches[0][1] + strlen($matches[0][0]));
 | 
						|
                        $conjuntos[] = [$elemento["clave"], preg_replace_callback('/\$([0-9])/', fn($valor) => $matches[$valor[1]][0], $patron[1])];
 | 
						|
                    };
 | 
						|
                    // $string = preg_replace($patron[0], "<span data-diccionario-sin-procesar=\"" . $elemento["clave"] . "\">" . $patron[1] . "</span>", $string);
 | 
						|
                };
 | 
						|
 | 
						|
            return preg_replace_callback('/\[{2}([0-9]+)\]{2}/', fn($valor) => "<span data-diccionario-sin-procesar=\"" . $conjuntos[$valor[1]][0] . "\">" . $conjuntos[$valor[1]][1] . "</span>", $string);
 | 
						|
        }
 | 
						|
 | 
						|
    };
 | 
						|
 | 
						|
    $mcw = new MapeateConstructorWeb(); |