aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/templater.php
blob: 7b666459f3be84a402322598963f87e101ae3d63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php namespace HashOver;

// Copyright (C) 2015-2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Templater
{
	public $mode;
	public $setup;
	public $template;

	protected $curlyRegex = '/\{([a-z]+):([a-z_-]+)\}/i';
	protected $newlineSearch = array ("\r\n", "\r", "\n");
	protected $newlineReplace = array ("\n", "\n", PHP_EOL);

	public function __construct ($mode = 'php', Setup $setup)
	{
		$this->mode = $mode;
		$this->setup = $setup;
	}

	public function loadFile ($file)
	{
		// Attempt to read template HTML file
		$theme_html = @file_get_contents ($file);

		// Check if template file read successfully
		if ($theme_html !== false) {
			// If so, return trimmed HTML template
			return trim ($theme_html);
		} else {
			// If not, throw exception
			throw new \Exception ('Failed to load template file.');
		}
	}

	protected function curlyVariable ($key)
	{
		return '{{' . $key . '}}';
	}

	protected function fromTemplate ($key)
	{
		if ($this->mode !== 'php') {
			return $this->curlyVariable ($key);
		}

		if (!empty ($this->template[$key])) {
			return $this->template[$key];
		}

		return '';
	}

	protected function placeholder ($id)
	{
		$span_id  = 'hashover-placeholder-' . $id;
		$span_id .= '-' . $this->fromTemplate ('permalink');

		$placeholder = new HTMLTag ('span', array (
			'id' => $span_id
		), false);

		if (!empty ($this->template[$id])) {
			$placeholder->innerHTML ($this->template[$id]);
		}

		return $placeholder->asHTML ();
	}

	protected function parser ($var)
	{
		if (empty ($var[1]) or empty ($var[2])) {
			return '';
		}

		switch ($var[1]) {
			case 'hashover': {
				return $this->fromTemplate ($var[2]);
			}

			case 'placeholder': {
				return $this->placeholder ($var[2]);
			}
		}
	}

	public function parseTemplate ($file, array $template = array ())
	{
		$this->template = $template;
		$data = $this->loadFile ($file);
		$template = preg_replace_callback ($this->curlyRegex, 'self::parser', $data);
		$template = str_replace ($this->newlineSearch, $this->newlineReplace, $template);
		$template = preg_replace ('/^[\s\n]+$/m', '', $template);

		return $template;
	}

	public function parseTheme ($file, array $template = array ())
	{
		// Get the file path for the configured theme
		$path = $this->setup->getThemePath ($file, false);
		$path = $this->setup->getAbsolutePath ($path);

		// Parse the theme HTML as template
		$theme = $this->parseTemplate ($path, $template);

		return $theme;
	}
}