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 MarkupRenderUtility
20{
21
22    /**
23     * @param $content
24     * @param bool $strip
25     * @return string
26     * @throws ExceptionCompile
27     */
28    public static function renderText2XhtmlAndStripPEventually($content, bool $strip = true): string
29    {
30
31        return FetcherMarkup::confRoot()
32            ->setRequestedMarkupString($content)
33            ->setDeleteRootBlockElement($strip)
34            ->setRequestedContextPathWithDefault()
35            ->setRequestedMimeToXhtml()
36            ->setIsStandAloneCodeExecution(true)
37            ->build()
38            ->getFetchString();
39
40    }
41
42    /**
43     * @param $pageContent - the text (not the id)
44     * @param bool $stripOpenAndEnd - to avoid the p element in test rendering
45     * @return array
46     */
47    public static function getInstructionsAndStripPEventually($pageContent, bool $stripOpenAndEnd = true): array
48    {
49
50        $markupRenderer = FetcherMarkup::confRoot()
51            ->setDeleteRootBlockElement($stripOpenAndEnd)
52            ->setRequestedMarkupString($pageContent)
53            ->setRequestedMimeToInstructions()
54            ->setRequestedContextPathWithDefault()
55            ->build();
56
57        return $markupRenderer->getInstructions();
58
59
60    }
61
62    /**
63     * @param $pageId
64     * @return string
65     * @throws ExceptionCompile
66     */
67    public
68    static function renderId2Xhtml($pageId): string
69    {
70
71        $wikiPath = WikiPath::createMarkupPathFromId($pageId);
72        return FetcherMarkup::confChild()
73            ->setRequestedExecutingPath($wikiPath)
74            ->setRequestedMimeToXhtml()
75            ->build()
76            ->getFetchString();
77
78
79    }
80
81    /**
82     * @param $callStackHeaderInstructions
83     * @param $contextData - the context data if any
84     * @return string|null
85     * @throws ExceptionCompile
86     */
87    public static function renderInstructionsToXhtml($callStackHeaderInstructions, array $contextData = null): string
88    {
89
90        $builder = FetcherMarkup::confChild()
91            ->setRequestedInstructions($callStackHeaderInstructions)
92            ->setIsDocument(false)
93            ->setRequestedMimeToXhtml();
94        if ($contextData !== null) {
95            $builder->setContextData($contextData);
96        }
97        $fetcherMarkup = $builder->build();
98        $fetchString = $fetcherMarkup->getFetchString();
99        return $fetchString;
100    }
101
102    /**
103     * @throws ExceptionCompile
104     */
105    public static function renderInstructionsToXhtmlFromPage($callStackHeaderInstructions, MarkupPath $renderingPageId): string
106    {
107        return self::renderInstructionsToXhtml($callStackHeaderInstructions, $renderingPageId->getMetadataForRendering());
108    }
109
110
111}
112