@@ -21,17 +21,14 @@ import ZMMVVM
2121class ZLRepoCodePreview3Controller : ZMViewController {
2222
2323 // model
24- let contentModel : ZLGithubContentModel
25- let repoFullName : String
26- let branch : String
24+ @ objc var repoFullName : String = " "
25+ @ objc var branch : String = " "
26+ @ objc var path : String = " "
2727
2828 var htmlStr : String ?
2929
3030
31- init ( repoFullName: String , contentModel: ZLGithubContentModel , branch: String ) {
32- self . repoFullName = repoFullName
33- self . contentModel = contentModel
34- self . branch = branch
31+ @objc init ( ) {
3532 super. init ( nibName: nil , bundle: nil )
3633 }
3734
@@ -48,7 +45,8 @@ class ZLRepoCodePreview3Controller: ZMViewController {
4845
4946 NotificationCenter . default. addObserver ( self , selector: #selector( onNotificationArrived ( notification: ) ) , name: ZLUserInterfaceStyleChange_Notification, object: nil )
5047
51- let fileExtension = ( URL . init ( string: self . contentModel. name. addingPercentEncoding ( withAllowedCharacters: CharacterSet . urlPathAllowed) ?? " " ) as NSURL ? ) ? . pathExtension ?? " "
48+
49+ let fileExtension = pathExtension ( )
5250 if fileExtension. lowercased ( ) == " md " || fileExtension. lowercased ( ) == " markdown " {
5351 self . sendQueryContentRequest ( )
5452 } else {
@@ -99,7 +97,7 @@ class ZLRepoCodePreview3Controller: ZMViewController {
9997
10098 override func setupUI( ) {
10199 super. setupUI ( )
102- self . title = self . contentModel . path
100+ self . title = path
103101
104102 self . zmNavigationBar. backButton. isHidden = false
105103 self . zmNavigationBar. addRightView ( moreButton)
@@ -110,9 +108,23 @@ class ZLRepoCodePreview3Controller: ZMViewController {
110108 } )
111109 }
112110
111+ func html_url( ) -> String {
112+ let html_url = " https://github.com/ \( repoFullName. urlPathEncoding) /blob/ \( branch. urlPathEncoding) / \( path. urlPathEncoding) "
113+ return html_url
114+ }
115+
116+ func download_url( ) -> String {
117+ let download_url = " https://raw.githubusercontent.com/ \( repoFullName. urlPathEncoding) / \( branch. urlPathEncoding) / \( path. urlPathEncoding) "
118+ return download_url
119+ }
120+
121+ func pathExtension( ) -> String {
122+ String ( path. split ( separator: " . " ) . last ?? " " )
123+ }
124+
113125 func switchToWebVC( ) {
114-
115- if let url = URL . init ( string: self . contentModel . html_url) ,
126+
127+ if let url = URL . init ( string: html_url ( ) ) ,
116128 let vc = ZLUIRouter . getVC ( key: . WebContentController, params: [ " requestURL " : url] ) {
117129 if var viewControllers = self . navigationController? . viewControllers,
118130 !viewControllers. isEmpty {
@@ -163,7 +175,7 @@ extension ZLRepoCodePreview3Controller {
163175
164176 @objc func onMoreButtonClick( button: UIButton ) {
165177
166- guard let url = URL ( string: self . contentModel . html_url) else {
178+ guard let url = URL ( string: html_url ( ) ) else {
167179 return
168180 }
169181 button. showShareMenu ( title: url. absoluteString, url: url, sourceViewController: self )
@@ -178,7 +190,7 @@ extension ZLRepoCodePreview3Controller {
178190 ZLProgressHUD . show ( )
179191
180192 ZLServiceManager . sharedInstance. repoServiceModel? . getRepositoryFileHTMLInfo ( withFullName: self . repoFullName,
181- path: self . contentModel . path,
193+ path: self . path,
182194 branch: self . branch,
183195 serialNumber: NSString . generateSerialNumber ( ) ) { [ weak self] ( resultModel: ZLOperationResultModel ) in
184196
@@ -205,7 +217,7 @@ extension ZLRepoCodePreview3Controller {
205217
206218 ZLProgressHUD . show ( )
207219 ZLServiceManager . sharedInstance. repoServiceModel? . getRepositoryFileRawInfo ( withFullName: self . repoFullName,
208- path: self . contentModel . path,
220+ path: self . path,
209221 branch: self . branch,
210222 serialNumber: NSString . generateSerialNumber ( ) ) { [ weak self] ( resultModel: ZLOperationResultModel ) in
211223
@@ -223,7 +235,7 @@ extension ZLRepoCodePreview3Controller {
223235 return
224236 }
225237
226- let code = " ``` \( self . getFileType ( fileExtension: URL . init ( string : self . contentModel . path ) ? . pathExtension ?? " " ) ) \n \( data) \n ``` "
238+ let code = " ``` \( self . getFileType ( fileExtension: self . pathExtension ( ) ) ) \n \( data) \n ``` "
227239
228240 ZLServiceManager . sharedInstance. additionServiceModel? . renderCodeToMarkdown ( withCode: code, serialNumber: NSString . generateSerialNumber ( ) , completeHandle: { [ weak self] ( resultModel: ZLOperationResultModel ) in
229241
@@ -339,7 +351,7 @@ extension ZLRepoCodePreview3Controller: WKUIDelegate, WKNavigationDelegate {
339351
340352 var url = URL ( string: urlStr)
341353 if url? . host == nil { // 如果是相对路径,组装baseurl
342- url = ( URL . init ( string: self . contentModel . html_url) as NSURL ? ) ? . deletingLastPathComponent
354+ url = ( URL . init ( string: html_url ( ) ) as NSURL ? ) ? . deletingLastPathComponent
343355 url = URL ( string: " \( url? . absoluteString ?? " " ) \( urlStr) " )
344356 }
345357
@@ -355,13 +367,13 @@ extension ZLRepoCodePreview3Controller: WKUIDelegate, WKNavigationDelegate {
355367 }
356368
357369 func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
358- if let download_url = self . contentModel . download_url {
359- let baseURLStr = ( URL . init ( string: download_url) as NSURL ? ) ? . deletingLastPathComponent? . absoluteString
370+
371+ let baseURLStr = ( URL . init ( string: download_url ( ) ) as NSURL ? ) ? . deletingLastPathComponent? . absoluteString
360372 let addBaseScript = " let a = ' \( baseURLStr ?? " " ) ';let array = document.getElementsByTagName('img');for(i=0;i<array.length;i++){let item=array[i];if(item.getAttribute('src').indexOf('http') == -1){item.src = a + item.getAttribute('src');}} "
361373
362374 webView. evaluateJavaScript ( addBaseScript) { ( _: Any ? , _: Error ? ) in
363375
364- }
376+
365377 }
366378
367379 }
0 commit comments