1
+ <?php
2
+ namespace app \controller ;
3
+
4
+ use GuzzleHttp \Psr7 \Response ;
5
+ use support \Request ;
6
+
7
+ class Doc
8
+ {
9
+ /*public function index(Request $request)
10
+ {
11
+ return view('doc/index', [
12
+ 'nav' => 'doc',
13
+ 'html_title' => 'webman',
14
+ ]);
15
+ }*/
16
+
17
+ public function view (Request $ request , $ lang )
18
+ {
19
+ $ uri = $ request ->uri ();
20
+ if (strpos ($ uri , '../ ' ) !== false ) {
21
+ return response ('<h1>400 Bad Request</h1> ' , 400 );
22
+ }
23
+
24
+ if ($ uri === "/doc/ $ lang " ) {
25
+ return redirect ("/doc/ $ lang/ " );
26
+ }
27
+
28
+ if ($ uri === "/doc/ $ lang/README.html " ) {
29
+ return redirect ("/doc/ $ lang/ " );
30
+ }
31
+
32
+ if ($ uri === "/doc/ $ lang/ " ) {
33
+ $ uri = "/doc/ $ lang/README.md " ;
34
+ $ index_title = "$ lang " ;
35
+ } else {
36
+ if (pathinfo ($ uri , PATHINFO_EXTENSION ) !== 'html ' ) {
37
+ return notfound ();
38
+ }
39
+ $ uri = strstr ($ uri , '.html ' , true ).'.md ' ;
40
+ }
41
+
42
+ $ file = resource_path () . "/ $ uri " ;
43
+
44
+ if (!is_file ($ file )) {
45
+ return response ('<h1>404 Not Found</h1> ' , 404 );
46
+ }
47
+
48
+ [$ title , $ sidebar ] = $ this ->sidebar ( resource_path () . "/doc/ {$ lang }/SUMMARY.md " , $ uri );
49
+
50
+ $ content = $ this ->format ($ file );
51
+
52
+ $ info = $ this ->getInfo ($ lang );
53
+
54
+ $ langs = $ this ->getLangs ();
55
+
56
+ return view ('doc/view ' , [
57
+ 'html_title ' => $ index_title ??($ title ? "$ title- {$ info ['name ' ]}" : $ info ['name ' ]),
58
+ 'repo ' => $ info ['repo ' ],
59
+ 'name ' => $ info ['name ' ],
60
+ 'sidebar ' => $ sidebar ,
61
+ 'content ' => $ content ,
62
+ 'current_lang ' => $ langs [$ lang ],
63
+ 'langs ' => $ langs
64
+ ]);
65
+ }
66
+
67
+ protected function sidebar ($ sidebar_file , $ uri )
68
+ {
69
+ $ sidebar_content = file_get_contents ($ sidebar_file );
70
+ $ title = '' ;
71
+ $ relative_uri_arr = explode ('/ ' , $ uri );
72
+ if (empty ($ relative_uri_arr [0 ])) {
73
+ unset($ relative_uri_arr [0 ], $ relative_uri_arr [1 ], $ relative_uri_arr [2 ]);
74
+ } else {
75
+ unset($ relative_uri_arr [0 ], $ relative_uri_arr [1 ]);
76
+ }
77
+
78
+ $ relative_uri = implode ('/ ' , $ relative_uri_arr );
79
+ if (preg_match ('/\[(.*?)\]\( ' .preg_quote ($ relative_uri , '/ ' ).'\)/ ' , $ sidebar_content , $ match )) {
80
+ $ title = $ match [1 ];
81
+ }
82
+ $ path = '' ;
83
+ $ count = substr_count ($ uri , '/ ' );
84
+ if ($ count > 3 ) {
85
+ for ($ i =0 ; $ i <$ count -3 ;$ i ++) {
86
+ $ path .= '../ ' ;
87
+ }
88
+ }
89
+ return [$ title , $ this ->formatContent ($ sidebar_content , $ path )];
90
+ }
91
+
92
+ protected function format ($ file , $ path = '' ) {
93
+ if (!is_file ($ file )) {
94
+ return '' ;
95
+ }
96
+ return $ this ->formatContent (file_get_contents ($ file , $ path ));
97
+ }
98
+
99
+ protected function formatContent ($ content , $ path = '' ) {
100
+ if ($ path ) {
101
+ $ content = preg_replace ('/\[(.*?)\]\((.*?\.md)\)/ ' , "[$1]( $ path$2) " , $ content );
102
+ }
103
+ return markdown (str_replace ('.md ' , '.html ' , $ content ), false );
104
+ }
105
+
106
+ public function file (Request $ request )
107
+ {
108
+ $ path = $ request ->path ();
109
+ if (strpos ($ path , '/.. ' ) !== false ) {
110
+ return notfound ();
111
+ }
112
+ $ file = resource_path ()."/ $ path " ;
113
+ if (!is_file ($ file )) {
114
+ return notfound ();
115
+ }
116
+ return \response ()->withFile ($ file );
117
+ }
118
+
119
+ protected function getLangs ()
120
+ {
121
+ $ langs = [];
122
+ foreach (glob (resource_path () . '/doc/*.json ' ) as $ file ) {
123
+ $ lang = basename ($ file , '.json ' );
124
+ $ data = json_decode (file_get_contents ($ file ), true );
125
+ $ langs [$ lang ] = $ data ['lang ' ];
126
+ }
127
+ return $ langs ;
128
+ }
129
+
130
+ protected function getInfo ($ lang )
131
+ {
132
+ return json_decode (file_get_contents (resource_path () . "/doc/ $ lang.json " ), true );
133
+ }
134
+
135
+ }
0 commit comments