| @@ -104,39 +104,66 @@ module RepositoriesHelper | |||
| end | |||
| # author hui.he | |||
| def new_readme_render_decode64_content(str, owner, repo, ref, readme_path, readme_name) | |||
| def new_readme_render_decode64_content(str, owner, repo, ref, readme_path, readme_name) | |||
| file_path = readme_path.include?('/') ? readme_path.gsub("/#{readme_name}", '') : readme_path.gsub("#{readme_name}", '') | |||
| return nil if str.blank? | |||
| content = Base64.decode64(str).force_encoding('UTF-8') | |||
| s_regex = /\s\[.*?\]\((.*?)\)\s/ | |||
| src_regex = /\ssrc=\"(.*?)\"\s/ | |||
| # s_regex = /\s\!\[.*?\]\((.*?)\)\s/ | |||
| s_regex = /```([\s\S]*?)```[\s]?/ | |||
| s_regex_1 = /\[.*?\]\((.*?)\)/ | |||
| src_regex = /src=\"(.*?)\"/ | |||
| src_regex_1 = /src=\'(.*?)\'/ | |||
| ss = content.to_s.scan(s_regex) | |||
| ss_1 = content.to_s.scan(s_regex_1) | |||
| ss_src = content.to_s.scan(src_regex) | |||
| total_sources = ss + ss_src | |||
| total_sources.uniq! | |||
| total_sources.each do |s| | |||
| begin | |||
| s_content = s[0] | |||
| # 链接直接跳过不做替换 | |||
| next if s_content.starts_with?('http://') || s_content.starts_with?('https://') || s_content.starts_with?('mailto:') || s_content.blank? | |||
| ext = File.extname(s_content)[1..-1] | |||
| if (image_type?(ext) || download_type(ext)) && !ext.blank? | |||
| s_content = File.expand_path(s_content, file_path) | |||
| s_content = s_content.split("#{Rails.root}/")[1] | |||
| # content = content.gsub(s[0], "/#{s_content}") | |||
| s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join | |||
| content = content.gsub(s[0], s_content) | |||
| else | |||
| path = [owner&.login, repo&.identifier, 'tree', ref, file_path].join("/") | |||
| s_content = File.expand_path(s_content, path) | |||
| s_content = s_content.split("#{Rails.root}/")[1] | |||
| content = content.gsub('('+s[0]+')', '('+"/#{s_content}"+')') | |||
| ss_src_1 = content.to_s.scan(src_regex_1) | |||
| total_sources = {ss: ss, ss_1: ss_1, ss_src: ss_src, ss_src_1: ss_src_1} | |||
| # total_sources.uniq! | |||
| total_sources.except(:ss).each do |k, sources| | |||
| sources.each do |s| | |||
| begin | |||
| s_content = s[0] | |||
| # 链接直接跳过不做替换 | |||
| next if s_content.starts_with?('http://') || s_content.starts_with?('https://') || s_content.starts_with?('mailto:') || s_content.blank? | |||
| ext = File.extname(s_content)[1..-1] | |||
| if (image_type?(ext) || download_type(ext)) && !ext.blank? | |||
| s_content = File.expand_path(s_content, file_path) | |||
| s_content = s_content.split("#{Rails.root}/")[1] | |||
| # content = content.gsub(s[0], "/#{s_content}") | |||
| s_content = [base_url, "/api/#{owner&.login}/#{repo.identifier}/raw?filepath=#{s_content}&ref=#{ref}"].join | |||
| case k | |||
| when 'ss_src' | |||
| content = content.gsub("src=\"#{s[0]}\"", "src=\"#{s_content}\"") | |||
| when 'ss_src_1' | |||
| content = content.gsub("src=\'#{s[0]}\'", "src=\'#{s_content}\'") | |||
| else | |||
| content = content.gsub("(#{s[0]})", "(#{s_content})") | |||
| end | |||
| else | |||
| path = [owner&.login, repo&.identifier, 'tree', ref, file_path].join("/") | |||
| s_content = File.expand_path(s_content, path) | |||
| s_content = s_content.split("#{Rails.root}/")[1] | |||
| case k | |||
| when 'ss_src' | |||
| content = content.gsub("src=\"#{s[0]}\"", "src=\"/#{s_content}\"") | |||
| when 'ss_src_1' | |||
| content = content.gsub("src=\'#{s[0]}\'", "src=\'/#{s_content}\'") | |||
| else | |||
| content = content.gsub("(#{s[0]})", "(/#{s_content})") | |||
| end | |||
| end | |||
| rescue | |||
| next | |||
| end | |||
| rescue | |||
| next | |||
| end | |||
| end | |||
| after_ss_souces = content.to_s.scan(s_regex) | |||
| index =0 | |||
| after_ss_souces.each do |s| | |||
| content = content.gsub("#{s[0]}","#{total_sources[:ss][index][0]}") | |||
| index += 1 | |||
| end | |||
| return content | |||
| rescue | |||
| return str | |||