|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'httparty' |
| 4 | +require 'fileutils' |
| 5 | + |
| 6 | +CONTENT_DIR = "./content" |
| 7 | + |
| 8 | +categories = { |
| 9 | + cleancode: "http://phpmd.org/rules/cleancode.txt", |
| 10 | + codesize: "http://phpmd.org/rules/codesize.txt", |
| 11 | + controversial: "http://phpmd.org/rules/controversial.txt", |
| 12 | + design: "http://phpmd.org/rules/design.txt", |
| 13 | + naming: "http://phpmd.org/rules/naming.txt", |
| 14 | + unused: "http://phpmd.org/rules/unusedcode.txt" |
| 15 | +} |
| 16 | + |
| 17 | +FileUtils.rm_rf(CONTENT_DIR) |
| 18 | + |
| 19 | +categories.each do |category, url| |
| 20 | + category_path = "#{CONTENT_DIR}/#{category}/" |
| 21 | + FileUtils.mkdir_p(category_path) |
| 22 | + |
| 23 | + text = HTTParty.get(url).body |
| 24 | + |
| 25 | + matches = text.split(/=+\n.*?\n=+/, 2).pop |
| 26 | + matches = matches.split(/^=+\n$/) |
| 27 | + |
| 28 | + sections = matches.each_with_object([]) do |match, array| |
| 29 | + split = match.split(/\n/) |
| 30 | + title = split.pop |
| 31 | + body = split.join("\n") |
| 32 | + |
| 33 | + body.gsub!(/(?<=Example:) ::/, "\n\n```php") |
| 34 | + body = body.split(/This rule.*/).shift |
| 35 | + body += "\n```" |
| 36 | + |
| 37 | + |
| 38 | + array << body |
| 39 | + array << title |
| 40 | + end |
| 41 | + |
| 42 | + sections.shift |
| 43 | + sections.pop |
| 44 | + |
| 45 | + sections.each_slice(2) do |(header, body)| |
| 46 | + next if header == "Remark" |
| 47 | + |
| 48 | + file_name = header.gsub(" ", "_").downcase |
| 49 | + File.open("#{category_path}/#{file_name}.txt", "w") do |file| |
| 50 | + file.write(body) |
| 51 | + end |
| 52 | + end |
| 53 | +end |
0 commit comments