Completed
Push — master ( 098b56...4e0e81 )
by Stéphane
02:32
created

Book::getPageUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Todaymade\Daux\Format\HTMLFile;
2
3
use RuntimeException;
4
use Todaymade\Daux\Tree\Content;
5
use Todaymade\Daux\Tree\Directory;
6
7
class Book
8
{
9
    protected $cover;
10
    protected $tree;
11
    protected $pages = [];
12
13
    public function __construct(Directory $tree, $cover)
14
    {
15
        $this->tree = $tree;
16
        $this->cover = $cover;
17
    }
18
19
    protected function getStyles()
20
    {
21
        // TODO :: un-hardcode that
22
        return '<style>' . file_get_contents('themes/daux_singlepage/css/main.min.css') . '</style>';
23
    }
24
25
    protected function getPageUrl($page)
26
    {
27
        return "file_" . str_replace('/', '_', $page->getUrl());
28
    }
29
30
    protected function buildNavigation(Directory $tree)
31
    {
32
        $nav = [];
33
        foreach ($tree->getEntries() as $node) {
34
            if ($node instanceof Content) {
35
                if ($node->isIndex()) {
36
                    continue;
37
                }
38
39
                $nav[] = [
40
                    'title' => $node->getTitle(),
41
                    'href' => "#" . $this->getPageUrl($node),
42
                ];
43
            } elseif ($node instanceof Directory) {
44
                if (!$node->hasContent()) {
45
                    continue;
46
                }
47
48
                $page_index = ($index = $node->getIndexPage()) ? $index : $node->getFirstPage();
49
50
                $nav[] = [
51
                    'title' => $node->getTitle(),
52
                    'href' => "#" . $this->getPageUrl($page_index),
53
                    'children' => $this->buildNavigation($node),
54
                ];
55
            }
56
        }
57
58
        return $nav;
59
    }
60
61
    private function renderNavigation($entries)
62
    {
63
        $nav = '';
64
        foreach ($entries as $entry) {
65
            if (array_key_exists('children', $entry)) {
66
                if (array_key_exists('href', $entry)) {
67
                    $link = '<a href="' . $entry['href'] . '" class="folder">' . $entry['title'] . '</a>';
68
                } else {
69
                    $link = '<a href="#" class="aj-nav folder">' . $entry['title'] . '</a>';
70
                }
71
72
                $link .= $this->renderNavigation($entry['children']);
73
            } else {
74
                $link = '<a href="' . $entry['href'] . '">' . $entry['title'] . '</a>';
75
            }
76
77
            $nav .= "<li>$link</li>";
78
        }
79
80
        return "<ul>$nav</ul>";
81
    }
82
83
    protected function generateTOC()
84
    {
85
        return '<h1>Table of Contents</h1>' .
86
        $this->renderNavigation($this->buildNavigation($this->tree)) .
87
        '</div><div class="PageBreak">&nbsp;</div>';
88
    }
89
90
    protected function generateCover()
91
    {
92
        return "<div>" .
93
        "<h1 style='font-size:40pt; margin-bottom:0;'>{$this->cover['title']}</h1>" .
94
        "<p><strong>{$this->cover['subject']}</strong> by {$this->cover['author']}</p>" .
95
        '</div><div class="PageBreak">&nbsp;</div>';
96
    }
97
98
    protected function generatePages()
99
    {
100
        $content = '';
101
        foreach ($this->pages as $page) {
102
            $content .= '<a id="' . $this->getPageUrl($page['page']) . '"></a>';
103
            $content .= '<h1>' . $page['page']->getTitle() . '</h1>';
104
            $content .= '<section class="s-content">' . $page['content'] . '</section>';
105
            $content .= '<div class="PageBreak">&nbsp;</div>';
106
        }
107
108
        return $content;
109
    }
110
111
    public function addPage($page, $content)
112
    {
113
        $this->pages[] = ['page' => $page, 'content' => $content];
114
    }
115
116
    public function generateHead()
117
    {
118
        $head = [
119
            "<title>{$this->cover['title']}</title>",
120
            "<meta name='description' content='{$this->cover['subject']}' />",
121
            "<meta name='author' content='{$this->cover['author']}'>",
122
            "<meta charset='UTF-8'>",
123
            $this->getStyles(),
124
        ];
125
126
        return '<head>' . implode('', $head) . '</head>';
127
    }
128
129
    public function generateBody()
130
    {
131
        return '<body>' . $this->generateCover() . $this->generateTOC() . $this->generatePages() . '</body>';
132
    }
133
134
    public function generate()
135
    {
136
        return '<!DOCTYPE html><html>' . $this->generateHead() . $this->generateBody() . '</html>';
137
    }
138
}
139