Passed
Push — master ( 5f2bd1...7c2064 )
by
unknown
13:34
created

render_php()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace MySociety\TheyWorkForYou\Renderer;
4
5
/**
6
 * Markdown Renderer
7
 *
8
 * Use for converting markdown into help pages.
9
 */
10
11
12
function render_php($template_file)
13
{
14
    $path = INCLUDESPATH . 'easyparliament/templates/html/' . $template_file . '.php';
0 ignored issues
show
Bug introduced by
The constant MySociety\TheyWorkForYou\Renderer\INCLUDESPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
    ob_start();
16
    include($path);
17
    return ob_get_clean();
18
}
19
20
class Markdown
21
{
22
    public function markdown_document($this_page, $show_menu = true){
23
        // This function takes a markdown file and converts it to HTML
24
    
25
        $markdown_file = '../../../markdown/' . $this_page . '.md';
26
        $Parsedown = new \Parsedown();
27
        
28
        $text = file_get_contents($markdown_file);
29
        $html = $Parsedown->text($text);
30
        
31
        # title is the first h1
32
        preg_match('/<h1>([^<]+)<\/h1>/i', $html, $matches);
33
        
34
        $title = $matches[1];
35
        
36
        $html = preg_replace_callback('/<h([1-3])>([^<]+)<\/h[1-3]>/i', function($matches) {
37
            $level = $matches[1];
38
            $htitle = $matches[2];
39
            $slug = slugify($htitle);
40
            if ($level == 1){
41
                $title_class = "js-toc-title";
42
            } else {
43
                $title_class = "js-toc-item";
44
            }
45
            return "<h$level id=\"$slug\" class=\"$title_class\">$htitle</h$level>";
46
        }, $html);
47
48
        // Create new panel when horizontal line used
49
        $html = preg_replace('/<hr \/>/i', '</div><div class="panel">', $html);
50
51
        // render donate box
52
        if (strpos($html, '{{ donate_box }}') !== false) {
53
            $html = str_replace('{{ donate_box }}', render_php('donate/_stripe_donate'), $html);
54
        }
55
56
        \MySociety\TheyWorkForYou\Renderer::output('static/markdown_template', array(
57
            'html' => $html,
58
            'this_page' => $this_page,
59
            'page_title' => $title,
60
            'show_menu' => $show_menu,
61
        ));
62
        
63
    }
64
}
65