-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderConfig.php
142 lines (118 loc) · 4.86 KB
/
RenderConfig.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
class RenderConfig {
private $git;
public $reporaw;
public $repogit;
public $repoapi;
public $accheader = array();
private $pathsep = "/";
private $cfg;
public $mdfile;
public $mdpageopt;
public $owner;
public $repo;
public $branch;
public $pagetitle;
public $metakeyw;
public $metadesc;
public $genstatic;
public $statname;
public $oghead;
public $ogjson;
public $codecolor;
public $codecolorfiles;
function __construct($cfgfile)
{
$this->git = json_decode(file_get_contents("./github.json"));
$this->reporaw = $this->git->reporaw;
$this->repogit = $this->git->repogit;
$this->repoapi = $this->git->repoapi;
$this->accheader = $this->git->accheader;
if(!file_exists($cfgfile)) $cfgfile = "./test.json";
$this->cfg = json_decode(file_get_contents($cfgfile));
if(isset($this->cfg->mdfilerem) && ($this->cfg->mdfilerem === true)) {
// create the path to the file that we will render
$this->mdfile = $this->git->reporaw . $this->cfg->owner . $this->pathsep . $this->cfg->repo . $this->pathsep . $this->cfg->branch . $this->pathsep . $this->cfg->mdfile;
} else {
$this->mdfile = $this->cfg->mdfile;
}
$this->mdpageopt = $this->cfg->mdpageopt;
// reduce the depth at which clients will need to
// reach into this object
$this->pagetitle = $this->cfg->pagetitle;
$this->owner = $this->cfg->owner;
$this->repo = $this->cfg->repo;
$this->branch = $this->cfg->branch;
// optional meta tags, it's not necessary to
// include them in the JSON file if not used.
$this->metaauth = (isset($this->cfg->metaauth) ? $this->cfg->metaauth : "");
//
// To Do : get this info from the owner's
// repository via the GitHub API.
$this->metadesc = (isset($this->cfg->metadesc) ? $this->cfg->metadesc : "");
$this->metakeyw = (isset($this->cfg->metakeyw) ? $this->cfg->metakeyw : "");
// finish the meta tags...
$this->configMeta();
// generate a static file?
$this->genstatic = (isset($this->cfg->genstatic) ? $this->cfg->genstatic : false);
$this->statname = ($this->genstatic === true ? $this->cfg->statname : "");
// render the opengraph header contents
$this->oghead = (isset($this->cfg->oghead) ? $this->cfg->oghead : false);
$this->ogjson = ($this->oghead === true ? $this->cfg->ogjson : "");
// optional colorizing of code blocks
$this->codecolor = (isset($this->cfg->codecolor) ? $this->cfg->codecolor : false);
$this->codecolorfiles = ($this->codecolor === true ? $this->cfg->codecolorfiles : "");
}
private function configMeta()
{
$gitresp = null;
$topics = "";
if(($this->cfg->gitdesc === true) || ($this->cfg->gittopics === true)) {
// put together the base URL,
$url = $this->git->repoapi . "repos/" . $this->cfg->owner . $this->pathsep . $this->cfg->repo;
}
if($this->cfg->gitdesc === true) {
$this->metadesc = "Document - ";
if($this->cfg->gittopics === true) {
// retrieve repo info w/ topics
$gitresp = $this->getFromGit($this->git->accheader[1], $url);
foreach($gitresp->topics as $topic) {
$topics = $topics . $topic . ",";
}
$this->metakeyw = $this->metakeyw . trim($topics,",");
} else {
// retrieve repo info w/o topics
$gitresp = $this->getFromGit($this->git->accheader[0], $url);
}
$this->metadesc = $this->metadesc . $gitresp->description;
} else {
if($this->cfg->gittopics === true) {
// retrieve repo topics only
$url = $url . $this->pathsep . "topics";
$gitresp = $this->getFromGit($this->git->accheader[1], $url);
foreach($gitresp->names as $topic) {
$topics = $topics . $topic . ",";
}
$this->metakeyw = $this->metakeyw . trim($topics,",");
}
}
}
private function getFromGit($accept, $url)
{
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "Accept: $accept\r\n" .
"user-agent: custom\r\n" .
"Content-Type: application/json; charset=utf-8\r\n" .
"Content-Encoding: text\r\n"
)
);
$context = stream_context_create($opts);
$resp = json_decode(file_get_contents($url, true, $context));
// uncomment and examine for debugging purposes
//$resphead = $http_response_header;
return $resp;
}
}
?>