Completed
Push — master ( e31e00...ea892f )
by Jitendra
11s
created

HtmlHelper::hr()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the HTMLUP package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc;
13
14
trait HtmlHelper
15
{
16
    public function escape($input)
17
    {
18
        return \htmlspecialchars($input);
19
    }
20
21
    public function h($level, $line)
22
    {
23
        if (\is_string($level)) {
24
            $level = \trim($level, '- ') === '' ? 2 : 1;
25
        }
26
27
        if ($level < 7) {
28
            return "\n<h{$level}>" . \ltrim(\ltrim($line, '# ')) . "</h{$level}>";
29
        }
30
31
        return '';
32
    }
33
34
    public function hr($prevLine, $line)
35
    {
36
        if ($prevLine === '' && \preg_match(BlockElementParser::RE_MD_RULE, $line)) {
37
            return "\n<hr />";
38
        }
39
    }
40
41
    public function codeStart($lang)
42
    {
43
        $lang = isset($lang[1])
44
            ? ' class="language-' . $lang[1] . '"'
45
            : '';
46
47
        return "\n<pre><code{$lang}>";
48
    }
49
50
    public function codeLine($line, $isBlock, $indentLen = 4)
51
    {
52
        $code  = "\n"; // @todo: donot use \n for first line
53
        $code .= $isBlock ? $line : \substr($line, $indentLen);
54
55
        return $code;
56
    }
57
58
    public function tableStart($line, $delim = '|')
59
    {
60
        $table = "<table>\n<thead>\n<tr>\n";
61
62
        foreach (\explode($delim, \trim($line, $delim)) as $hdr) {
63
            $table .= '<th>' . \trim($hdr) . "</th>\n";
64
        }
65
66
        $table .= "</tr>\n</thead>\n<tbody>\n";
67
68
        return $table;
69
    }
70
71
    public function tableRow($line, $colCount, $delim = '|')
72
    {
73
        $row = "<tr>\n";
74
75
        foreach (\explode($delim, \trim($line, $delim)) as $i => $col) {
76
            if ($i > $colCount) {
77
                break;
78
            }
79
80
            $col  = \trim($col);
81
            $row .= "<td>{$col}</td>\n";
82
        }
83
84
        $row .= "</tr>\n";
85
86
        return $row;
87
    }
88
}
89