1<?php 2/** 3 * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved. 4 * 5 * This source code is licensed under the GPL license found in the 6 * COPYING file in the root directory of this source tree. 7 * 8 * @license GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html) 9 * @author ComboStrap <support@combostrap.com> 10 * 11 */ 12 13namespace ComboStrap; 14 15 16use dokuwiki\Extension\SyntaxPlugin; 17use PHPUnit\Exception; 18 19class RenderUtility 20{ 21 /** 22 * When the rendering is a snippet or an instructions 23 */ 24 const DYNAMIC_RENDERING = "dynamic"; 25 /** 26 * The id used if 27 */ 28 const DEFAULT_SLOT_ID_FOR_TEST = "test-slot-id"; 29 30 /** 31 * @param $content 32 * @param bool $strip 33 * @return string|null 34 */ 35 public static function renderText2XhtmlAndStripPEventually($content, bool $strip = true): ?string 36 { 37 global $ID; 38 $keep = $ID; 39 if ($ID === null && PluginUtility::isTest()) { 40 $ID = self::DEFAULT_SLOT_ID_FOR_TEST; 41 } 42 try { 43 $instructions = self::getInstructionsAndStripPEventually($content, $strip); 44 return p_render('xhtml', $instructions, $info); 45 } finally { 46 $ID = $keep; 47 } 48 49 } 50 51 /** 52 * @param $pageContent - the text (not the id) 53 * @param bool $stripOpenAndEnd - to avoid the p element in test rendering 54 * @return array 55 */ 56 public static function getInstructionsAndStripPEventually($pageContent, bool $stripOpenAndEnd = true): array 57 { 58 59 $instructions = p_get_instructions($pageContent); 60 61 if ($stripOpenAndEnd) { 62 63 /** 64 * Delete the p added by {@link Block::process()} 65 * if the plugin of the {@link SyntaxPlugin::getPType() normal} and not in a block 66 * 67 * p_open = document_start in renderer 68 */ 69 if ($instructions[1][0] == 'p_open') { 70 unset($instructions[1]); 71 72 /** 73 * The last p position is not fix 74 * We may have other calls due for instance 75 * of {@link \action_plugin_combo_syntaxanalytics} 76 */ 77 $n = 1; 78 while (($lastPBlockPosition = (sizeof($instructions) - $n)) >= 0) { 79 80 /** 81 * p_open = document_end in renderer 82 */ 83 if ($instructions[$lastPBlockPosition][0] == 'p_close') { 84 unset($instructions[$lastPBlockPosition]); 85 break; 86 } else { 87 $n = $n + 1; 88 } 89 } 90 } 91 92 } 93 94 return $instructions; 95 } 96 97 /** 98 * @param $pageId 99 * @return string|null 100 */ 101 public 102 static function renderId2Xhtml($pageId) 103 { 104 $file = wikiFN($pageId); 105 if (file_exists($file)) { 106 global $ID; 107 $keep = $ID; 108 $ID = $pageId; 109 $content = file_get_contents($file); 110 $xhtml = self::renderText2XhtmlAndStripPEventually($content); 111 $ID = $keep; 112 return $xhtml; 113 } else { 114 return false; 115 } 116 } 117 118 /** 119 * @throws ExceptionCombo 120 */ 121 public static function renderInstructionsToXhtml($callStackHeaderInstructions): ?string 122 { 123 global $ACT; 124 $keep = $ACT; 125 try { 126 $ACT = self::DYNAMIC_RENDERING; 127 return p_render("xhtml", $callStackHeaderInstructions, $info); 128 } catch (Exception $e) { 129 /** 130 * Example of errors; 131 * method_exists() expects parameter 2 to be string, array given 132 * inc\parserutils.php:672 133 */ 134 throw new ExceptionCombo("Error while rendering instructions. Error was: {$e->getMessage()}"); 135 } finally { 136 $ACT = $keep; 137 } 138 } 139 140 141} 142