Class: Helper
- Inherits:
-
Object
- Object
- Helper
- Defined in:
- lib/Helper.rb
Class Method Summary collapse
- .compareVersion(version1, version2) ⇒ Object
- .createDirIfNotExist(dirPath) ⇒ Object
- .createPostInfo(postInfo, isPin, isForJekyll) ⇒ Object
- .createWatermark(postURL, isForJekyll) ⇒ Object
- .downloadLatestVersion ⇒ Object
- .escapeHTML(text) ⇒ Object
- .escapeMarkdown(text) ⇒ Object
- .fetchOGImage(url) ⇒ Object
- .getLocalVersion ⇒ Object
- .getRemoteVersionFromGithub ⇒ Object
- .makeWarningText(message) ⇒ Object
- .printNewVersionMessageIfExists ⇒ Object
Class Method Details
.compareVersion(version1, version2) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/Helper.rb', line 180 def self.compareVersion(version1, version2) if version1.major > version2.major true else if version1.minor > version2.minor true else if version1.patch > version2.patch true else false end end end end |
.createDirIfNotExist(dirPath) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/Helper.rb', line 32 def self.createDirIfNotExist(dirPath) dirs = dirPath.split("/") currentDir = "" begin dir = dirs.shift currentDir = "#{currentDir}/#{dir}" Dir.mkdir(currentDir) unless File.exists?(currentDir) end while dirs.length > 0 end |
.createPostInfo(postInfo, isPin, isForJekyll) ⇒ Object
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 |
# File 'lib/Helper.rb', line 97 def self.createPostInfo(postInfo, isPin, isForJekyll) title = postInfo.title&.gsub("[","") title = title&.gsub("]","") = "" if !postInfo..nil? && postInfo..length > 0 = "\"#{postInfo..map { |tag| tag&.gsub("\"", "\\\"") }.join("\",\"")}\"" end result = "---\n" result += "title: \"#{title&.gsub("\"", "\\\"")}\"\n" result += "author: \"#{postInfo.creator&.gsub("\"", "\\\"")}\"\n" result += "date: #{postInfo.firstPublishedAt.strftime('%Y-%m-%dT%H:%M:%S.%L%z')}\n" result += "last_modified_at: #{postInfo.latestPublishedAt.strftime('%Y-%m-%dT%H:%M:%S.%L%z')}\n" result += "categories: \"#{postInfo.collectionName&.gsub("\"", "\\\"")}\"\n" result += "tags: [#{}]\n" result += "description: \"#{postInfo.description&.gsub("\"", "\\\"")}\"\n" if !postInfo.previewImage.nil? result += "image:\r\n" result += " path: /#{postInfo.previewImage}\r\n" end if !isPin.nil? && isPin == true result += "pin: true\r\n" end if isForJekyll result += "render_with_liquid: false\n" end result += "---\n" result += "\r\n" result end |
.createWatermark(postURL, isForJekyll) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/Helper.rb', line 197 def self.createWatermark(postURL, isForJekyll) jekyllOpen = "" if isForJekyll jekyllOpen = "{:target=\"_blank\"}" end text = "\r\n\r\n\r\n" text += "_[Post](#{postURL})#{jekyllOpen} converted from Medium by [ZMediumToMarkdown](https://github.com/ZhgChgLi/ZMediumToMarkdown)#{jekyllOpen}._" text += "\r\n" text end |
.downloadLatestVersion ⇒ Object
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 |
# File 'lib/Helper.rb', line 52 def self.downloadLatestVersion() rootPath = File.('../', File.dirname(__FILE__)) if File.file?("#{rootPath}/ZMediumToMarkdown.gemspec") apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases' versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] } version = nil index = 0 while version == nil do thisVersion = versions[index] if thisVersion["prerelease"] == false version = thisVersion next end index += 1 end zipFilePath = version["zipball_url"] puts "Downloading latest version from github..." open('latest.zip', 'wb') do |fo| fo.print open(zipFilePath).read end puts "Unzip..." Zip::File.open("latest.zip") do |zipfile| zipfile.each do |file| fileNames = file.name.split("/") fileNames.shift filePath = fileNames.join("/") if filePath != '' puts "Unzinp...#{filePath}" zipfile.extract(file, filePath) { true } end end end File.delete("latest.zip") tagName = version["tag_name"] puts "Update to version #{tagName} successfully!" else system("gem update ZMediumToMarkdown") end end |
.escapeHTML(text) ⇒ Object
26 27 28 29 30 |
# File 'lib/Helper.rb', line 26 def self.escapeHTML(text) text = text.gsub(/(<)/, '<') text = text.gsub(/(>)/, '>') text end |
.escapeMarkdown(text) ⇒ Object
22 23 24 |
# File 'lib/Helper.rb', line 22 def self.escapeMarkdown(text) text.gsub(/(\*|_|`|\||\\|\{|\}|\[|\]|\(|\)|#|\+|\-|\.|\!)/){ |x| "\\#{x}" } end |
.fetchOGImage(url) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/Helper.rb', line 15 def self.fetchOGImage(url) html = Request.html(Request.URL(url)) return "" unless html image = html.search("meta[property='og:image']") image.attribute('content') || "" end |
.getLocalVersion ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/Helper.rb', line 140 def self.getLocalVersion() rootPath = File.('../', File.dirname(__FILE__)) result = nil if File.file?("#{rootPath}/ZMediumToMarkdown.gemspec") gemspecContent = File.read("#{rootPath}/ZMediumToMarkdown.gemspec") result = gemspecContent[/(gem\.version){1}\s+(\=)\s+(\'){1}(\d+(\.){1}\d+(\.){1}\d+){1}(\'){1}/, 4] else result = Gem.loaded_specs["ZMediumToMarkdown"].version.version end if !result.nil? Gem::Version.new(result) else nil end end |
.getRemoteVersionFromGithub ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/Helper.rb', line 158 def self.getRemoteVersionFromGithub() apiPath = 'https://api.github.com/repos/ZhgChgLi/ZMediumToMarkdown/releases' versions = JSON.parse(Request.URL(apiPath).body).sort { |a,b| b["id"] <=> a["id"] } tagName = nil index = 0 while tagName == nil do thisVersion = versions[index] if thisVersion["prerelease"] == false tagName = thisVersion["tag_name"] next end index += 1 end if !tagName.nil? Gem::Version.new(tagName.downcase.gsub! 'v','') else nil end end |
.makeWarningText(message) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/Helper.rb', line 42 def self.makeWarningText() puts "####################################################\n" puts "#WARNING:\n" puts "##{}\n" puts "#--------------------------------------------------#\n" puts "#Please feel free to open an Issue or submit a fix/contribution via Pull Request on:\n" puts "#https://github.com/ZhgChgLi/ZMediumToMarkdown\n" puts "####################################################\n" end |
.printNewVersionMessageIfExists ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/Helper.rb', line 131 def self.printNewVersionMessageIfExists() if Helper.getRemoteVersionFromGithub() > Helper.getLocalVersion() puts "##########################################################" puts "##### New Version Available!!! #####" puts "##### Please type `ZMediumToMarkdown -n` to update!! #####" puts "##########################################################" end end |