This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
/
Hack.php
86 lines (74 loc) · 3.21 KB
/
Hack.php
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
<?php
namespace Application\Resource\Literature;
use Application\Dispatcher\Kit;
use Application\Resource;
use Hoa\Promise;
use Hoa\Router;
use League\CommonMark\CommonMarkConverter;
class Hack extends Resource
{
public function get(Kit $_this, $chapter)
{
$_this
->promise
->then(function (Kit $kit) use ($chapter) {
$subPromise = new Promise(function ($fulfill) use ($kit) {
$fulfill($kit);
});
$subPromise
->then(curry([$this, 'doTranslation'], …, 'Literature', 'Literature'))
->then(function (Kit $kit) use ($chapter) {
return $this->doTitle(
$kit,
'Hoa\\' . ucfirst($chapter) .
$kit->view->getTranslation('Literature')
->_(', hack book')
);
});
return $subPromise;
})
->then(curry([$this, 'doMainOverlay'], …, 'Literature/Hack'))
->then(function (Kit $kit) use ($chapter) {
$chapter = ucfirst($chapter);
$language = ucfirst($kit->user->getLocale()->getLanguage());
$kit->data->chapter = $chapter;
$root = '/usr/local/lib/Hoa/' . $chapter . '/';
$hackChapter = $root . 'Documentation/';
$hackIndex = $hackChapter . $language . '/Index.xyl';
$readme = $root . 'README.md';
if (false === file_exists($root)) {
throw new Router\Exception\NotFound('Library does not exist.');
}
if (false === file_exists($hackIndex)) {
if (false === file_exists($readme)) {
throw new Router\Exception\NotFound(
'This library has no documentation yet.'
);
}
$readmeContent = file_get_contents($readme);
$readmeContent = preg_replace(
[
// Remove introduction but keep the abstract.
'/^.*# Hoa[^\n]+\n+/s',
// Remove Installation Section.
'/## Installation.*(?=## Quick usage)/s',
// Remove Documentation and next sections.
'/\n+## Documentation.*$/s'
],
'',
$readmeContent
);
$converter = new CommonMarkConverter();
$readmeCompiled = $converter->convertToHtml($readmeContent);
$kit->view->addOverlay('hoa://Application/View/Shared/Literature/Hack/Readme.xyl');
$kit->data->readme = $readmeCompiled;
} else {
$kit->view->addOverlay($hackIndex);
}
return $kit;
})
->then([$this, 'doFooter'])
->then([$this, 'doRender']);
return;
}
}