Class: Docwu::Utils
- Inherits:
-
Object
- Object
- Docwu::Utils
- Defined in:
- lib/docwu/utils.rb
Class Method Summary collapse
-
.copy_with_path(src, dst) ⇒ Object
复制文件 或者 文件夹.
- .cp_r(src, dest) ⇒ Object
-
.filename(path = '') ⇒ Object
获取文件名.
- .filename_extless(_path = '') ⇒ Object
-
.formated_hashed(hash = {}) ⇒ Object
将hash中所有非hash的类型,转为hash, 以便前端调用.
- .formated_hashed!(hash = {}) ⇒ Object
- .hash_deep_merge(hash, other_hash) ⇒ Object
- .hash_deep_merge!(hash, other_hash) ⇒ Object
-
.html_catalogable(html = '') ⇒ Object
获取一个html代码的目录结果.
-
.parse_marked_content(src_content = '') ⇒ Object
解析 mark.
- .read_file(path) ⇒ Object
- .syntax_highlighter(html) ⇒ Object
- .write_file(dst, content = '') ⇒ Object
Class Method Details
.copy_with_path(src, dst) ⇒ Object
复制文件 或者 文件夹
76 77 78 79 |
# File 'lib/docwu/utils.rb', line 76 def copy_with_path(src, dst) ::FileUtils.mkdir_p(::File.dirname(dst)) ::FileUtils.cp_r(src, dst) end |
.cp_r(src, dest) ⇒ Object
5 6 7 8 9 |
# File 'lib/docwu/utils.rb', line 5 def cp_r src, dest if File.exists?(src) FileUtils.cp_r(src, dest) end end |
.filename(path = '') ⇒ Object
获取文件名
55 56 57 |
# File 'lib/docwu/utils.rb', line 55 def filename path='' path.split('/').last end |
.filename_extless(_path = '') ⇒ Object
59 60 61 |
# File 'lib/docwu/utils.rb', line 59 def filename_extless _path='' _path.chomp(File.extname(_path)) end |
.formated_hashed(hash = {}) ⇒ Object
将hash中所有非hash的类型,转为hash, 以便前端调用
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 |
# File 'lib/docwu/utils.rb', line 28 def formated_hashed hash={} _res = {} hash.each do |key, value| if value.is_a?(Array) _res[key] = value.map do |_val| if _val.is_a?(Hash) self.formated_hashed(_val) else {'value' => _val} end end _res["#{key}_any?"] = value.any? _res["#{key}_count"] = value.size elsif value.is_a?(Hash) _res[key] = self.formated_hashed(value) else _res[key] = value end end _res end |
.formated_hashed!(hash = {}) ⇒ Object
23 24 25 |
# File 'lib/docwu/utils.rb', line 23 def formated_hashed! hash={} hash.replace(self.formated_hashed(hash)) end |
.hash_deep_merge(hash, other_hash) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/docwu/utils.rb', line 11 def hash_deep_merge(hash, other_hash) hash.merge(other_hash) do |key, oldval, newval| oldval = oldval.to_hash if oldval.respond_to?(:to_hash) newval = newval.to_hash if newval.respond_to?(:to_hash) oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? self.hash_deep_merge(oldval, newval) : newval end end |
.hash_deep_merge!(hash, other_hash) ⇒ Object
19 20 21 |
# File 'lib/docwu/utils.rb', line 19 def hash_deep_merge!(hash, other_hash) hash.replace(self.hash_deep_merge(hash, other_hash)) end |
.html_catalogable(html = '') ⇒ Object
获取一个html代码的目录结果
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/docwu/utils.rb', line 165 def html_catalogable html='' doc = ::Nokogiri::HTML(html) paths = doc.xpath('//h1|//h2|//h3|//h4|//h5|//h6') index = 1 catalogs = paths.map do |path| _name = path.name _text = path.text _anchor = "markup-#{_name}-#{index}" index += 1 path.replace("<#{_name}><a name='#{_anchor}'></a>#{_text}</#{_name}>") { 'text' => _text, 'name' => _name, 'anchor' => _anchor } end { 'catalogs' => catalogs, 'html' => doc.css('body').inner_html.to_s } end |
.parse_marked_content(src_content = '') ⇒ Object
解析 mark
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 161 162 |
# File 'lib/docwu/utils.rb', line 122 def parse_marked_content src_content='' _content_text = '' _content_data = {} # 读取页面的配置 content_lines = src_content.split(/\n/) # 根据换行分割 _data_lines = [] _text_lines = [] _data_num_a = -1 _data_num_b = -1 content_lines.each_with_index do |line, index| if line =~ /--+/ if _data_num_a == -1 _data_num_a = index elsif _data_num_b == -1 _data_num_b = index else break end end end if _data_num_a > -1 && _data_num_b > -1 # 说明有配置信息 _yaml = ::YAML.load(content_lines[_data_num_a + 1, _data_num_b -1].join("\n")) if _yaml.is_a?(Hash) _content_data.merge!(_yaml) end _content_text = content_lines[_data_num_b + 1, content_lines.size].join("\n") else # 无页面配置信息 _content_text = src_content end {:data => _content_data, :text => _content_text} end |
.read_file(path) ⇒ Object
63 64 65 |
# File 'lib/docwu/utils.rb', line 63 def read_file path ::File.read(path) end |
.syntax_highlighter(html) ⇒ Object
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 |
# File 'lib/docwu/utils.rb', line 81 def syntax_highlighter(html) doc = ::Nokogiri::HTML(html) doc.search("//pre").each do |pre| begin lang = pre.attr('lang') if lang _lang_class = pre.attr('class').to_s.split(' ').select {|_itm| _itm.include?('lang-') }.first if _lang_class lang = _lang_class.gsub('lang-', '') end end # begin # if pre_code=pre.css('code') # lang = pre_code.attr('class').to_s # end # rescue # end if pre_code=pre.css('code') lang = pre_code.attr('class').to_s end if lang.to_s.strip.size > 0 text = pre.text.rstrip # http://coderay.rubychan.de/ pre.replace ::CodeRay.scan(text, lang).div.to_s end rescue Exception => error # TODO: Add error log here end end doc.css('body').inner_html.to_s end |
.write_file(dst, content = '') ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/docwu/utils.rb', line 67 def write_file dst, content='' ::FileUtils.mkdir_p(::File.dirname(dst)) file = ::File.new(dst, 'w') file.write(content) file.close end |