1<?php
2/**
3 * @copyright Copyright (c) 2014 Carsten Brandt
4 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5 * @link https://github.com/cebe/markdown#readme
6 */
7
8namespace cebe\markdown\block;
9
10/**
11 * Adds the block quote elements
12 */
13trait QuoteTrait
14{
15	/**
16	 * identify a line as the beginning of a block quote.
17	 */
18	protected function identifyQuote($line)
19	{
20		return $line[0] === '>' && (!isset($line[1]) || ($l1 = $line[1]) === ' ' || $l1 === "\t");
21	}
22
23	/**
24	 * Consume lines for a blockquote element
25	 */
26	protected function consumeQuote($lines, $current)
27	{
28		// consume until newline
29		$content = [];
30		for ($i = $current, $count = count($lines); $i < $count; $i++) {
31			$line = $lines[$i];
32			if (ltrim($line) !== '') {
33				if ($line[0] == '>' && !isset($line[1])) {
34					$line = '';
35				} elseif (strncmp($line, '> ', 2) === 0) {
36					$line = substr($line, 2);
37				}
38				$content[] = $line;
39			} else {
40				break;
41			}
42		}
43
44		$block = [
45			'quote',
46			'content' => $this->parseBlocks($content),
47			'simple' => true,
48		];
49		return [$block, $i];
50	}
51
52
53	/**
54	 * Renders a blockquote
55	 */
56	protected function renderQuote($block)
57	{
58		return '<blockquote>' . $this->renderAbsy($block['content']) . "</blockquote>\n";
59	}
60
61	abstract protected function parseBlocks($lines);
62	abstract protected function renderAbsy($absy);
63}
64