-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassMarkdown.php
160 lines (151 loc) · 5.09 KB
/
ClassMarkdown.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
include_once(__DIR__ . '/ClassParser.php');
include_once(__DIR__ . '/TextTable.php');
/**
* class implementation to output markdown text from class file comments
*
* @author Marco Cesarato <[email protected]>
* @author PICCORO Lenz McKAY <[email protected]>
* @copyright Copyright (c) 2018, 2019
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @link https://github.com/marcocesarato/PHP-Class-Markdown-Docs
* @version 0.1.10 (2019-04-05)
*/
class ClassMarkdown
{
/**
* ClassMarkdown constructor
* @param none
*/
public function __construct()
{
}
/**
* a given class funtions as array keys
* @param $class
* @return array
*/
protected static function parseClass($class)
{
$rows = array();
foreach ($class['functions'] as $key => $value) {
$row = array();
$description = array();
$parameters = array();
$return = 'void';
$row[] = $key;
$value['doc'] = trim(str_replace(array("\r", "*", "/", '@', '|'), array('', '', '', '', '\|'), $value['doc']));
$value['doc'] = explode("\n", $value['doc']);
foreach ($value['doc'] as $line) {
$line = trim(preg_replace('~[.[:cntrl:]]~', '', $line));
if (preg_match('/^param/i', $line)) {
$line = str_replace('param ', '', $line);
$parameters[] = $line;
} else if (preg_match('/^return/i', $line)) {
$line = str_replace('\|', '<br>', $line);
$line = str_replace('return ', '', $line);
$return = $line;
} else {
$description[] = substr($line,0,50);
}
}
$row[] = implode(' <br> ', $description);
$row[] = implode(' <br> ', $value['modifiers']);
$row[] = implode(' <br> ', $parameters);
$row[] = $return;
$rows[] = $row;
}
return $rows;
}
/**
* a given class tags as markdown formated paragraph
* @param $class
* @return string
*/
protected static function parseExtended($class)
{
$name = $class['name'];
$autor = '';
$description = NULL;
$coping = '(c) '.date('Y');
$version = '';
$class['doc'] = trim(str_replace(array("\r", "*", "/", '|'), array('', '', '', '', '\|'), $class['doc']));
$class['doc'] = explode("\n", $class['doc']);
foreach ($class['doc'] as $line) {
if (is_null($description)){
$description = substr($line,0,55);
}
if (strpos($line, 'uthor') !== FALSE) {
$line = str_replace('@author ', '', $line);
$autor .= '* '.$line. PHP_EOL;
}
$line = str_replace('@', '', $line);
if (strpos($line, 'ersion') !== FALSE) {
$version = trim(preg_replace('~[[:cntrl:]]~', '', $line));
$version = $version;
}else {
$line = trim(preg_replace('~[.[:cntrl:]]~', '', $line));
if (preg_match('/^param/i', $line)) {
$line = str_replace('param ', '', $line);
$parameters = $line;
} else if (preg_match('/^copyright/i', $line)) {
$line = str_replace('\|', '<br>', $line);
$line = str_replace('copyright ', '', $line);
$coping = $line;
}
}
}
$info = '**'.$name.', '.$version.'**: '.$description. PHP_EOL. PHP_EOL;
$info .= $coping. PHP_EOL. PHP_EOL;
$info .= $autor;
return $info. PHP_EOL;
}
/**
* Get markdown class documentation
* @param $file
* @return string
*/
public static function getMarkdown($file)
{
$result = '';
$cp = new ClassParser();
$cp->parse($file);
$classes = $cp->getClasses();
foreach ($classes as $k => $class) {
$class['name'] = $k;
$result .= "## " . $class['name'] . PHP_EOL . PHP_EOL;
$rows = self::parseClass($class);
$columns = ['Method', 'Description', 'Type', 'Parameters', 'Return'];
$result .= self::parseExtended($class);
$t = new TextTable($columns, $rows);
$result .= $t->render();
$result .= PHP_EOL . PHP_EOL;
}
return $result;
}
/**
* Print Markdown class documentation
* @param $file
*/
public static function printMarkdown($file)
{
echo self::getMarkdown($file);
}
/**
* Get PHP file as array class documentation
* @param $file
* @return array
*/
public static function getArray($file)
{
$result = array();
$cp = new ClassParser();
$cp->parse($file);
$classes = $cp->getClasses();
foreach ($classes as $k => $class) {
$class['name'] = $k;
$result[] = self::parseClass($class);
}
return $result;
}
}